#include <bits/stdc++.h> n using namespace std; n int N, M; n int d[300010]; n int h[300010]; n pair<long long, int> L[8 * 300010]; n pair<long long, int> R[8 * 300010]; n void aggL(int nodo, int left, int right, int pos, long long val) { n   if (left == right) { n     L[nodo] = pair<long long, int>(val, pos); n     return; n   } n   int mid = (left + right) / 2; n   if (pos <= mid) n     aggL(nodo * 2, left, mid, pos, val); n   else n     aggL(nodo * 2 + 1, mid + 1, right, pos, val); n   L[nodo] = max(L[nodo * 2], L[nodo * 2 + 1]); n } n void aggR(int nodo, int left, int right, int pos, long long val) { n   if (left == right) { n     R[nodo] = pair<long long, int>(val, pos); n     return; n   } n   int mid = (left + right) / 2; n   if (pos <= mid) n     aggR(nodo * 2, left, mid, pos, val); n   else n     aggR(nodo * 2 + 1, mid + 1, right, pos, val); n   R[nodo] = max(R[nodo * 2], R[nodo * 2 + 1]); n } n pair<long long, int> qL(int nodo, int left, int right, int l, int r) { n   if (left == l && right == r) return L[nodo]; n   int mid = (left + right) / 2; n   if (r <= mid) return qL(nodo * 2, left, mid, l, r); n   if (l >= mid + 1) return qL(nodo * 2 + 1, mid + 1, right, l, r); n   if (l <= mid && r >= mid + 1) n     return max(qL(nodo * 2, left, mid, l, mid), n                qL(nodo * 2 + 1, mid + 1, right, mid + 1, r)); n } n pair<long long, int> qR(int nodo, int left, int right, int l, int r) { n   if (left == l && right == r) return R[nodo]; n   int mid = (left + right) / 2; n   if (r <= mid) return qR(nodo * 2, left, mid, l, r); n   if (l >= mid + 1) return qR(nodo * 2 + 1, mid + 1, right, l, r); n   if (l <= mid && r >= mid + 1) n     return max(qR(nodo * 2, left, mid, l, mid), n                qR(nodo * 2 + 1, mid + 1, right, mid + 1, r)); n } n int main() { n   ifstream in( input.txt ); n   cin >> N >> M; n   for (int i = 0; i < N; i++) cin >> d[i]; n   for (int i = 0; i < N; i++) cin >> h[i]; n   for (int i = 0; i < 8 * 300010; i++) L[i].first = R[i].first = LLONG_MIN; n   long long temp = 0; n   for (int i = 0; i < N; i++) { n     aggR(1, 0, 2 * 300010 - 1, i, temp + h[i] * 2); n     aggL(1, 0, 2 * 300010 - 1, i, h[i] * 2 - temp); n     temp += d[i]; n   } n   for (int i = 0; i < N; i++) { n     aggR(1, 0, 2 * 300010 - 1, i + N, temp + h[i] * 2); n     aggL(1, 0, 2 * 300010 - 1, i + N, h[i] * 2 - temp); n     temp += d[i]; n   } n   for (int i = 0; i < M; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     if (a <= b) { n       temp = qR(1, 0, 2 * 300010 - 1, b + 2, N + a - 1).first; n       int pos = qR(1, 0, 2 * 300010 - 1, b + 2, N + a - 1).second; n       temp += qL(1, 0, 2 * 300010 - 1, b + 1, pos - 1).first; n       long long res = temp; n       temp = qL(1, 0, 2 * 300010 - 1, b + 1, N + a - 2).first; n       pos = qL(1, 0, 2 * 300010 - 1, b + 1, N + a - 2).second; n       temp += qR(1, 0, 2 * 300010 - 1, pos + 1, N + a - 1).first; n       res = max(res, temp); n       cout << res << endl; n     } else { n       temp = qR(1, 0, 2 * 300010 - 1, b + 2, a - 1).first; n       int pos = qR(1, 0, 2 * 300010 - 1, b + 2, a - 1).second; n       temp += qL(1, 0, 2 * 300010 - 1, b + 1, pos - 1).first; n       long long res = temp; n       temp = qL(1, 0, 2 * 300010 - 1, b + 1, a - 2).first; n       pos = qL(1, 0, 2 * 300010 - 1, b + 1, a - 2).second; n       temp += qR(1, 0, 2 * 300010 - 1, pos + 1, a - 1).first; n       res = max(res, temp); n       cout << res << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 200005, INF = 2000000000000000000; n long double pi = 3.1415926535897932; n long long powere(long long a, long long b, long long p) { n   if (a == 0) return 0; n   long long res = 1; n   a %= p; n   while (b > 0) { n     if (b & 1) res = (res * a) % p; n     b >>= 1; n     a = (a * a) % p; n   } n   return res; n } n void copy(long long a[2][2], long long b[2][2]) { n   for (long long i = 0; i < 2; i++) { n     for (long long j = 0; j < 2; j++) b[i][j] = a[i][j]; n   } n } n void matmult(long long m1[2][2], long long m2[2][2], long long m3[2][2]) { n   for (long long i = 0; i < 2; i++) { n     for (long long j = 0; j < 2; j++) { n       m3[i][j] = 0; n       for (long long k = 0; k < 2; k++) { n         m3[i][j] += n             (m1[i][k] % 1000000007 * m2[k][j] % 1000000007) % 1000000007; n         m3[i][j] %= 1000000007; n       } n     } n   } n } n void power(long long mat[2][2], long long b, long long p, n            long long finres[2][2]) { n   long long res[2][2] = {{0}}, cc[2][2]; n   res[0][0] = res[1][1] = 1; n   while (b > 0) { n     copy(res, cc); n     if (b & 1) matmult(mat, cc, res); n     copy(mat, cc); n     b >>= 1; n     matmult(cc, cc, mat); n   } n   copy(res, finres); n } n int32_t main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout.precision(std::numeric_limits<double>::max_digits10); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     long long x = abs(x1 - x2), y = abs(y1 - y2); n     if (y < x) swap(x, y); n     long long ans = (x * (x + 1)); n     ans += ((y - x - 1) * x); n     cout << ans + 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void fun(long long int a, long long int b, long long int u, n          vector<long long int> &t) { n   long long int i, j, k, n; n   for (i = 1; i < b; i++) { n     if (u) t[i]++; n   } n   u = 1 - u; n   i = b; n   while (i < 1000) { n     for (j = 0; j < a; j++) { n       if (i >= 1000) return; n       if (u) t[i]++; n       i++; n     } n     u = 1 - u; n   } n } n void solve() { n   long long int i, j, k, n; n   cin >> n; n   string s; n   cin >> s; n   vector<pair<long long int, long long int> > v(n); n   vector<long long int> t(1000, 0); n   for (i = 0; i < n; i++) { n     if (s[i] ==  1 ) t[0]++; n   } n   for (i = 0; i < n; i++) { n     cin >> v[i].first >> v[i].second; n   } n   for (i = 0; i < n; i++) { n     fun(v[i].first, v[i].second, s[i] -  0 , t); n   } n   long long int mx = t[0]; n   for (i = 1; i < (int)t.size(); i++) mx = max(mx, t[i]); n   cout << mx; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, x, y; n char type; n map<int, pair<char, int> > Data; n int solve() { n   auto fi = Data.lower_bound(x); n   if (fi->first == x) return 0; n   if (type ==  L ) fi--; n   int ans = abs(x - fi->first); n   if (fi->second.first == type) ans += fi->second.second; n   Data[x] = make_pair(type, ans); n   return ans; n } n int main() { n   scanf( %d %d , &n, &q); n   Data[0] = make_pair( U , 0); n   Data[n + 1] = make_pair( L , 0); n   while (q--) { n     cin >> x >> y >> type; n     printf( %d n , solve()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 15, N = 15; n int m, n; n pair<int, int> a[M], b[N]; n vector<int> tot, cur; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> m >> n; n   for (int i = 1; i <= m; i++) { n     cin >> a[i].first >> a[i].second; n   } n   for (int i = 1; i <= n; i++) { n     cin >> b[i].first >> b[i].second; n   } n   for (int i = 1; i <= m; i++) { n     cur.clear(); n     for (int j = 1; j <= n; j++) { n       int cnt = 0; n       if (a[i].first == b[j].first || a[i].first == b[j].second) { n         cnt++; n       } n       if (a[i].second == b[j].first || a[i].second == b[j].second) { n         cnt++; n       } n       if (cnt == 1) { n         if (a[i].first == b[j].first || a[i].first == b[j].second) { n           cur.push_back(a[i].first); n         } else { n           cur.push_back(a[i].second); n         } n       } n     } n     sort(cur.begin(), cur.end()); n     cur.erase(unique(cur.begin(), cur.end()), cur.end()); n     if (cur.size() >= 2) { n       return cout << -1, 0; n     } else if (cur.size() == 1) { n       tot.push_back(cur[0]); n     } n   } n   for (int i = 1; i <= n; i++) { n     cur.clear(); n     for (int j = 1; j <= m; j++) { n       int cnt = 0; n       if (b[i].first == a[j].first || b[i].first == a[j].second) { n         cnt++; n       } n       if (b[i].second == a[j].first || b[i].second == a[j].second) { n         cnt++; n       } n       if (cnt == 1) { n         if (b[i].first == a[j].first || b[i].first == a[j].second) { n           cur.push_back(b[i].first); n         } else { n           cur.push_back(b[i].second); n         } n       } n     } n     sort(cur.begin(), cur.end()); n     cur.erase(unique(cur.begin(), cur.end()), cur.end()); n     if (cur.size() >= 2) { n       return cout << -1, 0; n     } else if (cur.size() == 1) { n       tot.push_back(cur[0]); n     } n   } n   sort(tot.begin(), tot.end()); n   tot.erase(unique(tot.begin(), tot.end()), tot.end()); n   if (tot.size() >= 2) { n     cout << 0; n   } else { n     cout << tot[0]; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n long long k, b, n, t, ans, cur; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> k >> b >> n >> t; n   ans = n; n   cur = k + b; n   while (cur <= t) { n     cur = cur * k + b; n     --ans; n   } n   if (ans < 0) ans = 0; n   cout << ans << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int n, q, L[maxn << 2], R[maxn << 2]; n double sum[maxn << 2], add[maxn << 2], mul[maxn << 2]; n void pushup(int o) { sum[o] = sum[o * 2] + sum[o * 2 + 1]; } n void pushdown(int o) { n   sum[o * 2] = sum[o * 2] * mul[o] + add[o] * (R[o * 2] - L[o * 2] + 1); n   sum[o * 2 + 1] = n       sum[o * 2 + 1] * mul[o] + add[o] * (R[o * 2 + 1] - L[o * 2 + 1] + 1); n   add[o * 2] = add[o * 2] * mul[o] + add[o]; n   add[o * 2 + 1] = add[o * 2 + 1] * mul[o] + add[o]; n   mul[o * 2] = mul[o * 2] * mul[o]; n   mul[o * 2 + 1] = mul[o * 2 + 1] * mul[o]; n   mul[o] = 1; n   add[o] = 0; n } n void build(int o, int l, int r) { n   L[o] = l; n   R[o] = r; n   add[o] = 0; n   mul[o] = 1; n   if (l == r) { n     cin >> sum[o]; n     return; n   } n   int mid = (l + r) >> 1; n   build(o * 2, l, mid); n   build(o * 2 + 1, mid + 1, r); n   pushup(o); n } n double query(int o, int l, int r, int x, int y) { n   if (x <= l && r <= y) { n     return sum[o]; n   } n   pushdown(o); n   int mid = (l + r) >> 1; n   double res = 0.0; n   if (x <= mid) res += query(o * 2, l, mid, x, y); n   if (y > mid) res += query(o * 2 + 1, mid + 1, r, x, y); n   return res; n } n void update1(int o, int l, int r, int x, int y, double v) { n   if (x <= l && r <= y) { n     sum[o] += v * (r - l + 1); n     add[o] += v; n     return; n   } n   pushdown(o); n   int mid = (l + r) >> 1; n   if (x <= mid) update1(o * 2, l, mid, x, y, v); n   if (y > mid) update1(o * 2 + 1, mid + 1, r, x, y, v); n   pushup(o); n } n void update2(int o, int l, int r, int x, int y, double v) { n   if (x <= l && r <= y) { n     add[o] *= v; n     sum[o] *= v; n     mul[o] *= v; n     return; n   } n   pushdown(o); n   int mid = (l + r) >> 1; n   if (x <= mid) update2(o * 2, l, mid, x, y, v); n   if (y > mid) update2(o * 2 + 1, mid + 1, r, x, y, v); n   pushup(o); n } n int main() { n   scanf( %d%d , &n, &q); n   build(1, 1, n); n   while (q--) { n     int l1, r1, l2, r2; n     int id; n     scanf( %d , &id); n     if (id == 1) { n       scanf( %d%d%d%d , &l1, &r1, &l2, &r2); n       double sum1 = query(1, 1, n, l1, r1), sum2 = query(1, 1, n, l2, r2); n       double len1 = r1 - l1 + 1, len2 = r2 - l2 + 1; n       update2(1, 1, n, l1, r1, (len1 - 1) / len1); n       update2(1, 1, n, l2, r2, (len2 - 1) / len2); n       update1(1, 1, n, l1, r1, 1.0 / len1 * (sum2 / len2)); n       update1(1, 1, n, l2, r2, 1.0 / len2 * (sum1 / len1)); n     } else { n       scanf( %d%d , &l1, &r1); n       printf( %.7lf n , query(1, 1, n, l1, r1)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void connected(vector<vector<bool> > chu, vector<bool>& v, int s) { n   v[s] = 1; n   for (int i = 0; i < chu.size(); i++) { n     if (chu[s][i] && !v[i]) { n       connected(chu, v, i); n     } n   } n } n int main() { n   int n, m; n   while (cin >> n >> m) { n     if (n != m) { n       cout <<  NO  << endl; n       break; n     } n     vector<vector<bool> > chu(n, vector<bool>(n, 0)); n     for (int i = 0; i < m; i++) { n       int e1, e2; n       cin >> e1 >> e2; n       chu[e1 - 1][e2 - 1] = 1; n       chu[e2 - 1][e1 - 1] = 1; n     } n     vector<bool> visisted(n, 0); n     connected(chu, visisted, 0); n     bool allseen = true; n     for (int i = 0; i < visisted.size(); i++) { n       if (!visisted[i]) allseen = false; n     } n     if (allseen) { n       cout <<  FHTAGN!  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, m, op, det; n int dir[2]; n long long mv[2]; n int ans[1111111]; n int main() { n   scanf( %d%d , &n, &m); n   dir[1] = 1; n   dir[0] = -1; n   mv[1] = mv[0] = 0LL; n   for (int i = 0; i < m; i++) { n     scanf( %d , &op); n     if (op == 1) { n       scanf( %d , &det); n       mv[1] += det; n       mv[0] += det; n       if (det % 2) { n         dir[1] = -dir[1]; n         dir[0] = -dir[0]; n       } n     } else { n       mv[1] += dir[1]; n       mv[0] += dir[0]; n       dir[1] = -dir[1]; n       dir[0] = -dir[0]; n     } n   } n   mv[1] %= n; n   if (mv[1] < 0) mv[1] += n; n   mv[0] %= n; n   if (mv[0] < 0) mv[0] += n; n   for (int i = 1; i <= n; i++) { n     int id = i % 2; n     int p = i + mv[id]; n     if (p > n) p -= n; n     ans[p] = i; n   } n   for (int i = 1; i <= n; i++) printf( %d%c , ans[i], i < n ?     :   n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 Rand(time(0)); n const int maxn = 1e5 + 5; n const long long maxc = 1e9 + 7; n int n, m, d, a[maxn]; n long long sum[maxn]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> d >> m; n   for (int i = (1); i <= (n); ++i) cin >> a[i]; n   sort(a + 1, a + n + 1); n   for (int i = (1); i <= (n); ++i) sum[i] = sum[i - 1] + a[i]; n   if (a[n] <= m) return cout << sum[n], 0; n   int id = upper_bound(a + 1, a + n + 1, m) - a; n   long long res = a[n] + sum[id - 1]; n   long long ans = res; n   for (int i = (n - 1); i >= (id); --i) { n     if (1LL * (n - i) * d > n) continue; n     int r = (n - i) * d; n     if (r - (i - id) < id) { n       ans = max(ans, res + sum[n - 1] - sum[i - 1] - sum[max(0, r - (i - id))]); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long inf = 0x3f3f3f3f; n const long long llinf = 1e18; n const long long nax = 0; n int n, m, batas; n vector<pair<int, int>> shift; n set<char> simpen; n map<char, bool> sabi; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> n >> m >> batas; n   char arr[n + 5][m + 5]; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       cin >> arr[i][j]; n       if (arr[i][j] ==  S ) { n         shift.push_back({i, j}); n       } else { n         simpen.insert(arr[i][j]); n       } n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       for (auto x : shift) { n         int x1 = i, x2 = x.first, y1 = j, y2 = x.second; n         int hitung = ceil(sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))); n         if (hitung <= batas) { n           sabi[arr[i][j]] = 1; n         } n       } n     } n   } n   int q, ans = 0; n   cin >> q; n   string s; n   cin >> s; n   for (int k = 0; k < q; ++k) { n     if (s[k] >= 97) { n       if (!simpen.count(s[k])) { n         cout <<  -1  <<   n ; n         return 0; n       } else n         continue; n     } else { n       if (shift.empty()) { n         cout <<  -1  <<   n ; n         return 0; n       } n       bool ok = 0, ada = 0; n       if (!simpen.count(s[k] + 32)) { n         cout <<  -1  <<   n ; n         return 0; n       } else { n         if (sabi[s[k] + 32]) n           continue; n         else n           ans++; n       } n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 55, mod = 1LL * 1000 * 1000 * 1000 + 7; n int dp[N][N][2], ans[N][N][2], n, cnt50, cnt100, W, c[N][N], seen[N][N]; n bool vis[N][N][2], vis2[N][N][2]; n int C(int r, int n) { n   if (r == 0 || r == n) return 1; n   if (r > n) return 0; n   if (seen[r][n]) return c[r][n]; n   seen[r][n] = true; n   return c[r][n] = (C(r - 1, n - 1) + C(r, n - 1)) % mod; n } n int dfs(int x, int y, int tp) { n   if (vis2[x][y][tp]) return ans[x][y][tp]; n   vis2[x][y][tp] = true; n   if (tp) { n     for (int i = 0; i <= cnt50 - x; i++) n       for (int j = 0; j <= cnt100 - y; j++) n         if (i * 50 + j * 100 <= W && (i || j) && n             dp[x][y][tp] == dp[x + i][y + j][!tp] + 1) n           (ans[x][y][tp] += 1LL * dfs(x + i, y + j, !tp) * C(i, cnt50 - x) % n                             mod * C(j, cnt100 - y) % mod) %= mod; n   } else { n     for (int i = 0; i <= x; i++) n       for (int j = 0; j <= y; j++) n         if ((i || j) && i * 50 + j * 100 <= W && n             dp[x][y][tp] == dp[x - i][y - j][!tp] + 1) n           (ans[x][y][tp] += n            1LL * dfs(x - i, y - j, !tp) * C(i, x) % mod * C(j, y) % mod) %= mod; n   } n   return ans[x][y][tp]; n } n struct Node { n   int x = 0, y = 0, tp = 0; n   Node(int a = 0, int b = 0, int c = 0) { x = a, y = b, tp = c; } n }; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) n       for (int k = 0; k < 2; k++) dp[i][j][k] = 1 << 29; n   cin >> n >> W; n   for (int i = 0, a; i < n; i++) { n     cin >> a; n     cnt50 += (a == 50); n     cnt100 += (a == 100); n   } n   dp[0][0][0] = dp[0][0][1] = 0; n   vis[0][0][0] = vis[0][0][1] = true; n   ans[0][0][0] = ans[0][0][1] = 1; n   queue<Node> q; n   q.push(Node(0, 0, 0)), q.push(Node(0, 0, 1)); n   while (q.size()) { n     Node tmp = q.front(); n     q.pop(); n     int x = tmp.x, y = tmp.y, tp = tmp.tp; n     if (tp) { n       for (int i = 0; i <= cnt50 - x; i++) n         for (int j = 0; j <= cnt100 - y; j++) n           if (i * 50 + j * 100 <= W && (i || j) && !vis[x + i][y + j][!tp]) { n             q.push(Node(x + i, y + j, !tp)); n             dp[x + i][y + j][!tp] = dp[x][y][tp] + 1; n             vis[x + i][y + j][!tp] = true; n           } n     } else { n       for (int i = 0; i <= x; i++) n         for (int j = 0; j <= y; j++) n           if ((i || j) && i * 50 + 100 * j <= W && !vis[x - i][y - j][!tp]) { n             q.push(Node(x - i, y - j, !tp)); n             dp[x - i][y - j][!tp] = dp[x][y][tp] + 1; n             vis[x - i][y - j][!tp] = true; n           } n     } n   } n   dfs(cnt50, cnt100, 0); n   if (dp[cnt50][cnt100][0] >= (1 << 29)) dp[cnt50][cnt100][0] = -1; n   cout << dp[cnt50][cnt100][0] <<   n  << ans[cnt50][cnt100][0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 100005; n const int inf = 2147483647; n long long f[maxN], s[maxN], ans[maxN]; n int n, m, c, g, r, l[maxN], a[maxN], sa[maxN]; n int t[maxN * 8], b[maxN], h, mm; n int query(int l, int r) { n   l = lower_bound(b + 1, b + 1 + c, l) - b; n   r = upper_bound(b + 1, b + 1 + c, r) - b - 1; n   l += mm - 1; n   r += mm + 1; n   int k = inf; n   for (; l != r - 1; l >>= 1, r >>= 1) { n     if ((l & 1) == 0) k = ((k) < (t[l + 1]) ? (k) : (t[l + 1])); n     if ((r & 1) == 1) k = ((k) < (t[r - 1]) ? (k) : (t[r - 1])); n   } n   return k; n } n int ask(int x, int y) { n   if (x < 0) x += g + r; n   if (y < 0) y += g + r; n   if (x <= y) return query(x, y); n   int k = query(x, g + r - 1), t = query(0, y); n   return ((k) < (t) ? (k) : (t)); n } n void chg(int x, int d) { n   x = lower_bound(b + 1, b + 1 + c, x) - b; n   x += mm; n   t[x] = d; n   for (x >>= 1; x; x >>= 1) n     t[x] = ((t[x + x]) < (t[x + x + 1]) ? (t[x + x]) : (t[x + x + 1])); n } n void Init() { n   scanf( %d%d%d , &n, &g, &r); n   for (int i = 0; i <= n; ++i) scanf( %d , l + i); n   for (int i = n; i; --i) { n     s[i] = s[i + 1] + l[i]; n     b[i] = s[i] % (g + r); n   } n   sort(b + 1, b + 1 + n); n   c = unique(b + 1, b + 1 + n) - b - 1; n   for (; (c + 1) >> h; ++h) n     ; n   mm = 1 << h; n   for (int i = 1; i <= mm + 1 + c; ++i) t[i] = inf; n   for (int i = n; i; --i) { n     int k = ask((s[i] - (g + r - 1)) % (g + r), (s[i] - g) % (g + r)); n     if (k > n) n       f[i] = s[i]; n     else n       f[i] = (s[i] - s[k] + g + r - 1) / (g + r) * (g + r) + f[k]; n     chg(s[i] % (g + r), i); n   } n } n void Work() { n   long long p; n   scanf( %d , &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %d , a + i); n     p = a[i] + l[0] + s[1]; n     int k = ask((p - (g + r - 1)) % (g + r), (p - g) % (g + r)); n     if (k > n) n       printf( %I64d n , p); n     else n       printf( %I64d n , (p - s[k] + g + r - 1) / (g + r) * (g + r) + f[k]); n   } n } n int main() { n   Init(); n   Work(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int N, M, dbg[100005]; n pair<int, int> A[100005]; n map<int, int> mp; n int main() { n   scanf( %d , &N); n   for (int i = 0; i < N; i++) { n     int a; n     scanf( %d , &a); n     mp[a]++; n   } n   for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) n     A[M++] = *it; n   for (int i = 1; i < M; i++) n     if (A[i].first != A[i - 1].first + 1) { n       puts( NO ); n       return 0; n     } n   int p = 0; n   for (int i = 0; i < M; i++) { n     A[i].second--; n     dbg[p++] = A[i].first; n   } n   for (int i = M - 1; i > 1; i--) { n     while (A[i].second) { n       if (!A[i - 1].second) { n         puts( NO ); n         return 0; n       } n       A[i - 1].second--; n       A[i].second--; n       dbg[p++] = A[i - 1].first; n       dbg[p++] = A[i].first; n     } n     if (!A[i - 1].second) { n       puts( NO ); n       return 0; n     } n     A[i - 1].second--; n     dbg[p++] = A[i - 1].first; n   } n   while (A[1].second) { n     if (!A[0].second) { n       puts( NO ); n       return 0; n     } n     A[0].second--; n     A[1].second--; n     dbg[p++] = A[0].first; n     dbg[p++] = A[1].first; n   } n   if (A[0].second) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1000010]; n int n; n bool check(int len) { n   int now = 0, lft = 0; n   bool flag = 1; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  R ) { n       flag = 0; n       if (now + 1 != len) now++; n     } else n       now--; n     if (now < lft) { n       lft = now; n       flag = 1; n     } n   } n   return flag; n } n int main() { n   scanf( %s , s); n   n = strlen(s); n   if (s[n - 1] ==  R ) { n     for (int i = 0; i < n; i++) { n       if (s[i] ==  R ) n         s[i] =  L ; n       else n         s[i] =  R ; n     } n   } n   if (check(n + 5)) { n     puts( 1 ); n     return 0; n   } n   int l = 0, r = n + 5, ret = 0; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (check(mid)) n       ret = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   printf( %d n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, cnt, flag; n vector<int> graph[100005]; n bool vis[100005]; n void dfs(int node, int parent) { n   vis[node] = 1; n   int x, u, v, i; n   cnt++; n   for (i = 0; i < graph[node].size(); i++) { n     v = graph[node][i]; n     if (v == parent) continue; n     if (vis[v] == 1) n       flag = 1; n     else n       dfs(v, node); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   int i, j, k, u, v, x, sum = 0; n   for (i = 0; i < m; i++) { n     cin >> u >> v; n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   for (i = 1; i <= n; i++) { n     if (vis[i] == 0) { n       cnt = 0; n       flag = 0; n       dfs(i, 0); n       if (flag == 1) n         sum += cnt; n       else n         sum += (cnt - 1); n     } n   } n   cout << n - sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> g(n); n   int mm = 99999; n   for (int i = 0; i < n; i++) { n     cin >> g[i]; n     mm = min(mm, abs(g[i])); n   } n   sort(g.begin(), g.end()); n   int rest = k; n   for (int i = 0; i < k; i++) { n     if (i < g.size() && g[i] < 0) { n       g[i] = -g[i]; n       rest--; n     } n   } n   int ans = 0; n   for (int i = 0; i < g.size(); i++) { n     ans += g[i]; n   } n   if (rest % 2) { n     ans -= 2 * mm; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int>> g[500010]; n int rad[2][500010], tata[500010], niv[500010]; n char in[500010], vaz[500010], tip[500010]; n struct edge { n   int x, y, c; n }; n edge v[500010]; n int radacina(int x, int t) { n   int y = x; n   while (y != rad[t][y]) y = rad[t][y]; n   while (rad[t][x] != y) { n     int aux = rad[t][x]; n     rad[t][x] = y; n     x = aux; n   } n   return y; n } n void unite(int x, int y) { n   x = radacina(x, 0); n   y = radacina(y, 0); n   rad[0][x] = y; n } n void dfs(int nod) { n   vaz[nod] = 1; n   for (int i = 0; i < g[nod].size(); i++) { n     int vec = g[nod][i].first; n     if (vaz[vec] == 1) continue; n     niv[vec] = niv[nod] + 1; n     tata[vec] = nod; n     tip[vec] = g[nod][i].second; n     dfs(vec); n   } n } n int main() { n   int n, k, m, x, y; n   long long sol = 0; n   scanf( %d%d%d , &n, &k, &m); n   for (int i = 1; i <= n; i++) rad[0][i] = rad[1][i] = i; n   for (int i = 1; i <= k; i++) { n     scanf( %d%d , &x, &y); n     unite(x, y); n     g[x].push_back({y, 1}); n     g[y].push_back({x, 1}); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &v[i].x, &v[i].y, &v[i].c); n     if (radacina(v[i].x, 0) != radacina(v[i].y, 0)) { n       unite(v[i].x, v[i].y); n       in[i] = 1; n       g[v[i].x].push_back({v[i].y, 0}); n       g[v[i].y].push_back({v[i].x, 0}); n     } n   } n   dfs(1); n   tata[1] = 1; n   int nr = 0; n   for (int i = 1; i <= m; i++) { n     if (in[i] == 1) continue; n     x = v[i].x; n     y = v[i].y; n     x = radacina(x, 1); n     y = radacina(y, 1); n     while (x != y) { n       if (niv[x] < niv[y]) swap(x, y); n       sol += tip[x] * v[i].c; n       nr += tip[x]; n       rad[1][x] = tata[x]; n       x = radacina(x, 1); n     } n   } n   if (nr < k) n     printf( -1 ); n   else n     printf( %I64d , sol); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int f(int y) { n   return y % 400 == 0 ? 2 : y % 100 == 0 ? 1 : y % 4 == 0 ? 2 : 1; n } n int main() { n   int n, r = 0, t; n   scanf( %d , &n); n   t = f(n); n   while (r % 7 != 0 || r == 0 || t != f(n)) { n     r += f(n); n     n++; n   } n   printf( %d n , n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long binSearch(long long n, long long pow2) { n   long long L = 0, R = (n / pow2) + 10, M; n   while (R - L > 1) { n     M = (L + R) / 2; n     double dM = M + 0.0; n     if ((2.0 * dM + 1.0) * (pow2 + dM - 1.0) < n + 0.0) n       L = M; n     else n       R = M; n   } n   if ((2 * L + 1) * (pow2 + L - 1) == n) n     return L; n   else if ((2 * R + 1) * (pow2 + R - 1) == n) n     return R; n   else n     return -1; n } n int main() { n   long long n; n   cin >> n; n   vector<long long> result; n   for (long long pow2 = 1; pow2 - 1 <= n; pow2 *= 2) { n     long long v = binSearch(n, pow2); n     if (v != -1) result.push_back((2 * v + 1) * pow2); n   } n   sort(result.begin(), result.end()); n   if (result.size() == 0) n     cout << -1; n   else { n     for (int i = 0; i < result.size(); i++) cout << result[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int max(int a, int b) { return a > b ? a : b; } n pair<int, int> a[100], ans[1000], p; n vector<pair<int, int>> v; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i + 1; n   } n   sort(a, a + n); n   int i = 0; n   while (i < k) { n     if (a[0].first - a[n - 1].first >= -1) break; n     a[0].first++; n     a[n - 1].first--; n     p.first = a[0].second; n     p.second = a[n - 1].second; n     v.push_back(p); n     sort(a, a + n); n     i++; n   } n   cout << a[n - 1].first - a[0].first <<     << i << endl; n   for (int k = 0; k < i; k++) { n     cout << v[k].second <<     << v[k].first << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<double> E[100010]; n int color[100010]; n int seen[100010]; n set<int> sets[100010]; n void dfs(int u) { n   for (int i = 0; i < E[u].size(); ++i) { n     int v = E[u][i]; n     if (color[u] != color[v]) sets[color[u]].insert(color[v]); n     if (!seen[v]) { n       seen[v] = true; n       dfs(v); n     } n   } n } n int main() { n   int n, m, u, v; n   cin >> n >> m; n   set<int> colorids; n   for (int i = 0; i < n; ++i) { n     cin >> color[i]; n     colorids.insert(color[i]); n   } n   for (int i = 0; i < m; ++i) { n     cin >> u >> v; n     --u; n     --v; n     E[u].push_back(v); n     E[v].push_back(u); n   } n   memset(seen, 0, sizeof(seen)); n   for (int i = 0; i < n; ++i) n     if (!seen[i]) { n       seen[i] = true; n       dfs(i); n     } n   int maxcolor = -1; n   int maxn = -(1 << 30); n   for (typeof(colorids.begin()) it = colorids.begin(); it != colorids.end(); n        ++it) { n     int color = *it; n     int ncolors = sets[*it].size(); n     if (maxn < ncolors) { n       maxn = ncolors; n       maxcolor = *it; n     } n   } n   cout << maxcolor << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll N = 1e6 + 100; n ll n; n ll m; n ll **arr() { n   ll **res = new ll *[n + 2]; n   for (ll i = 0; i < n + 2; ++i) { n     res[i] = new ll[m + 2]; n     fill_n(res[i], m + 2, 0); n   } n   return res; n } n ll **inp; n ll **ans; n void pref(ll **a) { n   for (ll i = 1; i <= n + 1; ++i) { n     for (ll j = 1; j <= m + 1; ++j) { n       a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; n     } n   } n } n ll sum(ll **a, ll il, ll ir, ll jl, ll jr) { n   il = max(il, 1ll); n   ir = min(ir, n); n   jl = max(jl, 1ll); n   jr = min(jr, m); n   --il; n   --jl; n   return a[ir][jr] - a[ir][jl] - a[il][jr] + a[il][jl]; n } n ll sq(ll first) { return first * first; } n bool check(ll t) { n   for (ll i = 1; i <= n; ++i) { n     for (ll j = 1; j <= m; ++j) { n       ans[i][j] = sum(inp, i - t, i + t, j - t, j + t) == sq(2 * t + 1); n     } n   } n   pref(ans); n   for (ll i = 1; i <= n; ++i) { n     for (ll j = 1; j <= m; ++j) { n       if (sum(inp, i, i, j, j) && sum(ans, i - t, i + t, j - t, j + t) == 0) { n         return false; n       } n     } n   } n   return true; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   inp = arr(); n   ans = arr(); n   for (ll i = 1; i <= n; ++i) { n     string s; n     cin >> s; n     for (ll j = 1; j <= m; ++j) { n       inp[i][j] = s[j - 1] ==  X ; n     } n   } n   pref(inp); n   ll lef = 0, rig = N; n   while (rig - lef > 1) { n     ll mid = (lef + rig) / 2; n     if (check(mid)) { n       lef = mid; n     } else { n       rig = mid; n     } n   } n   assert(check(lef)); n   cout << lef <<   n ; n   for (ll i = 1; i <= n; ++i) { n     for (ll j = 1; j <= m; ++j) { n       cout <<  .X [sum(ans, i, i, j, j)]; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n const int N = 1e3 + 5; n int f[N][N]; n struct node { n   long long x, y; n } a[N], b[N]; n bool cmp(node x, node y) { return x.y < y.y; } n long long area(node a, node b, node c) { n   return a.x * b.y - a.y * b.x + b.x * c.y - b.y * c.x + c.x * a.y - c.y * a.x; n } n bool toleft(node a, node b, node c) { n   if (c.y <= a.y || c.y > b.y) return 0; n   return area(a, b, c) > 0; n } n int n, m; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) scanf( %lld%lld , &a[i].x, &a[i].y); n   for (int i = 1; i <= m; ++i) scanf( %lld%lld , &b[i].x, &b[i].y); n   sort(a + 1, a + n + 1, cmp); n   int ans = 0; n   for (int i = 1; i < n; ++i) n     for (int j = i + 1; j <= n; ++j) n       for (int k = 1; k <= m; ++k) n         if (toleft(a[i], a[j], b[k])) f[i][j]++; n   for (int i = 1; i < n; ++i) n     for (int j = i + 1; j < n; ++j) n       for (int k = j + 1; k <= n; ++k) n         if (f[i][j] + f[j][k] == f[i][k]) ans++; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n const int INF = 2000000000; n const long long BIG = 1446803456761533460LL; n const int maxn = 5010; n int n, m, k, dp[maxn][maxn]; n int poww(int a, int b) { n   int r = 1; n   while (b) { n     if (b & 1) r = (1LL * (r) * (a)) % MOD; n     a = (1LL * (a) * (a)) % MOD; n     b >>= 1; n   } n   return r; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m >> k; n   int mi = poww(m, MOD - 2); n   dp[0][0] = 1; n   for (int i = 0; i < k; ++i) { n     for (int j = 0; j < k + 1; ++j) { n       dp[i + 1][j] = ((dp[i + 1][j]) + (1LL * dp[i][j] * j % MOD)) % MOD; n       dp[i + 1][j + 1] = n           ((dp[i + 1][j + 1]) + (1LL * dp[i][j] * (n - j) % MOD * mi % MOD)) % n           MOD; n     } n   } n   int ans = 0; n   for (int i = 0; i < k + 1; ++i) ans = ((ans) + (dp[k][i])) % MOD; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400000; n int a[N], t[N], type[N], us[N]; n int main() { n   ios::sync_with_stdio(0); n   int n, w, k; n   cin >> n >> w >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> t[i]; n   } n   int r = -1; n   int curr_time = 0; n   int earn = 0; n   set<pair<int, int> > half, full; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     r = max(r, i - 1); n     while (r < n - 1) { n       r++; n       int temp = curr_time; n       vector<pair<int, int> > pers_full, pers_half, pers_type; n       half.insert(make_pair(t[r], r)); n       pers_half.push_back(make_pair(+1, r)); n       pers_type.push_back(make_pair(r, type[r])); n       type[r] = 0; n       curr_time += (t[r] + 1) / 2; n       if (half.size() > w) { n         auto p = *half.begin(); n         curr_time -= (p.first + 1) / 2; n         half.erase(half.find(p)); n         pers_half.push_back(make_pair(-1, p.second)); n         curr_time += p.first; n         full.insert(p); n         pers_full.push_back(make_pair(+1, p.second)); n         pers_type.push_back(make_pair(p.second, type[p.second])); n         type[p.second] = 1; n       } n       if (curr_time > k) { n         while (pers_full.size()) { n           auto p = pers_full.back(); n           pers_full.pop_back(); n           if (p.first == -1) n             full.insert(make_pair(t[p.second], p.second)); n           else n             full.erase(make_pair(t[p.second], p.second)); n         } n         while (pers_half.size()) { n           auto p = pers_half.back(); n           pers_half.pop_back(); n           if (p.first == -1) n             half.insert(make_pair(t[p.second], p.second)); n           else n             half.erase(make_pair(t[p.second], p.second)); n         } n         while (pers_type.size()) { n           auto p = pers_type.back(); n           pers_type.pop_back(); n           type[p.first] = p.second; n         } n         curr_time = temp; n         r--; n         break; n       } else { n         earn += a[r]; n         us[r] = 1; n       } n     } n     ans = max(earn, ans); n     if (us[i] == 1) { n       if (type[i] == 0) { n         half.erase(make_pair(t[i], i)); n         curr_time -= (t[i] + 1) / 2; n       } else { n         full.erase(make_pair(t[i], i)); n         curr_time -= t[i]; n       } n       earn -= a[i]; n       us[i] = 0; n       while (full.size() && half.size() < w) { n         auto p = *full.rbegin(); n         full.erase(full.find(p)); n         half.insert(p); n         curr_time -= p.first; n         curr_time += (p.first + 1) / 2; n         type[p.second] = 0; n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n using ll = long long; n using pii = pair<ll, int>; n const int INF = 1e9 + 1e2; n void fin() {} n const bool flag = 0; n const int N = 1e5 + 10; n int MOD; n int mult(ll a, ll b) { return a * 1ll * b % MOD; } n ll binpow(ll a, int b) { n   ll res = 1; n   while (b) { n     if (b & 1) { n       res = mult(res, a); n     } n     a = mult(a, a); n     b >>= 1; n   } n   return res; n } n vector<pii> adj[N]; n int sz[N], po[N]; n bool cent[N]; n void calc_sz(int first, int p = -1) { n   sz[first] = 1; n   for (pii to : adj[first]) { n     if (to.first != p && !cent[to.first]) { n       calc_sz(to.first, first); n       sz[first] += sz[to.first]; n     } n   } n } n int c_sz; n int fnd(int first, int p = -1) { n   for (pii to : adj[first]) { n     if (to.first != p && !cent[to.first] && sz[to.first] > c_sz / 2) { n       return fnd(to.first, first); n     } n   } n   return first; n } n ll ans; n int add(ll a, int b) { return (a + b + MOD) % MOD; } n map<int, ll> cnt; n void dfsadd(int first, int d, int r, int p = -1) { n   cnt[r]++; n   for (pii to : adj[first]) { n     if (to.first != p && !cent[to.first]) { n       dfsadd(to.first, d + 1, add(r, mult(po[d], to.second)), first); n     } n   } n } n void dfscalc(int first, int d, int r1, int r2, int p = -1) { n   ans += (r1 == 0) + (r2 == 0); n   for (pii to : adj[first]) { n     if (to.first != p && !cent[to.first]) { n       dfscalc(to.first, d + 1, add(mult(r1, 10), to.second), n               add(r2, mult(po[d], to.second)), first); n     } n   } n } n int phi; n void dfsget(int first, int d, int r, int p = -1) { n   int inv = mult(MOD - r, binpow(po[d], phi - 1)); n   ans += cnt[inv]; n   for (pii to : adj[first]) { n     if (to.first != p && !cent[to.first]) { n       dfsget(to.first, d + 1, add(mult(r, 10), to.second), first); n     } n   } n } n void solve(int first = 0) { n   calc_sz(first); n   c_sz = sz[first]; n   first = fnd(first); n   cent[first] = 1; n   for (pii to : adj[first]) { n     if (!cent[to.first]) { n       dfsget(to.first, 1, to.second); n       dfsadd(to.first, 1, to.second); n       dfscalc(to.first, 1, to.second, to.second); n     } n   } n   cnt.clear(); n   reverse((adj[first]).begin(), (adj[first]).end()); n   for (pii to : adj[first]) { n     if (!cent[to.first]) { n       dfsget(to.first, 1, to.second); n       dfsadd(to.first, 1, to.second); n     } n   } n   cnt.clear(); n   for (pii to : adj[first]) { n     if (!cent[to.first]) { n       solve(to.first); n     } n   } n } n void ma1n() { n   int n; n   cin >> n >> MOD; n   for (int i = 1; i < n; ++i) { n     int u, v, w; n     cin >> u >> v >> w; n     w %= MOD; n     adj[u].push_back({v, w}); n     adj[v].push_back({u, w}); n   } n   int first = MOD; n   phi = MOD; n   for (int i = 2; i <= sqrt(first); ++i) { n     if (first % i == 0) { n       while (first % i == 0) { n         first /= i; n       } n       phi -= phi / i; n     } n   } n   if (first > 1) { n     phi -= phi / first; n   } n   po[0] = 1; n   for (int i = 1; i < N; ++i) { n     po[i] = mult(po[i - 1], 10); n   } n   solve(); n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr), fin(); n   int ts = 1; n   if (flag) { n     cin >> ts; n   } n   while (ts--) { n     ma1n(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sePuede(int rango[], int x) { n   if (x % 2 == 1 && rango[0] == 1 && rango[x - 1] == 1) return true; n   return false; n } n int main() { n   int x = 0; n   int impar = 0; n   cin >> x; n   int rango[x]; n   for (int y = 0; y < x; y++) { n     cin >> rango[y]; n     rango[y] = rango[y] % 2; n   } n   if (sePuede(rango, x)) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void go(int parent, int node, int depth, int& odd_count, int& even_count, n         vector<vector<int>>& tree) { n   int count = 0; n   for (int i = 0; i < tree[node].size(); i++) { n     if (tree[node][i] != parent) { n       count++; n       go(node, tree[node][i], depth + 1, odd_count, even_count, tree); n     } n   } n   if (count == 0) { n     odd_count += depth % 2; n     even_count += !(depth % 2); n   } n } n int main() { n   int n; n   scanf( %d , &n); n   vector<vector<int>> tree(n); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     tree[u - 1].push_back(v - 1); n     tree[v - 1].push_back(u - 1); n   } n   int root = -1; n   for (int i = 0; i < tree.size(); i++) { n     if (tree[i].size() > 1) { n       root = i; n       break; n     } n   } n   int odd_count = 0, even_count = 0; n   go(-1, root, 0, odd_count, even_count, tree); n   int min_ans = 0, max_ans = n - 1; n   if (!odd_count || !even_count) { n     min_ans = 1; n   } else { n     min_ans = 3; n   } n   vector<int> parent_leaf_count(n, 0); n   for (int i = 0; i < n; i++) { n     if (tree[i].size() == 1) { n       parent_leaf_count[tree[i][0]]++; n     } n   } n   for (int i = 0; i < n; i++) { n     if (parent_leaf_count[i]) { n       max_ans -= (parent_leaf_count[i] - 1); n     } n   } n   printf( %d %d n , min_ans, max_ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   cin >> s; n   bool ms = false; n   string a; n   string b; n   if (s[0] ==  - ) ms = true; n   bool flag = false; n   bool p = ms; n   for (int i = 0; i < s.size(); i++) { n     if (p) { n       p = false; n       continue; n     } n     if (s[i] ==  . ) { n       flag = true; n       continue; n     } n     if (!flag) n       a.push_back(s[i]); n     else n       b.push_back(s[i]); n   } n   int l = a.size(); n   if (ms) cout <<  ( ; n   cout <<  $ ; n   for (int i = 0; i < l; i++) { n     if ((l - i) % 3 == 0 && i != 0) cout <<  , ; n     cout << a[i]; n   } n   cout <<  . ; n   if (b.size() >= 2) n     cout << b[0] << b[1]; n   else if (b.size() == 1) n     cout << b[0] <<  0 ; n   else n     cout <<  00 ; n   if (ms) cout <<  ) ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     int a[n], b[n]; n     for (int i = 0; i < n; i++) cin >> a[i]; n     string s; n     cin >> s; n     vector<pair<long long, pair<long long, long long>>> c(n); n     for (int i = 0; i < n; i++) { n       c[i].second.first = a[i]; n       c[i].second.second = i; n       c[i].first = (s[i] -  0 ); n     } n     sort(c.begin(), c.end()); n     long long cnt = 1; n     for (int i = 0; i < n; i++) { n       b[c[i].second.second] = cnt; n       cnt++; n     } n     for (int i = 0; i < n; i++) { n       cout << b[i] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long dp_cata[3021][5021]; n int main() { n   dp_cata[0][0] = 1; n   for (long long j = 0; j <= 5001; j++) n     for (long long i = 0; i <= 3001; i++) { n       if (i == 0) { n         if (j != 0) dp_cata[i][j] = dp_cata[i + 1][j - 1]; n       } else n         dp_cata[i][j] = n             (dp_cata[i + 1][j - 1] + dp_cata[i - 1][j - 1]) % 1000000007; n     } n   long long n, m; n   string s; n   cin >> n >> m >> s; n   if (n % 2 != 0) { n     cout << 0; n     exit(0); n   } n   long long x = 0, y = 0; n   long long lientiep = 0; n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] ==  ( ) n       x++; n     else n       y++; n     lientiep = max(lientiep, y - x); n   } n   if (x > n / 2 || y > n / 2) { n     cout << 0; n     exit(0); n   } n   long long X = n / 2 - x; n   long long Y = n / 2 - y; n   long long ans = 0; n   for (int i = 0; i <= n - m; i++) { n     for (int j = 0; j <= i; j++) n       if (j + x >= i - j + y && j - (i - j) >= lientiep) { n         if (j < i - j) continue; n         ans = n             (ans + n              dp_cata[j - (i - j)][i] * n                  dp_cata[2 * (n / 2 - (i - j + y)) - (n - m - i)][n - m - i]) % n             1000000007; n       } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ans; n int G[10][10]; n int p[10]; n bool vis[10][10]; n void dfs(int cur) { n   if (cur > n) { n     memset(vis, 0, sizeof(vis)); n     int cnt = 0; n     for (int i = 1; i <= n; i++) { n       for (int j = i + 1; j <= n; j++) { n         if (!G[i][j]) continue; n         int u = p[i]; n         int v = p[j]; n         if (!vis[u][v]) { n           vis[u][v] = vis[v][u] = true; n           cnt++; n         } n       } n     } n     ans = max(ans, cnt); n     return; n   } n   for (int i = 1; i <= 6; i++) { n     p[cur] = i; n     dfs(cur + 1); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[u][v] = G[v][u] = 1; n   } n   dfs(1); n   printf( %d n , ans); n   return 0; n } n


