#include <bits/stdc++.h> n using namespace std; n const int M = 500; n int R, n, m, d[M][M]; n pair<int, int> hd[M], tl[M], par[M][M]; n bool wins = 0, draw = 0; n int mark[M][M]; n void dfs(int x, int y) { n   if (x < 0 || y < 0) return; n   if (x + y > R) { n     mark[x][y] = 2; n     return; n   } n   mark[x][y] = 1; n   if (x + y == 0) { n     wins = 1; n     return; n   } n   for (int i = 1; i <= min(n, x); i++) { n     int nx = x - i + hd[i].first, ny = y + hd[i].second; n     if (mark[nx][ny] == 1) n       draw = 1; n     else if (mark[nx][ny] == 0) n       dfs(nx, ny); n   } n   for (int i = 1; i <= min(m, y); i++) { n     int nx = x + tl[i].first, ny = y - i + tl[i].second; n     if (mark[nx][ny] == 1) n       draw = 1; n     else if (mark[nx][ny] == 0) n       dfs(nx, ny); n   } n   mark[x][y] = 2; n } n int dp[M][M], dis; n void dfs2(int x, int y) { n   if (x + y > R) { n     dis = max(dis, dp[x][y]); n     return; n   } n   for (int i = 1; i <= min(n, x); i++) { n     int nx = x - i + hd[i].first, ny = y + hd[i].second; n     if (nx >= 0 && ny >= 0 && (dp[nx][ny] == 0 || (dp[nx][ny] < dp[x][y] + 1))) n       dp[nx][ny] = dp[x][y] + 1, dfs2(nx, ny); n   } n   for (int i = 1; i <= min(m, y); i++) { n     int nx = x + tl[i].first, ny = y - i + tl[i].second; n     if (nx >= 0 && ny >= 0 && (dp[nx][ny] == 0 || (dp[nx][ny] < dp[x][y] + 1))) n       dp[nx][ny] = dp[x][y] + 1, dfs2(nx, ny); n   } n } n int main() { n   int h, t; n   scanf( %d %d %d , &h, &t, &R); n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d %d , &hd[i].first, &hd[i].second); n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) scanf( %d %d , &tl[i].first, &tl[i].second); n   dfs(h, t); n   if (!wins && draw) return printf( Draw n ), 0; n   memset(mark, 0, sizeof mark); n   if (wins) { n     printf( Ivan n ); n     queue<pair<int, int> > q; n     d[h][t] = 0; n     mark[h][t] = 1; n     q.push({h, t}); n     while (q.size()) { n       int x = q.front().first, y = q.front().second; n       q.pop(); n       for (int i = 1; i <= min(x, n); i++) { n         int nx = x - i + hd[i].first, ny = y + hd[i].second; n         if (nx >= 0 && ny >= 0 && nx + ny <= R) { n           if (!mark[nx][ny] or (mark[nx][ny] && d[nx][ny] > d[x][y] + 1)) n             mark[nx][ny] = 1, d[nx][ny] = d[x][y] + 1, q.push({nx, ny}); n         } n       } n       for (int i = 1; i <= min(m, y); i++) { n         int nx = x + tl[i].first, ny = y - i + tl[i].second; n         if (nx >= 0 && ny >= 0 && nx + ny <= R) { n           if (!mark[nx][ny] or (mark[nx][ny] && d[nx][ny] > d[x][y] + 1)) n             mark[nx][ny] = 1, d[nx][ny] = d[x][y] + 1, q.push({nx, ny}); n         } n       } n     } n     printf( %d , d[0][0]); n   } else { n     printf( Zmey n ); n     dfs2(h, t); n     printf( %d , dis); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   bool sorted = 0; n   int a, b, c; n   c = count(s.begin(), s.end(),  c ); n   a = count(s.begin(), s.end(),  a ); n   b = count(s.begin(), s.end(),  b ); n   string ss = s; n   sort(ss.begin(), ss.end()); n   for (int i = 0; i < s.size(); i++) { n     if (ss[i] == s[i]) { n       sorted = 1; n     } else { n       cout <<  NO ; n       return 0; n     } n   } n   s.erase(unique(s.begin(), s.end()), s.end()); n   if (s[0] ==  a  && s[1] ==  b  && s[2] ==  c ) sorted = 1; n   if (a >= 1 && b >= 1 && (c == a || c == b) && sorted) { n     cout <<  YES ; n     return 0; n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const long long mod = 1e9 + 7; n int mp[510][510]; n struct mon { n   int x, y, t; n } t[510 * 510]; n int n, m, k, q; n int cmp(mon a, mon b) { return a.t < b.t; } n int sum[510][510]; n int check(int tim) { n   int i, j; n   memset(mp, 0, sizeof(mp)); n   for (i = 1; i <= tim; i++) mp[t[i].x][t[i].y] = 1; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= m; j++) { n       sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + mp[i][j]; n     } n   for (i = 1; i <= n - k + 1; i++) { n     for (j = 1; j <= m - k + 1; j++) { n       int temp = sum[i + k - 1][j + k - 1] - sum[i + k - 1][j - 1] - n                  sum[i - 1][j + k - 1] + sum[i - 1][j - 1]; n       if (temp == k * k) return 1; n     } n   } n   return 0; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m >> k >> q; n   int i; n   for (i = 1; i <= q; i++) { n     cin >> t[i].x >> t[i].y >> t[i].t; n   } n   sort(t + 1, t + q + 1, cmp); n   int l = 1; n   int r = q + 1; n   while (l < r) { n     int mid = (l + r) >> 1; n     if (check(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   if (l == q + 1) n     cout << -1; n   else n     cout << t[l].t; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long MOD = (long long)1e9 + 7; n const int SIZE = 2e5 + 5; n const long long INF = 1LL << 60; n const double eps = 1e-4; n const double PI = 3.1415926535897932; n int last[26]; n int main() { n   string s; n   cin >> s; n   int n = ((int)(s).size()); n   for (int i = 0; i < (26); ++i) last[i] = -1; n   for (int i = 0; i < (n); ++i) last[s[i] -  a ] = i; n   int cl = 0; n   stack<int> st; n   string ans =   ; n   for (int i = 0; i < (n); ++i) { n     while (cl < 26 && last[cl] < i) { n       while (((int)(st).size()) && (st.top() <= cl)) { n         ans +=  a  + st.top(); n         st.pop(); n       } n       cl++; n     } n     if (cl >= 26) break; n     while (((int)(st).size()) && (st.top() <= cl)) { n       ans +=  a  + st.top(); n       st.pop(); n     } n     if (last[cl] > i) { n       if (s[i] ==  a  + cl) { n         ans += s[i]; n       } else { n         st.push(s[i] -  a ); n       } n     } else if (last[cl] == i) { n       ans += s[i]; n       cl++; n     } n   } n   while (((int)(st).size())) { n     int i = st.top(); n     st.pop(); n     ans +=  a  + i; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long l1, r1, l2, r2, k; n   scanf( %I64d %I64d %I64d %I64d %I64d , &l1, &r1, &l2, &r2, &k); n   long long l, r; n   l = max(l1, l2); n   r = min(r1, r2); n   long long ans = (r - l) + 1; n   if (k <= r && k >= l) ans--; n   ans = max(0LL, ans); n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(int x, int y) { return x > y; } n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int main() { n   int n; n   cin >> n; n   vector<pair<int, int> > a(n); n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     a[i] = make_pair(x, y); n   } n   int res = 0; n   for (int i = 0; i < n; i++) { n     int x1 = a[i].first; n     int y1 = a[i].second; n     int upper = 0, lower = 0, right = 0, left = 0; n     for (int j = 0; j < n; j++) { n       if (i != j) { n         int x2 = a[j].first; n         int y2 = a[j].second; n         if (x2 > x1 && y1 == y2) right++; n         if (x2 < x1 && y1 == y2) left++; n         if (x1 == x2 && y2 > y1) upper++; n         if (x1 == x2 && y2 < y1) lower++; n       } n     } n     if (upper >= 1 && lower >= 1 && right >= 1 && left >= 1) res++; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char kata[1005]; n int panjang, total[30], batas; n int main() { n   scanf( %s , kata); n   panjang = strlen(kata); n   for (int i = 0; i < panjang; ++i) ++total[kata[i] -  a ]; n   scanf( %d , &batas); n   int cek = 0; n   for (int i = 0; i < 26; ++i) n     if (total[i]) ++cek; n   if (cek > batas) { n     printf( -1 n ); n     return 0; n   } n   int kiri = 0; n   int kanan = panjang; n   while (kanan - kiri > 1) { n     int tengah = (kanan + kiri) / 2; n     int pakai = 0; n     for (int i = 0; i < 26; ++i) { n       pakai += (total[i] + tengah - 1) / tengah; n     } n     if (pakai > batas) n       kiri = tengah; n     else n       kanan = tengah; n   } n   printf( %d n , kanan); n   for (int i = 0; i < 26; ++i) { n     int temp = (total[i] + kanan - 1) / kanan; n     for (int j = 0; j < temp; ++j) printf( %c , i +  a ); n     batas -= temp; n   } n   for (int i = 0; i < batas; ++i) printf( a ); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, dem = 0; n   cin >> n; n   dem = n / 2520; n   cout << dem; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[1000000 + 10]; n int a[1000000 + 10]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] <= m) { n       cnt[a[i]]++; n     } n   } n   for (int i = m; i >= 1; i--) { n     for (int j = i; (j += i) <= m;) { n       cnt[j] += cnt[i]; n     } n   } n   int i, j; n   for (i = j = 1; i <= m; i++) { n     if (cnt[i] > cnt[j]) { n       j = i; n     } n   } n   cout << j <<     << cnt[j] << endl; n   for (i = 0; i < n; i++) { n     if (j % a[i] == 0) { n       cout << i + 1 <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 160; n const int maxM = (int)1e6 + 100; n const int mod = (int)1e9 + 7; n const int maxlog = (int)20; n const int P = mod; n int n, q, l[maxn], r[maxn], mx[maxn], mn[maxn], t[maxn], v[maxn], b[maxn], n     cnt[maxn], ans = INT_MAX, tot = 5000000, was[maxn], a[maxn]; n int get() { n   for (int i = 1; i <= n; i++) cnt[a[i]]++; n   int res = 0; n   for (int i = 1; i <= n; i++) { n     res += cnt[i] * cnt[i]; n   } n   return res; n } n int randint(int l, int r) { n   int v = (rand() << 15) ^ rand(); n   v %= r - l + 1; n   return v + l; n } n int main() { n   srand(time(0)); n   cin >> n >> q; n   if (q == 0) { n     cout << n; n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     mn[i] = 1; n     mx[i] = n; n   } n   for (int i = 1; i <= q; i++) { n     cin >> t[i] >> l[i] >> r[i] >> v[i]; n     for (int j = l[i]; j <= r[i]; j++) { n       if (t[i] == 2) n         mx[j] = min(mx[j], v[i]); n       else n         mn[j] = max(mn[j], v[i]); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (mx[i] < mn[i]) { n       cout << -1; n       return 0; n     } n     a[i] = mn[i]; n   } n   ans = get(); n   int timer = 0; n   while (clock() / (double)CLOCKS_PER_SEC < 2.95) { n     memset(cnt, 0, sizeof(cnt)); n     timer++; n     for (int i = 1; i <= n; i++) b[i] = a[i]; n     int x = min(n, randint(1, max(1, n / 3)) + 1); n     for (int i = 1; i <= x; i++) { n       int pos = randint(1, n); n       while (was[pos] == timer) pos = randint(1, n); n       was[pos] = timer; n       a[pos] = randint(mn[pos], mx[pos]); n     } n     for (int i = 1; i <= n; i++) cnt[a[i]]++; n     int cur = 0; n     for (int i = 1; i <= n; i++) cur += cnt[i] * cnt[i]; n     if (ans >= cur) n       ans = cur; n     else { n       for (int i = 1; i <= n; i++) a[i] = b[i]; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T sqr(T t) { n   return t * t; n } n inline void files() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int n1, m1, n2, m2; n int a[150][150], b[150][150]; n int mx, ansx, ansy; n int main() { n   files(); n   cin >> n1 >> m1; n   for (int i = 1; i <= n1; i++) n     for (int j = 1; j <= m1; j++) { n       char c; n       cin >> c; n       a[i][j] = c -  0 ; n     } n   cin >> n2 >> m2; n   for (int i = 1; i <= n2; i++) n     for (int j = 1; j <= m2; j++) { n       char c; n       cin >> c; n       b[i][j] = c -  0 ; n     } n   for (int x = -50; x <= 50; x++) { n     for (int y = -50; y <= 50; y++) { n       int cur = 0; n       for (int i = 1; i <= n1; i++) n         for (int j = 1; j <= m1; j++) n           if (i + x <= n2 && j + y <= m2 && i + x >= 1 && j + y >= 1) n             cur += (a[i][j] * b[i + x][j + y]); n       if (mx < cur) { n         mx = cur; n         ansx = x; n         ansy = y; n       } n     } n   } n   cout << ansx <<     << ansy; n } n