n #include<bits/stdc++.h> n #define ll long long n #define ld long double n #define ff first n #define ss second n #define eb emplace_back n #define pb push_back n #define mp make_pair n #define all(x) begin(x), end(x) n #define endl   n  n int dx[4] = {1, -1, 0, 0}; n int dy[4] = {0, 0, -1, 1}; n // ll dp[100005]; n ll mod = 1e9 + 7; n bool vis[100000]; n using namespace std; n bool isPrime(ll n) { n   ll j; n   for ( j = 2; j <= sqrt(n); j++) { n     if (n % j == 0) { n       break; n     } n   } n   if (j > sqrt(n)) { n     return 1; n   } n   else { n     return 0; n   } n } n ll modexpo(ll a, ll b) { n   ll ans = 1; n   a = a % mod; n   while (b > 0) { n     if ((b & 1) == 1) { n       ans = ((ans % mod) * (a % mod)) % mod; n     } n     b = b >> 1; n     a = ((a % mod) * (a % mod)) % mod; n   } n   return ans; n } n ll invmod(ll n) { n   return modexpo(n, mod - 2); n } n  n ll comb(ll n, ll r) { n   if (r == 0) { n     return 1; n   } n   ll fact[n + 1]; n   fact[0] = 1; n   for (ll i = 1; i <= n; i++) { n     fact[i] = (fact[i - 1] * i) % mod; n   } n   return (fact[n] * invmod(fact[r]) % mod * invmod(fact[n - r]) % mod) % mod; n } n ll gcd(ll a, ll b) { n   if (a == 0) { n     return b; n   } n   return gcd(b % a, a); n } n ll lcm(ll a, ll b) { n   return (a * b) / gcd(a, b); n } n void dfs(ll i, vector<ll>adj[], bool visited[]) { n   visited[i] = 1; n   for (auto j : adj[i]) { n     if (visited[j] == 0) { n       dfs(j, adj, visited); n     } n   } n } n bool sortbysecdesc(const pair<ll, ll> &a, const pair<ll, ll> &b) n { n   return a.second > b.second; n } n void solve(ll k) { n   ll n; n   cin >> n; n   ll w[n + 1], ans = 0; n   w[0] = 0; n   vector<pair<ll, ll>>p; n   for (ll i = 0; i < n; i++) { n     cin >> w[i + 1]; n     p.pb(mp(i + 1, w[i + 1])); n     ans += w[i + 1]; n   } n   ll in[n + 1]; n // memset(in, -1, sizeof(in)); n   for (ll i = 0; i <= n; i++) { n     in[i] = -1; n   } n   for (ll i = 0; i < n - 1; i++) { n     ll u, v; n     cin >> u >> v; n     in[u]++, in[v]++; n   } n   cout << ans <<    ; n   sort(p.begin(), p.end(), sortbysecdesc); n   for (ll i = 0; i < p.size(); i++) { n     while (in[p[i].first] > 0) { n       ans += p[i].second; n       cout << ans <<    ; n       in[p[i].first]--; n     } n   } n   cout << endl; n } n  n  n int main() { n #ifndef ONLINE_JUDGE n   // for getting input from input.txt n   freopen( input.txt ,  r , stdin); n   // for writing output to output.txt n   freopen( output.txt ,  w , stdout); n #endif n  n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ll t = 1; n   cin >> t; n   ll k = 1; n   while (k <= t) { n     solve(k); n     k++; n   } n   return 0; n } n // f3=f2-f1,f4=-f1,f5=-f2,f6=f1-f2,f7=f1,f8=f2,f9=f2-f1,


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<pair<int, int>, int> dat; n   vector<pair<int, int> > p; n   int np; n   cin >> np; n   int res = 0; n   while (np--) { n     int x, y; n     cin >> x >> y; n     p.push_back(make_pair(x, y)); n     dat[make_pair(x, y)] = 1; n   } n   int sz = p.size(); n   for (int i = 0; i < sz; i++) n     for (int j = i + 1; j < sz; j++) { n       double x = p[i].first + p[j].first; n       double y = p[i].second + p[j].second; n       x /= 2.0; n       y /= 2.0; n       pair<int, int> tmp; n       if (fabs(x - int(x)) < 1e-9 && fabs(y - int(y)) < 1e-9) { n         tmp = make_pair(int(x), int(y)); n         if (dat.find(tmp) != dat.end()) res++; n       } n     } n   cout << res << endl; n } n


#include <iostream> n #include <cstdio> n #define mod 998244353 n #define int long long n using namespace std; n int n,k,sa[200005],rk[200005]; n int num; n inline int Qpow(register int a,register int p){ n  if (p==0)return 1; n  register int t=Qpow(a,p/2); n  t=t*t%mod; n  if (p%2==1)t=t*a%mod; n  return t; n } n inline int inv(register int x){ n  return Qpow(x,mod-2); n } n inline int C(register int n,register int m){ n  if (n<m||m<0)return 0; n  register int ans=1; n  for (register int i=1;i<=n;i++)ans=ans*i%mod; n  for (register int i=1;i<=m;i++)ans=ans*inv(i)%mod; n  for (register int i=1;i<=n-m;i++)ans=ans*inv(i)%mod; n  return ans; n } n signed main(){ n  cin>>n>>k; n  for (register int i=1;i<=n;i++){ n   scanf( %d ,&sa[i]); n   sa[i]++; n   rk[sa[i]]=i; n  } n  for (register int i=1;i<n;i++) n   if (rk[sa[i]+1]>rk[sa[i+1]+1])k--; n  cout<<C(n+k-1,k-1)<<endl; n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int arr[7]; n int main() { n   int n, len; n   string str; n   scanf( %d n , &n); n   for (int i = 0; i < n; i++) { n     getline(cin, str); n     for (int j = 0; j < 7; j++) { n       arr[j] += (str[j] -  0 ); n     } n   } n   int mx = 0; n   for (int i = 0; i < 7; i++) mx = max(arr[i], mx); n   printf( %d n , mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct P { n   double x, y; n } a[259]; n long long f[259][259]; n int n; n int read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n double cross(P a, P b, P c) { n   P x = (P){b.x - a.x, b.y - a.y}; n   P y = (P){c.x - a.x, c.y - a.y}; n   return x.x * y.y - x.y * y.x; n } n bool jud() { n   double ans = 0; n   for (int i = 2; i <= n; i++) ans += cross(a[1], a[i - 1], a[i]); n   if (ans > 0) return 1; n   return 0; n } n void rever() { n   for (int i = 1; i <= n / 2; i++) swap(a[i], a[n - i + 1]); n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) scanf( %lf%lf , &a[i].x, &a[i].y); n   if (jud()) rever(); n   for (int i = 1; i <= n; i++) f[i][i + 1] = 1; n   for (int l = 1; l <= n - 1; l++) n     for (int i = 1; i + l <= n; i++) n       for (int j = i + 1; j < i + l; j++) n         f[i][i + l] = n             (f[i][i + l] + f[i][j] % 1000000007 * f[j][i + l] % 1000000007 * n                                (cross(a[i], a[j], a[i + l]) < 0 ? 1 : 0)) % n             1000000007; n   printf( %lld n , f[1][n] % 1000000007); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, f, a, b; n vector<long long> V; n long long ans; n int main() { n   scanf( %lld %lld , &n, &f); n   for (int i = 1; i <= n; i++) { n     scanf( %lld %lld , &a, &b); n     if (a >= b) { n       V.push_back(0); n       ans += b; n     } else { n       if (a * 2 <= b) { n         V.push_back(a); n       } else { n         V.push_back(b - a); n       } n       ans += a; n     } n   } n   sort(V.begin(), V.end()); n   reverse(V.begin(), V.end()); n   for (int i = 0; i < f; i++) { n     ans += V[i]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int customer[1000][1000]; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   deque<int> dq; n   for (int i = 0; i < k; i++) { n     int x; n     cin >> x; n     dq.push_back(x); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cin >> customer[i][j]; n   } n   int t = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int cnt = 0; n       for (deque<int>::iterator l = dq.begin(); l != dq.end(); l++) { n         cnt++; n         if (*l == customer[i][j]) { n           int x = *l; n           dq.erase(l); n           dq.push_front(x); n           t += cnt; n           break; n         } n       } n     } n   } n   cout << t; 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     string s; n     cin >> n >> s; n     string an =   ; n     for (int i = 0; i < s.length(); i += 2) { n       cout << s[i]; n     } n     cout << endl; 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     cout << 1; n     return 0; n   } n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       while (n % i == 0) { n         n /= i; n       } n       if (n == 1) { n         cout << i; n       } else { n         cout << 1; n       } n       getchar(); n       getchar(); n       return 0; n     } n   } n   cout << n; n   getchar(); n   getchar(); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   string s = bitset<32>(n).to_string(); n   vector<int> v; n   for (int i = 31; i >= 0; i--) { n     if (s[i] ==  1 ) { n       v.push_back(32 - i); n     } n   } n   reverse(v.begin(), v.end()); n   for (int& a : v) { n     cout << a <<    ; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t = 1, i = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 42; n long long dp[N][N]; n int f[N], n; n string s; n long long Solve(int x) { n   memset(dp, 0, sizeof dp); n   dp[0][x] = 1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < s.size(); j++) { n       int m = j; n       while (m && s[m] !=  0 ) m = f[m]; n       if (s[m] ==  0 ) m++; n       dp[i + 1][m] += dp[i][j]; n       m = j; n       while (m && s[m] !=  1 ) m = f[m]; n       if (s[m] ==  1 ) m++; n       dp[i + 1][m] += dp[i][j]; n     } n   } n   return dp[n][x]; n } n int main() { n   cin >> n >> s; n   for (int i = 2; i <= s.size(); i++) { n     f[i] = f[i - 1]; n     while (f[i] && s[f[i]] != s[i - 1]) f[i] = f[f[i]]; n     if (s[f[i]] == s[i - 1]) f[i]++; n   } n   long long ans = 0; n   for (int i = 0; i < s.size(); i++) ans += Solve(i); n   cout << (1ll << n) - ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200000 + 10; n const int hashnum = 3; n long long base[] = {233, 23, 97}; n long long mod[] = {122420729, 1000000007, 1000000009}; n int sum[maxn]; n char s[maxn]; n long long power[hashnum][maxn]; n int n; n struct node { n   long long hv[hashnum]; n   int pref, suf, len; n }; n node zer; n inline void init() { n   zer.pref = zer.suf = zer.len = 0; n   for (int i = (0); i < (hashnum); ++i) zer.hv[i] = 0; n   for (int i = (0); i < (hashnum); ++i) { n     power[i][0] = 1; n     for (int j = (1); j < (maxn); ++j) n       power[i][j] = power[i][j - 1] * base[i] % mod[i]; n   } n } n node merge(node& a, node& b) { n   node c = zer; n   if (a.len == 0 && b.len == 0) n     c.suf = c.pref = a.pref + b.pref; n   else if (a.len == 0) n     c = b, c.pref += a.pref; n   else if (b.len == 0) n     c = a, c.suf += b.pref; n   else { n     c.len = a.len + b.len + (a.suf + b.pref) % 2; n     c.pref = a.pref; n     c.suf = b.suf; n     for (int i = (0); i < (hashnum); ++i) { n       if ((a.suf + b.pref) % 2 == 1) { n         c.hv[i] = a.hv[i] * power[i][b.len + 1] % mod[i]; n         (c.hv[i] += 2 * power[i][b.len] % mod[i]) %= mod[i]; n         (c.hv[i] += b.hv[i]) %= mod[i]; n       } else { n         c.hv[i] = a.hv[i] * power[i][b.len] % mod[i]; n         (c.hv[i] += b.hv[i]) %= mod[i]; n       } n     } n   } n   return c; n } n struct segtree { n   node t[maxn << 2]; n   int n, ql, qr; n   node ans; n   void init(int n) { this->n = n; } n   void maintain(int o) { t[o] = merge(t[o << 1], t[o << 1 | 1]); } n   void build(int l, int r, int o) { n     t[o] = zer; n     if (l == r) { n       if (s[l] ==  0 ) { n         t[o].len++; n         for (int i = (0); i < (hashnum); ++i) t[o].hv[i] = 1; n       } else n         t[o].suf = t[o].pref = 1; n       return; n     } n     int mid = l + r >> 1; n     build(l, mid, o << 1), build(mid + 1, r, o << 1 | 1); n     maintain(o); n   } n   void query(int l, int r, int o) { n     if (ql <= l && qr >= r) n       ans = merge(ans, t[o]); n     else { n       int mid = l + r >> 1; n       if (ql <= mid) query(l, mid, o << 1); n       if (qr > mid) query(mid + 1, r, o << 1 | 1); n     } n   } n   node Query(int l, int r) { n     ans = zer; n     ql = l, qr = r; n     query(1, n, 1); n     return ans; n   } n }; n segtree tree; n bool judge(node& a, node& b) { n   if (a.suf % 2 != b.suf % 2 || a.pref % 2 != b.pref % 2 || a.len != b.len) n     return false; n   for (int i = (0); i < (hashnum); ++i) n     if (a.hv[i] != b.hv[i]) return false; n   return true; n } n int main(void) { n   scanf( %d%s , &n, s + 1); n   init(); n   tree.init(n); n   tree.build(1, n, 1); n   for (int i = (1); i <= (n); ++i) { n     sum[i] = sum[i - 1]; n     if (s[i] ==  1 ) sum[i]++; n   } n   int q; n   scanf( %d , &(q)); n   for (; q--;) { n     int l1, l2, r1, r2, len; n     scanf( %d%d%d , &l1, &l2, &len); n     r1 = l1 + len - 1; n     r2 = l2 + len - 1; n     if (sum[r2] - sum[l2 - 1] != sum[r1] - sum[l1 - 1]) { n       puts( No ); n       continue; n     } n     node x = tree.Query(l1, r1); n     node y = tree.Query(l2, r2); n     if (judge(x, y)) n       puts( Yes ); n     else n       puts( No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n const double Pi = 3.14159265; n const int N = 1e6 + 50; n const int M = 1e5 + 15; n const unsigned long long base = 163; n const int INF = 0x3f3f3f3f; n using namespace std; n int a[N]; n int main() { n   int n; n   scanf( %d , &n); n   int l = 0, r = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int pre = 1; n   int now = -1; n   int flag = 0; n   for (int i = 1; i <= n - 1; i++) { n     if (a[i] == a[i + 1]) { n       pre++; n     } else { n       flag = 1; n       if (now == -1) n         now = pre; n       else { n         if (pre != now) { n           cout <<  NO  << endl; n           return 0; n         } n       } n       pre = 1; n     } n   } n   if (pre != now && flag) return cout <<  NO n , 0; n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const int MOD = 1e9 + 7; n const int MAXN = 1e6 + 3; n struct Rcompare { n   bool operator()(pair<int, int> lhs, pair<int, int> rhs) { n     return lhs.first > rhs.first; n   } n }; n struct compare { n   bool operator()(pair<int, int> lhs, pair<int, int> rhs) { n     return lhs.first < rhs.first; n   } n }; n int solve(int x) { n   if (x < 4 || x == 5 || x == 7 || x == 11) { n     return -1; n   } n   if (x % 4 == 0) { n     return x / 4; n   } n   if (x % 4 == 2) { n     return x / 4; n   } n   if (x % 4 == 1) { n     return x / 4 - 1; n   } n   return x / 4 - 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < (n); ++i) { n     int x; n     cin >> x; n     cout << solve(x) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int bin[31]; n int cnt; n int ch[6000010][2]; n void insert(int x) { n   for (int i = 30; i >= 0; --i) { n     bin[i] = x & 1; n     x >>= 1; n   } n   int now = 0; n   for (int i = 0; i <= 30; ++i) { n     if (!ch[now][bin[i]]) { n       ch[now][bin[i]] = ++cnt; n     } n     now = ch[now][bin[i]]; n   } n } n int getans(int l, int r, int k) { n   if (k == 32) return 0; n   int firmin = 1 << 30; n   if (ch[l][0] && ch[r][0]) n     firmin = min(firmin, getans(ch[l][0], ch[r][0], k + 1)); n   if (ch[l][1] && ch[r][1]) n     firmin = min(firmin, getans(ch[l][1], ch[r][1], k + 1)); n   if (firmin != 1 << 30) return firmin; n   int secmin = 1 << 30; n   if (ch[l][1] && ch[r][0]) n     secmin = min(secmin, getans(ch[l][1], ch[r][0], k + 1)) + (1 << (31 - k)); n   if (ch[l][0] && ch[r][1]) n     secmin = min(secmin, getans(ch[l][0], ch[r][1], k + 1)) + (1 << (31 - k)); n   return secmin; n } n long long ans; n void dfs(int x, int k) { n   if (ch[x][0] && ch[x][1]) { n     ans += 1ll * (getans(ch[x][0], ch[x][1], k + 1) + (1 << (31 - k))); n   } n   if (ch[x][0]) dfs(ch[x][0], k + 1); n   if (ch[x][1]) dfs(ch[x][1], k + 1); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     int x; n     scanf( %d , &x); n     insert(x); n   } n   dfs(0, 1); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int arr[4], sum; n string s; n int main() { n   cin >> arr[0] >> arr[1] >> arr[2] >> arr[3] >> s; n   for (int i = 0; i < s.size(); i++) { n     sum += arr[s[i] - 49]; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long INFL = 0x3f3f3f3f3f3f3f3fLL; n const long double eps = 1e-8; n const int MAXN = 100000 + 5; n pair<long double, long double> X[MAXN << 2]; n inline pair<long double, long double> calc(const int& a, const int& b) { n   long double x = (long double)(b - a) / (long double)a; n   return make_pair(x, x); n } n inline void pushUp(const int& rt) { n   X[rt].first = X[rt << 1].first * X[rt << 1 | 1].first; n   X[rt].second = X[rt << 1].second + X[rt << 1 | 1].second * X[rt << 1].first; n } n void build(int l, int r, int rt) { n   static int ax, bx; n   if (l == r) { n     scanf( %d %d , &ax, &bx); n     X[rt] = calc(ax, bx); n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, rt << 1); n   build(mid + 1, r, rt << 1 | 1); n   pushUp(rt); n } n void update(const int& p, const pair<long double, long double>& v, int l, int r, n             int rt) { n   if (l == r) { n     X[rt] = v; n     return; n   } n   int mid = (l + r) >> 1; n   if (p <= mid) n     update(p, v, l, mid, rt << 1); n   else n     update(p, v, mid + 1, r, rt << 1 | 1); n   pushUp(rt); n } n pair<long double, long double> query(const int& L, const int& R, int l, int r, n                                      int rt) { n   static pair<long double, long double> z = n       make_pair((long double)1.0, (long double)0.0); n   if (L <= l && r <= R) return X[rt]; n   int mid = (l + r) >> 1; n   pair<long double, long double> x = z, y = z, ret; n   if (L <= mid) x = query(L, R, l, mid, rt << 1); n   if (R > mid) y = query(L, R, mid + 1, r, rt << 1 | 1); n   ret.first = x.first * y.first; n   ret.second = x.second + y.second * x.first; n   return ret; n } n int main() { n   static int n, q, op, x, ax, bx, lx, rx; n   while (~scanf( %d %d , &n, &q)) { n     build(1, n, 1); n     while (q--) { n       scanf( %d , &op); n       if (op == 1) { n         scanf( %d %d %d , &x, &ax, &bx); n         update(x, calc(ax, bx), 1, n, 1); n       } else { n         scanf( %d %d , &lx, &rx); n         long double ans = query(lx, rx, 1, n, 1).second; n         if (ans != ans || ans >= 1e20) n           ans = 0.0; n         else n           ans = 1 / (1 + ans); n         cout << fixed << setprecision(8) << ans <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read() { n   T x = 0, f = 0; n   char c = getchar(); n   while (!isdigit(c)) f = c ==  - , c = getchar(); n   while (isdigit(c)) x = x * 10 + c - 48, c = getchar(); n   return f ? -x : x; n } n namespace run { n const long long N = 2e5 + 9; n map<long long, long long> mp; n long long n, a[N], ans; n long long main() { n   n = read<long long>(); n   for (long long i = 1; i <= n; i++) a[i] = read<long long>(); n   mp[0] = 1; n   for (long long i = 1, sum = 0, las = 0; i <= n; i++) { n     sum += a[i]; n     las = max(las, mp[sum]); n     ans += i - las; n     mp[sum] = i + 1; n   } n   printf( %lld n , ans); n   return 0; n } n }  // namespace run n int main() { return run::main(); } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int id, a, b; n } node[111111], zhan[111111]; n bool cmp1(Node a, Node b) { return a.a > b.a || (a.a == b.a && a.b > b.b); } n bool cmp2(Node a, Node b) { return a.b > b.b || (a.b == b.b && a.a < b.a); } n int main() { n   int n, p, k; n   scanf( %d%d%d , &n, &p, &k); n   for (int i = 0; i < n; i++) n     scanf( %d%d , &node[i].a, &node[i].b), node[i].id = i + 1; n   sort(node, node + n, cmp2); n   sort(node, node + n - p + k, cmp1); n   int flag = 1 << 30, flag1 = -1; n   for (int i = 0; i < k; i++) { n     zhan[i] = node[i]; n     if (flag >= zhan[i].b) { n       if (flag == zhan[i].b) n         flag1 = max(zhan[i].a, flag1); n       else n         flag1 = zhan[i].a; n       flag = zhan[i].b; n     } n   } n   sort(node + k, node + n, cmp2); n   int cnt = k; n   for (int i = k; cnt < p; i++) { n     if (node[i].b < flag || (node[i].b == flag && node[i].a >= flag1)) n       zhan[cnt++] = node[i]; n   } n   for (int i = 0; i < p; i++) printf( %d  , zhan[i].id); n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n struct ent { n   int val, pos; n   inline bool operator<(const ent &cpr) const { return val < cpr.val; } n }; n set<ent> pq; n int n; n int val[300005]; n long long sum[300005]; n long long ret; n int a, b; n int del[300005]; n int dcnt; n int main() { n   ret = -1000000000000ll; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &val[i]); n   for (int i = 1; i <= n; i++) { n     sum[i] = sum[i - 1] + max(val[i], 0); n     ent nw; n     nw.pos = i; n     nw.val = val[i]; n     set<ent>::iterator it; n     it = pq.find(nw); n     if (it == pq.end()) n       pq.insert(nw); n     else { n       if (ret < 2 * val[i] + sum[i - 1] - sum[(*it).pos]) { n         ret = 2 * val[i] + sum[i - 1] - sum[(*it).pos]; n         a = (*it).pos; n         b = i; n       } n     } n   } n   cout << ret; n   for (int i = 1; i < a; i++) del[++dcnt] = i; n   for (int i = a + 1; i < b; i++) n     if (val[i] < 0) del[++dcnt] = i; n   for (int i = b + 1; i <= n; i++) del[++dcnt] = i; n   printf(  %d n , dcnt); n   for (int i = 1; i <= dcnt; i++) printf( %d  , del[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 100; n const int LVL = 18; n int n, m; n int a[N]; n int b[N]; n int c[N]; n int h[N]; n int inplace[N], parent[N]; n int inmst[N]; n int dp[N][LVL], mini[N][LVL], maxi[N][LVL]; n int ans[N]; n vector<int> tree[N]; n int find(int node) { n   if (parent[node] == node) { n     return node; n   } n   return parent[node] = find(parent[node]); n } n void addEdge(int edgeNo) { n   if (find(a[edgeNo]) != find(b[edgeNo])) { n     inmst[edgeNo] = true; n     parent[find(a[edgeNo])] = find(b[edgeNo]); n     tree[a[edgeNo]].push_back(edgeNo); n     tree[b[edgeNo]].push_back(edgeNo); n   } else { n     inmst[edgeNo] = false; n   } n } n pair<int, int> lca(int a, int b) { n   if (h[a] > h[b]) { n     int temp = a; n     a = b; n     b = temp; n   } n   int diff = abs(h[a] - h[b]); n   int max_weight_edge = 0; n   for (int i = 0; i <= 17; i++) { n     if ((diff >> i) & 1) { n       max_weight_edge = max(maxi[b][i], max_weight_edge); n       b = dp[b][i]; n     } n   } n   if (b == a) { n     return make_pair(a, max_weight_edge); n   } n   for (int i = 17; i >= 0; i--) { n     if (dp[a][i] != dp[b][i]) { n       max_weight_edge = max(max_weight_edge, maxi[a][i]); n       max_weight_edge = max(max_weight_edge, maxi[b][i]); n       a = dp[a][i]; n       b = dp[b][i]; n     } n   } n   return make_pair(dp[a][0], max(max_weight_edge, max(maxi[a][0], maxi[b][0]))); n } n void dfs(int node, int par) { n   dp[node][0] = par; n   for (int edgeNo : tree[node]) { n     int child = a[edgeNo] ^ b[edgeNo] ^ node; n     if (child != par) { n       maxi[child][0] = c[edgeNo]; n       h[child] = h[node] + 1; n       dfs(child, node); n     } n   } n } n void dfs2(int node, int par) { n   for (int edgeNo : tree[node]) { n     int next = a[edgeNo] ^ b[edgeNo] ^ node; n     if (next != par) { n       ans[edgeNo] = mini[next][0]; n       dfs2(next, node); n     } n   } n } n void update(int node, int lca, int val) { n   int diff = abs(h[node] - h[lca]); n   for (int i = LVL - 1; i >= 0; i--) { n     if (diff & (1 << i)) { n       mini[node][i] = min(mini[node][i], val); n       node = dp[node][i]; n     } n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> a[i] >> b[i] >> c[i]; n     inplace[i] = i; n   } n   for (int i = 1; i <= n; i++) { n     parent[i] = i; n   } n   sort(inplace + 1, inplace + 1 + m, [](int x, int y) { n     return make_pair(c[x], make_pair(a[x], b[x])) < n            make_pair(c[y], make_pair(a[y], b[y])); n   }); n   for (int i = 1; i <= m; i++) { n     addEdge(inplace[i]); n   } n   h[1] = 0; n   dfs(1, 0); n   for (int l = 1; l < LVL; l++) { n     for (int i = 1; i <= n; i++) { n       dp[i][l] = dp[dp[i][l - 1]][l - 1]; n       maxi[i][l] = max(maxi[i][l - 1], maxi[dp[i][l - 1]][l - 1]); n     } n   } n   for (int j = 0; j < LVL; j++) { n     for (int i = 1; i <= n; i++) { n       mini[i][j] = 1e9 + 7; n     } n   } n   for (int i = 1; i <= m; i++) { n     if (inmst[i]) { n       ans[i] = 1e9 + 7; n     } else { n       pair<int, int> result = lca(a[i], b[i]); n       ans[i] = result.second; n       update(a[i], result.first, c[i]); n       update(b[i], result.first, c[i]); n     } n   } n   for (int l = LVL - 1; l > 0; l--) { n     for (int i = 1; i <= n; i++) { n       mini[i][l - 1] = min(mini[i][l - 1], mini[i][l]); n       mini[dp[i][l - 1]][l - 1] = min(mini[i][l], mini[dp[i][l - 1]][l - 1]); n     } n   } n   dfs2(1, 0); n   for (int i = 1; i <= m; i++) { n     if (ans[i] == 1e9 + 7) { n       ans[i] = 0; n     } n   } n   for (int i = 1; i <= m; i++) { n     cout << ans[i] - 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <typename T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T, class T1> n inline void gn(T &first, T1 &second) { n   gn(first); n   gn(second); n } n template <class T, class T1, class T2> n inline void gn(T &first, T1 &second, T2 &z) { n   gn(first); n   gn(second); n   gn(z); n } n template <typename T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <typename T> n inline void println(T first) { n   print(first), putchar(  n ); n } n template <typename T> n inline void printsp(T first) { n   print(first), putchar(   ); n } n template <class T, class T1> n inline void print(T first, T1 second) { n   printsp(first), println(second); n } n template <class T, class T1, class T2> n inline void print(T first, T1 second, T2 z) { n   printsp(first), printsp(second), println(z); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int E, head[5050], to[5050], nxt[5050]; n void init(int n) { n   E = 0; n   for (int i = 1; i <= n; i++) head[i] = -1; n } n void add_edge(int u, int v) { n   nxt[E] = head[u]; n   to[E] = v; n   head[u] = E++; n } n int dp[2][5050][5050], c[5050], d[5050]; n int sz[5050]; n void dfs(int u) { n   dp[1][u][0] = 0; n   dp[0][u][0] = 0; n   dp[1][u][1] = c[u] - d[u]; n   dp[0][u][1] = c[u]; n   sz[u] = 1; n   for (int e = head[u]; ~e; e = nxt[e]) { n     int v = to[e]; n     dfs(v); n     for (int i = sz[u] + sz[v]; i >= 1; i--) { n       for (int j = min(i, sz[v]); j >= 0 && i - j <= sz[u]; j--) { n         smin(dp[0][u][i], dp[0][u][i - j] + dp[0][v][j]); n         if (i && j < i) smin(dp[1][u][i], dp[1][u][i - j] + dp[1][v][j]); n       } n     } n     sz[u] += sz[v]; n   } n   for (int i = 1; i <= sz[u]; i++) smin(dp[1][u][i], dp[0][u][i]); n } n int main() { n   int n, b; n   gn(n, b); n   init(n); n   memset(dp, 0x3f, sizeof(dp)); n   for (int i = 1, first; i <= n; i++) { n     gn(c[i], d[i]); n     if (i > 1) { n       gn(first); n       add_edge(first, i); n     } n   } n   dfs(1); n   int ans = 0; n   for (int i = 1; i <= n; i++) n     if (dp[1][1][i] <= b) ans = i; n   println(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   set<string> s1, s2, s3; n   s3.clear(); n   s1.clear(); n   s2.clear(); n   int n, m; n   cin >> n >> m; n   string str =   ; n   for (int i = 0; i < n; i++) { n     cin >> str; n     s1.insert(str); n   } n   for (int i = 0; i < m; i++) { n     cin >> str; n     s2.insert(str); n   } n   set<string>::iterator it; n   for (it = s1.begin(); it != s1.end(); it++) { n     if (s2.find(*it) != s2.end()) { n       s3.insert(*it); n     } n   } n   int z = s3.size(); n   int x = s1.size() - z; n   int y = s2.size() - z; n   if (z % 2 != 0) { n     if (x >= y) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } else { n     if (x - y >= 1) n       cout <<  YES n ; n     else { n       cout <<  NO n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int card[10010]; n int main() { n   int n, r, s = 0, d = 0, l = 1, num = 0; n   cin >> n; n   r = n; n   for (int i = 1; i <= n; i++) { n     cin >> card[i]; n   } n   while (l <= r) { n     num++; n     if (num & 1) { n       if (card[l] > card[r]) { n         s += card[l]; n         l++; n       } else { n         s += card[r]; n         r--; n       } n     } else { n       if (card[l] > card[r]) { n         d += card[l]; n         l++; n       } else { n         d += card[r]; n         r--; n       } n     } n   } n   cout << s <<     << d << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[200010][33]; n void generate() { n   for (long long i = 1; i <= (200000); i++) { n     for (long long j = 0; j < (32); j++) { n       dp[i][j] = dp[i - 1][j] + !((1 << j) & i); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   generate(); n   int t; n   cin >> t; n   while (t--) { n     int l, r, mn = 0x3F3F3F3F; n     cin >> l >> r; n     for (long long j = 0; j < (32); j++) { n       mn = min(mn, dp[r][j] - dp[l - 1][j]); n     } n     cout << mn <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const long long inf = 9e18; n struct NODE { n   long long v, id; n   bool operator<(const NODE& other) const { n     return v > other.v || (v == other.v && id > other.id); n   } n } node[N]; n long long a[N], b[N]; n long long n; n long long f(long long x, long long y) { return x - 3LL * y * y + 3LL * y - 1; } n long long find(long long v, long long x) { n   long long l, r, mid; n   l = 0; n   r = x + 1; n   while (r - l > 1) { n     mid = (l + r) >> 1; n     if (f(x, mid) >= v) n       l = mid; n     else n       r = mid; n   } n   return l; n } n long long solve(long long x) { n   long long tot = 0; n   int i; n   for (i = 1; i <= n; i++) { n     tot += (b[i] = find(x, a[i])); n   } n   return tot; n } n int main() { n   long long k; n   int i, cnt = 0; n   long long l = inf, r = -inf, mid, tot; n   cin >> n >> k; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     l = min(l, f(a[i], a[i])); n     r = max(r, f(a[i], 1)); n   } n   while (r - l > 1) { n     mid = (l + r) >> 1; n     if (solve(mid) <= k) n       r = mid; n     else n       l = mid; n   } n   tot = solve(r); n   for (i = 1; i <= n; i++) n     if (b[i] < a[i]) node[++cnt].v = f(a[i], b[i] + 1), node[cnt].id = i; n   sort(node + 1, node + cnt + 1); n   for (i = 1; i <= cnt; i++) { n     if (tot == k) break; n     b[node[i].id]++; n     tot++; n   } n   for (i = 1; i <= n; i++) cout << b[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5, M = 2e4 + 5, inf = 0x3f3f3f3f, mod = 1e9 + 7; n void Print(int *a, int n) { n   for (int i = 1; i < n; i++) printf( %d  , a[i]); n   printf( %d n , a[n]); n } n int e[N], nt[N]; n void Gnt(string a, int *nt) { n   int k = 0, p = 0, n = (int)a.size(); n   nt[0] = n; n   for (int i = 1; i < n; i++) { n     if (i + nt[i - k] < p) n       nt[i] = nt[i - k]; n     else { n       if (i >= p) p = i; n       while (p < n && a[p] == a[p - i]) p++; n       nt[i] = p - i; n       k = i; n     } n   } n } n void Exd(string a, string b, int *e, int *nt) { n   int k = 0, p = 0, n = (int)a.size(), m = (int)b.size(); n   Gnt(b, nt); n   for (int i = 0; i < n; i++) { n     if (i + nt[i - k] < p) n       e[i] = nt[i - k]; n     else { n       if (i >= p) p = i; n       while (p < n && p - i < m && a[p] == b[p - i]) p++; n       e[i] = p - i; n       k = i; n     } n   } n } n int cycle(string a) { n   int n = a.size(); n   Gnt(a, nt); n   int t = n; n   for (int i = 1; i < n; i++) n     if (i + nt[i] == n) { n       t = n % i ? n : i; n       break; n     } n   return t; n } n string a, b; n int k, dp[N][2]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   cin >> a >> b >> k; n   dp[0][a != b] = 1; n   int n = (int)a.size(); n   a = a + a; n   Exd(a, b, e, nt); n   int x = 0, len = (int)b.size(); n   for (int i = 0; i < n; i++) { n     if (e[i] >= len) x++; n   } n   int y = n - x; n   for (int i = 1; i <= k; i++) { n     dp[i][0] = (1LL * x * dp[i - 1][1] + 1LL * (x - 1) * dp[i - 1][0]) % mod; n     dp[i][1] = (1LL * y * dp[i - 1][0] + 1LL * (y - 1) * dp[i - 1][1]) % mod; n   } n   printf( %d n , dp[k][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> gr[50005]; n int parent[50005]; n void dfs(int src, int par) { n   parent[src] = par; n   for (auto nbr : gr[src]) { n     if (nbr != par) dfs(nbr, src); n   } n } n int main() { n   int n, x, y; n   cin >> n >> x >> y; n   for (int i = 1; i <= n; i++) { n     if (i == x) continue; n     int p; n     cin >> p; n     gr[i].push_back(p); n     gr[p].push_back(i); n   } n   dfs(y, 0); n   for (int i = 1; i <= n; i++) { n     if (i == y) continue; n     cout << parent[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int casos; n   cin >> casos; n   while (casos > 0) { n     int a, b; n     cin >> a >> b; n     if (a > b) { n       int dif = a - b; n       b -= dif; n       a -= dif * 2; n     } else if (b > a) { n       int dif = b - a; n       a -= dif; n       b -= dif * 2; n     } n     if (a % 3 == 0 && a == b && a >= 0) { n       printf( YES n ); n     } else { n       printf( NO n ); n     } n     casos--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[100005], is[100005], isn[100005], N, used[100005]; n vector<int> v; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     char t; n     int ind; n     cin >> t >> ind; n     a[i] = ind; n     if (t ==  + ) n       is[ind]++; n     else { n       a[i] *= -1; n       isn[ind]++; n       N++; n     } n   } n   for (int i = 1; i <= n; i++) n     if (is[i] + N - isn[i] == m) { n       v.push_back(i); n       used[i] = 1; n     } n   if (v.size() == 1) { n     for (int i = 1; i <= n; i++) n       if (a[i] == v[0] || (a[i] < 0 && -a[i] != v[0])) n         cout <<  Truth  << endl; n       else n         cout <<  Lie  << endl; n   } else { n     for (int i = 1; i <= n; i++) n       if (a[i] > 0 && !used[a[i]]) n         cout <<  Lie  << endl; n       else if (a[i] < 0 && !used[-a[i]]) n         cout <<  Truth  << endl; n       else n         cout <<  Not defined  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool solved; n const int MX = 1500000; n int cache_map[1 << 22]; n int nxt; n int cache[MX][22]; n void check_solution(vector<int>& g, vector<int>& sol, int n) { n   int mask(0); n   int ind(nxt); n   nxt += 1; n   if (nxt == MX) nxt = 0; n   if (((int)sol.size()) != 1) { n     for (int i(0), _l((int)(((int)sol.size())) - 1); i <= _l; ++i) n       mask |= (1 << sol[i]); n     int mask_prev = mask ^ (1 << sol.back()); n     int ind_prev(cache_map[mask_prev]); n     memcpy(cache[ind], cache[ind_prev], 22 * sizeof(int)); n   } else { n     mask |= (1 << sol.back()); n     for (int i(0), _l((int)(n)-1); i <= _l; ++i) cache[ind][i] = g[i]; n   } n   cache_map[mask] = ind; n   int v(sol.back()); n   for (int j(0), _l((int)(n)-1); j <= _l; ++j) { n     if ((cache[ind][v] & (1 << j)) != 0) { n       cache[ind][j] |= cache[ind][v]; n     } n   } n   int need((1 << n) - 1); n   for (int i(0), _l((int)(n)-1); i <= _l; ++i) n     if (cache[ind][i] != need) return; n   printf( %d n , ((int)sol.size())); n   for (int i(0), _l((int)(((int)sol.size())) - 1); i <= _l; ++i) n     printf( %d%c , sol[i] + 1, i + 1 == ((int)sol.size()) ?   n  :    ); n   solved = true; n } n void try_solve(vector<int>& g, int n, int k, vector<int>& cur, int ind) { n   if (solved) return; n   if (((int)cur.size()) == k) { n     check_solution(g, cur, n); n   } else { n     if (ind == n) return; n     cur.push_back(ind); n     try_solve(g, n, k, cur, ind + 1); n     cur.pop_back(); n     if (solved) return; n     try_solve(g, n, k, cur, ind + 1); n   } n } n void solve() { n   nxt = 0; n   int n, m; n   scanf( %d%d , &n, &m); n   vector<int> g(n); n   for (int i(0), _l((int)(n)-1); i <= _l; ++i) { n     g[i] |= (1 << i); n   } n   for (int i(0), _l((int)(m)-1); i <= _l; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     --a, --b; n     g[a] |= (1 << b); n     g[b] |= (1 << a); n   } n   if (m == n * (n - 1) / 2) { n     puts( 0 ); n     return; n   } n   solved = false; n   vector<int> s; n   for (int i(1), _l(n); i <= _l; ++i) { n     try_solve(g, n, i, s, 0); n     if (solved) break; n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100009; n int n; n long long a[N]; n void print_ans(int l, int r, vector<long long>& v) { n   cout << l <<     << r <<   n ; n   for (long long vv : v) { n     cout << vv <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   vector<long long> fst; n   fst.push_back(-a[0]); n   a[0] = 0; n   print_ans(1, 1, fst); n   if (n == 1) { n     vector<long long> zero = {0}; n     print_ans(1, 1, zero); n     print_ans(1, 1, zero); n     return 0; n   } n   vector<long long> scd; n   for (int i = 1; i < n; ++i) { n     long long md = a[i] % n; n     if (md < 0) md += n; n     long long nm = md * (n - 1); n     scd.push_back(nm); n     a[i] += nm; n   } n   print_ans(2, n, scd); n   vector<long long> all; n   for (int i = 0; i < n; ++i) { n     assert(a[i] % n == 0); n     all.push_back(-a[i]); n   } n   print_ans(1, n, all); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long f = n * (n - 1) * (n - 2) * (n - 3) * (n - 4); n   cout << (f / 120) * ((n * n) - (4 * n) + 37) / 42; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, x; n vector<long long> a; n void ans() { n   for (int i = 0; i < n; i++) { n     cout << a[i] <<    ; n   } n   exit(0); n } n int main(int argc, char* argv[]) { n   srand((unsigned int)time(NULL)); n   cin >> n >> k >> x; n   a.resize(n); n   int ch = 1; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] < 0) { n       ch *= -1; n     } n   } n   if (ch == 1) { n     set<pair<long long, int> > s; n     for (int i = 0; i < n; i++) { n       if (a[i] >= 0) { n         s.insert({abs(-1 - a[i]), i}); n       } else { n         s.insert({abs(1 - a[i]), i}); n       } n     } n     auto q = *s.begin(); n     int id = q.second; n     if (a[id] >= 0) { n       while (a[id] >= 0) { n         a[id] -= x; n         k--; n         if (k == 0) { n           ans(); n         } n       } n     } else { n       while (a[id] <= 0) { n         a[id] += x; n         k--; n         if (k == 0) { n           ans(); n         } n       } n     } n   } n   set<pair<long long, int> > s; n   for (int i = 0; i < n; i++) { n     s.insert({abs(a[i]), i}); n   } n   while (k > 0) { n     auto q = *s.begin(); n     s.erase(s.begin()); n     int id = q.second; n     if (a[id] >= 0) { n       a[id] += x; n     } else { n       a[id] -= x; n     } n     k--; n     s.insert({abs(a[id]), id}); n   } n   ans(); n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> v(n), v1(m); n   for (int i = 0; i < n; ++i) { n     cin >> v[i]; n   } n   for (int i = 0; i < m; ++i) { n     cin >> v1[i]; n   } n   vector<int> ans; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (v[i] == v1[j]) { n         ans.push_back(v[i]); n         break; n       } n     } n   } n   for (auto i : ans) { n     cout << i <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, d; n int main() { n   cin >> n >> d; n   int m; n   cin >> m; n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     puts((d <= x + y && x + y <= 2 * n - d && -d <= x - y && x - y <= d) n              ?  YES  n              :  NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int x[2600], y[2600]; n string a[60]; n bool valid(int y, int x1, int y1, int x, int x2, int y2) { n   for (int i = min(x1, x2); i <= max(x1, x2); i++) { n     if (a[i][y] !=  B ) { n       return false; n     } n   } n   for (int i = min(y1, y2); i <= max(y1, y2); i++) { n     if (a[x][i] !=  B ) { n       return false; n     } n   } n   return true; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   int k = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  B ) { n         x[k] = i; n         y[k] = j; n         k++; n       } n     } n   } n   for (int i = 0; i < k; i++) { n     for (int j = i + 1; j < k; j++) { n       if (valid(y[i], x[i], y[i], x[j], x[j], y[j])) { n         continue; n       } n       if (valid(y[j], x[i], y[i], x[i], x[j], y[j])) { n         continue; n       } n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX_N = 1e5 + 5; n long long n, val; n pair<pair<long long, long long>, pair<long long, long long> > edge[MAX_N]; n long long par[MAX_N]; n long long len[MAX_N]; n vector<long long> h; n vector<long long> ans; n vector<pair<long long, long long> > G[MAX_N]; n vector<long long> rot, ver; n long long P[MAX_N]; n long long down[MAX_N]; n long long up[MAX_N]; n bool mark[MAX_N]; n long long root(long long v) { n   return (par[v] == v ? v : par[v] = root(par[v])); n } n void cheak(long long tmp, long long id) { n   if (val < tmp) { n     val = tmp; n     ans.clear(); n   } n   if (val == tmp) ans.push_back(id); n } n long long dfs1(long long v, long long p) { n   P[v] = p; n   down[v] = len[v]; n   mark[v] = true; n   for (pair<long long, long long> u : G[v]) n     if (!mark[u.first]) down[v] += dfs1(u.first, v); n   return down[v]; n } n void dfs2(long long v, long long uval) { n   up[v] = uval; n   for (pair<long long, long long> u : G[v]) n     if (u.first != P[v]) dfs2(u.first, uval + down[v] - down[u.first]); n } n void dfs3(long long v) { n   for (pair<long long, long long> u : G[v]) n     if (u.first != P[v]) { n       dfs3(u.first); n       cheak(up[u.first] * down[u.first], u.second); n     } n } n void calc() { n   for (long long id : h) { n     ver.push_back(root(edge[id].second.first)); n     ver.push_back(root(edge[id].second.second)); n   } n   sort(ver.begin(), ver.end()); n   ver.resize(unique(ver.begin(), ver.end()) - ver.begin()); n   for (long long id : h) { n     G[root(edge[id].second.first)].push_back( n         {root(edge[id].second.second), edge[id].first.second}); n     G[root(edge[id].second.second)].push_back( n         {root(edge[id].second.first), edge[id].first.second}); n   } n   for (long long v : ver) n     if (!mark[v]) { n       rot.push_back(v); n       dfs1(v, v); n     } n   for (long long v : rot) dfs2(v, 0); n   for (long long v : rot) dfs3(v); n   rot.clear(); n   for (long long v : ver) { n     G[v].clear(); n     mark[v] = false; n   } n   ver.clear(); n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   cin >> n; n   for (long long i = 0; i < n - 1; i++) { n     long long v, u, d; n     cin >> edge[i].second.first >> edge[i].second.second >> edge[i].first.first; n     edge[i].first.second = i + 1; n     edge[i].second.first--, edge[i].second.second--; n   } n   sort(edge, edge + n - 1); n   for (long long i = 0; i < n; i++) { n     par[i] = i; n     len[i] = 1; n   } n   h.push_back(0); n   for (long long i = 1; i < n - 1; i++) { n     if (edge[i].first.first != edge[i - 1].first.first) { n       calc(); n       for (long long id : h) { n         len[root(edge[id].second.second)] += len[root(edge[id].second.first)]; n         par[root(edge[id].second.first)] = root(edge[id].second.second); n       } n       h.clear(); n     } n     h.push_back(i); n   } n   calc(); n   cout << 2 * val <<     << ans.size() <<   n ; n   sort(ans.begin(), ans.end()); n   for (long long u : ans) cout << u <<    ; n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n struct xgcd_s { n   T m, n, gcd; n }; n template <typename T> n xgcd_s<T> xgcd(T a, T b) { n   T m[] = {1, 0}, n[] = {0, 1}, q[] = {0, 0}; n   for (int i = 0;; i ^= 1) { n     m[i] -= m[i ^ 1] * q[i]; n     n[i] -= n[i ^ 1] * q[i]; n     if (!b) return xgcd_s<T>{m[i], n[i], a}; n     q[i] = a / b; n     a %= b; n     std::swap(a, b); n   } n } n template <unsigned M()> n struct _intmod { n  private: n   struct _p { n     unsigned x; n   }; n   _intmod(_p x) { n = x.x; } n  n  public: n   unsigned n; n   _intmod() { n = 0; } n   _intmod(int x) { n = x < 0 ? M() + x % (int)M() : x % M(); } n   _intmod(unsigned x) { n = x % M(); } n   _intmod(long x) { n = unsigned(x < 0 ? M() + x % (long)M() : x % M()); } n   _intmod(unsigned long x) { n = x % M(); } n   _intmod(long long x) { n     n = unsigned(x < 0 ? M() + x % (long long)M() : x % M()); n   } n   _intmod(unsigned long long x) { n = x % M(); } n   _intmod operator+(_intmod b) const { return {n + b.n}; } n   _intmod operator*(_intmod b) const { n     return {_p{unsigned((unsigned long long)n * b.n % M())}}; n   } n   _intmod operator-(_intmod b) const { n     return {_p{n >= b.n ? n - b.n : M() + n - b.n}}; n   } n   _intmod operator-() const { return {_p{n ? M() - n : 0U}}; } n   _intmod& operator+=(_intmod b) { return (*this = *this + b); } n   _intmod& operator-=(_intmod b) { return (*this = *this - b); } n   _intmod& operator/=(_intmod b) { return (*this = *this / b); } n   _intmod& operator*=(_intmod b) { return (*this = *this * b); } n   static _intmod mulinv(_intmod n) { n     xgcd_s<signed> x = xgcd<signed>(n.n, M()); n     (void)0; n     return x.m; n   } n   _intmod operator/(_intmod b) const { return *this * mulinv(b); } n   bool operator==(_intmod b) const { return n == b.n; } n   bool operator!=(_intmod b) const { return n != b.n; } n   explicit operator int() const { return n; } n }; n template <unsigned M()> n std::ostream& operator<<(std::ostream& o, _intmod<M> n) { n   return o << n.n; n } n template <unsigned M()> n std::istream& operator>>(std::istream& i, _intmod<M>& n) { n   long long x; n   i >> x; n   n = x; n   return i; n } n template <unsigned M> n unsigned intmod_unsignedconst() { n   static_assert(M > 0 && M < (1U << 31),  modulus bad ); n   return M; n } n template <unsigned* M> n unsigned intmod_unsignedptr() { n   return *M; n } n template <unsigned M> n using intmod = _intmod<intmod_unsignedconst<M> >; n template <unsigned* M> n using intmodv = _intmod<intmod_unsignedptr<M> >; n static_assert(std::is_trivially_copyable<intmod<3> >::value,   ); n using namespace std; n using I = intmod<998244353>; n I topGroupSizeP[2001][2001]; n I favoritePow[2001]; n I upsetPow[2001]; n I expGames[2001]; n I rrGames[2001]; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   I favoriteP = I(a) / b; n   I upsetP = I(1) - favoriteP; n   favoritePow[0] = upsetPow[0] = 1; n   for (int i = 1; i <= n; i++) favoritePow[i] = favoriteP * favoritePow[i - 1]; n   for (int i = 1; i <= n; i++) upsetPow[i] = upsetP * upsetPow[i - 1]; n   for (int i = 1; i <= n; i++) rrGames[i] = i * (i - 1) / 2; n   for (int m = 1; m <= n; m++) { n     I notAllTiedP = 0; n     for (int tgSize = 1; tgSize < m; tgSize++) { n       I tgSizeP = topGroupSizeP[m - 1][tgSize] * favoritePow[tgSize]; n       I superiorGroupExcludingLastP; n       if (tgSize == 1) n         superiorGroupExcludingLastP = 1; n       else if (tgSize == 3) n         superiorGroupExcludingLastP = n             topGroupSizeP[m - 1][1] * topGroupSizeP[m - 2][1]; n       else n         superiorGroupExcludingLastP = topGroupSizeP[m - 1][tgSize - 1] / n                                       topGroupSizeP[tgSize - 1][tgSize - 1]; n       tgSizeP += superiorGroupExcludingLastP * topGroupSizeP[tgSize][tgSize] * n                  upsetPow[m - tgSize]; n       notAllTiedP += tgSizeP; n       topGroupSizeP[m][tgSize] = tgSizeP; n     } n     topGroupSizeP[m][m] = I(1) - notAllTiedP; n   } n   expGames[1] = 0; n   for (int m = 2; m <= n; m++) { n     I costExcludingAllTied = 0; n     for (int tgSize = 1; tgSize < m; tgSize++) n       costExcludingAllTied += n           topGroupSizeP[m][tgSize] * n           (expGames[tgSize] + expGames[m - tgSize] - rrGames[m - tgSize]); n     I allTiedP = topGroupSizeP[m][m]; n     costExcludingAllTied /= I(1) - allTiedP; n     I allTiedCost = rrGames[m] / (I(1) - allTiedP); n     expGames[m] = costExcludingAllTied + allTiedCost; n   } n   cout << expGames[n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, k, d; n void print(int a, int b) { n   printf( %d %d n , a, b); n   a += d, b += d; n   printf( %d %d n , a, b); n } n int main() { n   cin >> k; n   if (k % 2 == 0) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES  << endl; n   d = 4 * k - 2, m = (k * d) / 2; n   cout << d <<     << m << endl; n   d /= 2; n   cout << 1 <<     << 1 + d << endl; n   for (int i = 2; i <= k; i++) print(1, i); n   for (int i = 2; i <= k; i++) n     for (int j = k + 1; j <= 2 * k - 1; j++) print(i, j); n   for (int i = 0; i < (k / 2); i++) print(k + 1 + 2 * i, k + 2 + 2 * i); n   return 0; n } n