#include <bits/stdc++.h> n int main() { n   char a[10], e[10]; n   int b[10], d[10], i, j, c = 0, t; n   scanf( %s , a); n   scanf( %s , e); n   for (i = 0; i < strlen(a); i++) { n     b[i] = a[i] -   0 ; n     b[i] = b[i] - 48; n     d[i] = e[i] -   0 ; n     d[i] = d[i] - 48; n   } n   for (i = 0; i < strlen(a); i++) { n     for (j = i + 1; j < strlen(a); j++) { n       if (b[i] > b[j]) { n         t = b[i]; n         b[i] = b[j]; n         b[j] = t; n       } n     } n   } n   for (i = 0; i < strlen(a); i++) { n     if (b[i] == 0) n       c++; n     else n       break; n   } n   if (c != strlen(a)) { n     t = b[c]; n     b[c] = b[0]; n     b[0] = t; n   } n   c = 0; n   for (i = 0; i < strlen(a); i++) { n     if (b[i] == d[i]) c++; n   } n   if (c == strlen(a) && c == strlen(e)) n     printf( OK ); n   else n     printf( WRONG_ANSWER ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename S> n ostream &operator<<(ostream &os, const pair<T, S> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename C, typename T = decay<decltype(*begin(declval<C>()))>, n           typename enable_if<!is_same<C, string>::value>::type * = nullptr> n ostream &operator<<(ostream &os, const C &c) { n   bool f = true; n   os <<  [ ; n   for (const auto &x : c) { n     if (!f) os <<  ,  ; n     f = false; n     os << x; n   } n   return os <<  ] ; n } n template <typename T> n void debug(string s, T x) { n   cerr << s <<   =   << x <<   n ; n } n template <typename T, typename... Args> n void debug(string s, T x, Args... args) { n   cerr << s.substr(0, s.find( , )) <<   =   << x <<   |  ; n   debug(s.substr(s.find( , ) + 2), args...); n } n int a[200000]; n bool isMountain[200000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     for (int i = 0; i < n; i++) cin >> a[i]; n     int ret = 0, cnt = 0; n     for (int i = 0; i < n; i++) n       isMountain[i] = n           (i > 0 && i < n - 1 && a[i] > a[i - 1] && a[i] > a[i + 1]); n     for (int i = 1; i < k - 1; i++) cnt += isMountain[i]; n     ret = cnt; n     int l = 0; n     for (int i = k; i < n; i++) { n       cnt -= isMountain[i - (k - 1)]; n       cnt += isMountain[i - 1]; n       if (cnt > ret) { n         ret = cnt; n         l = i - k + 1; n       } n     } n     cout << ret + 1 <<     << l + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f(string s) { n   int answer = 0; n   answer += 600 * (int(s[0]) - 48); n   answer += 60 * (int(s[1]) - 48); n   answer += 10 * (int(s[3]) - 48); n   answer += (int(s[4]) - 48); n   return answer; n } n string s(int a) { n   string answer; n   answer.clear(); n   answer += char(a / 600 + 48); n   a = a % 600; n   answer += char(a / 60 + 48); n   a = a % 60; n   answer +=  : ; n   answer += char(a / 10 + 48); n   a = a % 10; n   answer += char(a + 48); n   return answer; n } n int timer[2880] = {0}; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     timer[f(s) % 1440] = 1; n     timer[(f(s) % 1440) + 1440] = 1; n   } n   int best = 0; n   int temp = 0; n   for (int i = 0; i < 2880; i++) { n     if (timer[i] == 0) n       temp++; n     else { n       best = max(best, temp); n       temp = 0; n     } n   } n   cout << s(best); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   pair<int, char> p[200005]; n   for (int i = 0; i < n; i++) { n     scanf( %d %c , &p[i].first, &p[i].second); n   } n   sort(p, p + n); n   int minred = 1e9 + 1, maxred = 1e9 + 1; n   int mingreen = 1e9 + 1, maxgreen = 1e9 + 1; n   for (int i = 0; i < n; i++) { n     if (p[i].second ==  R ) { n       maxred = p[i].first; n       minred = min(minred, p[i].first); n     } else if (p[i].second ==  B ) { n       maxgreen = p[i].first; n       mingreen = min(mingreen, p[i].first); n     } else { n       maxred = p[i].first; n       minred = min(minred, p[i].first); n       maxgreen = p[i].first; n       mingreen = min(mingreen, p[i].first); n     } n   } n   long long ans = (long long)maxred - minred + maxgreen - mingreen; n   int lastp = -1; n   for (int i = 0; i < n; i++) { n     if (p[i].second ==  P ) { n       if (lastp != -1) { n         int lastr = p[lastp].first; n         int lastg = p[lastp].first; n         int maxr = 0, maxg = 0; n         for (int j = lastp + 1; j < i; j++) { n           if (p[j].second ==  R ) { n             maxr = max(p[j].first - lastr, maxr); n             lastr = p[j].first; n           } else { n             maxg = max(p[j].first - lastg, maxg); n             lastg = p[j].first; n           } n         } n         maxr = max(p[i].first - lastr, maxr); n         maxg = max(p[i].first - lastg, maxg); n         ans -= max(0, maxr + maxg - (p[i].first - p[lastp].first)); n       } n       lastp = i; n     } n   } n   printf( %lld n , ans); n } n


#include  bits/stdc++.h  n using namespace std; n  n #define ffor(n) for(int i = 0; i < n; i++) n #define fffor(n) for(int j = 0; j < n; j++) n #define uwu ios_base::sync_with_stdio(false);cin.tie(NULL); n #pragma GCC optimize( Ofast ) n  n const int INF = 1e9 + 7; n const long long INF2 = 1e15; n  n void solve(void) { n   n } n  n bool valid = true; n  n void verify(int current, vector <vector <int>>& asd, vector <int>& state, vector <int>& res) { n  if (valid == false) return; n  if (state[current] == 1) { n   valid = false; n   return; n  } n  if (state[current] == 2) return; n  if (state[current] == 0) { n   state[current] = 1; n   for (int nextnode: asd[current]) { n    verify(nextnode, asd, state, res); n   } n   state[current] = 2; n   res.push_back(current); n  } n } n  n  n int main(void) { n  uwu n  n  int n, k; n  cin >> n >> k; n  vector <int> v(n); n  ffor(n) cin >> v[i]; n  vector <pair <int, int>> v2(k); n  ffor(k) cin >> v2[i].first >> v2[i].second; n  n  set <pair <int, int>> edges; n  ffor(n) { n   if (v[i] != 0) edges.insert({v[i] - 1, i}); n  } n  ffor(k) { n   edges.insert({v2[i].first - 1, v2[i].second - 1}); n  } n   n  // next in chain n  vector <int> next(n, -1), prev(n, -1); n  for(int i = 0; i < k; i++) { n   next[v2[i].first - 1] = v2[i].second - 1; n   prev[v2[i].second - 1] = v2[i].first - 1; n  } n  vector <int> chained(n, -1);  n  for(int i = 0; i < n; i++) { n   if (chained[i] != -1) continue; n   auto head = i, tail = i; n   chained[i] = i; n   while(prev[head] != -1) { n    if (chained[prev[head]] != -1) { n     valid = false; n     break; n    } n    head = prev[head]; n    chained[head] = i; n   } n   while(next[tail] != -1) { n    if (chained[next[tail]] != -1) { n     valid = false; n     break; n    } n    tail = next[tail]; n    chained[tail] = i; n   } n   while(tail != head) { n    if (v[tail] != 0 && chained[v[tail] - 1] != i) { n     edges.insert({v[tail] - 1, head}); n    } n    tail = prev[tail]; n   } n  } n   n  // adjacency list n  vector <vector <int>> asd(n); n  for(auto e: edges) { n   asd[e.first].push_back(e.second); n  } n   n  // topological sort n  vector <int> state(n), res; n  for(int i = 0; i < n; i++) { n   if (v[i] == 0) { n    verify(i, asd, state, res); n   } n  } n   n  vector <bool> visited(n); n  reverse(res.begin(), res.end()); // reverse reversed topological sort n  vector <int> ans, order_of(n); n   n   n  for(int e: res) { n   auto current = e; n   if (visited[current]) continue; n   // follow chain n   while(next[current] != -1 && visited[next[current]] != true) { n    ans.push_back(current); n    visited[current] = true; n    current = next[current]; n   } n   ans.push_back(current); n   visited[current] = true; n   current = next[current]; n  } n    n  if (valid) { n   // find position in answer n   for(int i = 0; i < ans.size(); i++) { n    order_of[ans[i]] = i; n   } n   // verify ordering n   for(int i = 0; i < v.size(); i++) { n    if (v[i] == 0) continue; n    if (order_of[v[i] - 1] > order_of[i]) valid = false; n   } n   for(int i = 0; i < k; i++) { n    if (order_of[v2[i].first - 1] + 1 != order_of[v2[i].second - 1]) valid = false; n   } n   if (valid) { n    for (auto e: ans) cout << e + 1 <<    ; n    cout <<   n ; n   } n   else cout << 0 <<   n ; n  } n  else cout << 0 <<   n ; n }  n  n  n  n  n  n  n  n /* n C: Users User Desktop comp_programming final_straw_after_this_im_switching_to_vs_code finally_switched_to_geany.cpp n */ n


#include <bits/stdc++.h> n using namespace std; n int a[1005], dp[1005][1005], dp_sum[1005][1005]; n int main() { n   ios::sync_with_stdio(false); n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + n + 1); n   int limit = a[n] / (k - 1); n   int ans = 0; n   for (int diff = 1; diff <= limit; diff++) { n     for (int j = 1; j <= n; j++) dp_sum[j][1] = j, dp[j][1] = 1; n     for (int ct = 2; ct <= k; ct++) { n       for (int j = 1, l = 1; j <= n; j++) { n         while (l < j && a[j] - a[l] >= diff) l++; n         dp[j][ct] = dp_sum[l - 1][ct - 1]; n         dp_sum[j][ct] = dp[j][ct] + dp_sum[j - 1][ct]; n         if (dp_sum[j][ct] >= 998244353) dp_sum[j][ct] -= 998244353; n       } n     } n     ans += dp_sum[n][k]; n     if (ans >= 998244353) ans -= 998244353; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n, a[205], b, c[205]; n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   string s; n   cin >> t; n   while (t--) { n     cin >> n >> b; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       c[a[i]] = 1; n     } n     for (int i = 1;; i++) { n       if (c[i] == 0) { n         b--; n       } n       if (b == 0) { n         for (int j = i + 1;; j++) { n           if (c[j] == 0) { n             cout << j - 1 <<   n ; n             break; n           } n         } n         break; n       } n     } n     for (int i = 0; i < 105; i++) { n       a[i] = 0; n       c[i] = 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n vector<pair<int, int>> t; n bool pairsort(pair<int, int> e1, pair<int, int> e2) { n   return e1.first <= e2.first; n } n int main() { n   int n; n   cin >> n; n   int pos, pow; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &pos, &pow); n     t.push_back(make_pair(pos, pow)); n   } n   sort(t.begin(), t.end(), pairsort); n   memset(a, 0, sizeof(a)); n   a[0] = 0; n   a[1] = 1; n   for (int i = 1; i < t.size(); i++) { n     if (t[i].first - t[i].second > t[i - 1].first) { n       a[i + 1] = a[i] + 1; n     } else { n       int l = 0; n       int r = i - 1; n       int k = t[i].first - t[i].second; n       while (r > l) { n         int mid = (r + l) / 2; n         if (t[mid].first >= k) n           r = mid; n         else n           l = mid + 1; n       } n       a[i + 1] = a[l] + 1; n     } n   } n   int max = 0; n   for (int i = 0; i <= n; i++) { n     max = max > a[i] ? max : a[i]; n   } n   int ans = n - max; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct fenwickTree { n   long long n; n   vector<long long> bit; n   fenwickTree() {} n   fenwickTree(long long siz) : n(siz + 5), bit(siz + 5, 0) {} n   void add(long long pos, long long upd) { n     for (; pos < n; pos += pos & (-pos)) bit[pos] += upd; n   } n   long long sum(long long pos) { n     long long ret = 0; n     for (; pos; pos -= pos & (-pos)) ret += bit[pos]; n     return ret; n   } n   long long query(long long pos) { n     return sum(n - 1) - sum(min(n - 1, pos - 1)); n   } n }; n const long long nax = 100005; n long long c[nax], ss[nax] = {0}, dsu[nax], siz[nax] = {0}, ans[nax]; n vector<long long> g[nax]; n vector<pair<long long, long long>> q[nax]; n vector<map<long long, long long>> mp(nax); n vector<fenwickTree> bitarr(nax); n void dfs(long long u, long long p) { n   ss[u] = siz[u] = 1; n   long long mx = -1, mxc = -1; n   for (auto &k : g[u]) n     if (k != p) { n       dfs(k, u); n       ss[u] += ss[k]; n       if (ss[k] > mx) mx = ss[k], mxc = k; n     } n   if (mxc != -1) { n     dsu[u] = dsu[mxc]; n     siz[dsu[mxc]] = ss[u]; n   } n } n void add(long long u, long long col, long long cnt) { n   if (!cnt) return; n   if (mp[u].count(col)) bitarr[u].add(mp[u][col], -1); n   mp[u][col] += cnt; n   bitarr[u].add(mp[u][col], 1); n } n void dfs2(long long u, long long p) { n   for (auto &k : g[u]) n     if (k != p) dfs2(k, u); n   add(dsu[u], c[u], 1); n   for (auto &k : g[u]) n     if (k != p && dsu[k] != dsu[u]) n       for (auto &col : mp[dsu[k]]) add(dsu[u], col.first, col.second); n   for (auto &qry : q[u]) ans[qry.first] = bitarr[dsu[u]].query(qry.second); n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, m; n   cin >> n >> m; n   for (long long i = 1; i <= n; i++) dsu[i] = i; n   for (long long i = 1; i <= n; i++) cin >> c[i]; n   for (long long i = 0; i < n - 1; i++) { n     long long u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(1, 1); n   for (long long i = 1; i <= n; i++) bitarr[i] = fenwickTree(siz[i]); n   for (long long i = 0; i < m; i++) { n     long long a, b; n     cin >> a >> b; n     q[a].push_back({i + 1, b}); n   } n   dfs2(1, 1); n   for (long long i = 1; i <= m; i++) cout << ans[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5, inf = 0x3f3f3f3f; n int main() { n   int a, b, h1, h2; n   while (cin >> h1 >> h2) { n     cin >> a >> b; n     int add = a * 12, de = b * 12, first = 8 * a; n     if (a <= b) { n       if (h1 + first >= h2) n         puts( 0 ); n       else n         puts( -1 ); n       continue; n     } n     int ans = 0; n     h1 += first; n     if (h1 >= h2) n       puts( 0 ); n     else { n       add -= de; n       ans += (h2 - h1) / add; n       if ((h2 - h1) % add) ans++; n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 159, INF = 2e9 + 9; n struct mat { n   bitset<MAX> a[MAX]; n   mat(int v = 0) { n     for (int i = 0; i < MAX; i++) n       for (int j = 0; j < MAX; j++) n         if (i == j) n           a[i][j] = v; n         else n           a[i][j] = 0; n   } n } adj(0); n mat operator*(mat a, mat b) { n   mat tmp(0), ret(0); n   for (int i = 0; i < MAX; i++) n     for (int j = 0; j < MAX; j++) tmp.a[i][j] = b.a[j][i]; n   for (int i = 0; i < MAX; i++) n     for (int j = 0; j < MAX; j++) n       ret.a[i][j] = ((a.a[i] & tmp.a[j]).count() > 0); n   return ret; n } n mat operator^(mat a, int n) { n   mat ret(1); n   if (!n) return ret; n   ret = a ^ (n >> 1); n   if (n & 1) return ret * ret * a; n   return ret * ret; n } n map<int, vector<pair<int, int> > > m; n bitset<MAX> can, t; n bool mark[MAX]; n vector<int> g[MAX]; n long long n, e, a, b, d, dis[MAX][MAX], h[MAX]; n queue<int> q; n long long bfs() { n   while (q.size()) { n     int v = q.front(); n     q.pop(); n     if (v == n - 1) return h[v]; n     for (auto u : g[v]) n       if (!mark[u]) q.push(u), h[u] = h[v] + 1, mark[u] = true; n   } n   return INF; n } n int main() { n   cin >> n >> e; n   for (int i = 0; i < MAX; i++) n     for (int j = 0; j < MAX; j++) dis[i][j] = (i == j ? 0 : INF); n   for (int i = 0; i < e; i++) n     cin >> a >> b >> d, a--, b--, m[d].push_back({a, b}), dis[a][b] = 1; n   can[0] = 1, m[0].push_back({0, 0}), m[0].pop_back(); n   long long last, ans = INF; n   for (map<int, vector<pair<int, int> > >::iterator it = m.begin(); n        it != m.end(); it++) { n     long long d1 = it->first; n     it++; n     if (it == m.end()) { n       it--; n       last = it->first; n       break; n     } n     long long d2 = it->first; n     it--; n     for (auto i : it->second) n       adj.a[i.first][i.second] = 1, g[i.first].push_back(i.second); n     memset(h, 0, sizeof h), memset(mark, 0, sizeof mark); n     while (q.size()) q.pop(); n     for (int i = 0; i < MAX; i++) n       if (can[i]) mark[i] = true, q.push(i); n     ans = min(ans, bfs() + d1); n     mat tmp = adj, tmp2; n     tmp = tmp ^ (d2 - d1); n     for (int i = 0; i < MAX; i++) n       for (int j = 0; j < MAX; j++) tmp2.a[i][j] = tmp.a[j][i]; n     t = can; n     for (int i = 0; i < MAX; i++) can[i] = (t & tmp2.a[i]).count() > 0; n   } n   for (int k = 0; k < MAX; k++) n     for (int i = 0; i < MAX; i++) n       for (int j = 0; j < MAX; j++) n         dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); n   for (int i = 0; i < MAX; i++) n     if (can[i]) ans = min(ans, dis[i][n - 1] + last); n   if (ans < INF) n     cout << ans; n   else n     cout <<  Impossible ; n } n


#include <bits/stdc++.h> n using namespace std; n int getint() { n   int x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) x *= 10, x += (c -  0 ), c = getchar(); n   return x; n } n vector<pair<int, int> > v[2]; n int n, a[2][10010]; n void XOR(int i, int j, int idx) { n   v[idx].push_back(make_pair(i, j)); n   a[idx][i] ^= a[idx][j]; n } n void init() { n   n = getint(); n   for (int j = 0; j < 2; j++) n     for (int i = 0; i < n; i++) a[j][i] = getint(); n } n void Gauss(int idx) { n   int pre = 0; n   for (int i = 30; i >= 0; i--) n     for (int j = pre; j < n; j++) n       if (a[idx][j] & (1 << i)) { n         if (j != pre) { n           XOR(j, pre, idx); n           XOR(pre, j, idx); n           XOR(j, pre, idx); n         } n         for (int k = 0; k < n; k++) n           if (k != pre && (a[idx][k] & (1 << i))) XOR(k, pre, idx); n         pre++; n         break; n       } n } n void solve() { n   for (int i = 0; i < 2; i++) Gauss(i); n   for (int i = 0; i < n; i++) n     if (a[0][i]) { n       int bs = (1 << 30); n       while ((a[0][i] & bs) == 0) bs >>= 1; n       for (int j = 0; j <= 30 && j < n; j++) n         if ((a[0][j] ^ a[1][j]) & bs) XOR(j, i, 0); n     } else n       break; n   for (int i = 0; i < n; i++) n     if (a[0][i] != a[1][i]) { n       puts( -1 ); n       return; n     } n   printf( %d n , (int)v[0].size() + (int)v[1].size()); n   for (int i = 0; i < (int)v[0].size(); i++) n     printf( %d %d n , v[0][i].first + 1, v[0][i].second + 1); n   for (int i = (int)v[1].size() - 1; i >= 0; i--) n     printf( %d %d n , v[1][i].first + 1, v[1][i].second + 1); n } n int main() { n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, zheng = 0, mmax = 0, strc, stra, cuowu; n   char a[1000], b[26], c[1000]; n   double bb[26], sheng[26], bisheng[26], cc[26]; n   cin >> a; n   cin >> n; n   sort(a, a + strlen(a)); n   memset(c, 0, sizeof(c)); n   b[0] =  a ; n   for (int i = 1; i < 26; i++) { n     b[i] = b[i - 1] + 1; n   } n   for (int i = 0; i < 26; i++) { n     bb[i] = 0, cc[i] = 0, sheng[i] = 0, bisheng[i] = 0; n   } n   for (int i = 0; i < strlen(a); i++) { n     for (int j = 0; j < 26; j++) { n       if (a[i] == b[j]) bb[j]++; n     } n   } n   for (int i = 0; i < 26; i++) { n     if (bb[i] != 0) zheng++; n     if (bb[i] > mmax) mmax = bb[i]; n   } n   stra = strlen(a); n   if (n > strlen(a)) { n     cout << 1 << endl; n     int cuowu = 0; n     for (int k = 0; k < 26; k++) { n       if (sheng[k] > sheng[cuowu]) cuowu = k; n     } n     for (int i = 0; i < n - stra; i++) { n       a[i + stra] = a[cuowu]; n     } n     for (int i = 0; i < n; i++) { n       cout << a[i]; n     } n     cout << endl; n   } else if (n < zheng) { n     cout << -1 << endl; n   } else { n     int j = 0, yu; n     for (int i = 0; i < 26; i++) { n       sheng[i] = bb[i]; n       bisheng[i] = bb[i]; n       if (bb[i] != 0) { n         c[j] = b[i]; n         cc[i] = 1; n         j++; n       } n     } n     yu = n - strlen(c); n     if (yu == 0) { n       cout << mmax << endl; n       cout << c << endl; n     } else { n       strc = strlen(c); n       mmax = 0; n       for (int i = 0; i < yu; i++) { n         for (int k = 0; k < 26; k++) { n           if (sheng[k] > sheng[mmax]) mmax = k; n         } n         cc[mmax] += 1; n         sheng[mmax] = bisheng[mmax] / cc[mmax]; n         c[i + strc] = b[mmax]; n       } n       for (int k = 0; k < 26; k++) { n         if (sheng[k] > sheng[mmax]) mmax = k; n       } n       cout << ceil(sheng[mmax]) << endl; n       cout << c << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   vector<pair<int, int>> vec; n   int n, a, b; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     if (a != b) { n       cout <<  rated ; n       return 0; n     } n     vec.push_back({a, b}); n   } n   for (int i = 0; i < vec.size() - 1; i++) { n     if (vec[i].first < vec[i + 1].first) { n       cout <<  unrated ; n       return 0; n     } n   } n   cout <<  maybe ; n } n


#include <bits/stdc++.h> n #define int long long n #define F0R(i, n) for(int i = 0; i < (n); i++) n using namespace std; n  n signed main() n { n     ios::sync_with_stdio(0); n     cin.tie(0), cout.tie(0); n     int T; cin >> T; n     F0R(cas,T) { n         int N; cin >> N; n         vector<int> a(N); n         for(int &x : a) n             cin >> x; n         sort(a.begin(),a.end(),greater<int>()); n         int evsum = 0, odsum = 0; n         F0R(i,N) { n             if(i%2 && a[i]%2) n                 odsum += a[i]; n             if((i%2 == 0) && (a[i]%2 == 0)) n                 evsum += a[i]; n         } n         if(evsum >  odsum) cout <<  Alice n ; n         if(evsum == odsum) cout <<  Tie n ; n         if(evsum <  odsum) cout <<  Bob n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n #pragma optimize( TKACHENKO-GORYACHENKO ) n using namespace std; n const int maxn = 11e5; n const int inf = 1e18; n int n, m, k; n int A[maxn]; n int B[maxn]; n int C[maxn]; n int V[4 * maxn]; n int T[4 * maxn]; n inline void push(int v, int tl, int tr) { n   if (T[v] == 0) { n     return; n   } n   V[v] += T[v]; n   if (tl != tr) { n     T[2 * v] += T[v]; n     T[2 * v + 1] += T[v]; n   } n   T[v] = 0; n } n inline void build(int v, int tl, int tr) { n   if (tl == tr) { n     V[v] = C[tl]; n     return; n   } n   int tm = (tl + tr) / 2; n   build(2 * v, tl, tm); n   build(2 * v + 1, tm + 1, tr); n   V[v] = min(V[2 * v], V[2 * v + 1]); n } n inline void update(int v, int tl, int tr, int l, int r, int color) { n   push(v, tl, tr); n   if (l > r || tl > r || l > tr) { n     return; n   } n   if (tl == l && tr == r) { n     T[v] += color; n     push(v, tl, tr); n     return; n   } n   int tm = (tl + tr) / 2; n   update(2 * v, tl, tm, l, min(r, tm), color); n   update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, color); n   V[v] = min(V[2 * v], V[2 * v + 1]); n } n inline void changea(int i, int j) { n   if (j >= i) { n     update(1, 0, maxn - 1, i + 1, j, -1); n   } else { n     update(1, 0, maxn - 1, j + 1, i, 1); n   } n } n inline void changeb(int i, int j) { n   if (j >= i) { n     update(1, 0, maxn - 1, i + 1, j, 1); n   } else { n     update(1, 0, maxn - 1, j + 1, i, -1); n   } n } n inline int getans(int v, int tl, int tr) { n   push(v, tl, tr); n   int tm = (tl + tr) / 2; n   if (tl != tr) { n     push(2 * v, tl, tm); n     push(2 * v + 1, tm + 1, tr); n   } n   if (V[v] >= 0) { n     return -1; n   } n   if (tl == tr) { n     return tl; n   } n   if (V[2 * v + 1] < 0) { n     return getans(2 * v + 1, tm + 1, tr); n   } n   if (V[2 * v] < 0) { n     return getans(2 * v, tl, tm); n   } n } n int main() { n   int q, w, e, a, b, f; n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> m; n   for (q = 0; q < n; q++) { n     cin >> A[q]; n     C[A[q]]--; n   } n   for (q = 0; q < m; q++) { n     cin >> B[q]; n     C[B[q]]++; n   } n   cin >> k; n   for (q = maxn - 2; q >= 0; q--) { n     C[q] += C[q + 1]; n   } n   build(1, 0, maxn - 1); n   for (e = 0; e < k; e++) { n     int i, x, comm; n     cin >> comm >> i >> x; n     i--; n     if (comm == 1) { n       changea(A[i], x); n       A[i] = x; n     } else { n       changeb(B[i], x); n       B[i] = x; n     } n     cout << getans(1, 0, maxn - 1) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   bool tr = false; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     if (x != y) tr = true; n   } n   if (tr) n     cout <<  Happy Alex n ; n   else n     cout <<  Poor Alex n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool ACS(string s) { n   int n1 = s.length(); n   for (int i = 1; i < n1; i++) n     if (s[i] != s[0]) return false; n   return true; n } n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     if (s.length() == 1) n       cout << 0 << endl; n     else if (s[0] == s[1] && ACS(s)) n       cout << 1 << endl; n     else { n       int ss1 = 0; n       map<char, int> m1; n       for (int i = 0; i < s.length(); i++) m1[s[i]]++; n       for (int i = 0; i < m1.size(); i++) { n         if (m1[i] > 2) n           ss1 = ss1 + 2; n         else n           ss1 = ss1 + m1[i]; n       } n       if (ss1 % 2) n         cout << (ss1 - 1) / 2 << endl; n       else n         cout << ss1 / 2 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n void fn(long long int a[], long long int a1, long long int b, long long int n) { n   for (long long int i = 0; i < n; i++) { n     a[i] = a1; n     a1 += b; n   } n } n void solve() { n   long long int n, x, y; n   cin >> n >> x >> y; n   long long int bst = INT_MAX; n   long long int a[n]; n   for (long long int i = 1; i < 51; i++) { n     for (long long int j = 1; j < 51; j++) { n       long long int x1 = 0, y1 = 0; n       long long int a1[n]; n       fn(a1, i, j, n); n       for (long long int i = 0; i < n; i++) { n         if (a1[i] == x) { n           x1 = 1; n         } n         if (a1[i] == y) { n           y1 = 1; n         } n       } n       if (x1 and y1 and a1[n - 1] < bst) { n         for (long long int i = 0; i < n; i++) { n           a[i] = a1[i]; n         } n         bst = a[n - 1]; n       } n     } n   } n   for (long long int i = 0; i < n; i++) { n     cout << a[i] <<    ; n   } n   cout <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[3]; n int main() { n   int n; n   cin >> n; n   string s1, s2; n   cin >> s1 >> s2; n   int ans1 = 0, ans2 = 0; n   for (int i = 0; i < s1.size(); i++) { n     if (s1[i] ==  a ) { n       ans1++; n     } n     if (s2[i] ==  a ) { n       ans1++; n     } n     if (s1[i] ==  b ) { n       ans2++; n     } n     if (s2[i] ==  b ) { n       ans2++; n     } n   } n   if (s1 == s2) { n     cout << 0 << endl; n     return 0; n   } n   if (ans1 % 2 || ans2 % 2) { n     cout << -1 << endl; n     return 0; n   } else { n     s1 =     + s1; n     s2 =     + s2; n     for (int i = 1; i <= s1.size(); i++) { n       if (s1[i] == s2[i]) { n         continue; n       } n       if (s1[i] ==  a ) { n         v[1].push_back(i); n         continue; n       } else { n         v[2].push_back(i); n       } n     } n     int ans = 0; n     int flag = 0; n     if (v[1].size() % 2) { n       flag = v[1].back(); n       v[2].push_back(v[1].back()); n       v[1].pop_back(); n       ans++; n     } n     ans = ans + v[1].size() / 2 + v[2].size() / 2; n     cout << ans << endl; n     if (flag) cout << flag <<     << flag << endl; n     if (v[1].size() > 0) n       for (int i = 0; i < v[1].size() - 1; i += 2) { n         cout << v[1][i] <<     << v[1][i + 1] << endl; n       } n     if (v[2].size() > 0) n       for (int i = 0; i < v[2].size() - 1; i += 2) { n         cout << v[2][i] <<     << v[2][i + 1] << endl; n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace sat { n const int MAX = 200010; n bool vis[MAX]; n vector<int> ed[MAX], rev[MAX]; n int n, m; n vector<int> order; n int comp[MAX]; n inline int inv(int x) { return ((x) <= n ? (x + n) : (x - n)); } n void init(int vars) { n   n = vars, m = vars << 1; n   for (int i = 1; i <= m; i++) { n     ed[i].clear(); n     rev[i].clear(); n   } n } n inline void add(int a, int b) { n   ed[a].push_back(b); n   rev[b].push_back(a); n } n inline void OR(int a, int b) { n   add(inv(a), b); n   add(inv(b), a); n } n inline void AND(int a, int b) { n   add(a, b); n   add(b, a); n } n void XOR(int a, int b) { n   add(inv(b), a); n   add(a, inv(b)); n   add(inv(a), b); n   add(b, inv(a)); n } n inline void XNOR(int a, int b) { n   add(a, b); n   add(b, a); n   add(inv(a), inv(b)); n   add(inv(b), inv(a)); n } n inline void force_true(int x) { add(inv(x), x); } n void dfs(int s) { n   vis[s] = true; n   for (int x : ed[s]) { n     if (vis[x]) continue; n     dfs(x); n   } n   order.push_back(s); n } n void mark(int s, int c) { n   comp[s] = c; n   vis[s] = true; n   for (int x : rev[s]) { n     if (vis[x]) continue; n     mark(x, c); n   } n } n bool satisfy(vector<int> &res) { n   memset(vis, 0, sizeof(vis)); n   for (int i = 1; i <= m; i++) { n     if (vis[i]) continue; n     dfs(i); n   } n   memset(vis, 0, sizeof(vis)); n   reverse(order.begin(), order.end()); n   int it = 0; n   for (int s : order) { n     if (vis[s]) continue; n     mark(s, ++it); n   } n   res.clear(); n   for (int i = 1; i <= n; i++) { n     if (comp[i] == comp[inv(i)]) return false; n     if (comp[i] > comp[inv(i)]) res.push_back(i); n   } n   return true; n } n }  // namespace sat n int main() { n   int n, m, a, b, c; n   scanf( %d %d , &n, &m); n   sat::init(n); n   for (int i = 1; i <= m; i++) { n     scanf( %d %d %d , &a, &b, &c); n     if (c == 0) n       sat::XOR(a, b); n     else n       sat::XNOR(a, b); n   } n   vector<int> V; n   if (sat::satisfy(V)) { n     cout << V.size() << endl; n     for (int i = 0; i < V.size(); i++) cout << V[i] <<    ; n     puts(  ); n   } else n     puts( Impossible ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modpow(int a, int b, int m) { n   long long res = 1; n   long long base = a; n   base %= m; n   while (b > 0) { n     if (b % 2 == 1) { n       res = (res * base) % m; n     } n     b >>= 1; n     base = (base * base) % m; n   } n   return res; n } n long long modInverse(int a, int m) { return modpow(a, m - 2, m); } n long long modify(long long num) { return (num + 1000000007) % 1000000007; } n long long mypow(int base, int exp) { n   long long ret = 1; n   for (int i = 0; i < (exp); ++i) { n     ret *= base; n   } n   return ret; n } n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> a; n   vector<int> b; n   for (int i = 0; i < (n / k); ++i) { n     int nxt; n     cin >> nxt; n     a.push_back(nxt); n   } n   for (int i = 0; i < (n / k); ++i) { n     int nxt; n     cin >> nxt; n     b.push_back(nxt); n   } n   long long ans = 1; n   for (int i = 0; i < (n / k); ++i) { n     long long minians = 0; n     for (int dig = 0; dig <= 9; dig++) { n       if (dig == b[i]) continue; n       long long top = (long long)(dig + 1) * mypow(10, k - 1) - 1l; n       long long bot = (long long)(dig)*mypow(10, k - 1) - 1l; n       long long topcnt = (long long)(top / a[i]); n       topcnt++; n       if (dig == 0) { n         minians += topcnt; n         continue; n       } n       long long botcnt = (long long)(bot / a[i]); n       botcnt++; n       minians += topcnt - botcnt; n       minians = (minians + 1000000007) % 1000000007; n     } n     ans = (ans * minians) % 1000000007; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int sum, limit, start = 0, end = 0; n   cin >> sum >> limit; n   vector<pair<long long int, long long int>> v; n   vector<long long int> ans; n   for (long long int i = 1; i <= limit; i++) { n     long long int t = i, cn = 0; n     while (t > 0) { n       if (t % 2 == 0) n         t /= 2, cn++; n       else n         break; n     } n     long long int val = pow(2, cn); n     v.push_back(make_pair(val, i)); n   } n   sort(v.begin(), v.end()); n   for (long long int i = v.size() - 1; i >= 0; i--) { n     if (sum < v[i].first) n       continue; n     else n       sum -= v[i].first, ans.push_back(v[i].second); n     if (sum == 0) break; n   } n   if (sum) { n     cout <<  -1 ; n     return 0; n   } n   cout << ans.size() << endl; n   for (long long int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long ret = 0, f = 1; n   char x = getchar(); n   while (!(x >=  0  && x <=  9 )) { n     if (x ==  - ) f = -1; n     x = getchar(); n   } n   while (x >=  0  && x <=  9 ) ret = ret * 10 + x -  0 , x = getchar(); n   return ret * f; n } n char get_char() { n   char x = getchar(); n   while (!((x >=  a  && x <=  z ) || (x >=  A  && x <=  Z ))) x = getchar(); n   return x; n } n const double eps = 1e-10; n int dcmp(double n) { n   if (fabs(n) < eps) n     return 0; n   else if (n < 0) n     return -1; n   else n     return 1; n } n struct point { n   double x, y; n   point() {} n   point(double x, double y) : x(x), y(y) {} n   bool operator<(const point& p) const { n     if (dcmp(x - p.x) != 0) return x < p.x; n     if (dcmp(y - p.y) != 0) return y < p.y; n     return false; n   } n   bool operator==(const point& p) const { n     return dcmp(x - p.x) == 0 && dcmp(y - p.y) == 0; n   } n   void read() { scanf( %lf %lf , &x, &y); } n   void print() const { printf( x:%.3lf y:%.3lf n , x, y); } n   double len() const { return sqrt(x * x + y * y); } n }; n point operator-(const point& a, const point& b) { n   return point(a.x - b.x, a.y - b.y); n } n point operator+(const point& a, const point& b) { n   return point(a.x + b.x, a.y + b.y); n } n point operator*(double num, const point& a) { n   return point(a.x * num, a.y * num); n } n double det(point a, point b) { return a.x * b.y - b.x * a.y; } n double det(point a, point b, point o) { return det(a - o, b - o); } n double dot(point a, point b) { return a.x * b.x + a.y * b.y; } n double ang(point a, point b) { n   double cosx = dot(a, b) / (a.len() * b.len()); n   return acos(cosx); n } n point rotate(point p, double d) { n   double tx = cos(d) * p.x - sin(d) * p.y; n   double ty = sin(d) * p.x + cos(d) * p.y; n   return point(tx, ty); n } n bool on_line(point p, point a, point b) { n   if (b < a) swap(a, b); n   if (dcmp(p.x - a.x) < 0 || dcmp(b.x - p.x) > 0) return false; n   int m = dcmp(det(b - a, p - a)); n   return m == 0; n } n bool is_inter(point a1, point a2, point b1, point b2) { n   int m1 = dcmp(det(a2, b1, a1)); n   int m2 = dcmp(det(a2, b2, a1)); n   int m3 = dcmp(det(b2, a1, b1)); n   int m4 = dcmp(det(b2, a2, b1)); n   return m1 != m2 && m3 != m4; n } n point inter_point(point a1, point a2, point b1, point b2) { n   double t = det(a1, b2, b1) / det(b2 - b1, a2 - a1); n   return (a1 + t * (a2 - a1)); n } n const int L_N = 30 + 10; n const double INF = 1e9; n point S, E; n vector<point> PS; n set<point> INT_SET; n int N; n double dis[L_N][L_N]; n bool get_inter() { n   for (int i = 0; i < PS.size(); i++) { n     int nxt = (i + 1) % PS.size(); n     if (is_inter(S, E, PS[i], PS[nxt])) { n       point t = inter_point(S, E, PS[i], PS[nxt]); n       if (INT_SET.count(t)) continue; n       INT_SET.insert(t); n       PS.insert(PS.begin() + i + 1, t); n       return true; n     } n   } n   return false; n } n double get_ans_int() { n   point p1, p2; n   int sid = 0, eid = 0; n   set<point>::iterator it = INT_SET.begin(); n   p1 = *it; n   it++; n   p2 = *it; n   for (int i = (0); i <= ((int)PS.size() - 1); i++) { n     if (PS[i] == p1) n       sid = i; n     else if (PS[i] == p2) n       eid = i; n   } n   double ans = 2 * (p1 - p2).len(); n   ans = min(ans, dis[sid][eid]); n   ans += min((S - p1).len(), (S - p2).len()); n   ans += min((E - p1).len(), (E - p2).len()); n   return ans; n } n int main() { n   for (int i = (0); i <= (L_N - 1); i++) n     for (int j = (0); j <= (L_N - 1); j++) n       if (i != j) dis[i][j] = INF; n   S.read(), E.read(); n   N = read(); n   PS.resize(N); n   for (int i = (0); i <= (N - 1); i++) PS[i].read(); n   while (get_inter()) n     ; n   for (int i = (0); i <= ((int)PS.size() - 1); i++) { n     int nxt = (i + 1) % PS.size(); n     dis[i][nxt] = dis[nxt][i] = (PS[i] - PS[nxt]).len(); n   } n   for (int k = (0); k <= (N + 2); k++) n     for (int i = (0); i <= (N + 2); i++) n       for (int j = (0); j <= (N + 2); j++) { n         if (dcmp(dis[i][k] + dis[k][j] - dis[i][j]) < 0) n           dis[i][j] = dis[i][k] + dis[k][j]; n       } n   double ans = 0; n   if (INT_SET.size() < 2) n     ans = (S - E).len(); n   else n     ans = get_ans_int(); n   printf( %.10lf n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     char arr[n][n]; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         cin >> arr[i][j]; n       } n     } n     vector<pair<int, int>> vec; n     if (arr[0][1] == arr[1][0]) { n       if (arr[0][1] ==  1 ) { n         if (arr[n - 2][n - 1] !=  0 ) { n           vec.push_back({n - 1, n}); n         } n         if (arr[n - 1][n - 2] !=  0 ) { n           vec.push_back({n, n - 1}); n         } n       } else { n         if (arr[0][1] ==  0 ) { n           if (arr[n - 2][n - 1] !=  1 ) { n             vec.push_back({n - 1, n}); n           } n           if (arr[n - 1][n - 2] !=  1 ) { n             vec.push_back({n, n - 1}); n           } n         } n       } n     } else if (arr[n - 1][n - 2] == arr[n - 2][n - 1]) { n       if (arr[n - 1][n - 2] ==  1 ) { n         if (arr[0][1] !=  0 ) { n           vec.push_back({1, 2}); n         } n         if (arr[1][0] !=  0 ) { n           vec.push_back({2, 1}); n         } n       } else { n         if (arr[n - 1][n - 2] ==  0 ) { n           if (arr[1][0] !=  1 ) { n             vec.push_back({2, 1}); n           } n           if (arr[0][1] !=  1 ) { n             vec.push_back({1, 2}); n           } n         } n       } n     } else { n       if (arr[n - 1][n - 2] !=  1 ) { n         vec.push_back({n, n - 1}); n       } n       if (arr[n - 2][n - 1] !=  1 ) { n         vec.push_back({n - 1, n}); n       } n       if (arr[0][1] !=  0 ) { n         vec.push_back({1, 2}); n       } n       if (arr[1][0] !=  0 ) { n         vec.push_back({2, 1}); n       } n     } n     cout << vec.size() << endl; n     for (auto x : vec) { n       cout << x.first <<     << x.second << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n long long x[200001], y[200001]; n set<long long> sx, sy; n long long indx[200001], indy[200001]; n long long find_indx(long long a) { n   long long l = 0, r = n - 1; n   long long ind = -1; n   while (l <= r) { n     long long mid = (l + r) / 2; n     if (x[mid] >= a) { n       ind = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   assert(ind != -1); n   return ind; n } n long long find_indy(long long b) { n   long long l = 0, r = m - 1; n   long long ind = -1; n   while (l <= r) { n     long long mid = (l + r) / 2; n     if (y[mid] >= b) { n       ind = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   assert(ind != -1); n   return ind; n } n void solv() { n   long long res = 0; n   cin >> n >> m >> k; n   sx.clear(); n   sy.clear(); n   for (long long i = 0; i < n; i++) { n     indx[i] = 0; n     cin >> x[i]; n     sx.insert(x[i]); n   } n   for (long long i = 0; i < m; i++) { n     indy[i] = 0; n     cin >> y[i]; n     sy.insert(y[i]); n   } n   vector<pair<long long, long long>> onx, ony; n   for (long long i = 0; i < k; i++) { n     long long a, b; n     cin >> a >> b; n     if (sx.count(a) && sy.count(b)) continue; n     if (sx.count(a)) n       onx.push_back({a, b}); n     else n       ony.push_back({b, a}); n   } n   pair<long long, long long> las = {-1, -1}; n   long long cnt = 0; n   sort(onx.begin(), onx.end()); n   sort(ony.begin(), ony.end()); n   for (long long i = 0; i < onx.size(); i++) { n     long long xx = find_indx(onx[i].first), yy = find_indy(onx[i].second); n     if (make_pair(xx, yy) != las) n       cnt = 0, las = {xx, yy}; n     else n       cnt++; n     res += indy[yy] - cnt; n     indy[yy]++; n   } n   las = {-1, -1}, cnt = 0; n   for (long long i = 0; i < ony.size(); i++) { n     long long xx = find_indx(ony[i].second), yy = find_indy(ony[i].first); n     if (make_pair(xx, yy) != las) n       cnt = 0, las = {xx, yy}; n     else n       cnt++; n     res += indx[xx] - cnt; n     indx[xx]++; n   } n   cout << res <<   n ; n } n signed main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   long long t; n   cin >> t; n   while (t--) solv(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long MAX_N = 1e6 + 5; n using pii = pair<long long, long long>; n struct edge { n   long long to, flow, cap, cost, rev; n }; n struct MinCostMaxFlow { n   long long nodes; n   vector<long long> prio, curflow, prevedge, prevnode, q, pot; n   vector<bool> inqueue; n   vector<vector<edge>> graph; n   MinCostMaxFlow() {} n   MinCostMaxFlow(long long n) n       : nodes(n), n         prio(n, 0), n         curflow(n, 0), n         prevedge(n, 0), n         prevnode(n, 0), n         q(n, 0), n         pot(n, 0), n         inqueue(n, 0), n         graph(n) {} n   void addEdge(long long source, long long to, long long capacity, n                long long cost) { n     edge a = {to, 0, capacity, cost, (long long)graph[to].size()}; n     edge b = {source, 0, 0, -cost, (long long)graph[source].size()}; n     graph[source].push_back(a); n     graph[to].push_back(b); n   } n   void bellman_ford(long long source, vector<long long> &dist) { n     fill(dist.begin(), dist.end(), INT_MAX); n     dist[source] = 0; n     long long qt = 0; n     q[qt++] = source; n     for (long long qh = 0; (qh - qt) % nodes != 0; qh++) { n       long long u = q[qh % nodes]; n       inqueue[u] = false; n       for (auto &e : graph[u]) { n         if (e.flow >= e.cap) continue; n         long long v = e.to; n         long long newDist = dist[u] + e.cost; n         if (dist[v] > newDist) { n           dist[v] = newDist; n           if (!inqueue[v]) { n             inqueue[v] = true; n             q[qt++ % nodes] = v; n           } n         } n       } n     } n   } n   pair<long long, long long> minCostFlow(long long source, long long dest, n                                          long long maxflow) { n     bellman_ford(source, pot); n     long long flow = 0; n     long long flow_cost = 0; n     while (flow < maxflow) { n       priority_queue<pair<long long, long long>, n                      vector<pair<long long, long long>>, n                      greater<pair<long long, long long>>> n           q; n       q.push({0, source}); n       fill(prio.begin(), prio.end(), INT_MAX); n       prio[source] = 0; n       curflow[source] = INT_MAX; n       while (!q.empty()) { n         long long d = q.top().first; n         long long u = q.top().second; n         q.pop(); n         if (d != prio[u]) continue; n         for (long long i = 0; i < graph[u].size(); i++) { n           edge &e = graph[u][i]; n           long long v = e.to; n           if (e.flow >= e.cap) continue; n           long long newPrio = prio[u] + e.cost + pot[u] - pot[v]; n           if (prio[v] > newPrio) { n             prio[v] = newPrio; n             q.push({newPrio, v}); n             prevnode[v] = u; n             prevedge[v] = i; n             curflow[v] = min(curflow[u], e.cap - e.flow); n           } n         } n       } n       if (prio[dest] == INT_MAX) break; n       for (long long i = 0; i < nodes; i++) pot[i] += prio[i]; n       long long df = min(curflow[dest], maxflow - flow); n       flow += df; n       for (long long v = dest; v != source; v = prevnode[v]) { n         edge &e = graph[prevnode[v]][prevedge[v]]; n         e.flow += df; n         graph[v][e.rev].flow -= df; n         flow_cost += df * e.cost; n       } n     } n     return {flow, flow_cost}; n   } n }; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   MinCostMaxFlow network = MinCostMaxFlow(2 * n + 3); n   long long s = 0; n   long long t = 2 * n + 2; n   for (long long i = 0; i < n; i++) { n     network.addEdge(s, i + 1, 1, 0); n     network.addEdge(i + 1 + n, t, 1, 0); n     long long a, b, k; n     cin >> a >> b >> k; n     for (long long j = 0; j <= n; j++) { n       long long cc = max(0LL, a - min(k, j) * b); n       network.addEdge(i + 1, j + 1 + n, 1, -cc); n     } n   } n   network.addEdge(t - 1, t, 1, 0); n   cout << -network.minCostFlow(s, t, n + 1).second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int L = 2e5 + 7; n long long out[L], a[L], counter[1000001], szs = 900; n struct node { n   long long l, r, q; n }; n bool comp(node x, node y) { n   long long xx = x.l / szs, yy = y.l / szs; n   if (xx == yy) return x.r < y.r; n   return x.l < y.l; n } n long long ans = 0; n void add(int idx) { n   counter[a[idx]]++; n   ans += ((counter[a[idx]]) * (counter[a[idx]]) * a[idx] - n           (counter[a[idx]] - 1) * (counter[a[idx]] - 1) * a[idx]); n } n void remove(int idx) { n   counter[a[idx]]--; n   ans += ((counter[a[idx]]) * (counter[a[idx]]) * a[idx] - n           (counter[a[idx]] + 1) * (counter[a[idx]] + 1) * a[idx]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int curl, curr, n, m, cur = 0, l, r, b, k; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   std::vector<node> v(m); n   node temp; n   for (int i = 0; i < m; ++i) { n     cin >> l >> r; n     l--, r--; n     v[i].l = l, v[i].r = r, v[i].q = i; n   } n   sort(v.begin(), v.end(), comp); n   l = 0, r = -1; n   for (int i = 0; i < m; ++i) { n     curl = v[i].l, curr = v[i].r; n     while (r < curr) { n       ++r; n       add(r); n     } n     while (r > curr) { n       remove(r); n       --r; n     } n     while (l < curl) { n       remove(l); n       l++; n     } n     while (l > curl) { n       --l; n       add(l); n     } n     out[v[i].q] = ans; n   } n   for (int i = 0; i < m; ++i) cout << out[i] <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 7; n const double pi = acos(-1); n long long dp[1000006]; n int main() { n   long long n; n   cin >> n; n   long long a[n + 1]; n   memset(dp, 0, sizeof(dp)); n   dp[0] = 1; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     vector<long long> v; n     for (long long j = 1; j * j <= a[i]; j++) { n       if (a[i] % j == 0) { n         if (a[i] / j == j) { n           v.push_back(j); n         } else { n           v.push_back(j); n           v.push_back(a[i] / j); n         } n       } n     } n     sort(v.begin(), v.end(), greater<long long>()); n     for (long long j = 0; j < v.size(); j++) { n       dp[v[j]] += dp[v[j] - 1]; n       dp[v[j]] %= M; n     } n   } n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     ans += dp[i]; n     ans %= M; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n const int N = 50; n const int M = 10000000; n const int MOD = 1e9 + 7; n string st[N]; n int fa[N], lst[N], tot, cnt[N], ss[N]; n int V[N], col[N], res; n int get_f(int t) { n   if (fa[t] == t) n     return t; n   else n     return get_f(fa[t]); n } n int ID[M], F[M], G[M]; n int tmpA[M], tmpB[M], tmpC[M]; n void FWT(int A[], int n) { n   for (int i = 2; i <= n; i <<= 1) n     for (int j = 0; j < n; j += i) n       for (int k = j; k < (j + (i >> 1)); ++k) n         A[k + (i >> 1)] = (A[k + (i >> 1)] + A[k]) % MOD; n } n void IFWT(int A[], int n) { n   for (int i = 2; i <= n; i <<= 1) n     for (int j = 0; j < n; j += i) n       for (int k = j; k < (j + (i >> 1)); ++k) n         A[k + (i >> 1)] = (A[k + (i >> 1)] - A[k] + MOD) % MOD; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> st[i]; n   for (int i = 0; i < n; ++i) fa[i] = i; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < i; ++j) n       if (st[i][j] ==  A ) fa[get_f(i)] = get_f(j); n   for (int i = 0; i < n; ++i) cnt[get_f(i)]++; n   tot = 0; n   for (int i = 0; i < n; ++i) n     if (fa[i] == i) { n       if (cnt[i] == 1) n         res++; n       else n         lst[i] = tot++; n     } n   for (int i = 0; i < n; ++i) n     if (cnt[i] != 1) n       for (int j = 0; j < i; ++j) n         if (cnt[j] != 1) n           if (st[i][j] ==  X ) { n             int u = lst[get_f(i)], v = lst[get_f(j)]; n             if (u == v) { n               puts( -1 ); n               exit(0); n             } n             if (u > v) swap(u, v); n             V[u] |= 1 << v; n             V[v] |= 1 << u; n           } n   for (int i = 0; i < tot; ++i) ID[1 << i] = i; n   F[0] = 1; n   for (int i = 1; i < (1 << tot); ++i) { n     int x = i & (-i); n     if (F[i ^ x] && !(V[ID[x]] & (i ^ x))) F[i] = 1; n   } n   G[0] = 1; n   FWT(F, 1 << tot); n   FWT(G, 1 << tot); n   for (int i = 0;; ++i) { n     int v = 0; n     for (int j = 0; j < (1 << tot); ++j) n       if (__builtin_popcount(((1 << tot) - 1) ^ j) & 1) n         v = (v + MOD - G[j]) % MOD; n       else n         v = (v + G[j]) % MOD; n     if (v) { n       cout << i - 1 + n; n       return 0; n     } n     for (int j = 0; j < (1 << tot); ++j) G[j] = 1ll * F[j] * G[j] % MOD; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long sol(long long a, long long b, long long h, long long w, int n, n               vector<long long> v) { n   int p = 0; n   long long th = h, tw = w; n   for (; p < n; ++p) { n     if (th < a) n       th *= v[p]; n     else if (tw < b) n       tw *= v[p]; n     else n       break; n   } n   n = p; n   v.resize(p); n   auto solve = [&]() { n     if (h >= a && w >= b) return true; n     if (n < 20) { n       for (int i = 0; i < 1 << n; ++i) { n         th = h, tw = w; n         for (int j = 0; j < n; ++j) { n           if (i >> j & 1) { n             if (th < a) th *= v[j]; n           } else { n             if (tw < b) tw *= v[j]; n           } n         } n         if (th >= a && tw >= b) return true; n       } n       return false; n     } n     int m1 = n / 2, m2 = n - m1; n     vector<pair<long long, long long>> x; n     for (int i = 0; i < 1 << m1; ++i) { n       th = 1, tw = 1; n       for (int j = 0; j < m1; ++j) { n         if (i >> j & 1) { n           if (th * h < a) th *= v[j]; n         } else { n           if (tw * w < b) tw *= v[j]; n         } n       } n       x.push_back({min(th, a), min(tw, b)}); n     } n     sort(x.begin(), x.end()); n     vector<long long> mx(x.size()); n     for (int i = x.size() - 1; i >= 0; --i) { n       mx[i] = x[i].second; n       if (i + 1 < x.size()) mx[i] = max(mx[i], mx[i + 1]); n     } n     for (int i = 0; i < 1 << m2; ++i) { n       th = 1, tw = 1; n       for (int j = 0; j < m2; ++j) { n         if (i >> j & 1) { n           if (th * h < a) th *= v[m1 + j]; n         } else { n           if (tw * w < b) tw *= v[m1 + j]; n         } n       } n       long long val = (a + h * th - 1) / (h * th); n       auto it = lower_bound(x.begin(), x.end(), make_pair(val, 0LL)); n       if (it != x.end()) { n         if (mx[it - x.begin()] * min(tw, b) * w >= b) return true; n       } n     } n     return false; n   }; n   int l = 0, r = p; n   while (l < r) { n     n = (l + r) >> 1; n     if (solve()) n       r = n; n     else n       l = n + 1; n   } n   n = l; n   if (solve()) return n; n   return 1 << 30; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   long long a, b, h, w, n; n   cin >> a >> b >> h >> w >> n; n   vector<long long> v(n); n   for (auto &i : v) cin >> i; n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   int ans = min(sol(a, b, h, w, n, v), sol(b, a, h, w, n, v)); n   if (ans == 1 << 30) ans = -1; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n class event { n  public: n   long long t; n   int id, type; n   event() {} n   event(long long a, int b, int c) { n     t = a; n     id = b; n     type = c; n   } n   bool operator<(const event& other) const { n     if (t != other.t) return other.t < t; n     if (type != other.type) return other.type < type; n     return other.id < id; n   } n }; n int n, p; n long long A[MAXN]; n priority_queue<event> pq; n set<int> hold, qu; n long long last; n int main() { n   scanf( %d %d , &n, &p); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &A[i]); n     pq.push(event(A[i], i, 2)); n   } n   while (!pq.empty()) { n     event top = pq.top(); n     pq.pop(); n     if (top.type == 2) { n       if (qu.empty()) { n         qu.insert(top.id); n         pq.push(event(top.t + p, top.id, 1)); n         last = top.t + p; n       } else if (*qu.begin() > top.id) { n         qu.insert(top.id); n         pq.push(event(last + p, top.id, 1)); n         last = last + p; n       } else { n         hold.insert(top.id); n       } n     } else { n       A[top.id] = top.t; n       qu.erase(top.id); n       set<int>::iterator lb = hold.lower_bound(top.id); n       if (lb != hold.end()) { n         pq.push(event(top.t, *lb, 2)); n         hold.erase(lb); n       } n     } n   } n   for (int i = 1; i <= n; i++) printf( %lld  , A[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const int MAXN = 1e5 + 10; n int n, k; n int ans; n int data[MAXN]; n int has[MAXN]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &data[i]); n     ++has[data[i] % 10]; n     ans += data[i] / 10; n   } n   for (int i = 1; i < 10; ++i) { n     int s = min(has[10 - i], k / i); n     k -= i * s; n     ans += s; n     if (s != has[10 - i]) break; n   } n   ans += min(10 * n - ans, k / 10); n   printf( %d , ans); n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n long long sum1[maxn]; n long long sum2[maxn]; n long long a[maxn]; n long long pos[maxn]; n int n; n long long lowbit(long long x) { return x & -x; } n void add(long long *sum, long long x, long long v) { n   while (x <= n) { n     sum[x] += v; n     x += lowbit(x); n   } n } n long long query(long long *sum, long long x) { n   long long res = 0; n   while (x > 0) { n     res += sum[x]; n     x -= lowbit(x); n   } n   return res; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     pos[a[i]] = i; n   } n   long long ans1 = 0; n   for (int i = 1; i <= n; i++) { n     ans1 += i - 1 - query(sum1, pos[i]); n     add(sum1, pos[i], 1); n     add(sum2, pos[i], pos[i]); n     int mid, l = 1, r = n; n     while (l <= r) { n       mid = (l + r) >> 1; n       if (query(sum1, mid) * 2 <= i) n         l = mid + 1; n       else n         r = mid - 1; n     } n     long long ans2 = 0; n     long long cnt = query(sum1, mid); n     long long sum = query(sum2, mid); n     ans2 += mid * cnt - sum - cnt * (cnt - 1) / 2; n     cnt = i - cnt; n     sum = query(sum2, n) - sum; n     ans2 += sum - cnt * (mid + 1) - cnt * (cnt - 1) / 2; n     cout << ans1 + ans2 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   int h[100000], max[100001]; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> h[i]; n   } n   max[n] = h[n - 1] - 1; n   for (int i = n - 1; i >= 0; --i) { n     if (h[i] > max[i + 1]) n       max[i] = h[i]; n     else n       max[i] = max[i + 1]; n   } n   for (int i = 0; i < n; ++i) { n     if (max[i + 1] >= h[i]) n       cout << max[i + 1] - h[i] + 1 <<    ; n     else n       cout << 0 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n int const N = 1100; n int n, m, ans, ans1; n int map[N][N]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) map[i][j] = -1; n   for (int i = 1; i <= n; ++i) { n     int x; n     scanf( %d , &x); n     for (int j = 1; j <= x; ++j) n       if (map[i][j] != 0) n         map[i][j] = 1; n       else { n         printf( %d n , 0); n         return 0; n       } n     if (map[i][x + 1] == 1) { n       printf( %d n , 0); n       return 0; n     } n     map[i][x + 1] = 0; n   } n   for (int i = 1; i <= m; ++i) { n     int x; n     scanf( %d , &x); n     for (int j = 1; j <= x; ++j) n       if (map[j][i] != 0) n         map[j][i] = 1; n       else { n         printf( %d n , 0); n         return 0; n       } n     if (map[x + 1][i] == 1) { n       printf( %d n , 0); n       return 0; n     } n     map[x + 1][i] = 0; n   } n   ans = 0; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       if (map[i][j] == -1) ans++; n   ans1 = 1; n   for (int i = 1; i <= ans; ++i) ans1 = (ans1 * 2) % 1000000007; n   printf( %d n , ans1); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:60000000 ) n using namespace std; n const long double eps = 1e-9; n const int inf = (1 << 30) - 1; n const long long inf64 = ((long long)1 << 62) - 1; n const long double pi = 3.1415926535897932384626433832795; n template <class T> n T sqr(T x) { n   return x * x; n } n int n, m; n long long w[300300]; n long long t[300300]; n vector<pair<pair<int, int>, int> > Q; n long long ans[300300]; n int main() { n   memset(w, 0, sizeof(w)); n   scanf( %d , &n); n   for (int i = 0; i < (int)(n); i++) { n     int x; n     scanf( %d , &x); n     w[i] = x; n   } n   scanf( %d , &m); n   Q.resize(m); n   for (int i = 0; i < (int)(m); i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     Q[i] = make_pair(make_pair(b, a), i); n   } n   sort(Q.begin(), Q.end()); n   int cur = 0; n   for (int i = 0; i < (int)(Q.size()); i++) { n     int a = Q[i].first.second; n     int b = Q[i].first.first; n     int num = Q[i].second; n     if (b > 1000) { n       ans[num] = 0; n       while (a < n) { n         ans[num] += w[a]; n         a += b; n       } n     } else { n       if (b > cur) { n         cur = b; n         memset(t, 0, sizeof(t)); n         for (int i = n - 1; i >= 0; i--) { n           t[i] = w[i]; n           if (i + cur < n) t[i] += t[i + cur]; n         } n       } n       if (a < n) n         ans[num] = t[a]; n       else n         ans[num] = 0; n     } n   } n   for (int i = 0; i < (int)(m); i++) printf( %I64d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Size = 1e5; n long long n, m; n bool onField(long long x, long long y) { n   return (x <= n && x >= 1) && (y <= m && y >= 1); n } n int main() { n   long long x, y, dx, dy, k; n   long long ans = 0; n   cin >> n >> m; n   cin >> x >> y; n   cin >> k; n   for (int i = 0; i < k; i++) { n     cin >> dx >> dy; n     for (long long cof = 1100000000; cof; cof /= 2) n       while (onField(x + (cof * dx), y + (cof * dy))) { n         x += (cof * dx); n         y += (cof * dy); n         ans += cof; n       } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n, k, ans = INT_MIN; n     cin >> n >> k; n     long long A[n]; n     for (int i = 0; i < n; i++) cin >> A[i]; n     for (long long j = n - 1; ~j; j--) { n       for (long long i = j - 1; ~i; i--) { n         if (ans >= (i + 1) * (j + 1)) break; n         ans = max(ans, (i + 1) * (j + 1) - (k * (A[i] | A[j]))); n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n = 0; n   cin >> n; n   string s; n   cin >> s; n   int pos = 0; n   vector<int> v(n); n   int flag = 1; n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   for (int i = 0; i < n; i++) { n     if (s[pos] ==  > ) { n       pos += v[pos]; n     } else { n       pos -= v[pos]; n     } n     if (pos < 0 || pos > n - 1) { n       cout <<  FINITE ; n       flag = 0; n       break; n     } n   } n   if (flag == 1) { n     cout <<  INFINITE ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[300001]; n int arr[300001]; n int dp[300001]; n int maxi = -(int)1e10, max_pos; n bool flag = false; n void dfs(int beta, int baap) { n   if (flag) return; n   if (arr[beta] == maxi) n     dp[beta] = 0; n   else n     dp[beta] = -(int)1e10; n   for (int i = 0; i < g[beta].size(); i++) { n     int pota = g[beta][i]; n     if (pota == baap) continue; n     dfs(pota, beta); n     if (dp[pota] != -(int)1e10) { n       if (dp[beta] == -(int)1e10) n         dp[beta] = 1 + dp[pota]; n       else { n         int dist = 1 + dp[beta] + dp[pota]; n         if (dist > 2) flag = true; n         dp[beta] = max(dp[beta], 1 + dp[pota]); n       } n     } n   } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n     if (arr[i] > maxi) { n       maxi = arr[i]; n       max_pos = i; n     } n   } n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n   } n   int max_count = 0, max_cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (arr[i] == maxi) max_count++; n     if (arr[i] == maxi - 1) max_cnt++; n   } n   if (max_count == 1) { n     for (int i = 0; i < g[max_pos].size(); i++) { n       int j = g[max_pos][i]; n       if (arr[j] == maxi - 1) max_cnt--; n     } n     if (max_cnt) n       printf( %d n , maxi + 1); n     else n       printf( %d n , maxi); n   } else { n     dfs(1, 0); n     if (flag) n       printf( %d n , maxi + 2); n     else n       printf( %d n , maxi + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int INF = INT_MAX; n using namespace std; n const int N = 3e5 + 10; n int arr[N]; n int main() { n   ios_base::sync_with_stdio(false); n   stringstream ss; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   map<int, bool> ma; n   vector<pair<int, int> > ans; n   int i = 0, j; n   int start, end; n   bool flag = false; n   while (i < n) { n     j = i; n     start = i; n     ma.clear(); n     while (j < n) { n       if (ma.find(arr[j]) == ma.end()) { n         ma[arr[j]] = true; n       } else { n         break; n       } n       j++; n     } n     if (j < n) { n       end = j; n       ans.push_back(make_pair(start, end)); n     } else { n       if (ans.size() > 0) { n         ans[ans.size() - 1].second = n - 1; n       } else { n         flag = true; n       } n       break; n     } n     i = j + 1; n   } n   if (flag) n     ss <<  -1 n ; n   else { n     ss << ans.size() <<   n ; n     for (int k = 0, end = ans.size(); k < end; k++) { n       ss << (ans[k].first + 1) <<     << (ans[k].second + 1) <<   n ; n     } n   } n   cout << ss.str() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n int t; n void solve() { n   int n, res = 0, j = 0; n   string s; n   cin >> n >> s; n   map<int, int> m; n   for (int i = 0; i <= n; i++) { n     if (s[i] ==  x ) { n       m[j]++; n     } else { n       j++; n     } n   } n   for (auto i : m) { n     res += max(0, i.second - 2); n   } n   cout << res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   if (0) { n     cin >> t; n     while (t--) solve(); n   } else { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n long long arr[100010], dpArr[100010], dpQuery[100010]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, m, k; n   cin >> n >> m >> k; n   for (long long i = (1); i < (n + 1); ++i) cin >> arr[i]; n   pair<pair<long long, long long>, long long> q[m + 1]; n   for (long long i = (1); i < (m + 1); ++i) n     cin >> q[i].first.first >> q[i].first.second >> q[i].second; n   for (long long i = 0; i < (k); ++i) { n     long long x, y; n     cin >> x >> y; n     ++dpQuery[x]; n     --dpQuery[y + 1]; n   } n   long long sum = 0LL; n   for (long long i = (1); i < (m + 1); ++i) { n     sum += dpQuery[i]; n     dpArr[q[i].first.first] += (sum * q[i].second); n     dpArr[q[i].first.second + 1] -= (sum * q[i].second); n   } n   sum = 0LL; n   for (long long i = (1); i < (n + 1); ++i) { n     sum += dpArr[i]; n     cout << (sum + arr[i]) <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, x, y, z, flag = 0; n vector<long long> sum(200005), mx(200005, INT_MIN); n vector<int> adj[200005 + 1]; n vector<int> pl(200005 + 1); n map<int, int> vis; n void dfs(int a) { n   vis[a] = 1; n   for (int i = 0; i < adj[a].size(); i++) { n     if (vis[adj[a][i]] == 0) { n       dfs(adj[a][i]); n       sum[a] += sum[adj[a][i]]; n       mx[a] = max(mx[a], mx[adj[a][i]]); n     } n   } n   sum[a] += pl[a]; n   mx[a] = max(mx[a], sum[a]); n   return; n } n map<int, int> mp; n long long ans = INT_MIN; n void dfs2(int x) { n   mp[x] = 1; n   long long k = 0; n   priority_queue<long long> q; n   for (int i = 0; i < adj[x].size(); i++) { n     if (mp[adj[x][i]] == 0) q.push(mx[adj[x][i]]); n   } n   long long s = q.size(); n   int j = 2; n   while (j > 0 && q.size()) { n     k += q.top(); n     q.pop(); n     j--; n   } n   if (s > 1) ans = max(ans, k); n   for (int i = 0; i < adj[x].size(); i++) { n     if (mp[adj[x][i]] == 0) dfs2(adj[x][i]); n   } n } n int main() { n   int t; n   t = 1; n   for (int r = 0; r < t; r++) { n     cin >> n; n     long long i; n     for (i = 1; i <= n; i++) cin >> pl[i]; n     for (i = 0; i < n - 1; i++) { n       cin >> x >> y; n       adj[x].push_back(y); n       adj[y].push_back(x); n     } n     dfs(1); n     dfs2(1); n     if (ans == INT_MIN) n       cout <<  Impossible n ; n     else n       cout << ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int n, m, i, j, k, l, a, b, sz[2], v1[2][5005]; n string s; n bool used[5005]; n int main() { n   ios_base ::sync_with_stdio(); n   cin.tie(0); n   cin >> s; n   n = s.size(); n   if (n == 1) { n     cout << s; n     return 0; n   } n   int pw = 1; n   while (pw * 2 <= n) pw *= 2, k++; n   a = 0; n   b = 1; n   v1[a][0] = 0; n   sz[a] = 1; n   for (l = 0; l < n - (1 << k) + 1; l++) { n     char res =  z ; n     int v, mask; n     for (i = 0; i < sz[a]; i++) { n       int v = v1[a][i], mask = ((1 << k) - 1) ^ (v - l); n       used[v] = 0; n       for (j = mask; j > 0; j = mask & (j - 1)) res = min(res, s[v + j]); n       res = min(res, s[v]); n     } n     for (i = 0; i < sz[a]; i++) { n       int v = v1[a][i], mask = ((1 << k) - 1) ^ (v - l); n       for (j = mask; j >= 0; j = mask & (j - 1)) { n         if (!used[v + j + 1] && s[v + j] == res) n           v1[b][sz[b]++] = v + j + 1, used[v + j + 1] = true; n         if (j == 0) break; n       } n     } n     cout << res; n     sz[a] = 0; n     b = a; n     a ^= 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAXQ = 5e5 + 5; n using namespace std; n struct edge { n   int v, next; n } ed[MAXQ * 4]; n int head[MAXQ * 2]; n int topE; n void addE(int u, int v) { n   ed[++topE].v = v; n   ed[topE].next = head[u]; n   head[u] = topE; n   return; n } n int dep[MAXQ * 2]; n int fa[MAXQ * 2][25]; n int Q; n int ans; n int topN; n int lca(int x, int y) { n   int res = 0; n   if (dep[x] < dep[y]) { n     swap(x, y); n   } n   res += dep[x] - dep[y]; n   while (dep[x] != dep[y]) { n     x = fa[x][int(log2(dep[x] - dep[y]))]; n   } n   for (int i = int(log2(dep[x])); i >= 0; --i) { n     if (fa[x][i] != fa[y][i]) { n       res += dep[x] - dep[fa[x][i]]; n       res += dep[y] - dep[fa[y][i]]; n       x = fa[x][i]; n       y = fa[y][i]; n     } n   } n   if (x == y) { n     return res; n   } n   x = fa[x][0]; n   y = fa[y][0]; n   res += 2; n   return res; n } n void insertNode(int u, int v) { n   fa[u][0] = v; n   dep[u] = dep[v] + 1; n   for (int i = 1; i <= int(log2(dep[u])); ++i) { n     fa[u][i] = fa[fa[u][i - 1]][i - 1]; n   } n   return; n } n int da, db; n int main() { n   scanf( %d , &Q); n   topN = 4; n   dep[2] = dep[3] = dep[4] = 1; n   fa[2][0] = fa[3][0] = fa[4][0] = 1; n   da = 2; n   db = 3; n   ans = 2; n   for (int i = 1; i <= Q; ++i) { n     int v; n     scanf( %d , &v); n     insertNode(++topN, v); n     insertNode(++topN, v); n     if (lca(da, topN) > ans) { n       ans = lca(da, topN); n       db = topN; n     } else if (lca(db, topN) > ans) { n       ans = lca(db, topN); n       da = topN; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char m[1000000]; n int main() { n   int t, i, j; n   string s; n   cin >> t; n   cin >> s; n   int l = s.length(); n   for (i = 0; i < l; i++) m[i] = s[i]; n   for (i = 1; i < t; i++) { n     cin >> s; n     for (j = 0; j < l; j++) { n       if (m[j] ==  ? ) m[j] = s[j]; n       if (m[j] != s[j] && s[j] !=  ? ) m[j] =  ! ; n     } n   } n   for (i = 0; i < l; i++) { n     if (m[i] ==  ? ) n       cout <<  x ; n     else if (m[i] ==  ! ) n       cout <<  ? ; n     else n       cout << m[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long lb = 1000; n void azmuth(long long n = 12) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << setprecision(n) << fixed; n } n void solve() { n   long long n; n   string s; n   cin >> n >> s; n   vector<long long> ans(n); n   ans[0] = 1; n   for (long long i = 0; i < n - 1; i++) { n     if (s[i] ==  = ) { n       ans[i + 1] = ans[i]; n       continue; n     } n     if (s[i] ==  L ) { n       ans[i + 1] = 1; n       continue; n     } n     ans[i + 1] = 1 + ans[i]; n   } n   for (long long i = n - 2; i >= 0; i--) { n     if (s[i] ==  L ) { n       ans[i] = max(ans[i], ans[i + 1] + 1); n     } else if (s[i] ==  R ) { n       ans[i] = min(ans[i], ans[i + 1] - 1); n     } else n       ans[i] = ans[i + 1]; n   } n   for (auto a : ans) cout << a <<    ; n   cout <<   n ; n   ; n } n int32_t main() { n   azmuth(); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, class F = function<T(const T&, const T&)>> n class SparseTable { n  public: n   int n; n   vector<vector<T>> sp; n   F func; n   SparseTable(const vector<T>& a, const F& f) : func(f) { n     n = static_cast<int>(a.size()); n     int max_log = 32 - __builtin_clz(n); n     sp.resize(max_log); n     sp[0] = a; n     for (int j = 1; j < max_log; ++j) { n       sp[j].resize(n - (1 << j) + 1); n       for (int i = 0; i <= n - (1 << j); ++i) { n         sp[j][i] = func(sp[j - 1][i], sp[j - 1][i + (1 << (j - 1))]); n       } n     } n   } n   T get(int l, int r) const { n     int lg = 32 - __builtin_clz(r - l + 1) - 1; n     return func(sp[lg][l], sp[lg][r - (1 << lg) + 1]); n   } n }; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   vector<int> a(n + 1), nxt(n + 1), prv(n + 1), cum(n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     cum[i] = cum[i - 1] + a[i]; n   } n   for (int i = n; i >= 1; i--) { n     nxt[i] = i + 1; n     while (nxt[i] <= n && a[nxt[i]] <= a[i]) nxt[i] = nxt[nxt[i]]; n   } n   for (int i = 1; i <= n; i++) { n     prv[i] = i - 1; n     while (prv[i] >= 1 && a[prv[i]] <= a[i]) prv[i] = prv[prv[i]]; n   } n   SparseTable<int> mn_sp(cum, [&](int i, int j) { return min(i, j); }); n   SparseTable<int> mx_sp(cum, [&](int i, int j) { return max(i, j); }); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     int l = prv[i] + 1, r = nxt[i] - 1; n     ans = max(ans, mx_sp.get(i, r) - mn_sp.get(l - 1, i - 1) - a[i]); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int treas[310][310]; n long long dp[310][310]; n long long d[310][310]; n vector<pair<int, int> > G[110000]; n vector<pair<long long, pair<int, int> > > lst; n queue<pair<long long, pair<int, int> > > bfs; n int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; n long long n, m; n long long Dist(int x1, int y1, int x2, int y2) { n   return abs(x1 - x2) + abs(y1 - y2); n } n int main() { n   ios_base::sync_with_stdio(0); n   int p, fx, fy; n   cin >> n >> m >> p; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> treas[i][j]; n       G[treas[i][j]].push_back({i, j}); n       if (treas[i][j] == p) fx = i, fy = j; n       if (treas[i][j] == 1) dp[i][j] = i + j; n     } n   for (int i = 2; i <= p; i++) { n     long long cur_size = G[i].size(); n     long long prev_size = G[i - 1].size(); n     if (cur_size * prev_size <= n * m) { n       for (int j = 0; j < cur_size; j++) { n         int cur_x = G[i][j].first; n         int cur_y = G[i][j].second; n         dp[cur_x][cur_y] = 1e9; n         for (int k = 0; k < prev_size; k++) { n           int prev_x = G[i - 1][k].first; n           int prev_y = G[i - 1][k].second; n           dp[cur_x][cur_y] = n               min(dp[cur_x][cur_y], n                   dp[prev_x][prev_y] + Dist(cur_x, cur_y, prev_x, prev_y)); n         } n       } n     } else { n       lst.clear(); n       for (int j = 0; j < n; j++) n         for (int k = 0; k < n; k++) d[j][k] = -1; n       for (int j = 0; j < prev_size; j++) { n         int prev_x = G[i - 1][j].first; n         int prev_y = G[i - 1][j].second; n         lst.push_back({dp[prev_x][prev_y], {prev_x, prev_y}}); n       } n       sort(lst.begin(), lst.end()); n       int ptr = 1; n       bfs.push(lst[0]); n       d[lst[0].second.first][lst[0].second.second] = lst[0].first; n       while (!bfs.empty()) { n         int x = bfs.front().second.first; n         int y = bfs.front().second.second; n         long long val = bfs.front().first; n         bfs.pop(); n         while (ptr < lst.size() && lst[ptr].first <= val) bfs.push(lst[ptr++]); n         int X, Y; n         for (int k = 0; k < 4; k++) { n           X = x + dir[k][0]; n           Y = y + dir[k][1]; n           if (X >= 0 && X <= n && Y >= 0 && Y < m && d[X][Y] == -1) { n             d[X][Y] = val + 1; n             bfs.push({val + 1, {X, Y}}); n           } n         } n       } n       for (int j = 0; j < cur_size; j++) { n         int x = G[i][j].first; n         int y = G[i][j].second; n         dp[x][y] = d[x][y]; n       } n     } n   } n   cout << dp[fx][fy] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x, y, i, j, sum = 0, k; n   cin >> x >> y; n   for (i = 1; i <= x; i++) { n     j = i % 5; n     k = y + j; n     sum = sum + (k / 5); n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)2e9; n const long long md = 1000000007; n int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<pair<int, int> > v(n); n   for (int i = 0; i < (int)n; i++) { n     cin >> v[i].first >> v[i].second; n   } n   sort((v).begin(), (v).end(), [&](pair<int, int>& a, pair<int, int>& b) { n     return abs(a.first) + abs(a.second) < abs(b.first) + abs(b.second); n   }); n   vector<string> out; n   for (pair<int, int>& p : v) { n     int first = p.first, second = p.second; n     if (first) { n       out.emplace_back( 1   + to_string(abs(first)) +     + n                        (first > 0 ?  R  :  L )); n     } n     if (second) { n       out.emplace_back( 1   + to_string(abs(second)) +     + n                        (second > 0 ?  U  :  D )); n     } n     out.emplace_back(to_string(2)); n     if (second) { n       out.emplace_back( 1   + to_string(abs(second)) +     + n                        (second < 0 ?  U  :  D )); n     } n     if (first) { n       out.emplace_back( 1   + to_string(abs(first)) +     + n                        (first < 0 ?  R  :  L )); n     } n     out.emplace_back(to_string(3)); n   } n   cout << (int)(out.size()) <<   n ; n   for (string& s : out) cout << s <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void yes() { n   cout <<  YES  n        <<   n ; n   return; n } n void no() { n   cout <<  NO  n        <<   n ; n   return; n } n template <typename T> n void p(T a) { n   cout << a <<    ; n   return; n } n template <typename T> n void pnl(T a) { n   cout << a <<   n ; n   return; n } n const int ten4 = 1e4; n const int ten5 = 1e5; n const int ten6 = 1e6; n const int ten7 = 1e7; n const int ten9 = 1e9; n const int mod = 1e9 + 7; n struct pair_hash { n   inline std::size_t operator()(const std::pair<int, int>& v) const { n     return v.first * 31 + v.second; n   } n }; n long long bin_to_integer(string strin) { n   long long r = 1, sum = 0; n   for (long long i = strin.length() - 1; i >= 0; i--) { n     if (strin[i] ==  1 ) sum += r; n     r *= 2; n   } n   return sum; n } n string to_binary(int n) { n   string strng; n   while (n > 0) { n     if (n & 1) n       strng +=  1 ; n     else n       strng +=  0 ; n     n >>= 1; n   } n   reverse(strng.begin(), strng.end()); n   return strng; n } n int compare(const void* a, const void* b) { n   const long long* x = (long long*)a; n   const long long* y = (long long*)b; n   if (*x > *y) n     return 1; n   else if (*x < *y) n     return -1; n   return 0; n } n int power(int a, int n) { n   int res = 1; n   while (n) { n     if (n & 1) res = (res * 1LL * a) % mod; n     n /= 2; n     a = (a * 1LL * a) % mod; n   } n   return res; n } n const int Nc = 200001; n int F[Nc]; n bool Fcomputed = false; n int nCr(int n, int r) { n   if (!Fcomputed) { n     F[0] = 1, Fcomputed = true; n     for (int i = 1; i < Nc; i++) F[i] = (F[i - 1] * 1LL * i) % mod; n   } n   if (n < r) return 0; n   int res = F[n]; n   res = res * 1LL * power(F[r], mod - 2) % mod; n   res = res * 1LL * power(F[n - r], mod - 2) % mod; n   return res; n } n struct unionfind { n   vector<int> p; n   unionfind(int N) { p = vector<int>(N, -1); } n   int root(int x) { n     if (p[x] < 0) { n       return x; n     } else { n       p[x] = root(p[x]); n       return p[x]; n     } n   } n   bool same(int x, int y) { return root(x) == root(y); } n   void unite(int x, int y) { n     x = root(x); n     y = root(y); n     if (x != y) { n       if (p[x] < p[y]) { n         swap(x, y); n       } n       p[y] += p[x]; n       p[x] = y; n     } n   } n }; n void testcase(int test) { n   int n; n   cin >> n; n   int arr[n], cnt = 0; n   for (int i = 0; i < (n); i++) cin >> arr[i]; n   if (n % 2 == 0) return yes(); n   for (int i = 0; i < (n - 1); i++) { n     if (arr[i] >= arr[i + 1]) return yes(); n   } n   no(); n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   ; n   int test = 1, z = 1; n   cin >> test; n   while (z <= test) { n     testcase(z); n     z++; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( avx2,avx ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n string YES[2] = { NO ,  YES }; n string Yes[2] = { No ,  Yes }; n string yes[2] = { no ,  yes }; n template <class T> n using pq = priority_queue<T>; n template <class T> n using pqg = priority_queue<T, vector<T>, greater<T>>; n int scan() { return getchar(); } n void scan(int &a) { cin >> a; } n void scan(long long &a) { cin >> a; } n void scan(char &a) { cin >> a; } n void scan(double &a) { cin >> a; } n void scan(string &a) { cin >> a; } n template <class T, class S> n void scan(pair<T, S> &p) { n   scan(p.first), scan(p.second); n } n template <class T> n void scan(vector<T> &); n template <class T> n void scan(vector<T> &a) { n   for (auto &i : a) scan(i); n } n template <class T> n void scan(T &a) { n   cin >> a; n } n void IN() {} n template <class Head, class... Tail> n void IN(Head &head, Tail &...tail) { n   scan(head); n   IN(tail...); n } n template <class T, class S> n inline bool chmax(T &a, S b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T, class S> n inline bool chmin(T &a, S b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n vector<int> iota(int n) { n   vector<int> a(n); n   iota(begin(a), end(a), 0); n   return a; n } n template <typename T> n vector<int> iota(vector<T> &a, bool greater = false) { n   vector<int> res(a.size()); n   iota(begin(res), end(res), 0); n   sort(begin(res), end(res), [&](int i, int j) { n     if (greater) return a[i] > a[j]; n     return a[i] < a[j]; n   }); n   return res; n } n vector<pair<long long, long long>> factor(long long x) { n   vector<pair<long long, long long>> ans; n   for (long long i = 2; i * i <= x; i++) n     if (x % i == 0) { n       ans.push_back({i, 1}); n       while ((x /= i) % i == 0) ans.back().second++; n     } n   if (x != 1) ans.push_back({x, 1}); n   return ans; n } n template <class T> n vector<T> divisor(T x) { n   vector<T> ans; n   for (T i = 1; i * i <= x; i++) n     if (x % i == 0) { n       ans.push_back(i); n       if (i * i != x) ans.push_back(x / i); n     } n   return ans; n } n template <typename T> n void zip(vector<T> &x) { n   vector<T> y = x; n   sort(begin(y), end(y)); n   for (int i = 0; i < x.size(); ++i) { n     x[i] = distance((y).begin(), lower_bound(begin(y), end(y), (x[i]))); n   } n } n int popcount(long long x) { return __builtin_popcountll(x); } n struct Setup_io { n   Setup_io() { n     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n     cout << fixed << setprecision(15); n   } n } setup_io; n int in() { n   int x; n   cin >> x; n   return x; n } n long long lin() { n   unsigned long long x; n   cin >> x; n   return x; n } n template <typename T> n struct edge { n   int from, to; n   T cost; n   int id; n   edge(int to, T cost) : from(-1), to(to), cost(cost) {} n   edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} n   edge(int from, int to, T cost, int id) n       : from(from), to(to), cost(cost), id(id) {} n   edge &operator=(const int &x) { n     to = x; n     return *this; n   } n   operator int() const { return to; } n }; n template <typename T> n using Edges = vector<edge<T>>; n using Tree = vector<vector<int>>; n using Graph = vector<vector<int>>; n template <class T> n using Wgraph = vector<vector<edge<T>>>; n Graph getG(int n, int m = -1, bool directed = false, int margin = 1) { n   Tree res(n); n   if (m == -1) m = n - 1; n   while (m--) { n     int a, b; n     cin >> a >> b; n     a -= margin, b -= margin; n     res[a].emplace_back(b); n     if (!directed) res[b].emplace_back(a); n   } n   return move(res); n } n template <class T> n Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) { n   Wgraph<T> res(n); n   if (m == -1) m = n - 1; n   while (m--) { n     int a, b; n     T c; n     cin >> a >> b >> c; n     a -= margin, b -= margin; n     res[a].emplace_back(b, c); n     if (!directed) res[b].emplace_back(a, c); n   } n   return move(res); n } n template <class T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (auto &e : v) cout << e <<    ; n   cout << endl; n   return os; n } n template <class T, class S> n ostream &operator<<(ostream &os, const pair<T, S> &p) { n   cout <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n template <class S, class T> n string to_string(pair<S, T> p) { n   return  (  + to_string(p.first) +  ,  + to_string(p.second) +  ) ; n } n template <class A> n string to_string(A v) { n   if (v.empty()) return  {} ; n   string ret =  { ; n   for (auto &x : v) ret += to_string(x) +  , ; n   ret.back() =  } ; n   return ret; n } n void dump() { cerr << endl; } n template <class Head, class... Tail> n void dump(Head head, Tail... tail) { n   cerr << to_string(head) <<    ; n   dump(tail...); n } n template <typename T> n static constexpr T inf = numeric_limits<T>::max() / 2; n int main() { n   int n, k; n   IN(n, k); n   int ans = 0; n   while (k--) { n     int m; n     IN(m); n     vector<int> a(m); n     IN(a); n     if (a[0] == 1) { n       int c = 0; n       while (c < m and a[c] == c + 1) c++; n       ans += m - c; n       ans += m - c; n     } else n       ans += m * 2 - 1; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300010; n long long p, q, b; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n bool ok() { n   long long m = gcd(p, q); n   q /= m; n   m = b; n   while (q != 1) { n     m = gcd(q, m); n     q /= m; n     if (m == 1) break; n   } n   if (q == 1) return true; n   return false; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %I64d%I64d%I64d , &p, &q, &b); n     if (!p || ok()) n       printf( Finite n ); n     else n       printf( Infinite n ); n   } n   return 0; n } n