#include <bits/stdc++.h>   using namespace std;   const long long N = 1e3 + 7;   long long a[N][N];   int32_t main() {     long long n;     cin >> n;     for (long long i = 0; i < n; i++)       for (long long j = 0; j < n; j++) cin >> a[i][j];     long long a12 = a[0][1], a23 = a[1][2], a13 = a[0][2];     long double aa123 = (sqrt(a12 * 1.0) * sqrt(a23 * 1.0) * sqrt(a13 * 1.0));     long long a123 = round(aa123);     long long a1 = a123 / a23;     cout << a1 <<    ;     for (long long i = 1; i < n; i++) cout << a[0][i] / a1 <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   const int mod = 1e9 + 7;   int n;   struct block {     vector<int> v;     int id;   };   vector<block> b;   int a[maxn];   void get_blocks() {     b.clear();     block cur;     int j = 1;     while (j <= n) {       cur.v.clear();       while (1) {         cur.v.push_back(a[j]);         if (j == n || a[j + 1] != a[j] + 1) {           ++j;           break;         }         ++j;       }       b.push_back(cur);     }     vector<int> bv;     for (auto& w : b) bv.push_back(w.v[0]);     sort(bv.begin(), bv.end());     for (auto& w : b) {       int u = lower_bound(bv.begin(), bv.end(), w.v[0]) - bv.begin() + 1;       w.id = u;     }   }   vector<vector<int>> tv;   void tr() {     vector<vector<block>> seq;     int la = b.back().id;     int en = b.size() - 1;     vector<block> last;     last.push_back(b.back());     vector<int> e;     vector<block> mid;     if (la == b.size()) {       la = b[b.size() - 2].id;       vector<block> q;       q.push_back(b[b.size() - 2]);       --en;       for (int i = 0; i < b.size(); ++i) {         if (b[i].id == la + 1) {           for (int j = i; j < en; ++j) mid.push_back(b[j]);           vector<block> fr;           for (int j = 0; j < i; ++j) fr.push_back(b[j]);           if (fr.size()) seq.push_back(fr);           seq.push_back(mid);           seq.push_back(q);           seq.push_back(last);           break;         }       }       goto N;     }     for (int i = 0; i < b.size(); ++i) {       if (b[i].id == la + 1) {         for (int j = i; j < en; ++j) mid.push_back(b[j]);         vector<block> fr;         for (int j = 0; j < i; ++j) fr.push_back(b[j]);         if (fr.size()) seq.push_back(fr);         seq.push_back(mid);         seq.push_back(last);         break;       }     }   N:;     e.push_back(seq.size());     for (auto& w : seq) {       int cnt = 0;       for (auto& bb : w) cnt += bb.v.size();       e.push_back(cnt);     }     tv.push_back(e);     reverse(seq.begin(), seq.end());     int j = 1;     for (auto& w : seq) {       for (auto& ee : w) {         for (auto& ii : ee.v) a[j++] = ii;       }     }   }   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) cin >> a[i];     while (1) {       get_blocks();       if (b.size() == 1) break;       tr();     }     cout << tv.size() <<   n ;     for (auto& w : tv) {       for (int i = 0; i < w.size(); ++i) {         cout << w[i];         if (i < w.size() - 1)           cout <<    ;         else           cout <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> dp(2 * n);       int mx = -1;       int c = 0;       dp[0] = 1;       for (int i = 0; i < 2 * n; i++) {         int u;         cin >> u;         if (mx > u) {           c++;         } else {           for (int j = n + 1; j >= c; j--) {             dp[j] |= dp[j - c];           }           c = 1;           mx = u;         }       }       for (int j = n; j >= c; j--) {         dp[j] |= dp[j - c];       }       if (dp[n])         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int A[101];     int B[101];     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> A[i];     int t = n / k;     for (int i = 0; i < k; i++) {       int max1 = 0;       int max2 = 0;       for (int j = 0; j < t; j++)         if (A[i + j * k] == 1)           max1++;         else           max2++;       if (max2 > max1)         B[i] = 2;       else         B[i] = 1;     }     int d = 0;     for (int i = 0; i < t; i++) {       for (int j = 0; j < k; j++)         if (A[i * k + j] != B[j]) d++;     }     cout << d;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void A() {     cout <<  sjfnb  << endl;     exit(0);   }   void B() {     cout <<  cslnb  << endl;     exit(0);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     sort(a.begin(), a.end());     if (n == 1) {       if (a[0] % 2 == 0) {         B();       } else {         A();       }     }     if (a[1] == 0) {       B();     }     vector<int> b = a;     if (unique(b.begin(), b.end()) - b.begin() <= n - 2) {       B();     }     int t = 0;     for (int i = 0; i < n - 1; ++i) {       if (a[i] == a[i + 1]) {         if (i > 0 && a[i - 1] == a[i] - 1) {           B();         }         --a[i];         t = 1;       }     }     for (int i = 0; i < n; ++i) {       t = (t + a[i] - i) % 2;     }     if (t % 2 == 0) {       B();     } else {       A();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long bins(long long l, long long r) {     while (l + 1 < r) {       long long m = (l + r) >> 1;       cout <<  ?   << l <<     << m << endl;       cout.flush();       char c;       cin >> c;       if (c ==  e ) return 0;       if (c ==  y ) {         l = m;       } else {         r = m;       }     }     return r;   }   signed main() {     string s;     cin >> s;     long long x, y;     while (s ==  start ) {       x = 0, y = 1;       char c =  y ;       bool z = false;       while (c !=  x ) {         if (!z)           z = true;         else           x = y, y *= 2;         cout <<  ?   << x <<     << y << endl;         cout.flush();         cin >> c;         if (c ==  e ) return 0;       }       long long p = bins(x, y);       cout <<  !   << p << endl;       cout.flush();       cin >> s;       if (s ==  mistake ) {         return 0;       }       if (s ==  end ) return 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = 2 * acos(0.0);   const int inf = 0x3f3f3f3f;   const double infd = 1.0 / 0.0;   long long power(long long x, long long y, long long MOD) {     long long res = 1;     x = x % MOD;     while (y > 0) {       if (y & 1) res = (res * x) % MOD;       y = y >> 1;       x = (x * x) % MOD;     }     return res;   }   long long mul(long long a, long long b, long long MOD) {     if (b == 1) {       return a;     }     if (b % 2 == 0) {       return 2 * (mul(a, b / 2, MOD) % MOD);     } else {       return (a + (2 * (mul(a, b / 2, MOD)) % MOD)) % MOD;     }   }   bool ispow2(long long n) {     if (((n & (n - 1)) == 0) && n != 0) {       return true;     }     return false;   }   bool prime(int x) {     if (x == 1) {       return false;     }     if (x == 2) {       return true;     }     if (x % 2 == 0) {       return false;     }     for (int i = 3; i * i <= x; i += 2) {       if (x % i == 0) {         return false;       }     }     return true;   }   int __gcd(int a, int b) {     if (b == 0) return a;     return __gcd(b, a % b);   }   int coprime(int a, int b) {     if (a == b) return 0;     if (prime(a) && prime(b)) return 1;     if ((a == 2 && b % 2 != 0) || (b == 2 && a % 2 != 0)) return 1;     if ((a % 2 != 0 && prime(b) && a < b) || (b % 2 != 0 && prime(a) && a > b))       return 1;     if (abs(a - b) == 1) return 1;     if (a == 1 || b == 1) return 1;     return __gcd(a, b);   }   unsigned long long lcm(unsigned a, unsigned b) {     return ((unsigned long long)a) * (b / __gcd(a, b));   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, diff;     string s;     cin >> n;     cin >> s;     map<int, int> m;     int ans = 0;     m[0] = -1;     diff = 0;     for (int i = 0; i < n; i++) {       int tmp = (s[i] ==  0  ? -1 : 1);       diff += tmp;       if (m.find(diff) == m.end()) {         m[diff] = i;       }       ans = max(ans, i - m[diff]);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MX = 25;   int main(int argc, char *argv[]) {     ios ::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     string s;     cin >> n >> k;     cin >> s;     if (k > (n + 1) / 2) {       --k;       while (k < n - 1) {         cout <<  RIGHT  <<   n ;         ++k;       }       while (k > -1) {         cout <<  PRINT   << s[k] <<   n ;         if (k - 1 >= 0) cout <<  LEFT  <<   n ;         --k;       }     } else {       --k;       while (k > 0) {         cout <<  LEFT  <<   n ;         --k;       }       while (k < n) {         cout <<  PRINT   << s[k] <<   n ;         if (k + 1 < n) cout <<  RIGHT  <<   n ;         ++k;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, a[212345], ans = (1ll << 62);   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (long long i = 0; i < 2 * n; ++i) {       cin >> a[i];     }     sort(a, a + 2 * n);     for (long long i = n; i < 2 * n - 1; ++i) {       ans = min(ans, (a[2 * n - 1] - a[0]) * (a[i] - a[i - n + 1]));     }     ans = min(ans, (a[n - 1] - a[0]) * (a[2 * n - 1] - a[n]));     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2005, mod = 998244353;   int n, a[N][N], f[N][N], jie[N], ans, vis1[N], vis2[N], c[N], c2[N], vis3[N];   inline int qpow(int a, int b, int res = 1) {     for (; b; b >>= 1, a = 1ll * a * a % mod)       if (b & 1) res = 1ll * res * a % mod;     return res;   }   void init() {     jie[0] = 1;     for (register int i = 1; i <= n; ++i) jie[i] = 1ll * jie[i - 1] * i % mod;     f[0][0] = 1, f[1][1] = 1;     for (register int i = 2; i <= n; i++) {       f[i][0] = 1ll * (i - 1) * (f[i - 1][0] + f[i - 2][0]) % mod;       for (register int j = 1; j <= i; j++)         f[i][j] = (f[i][j - 1] + f[i - 1][j - 1]) % mod;     }   }   inline int lowbit(int x) { return x & -x; }   inline void add(int x, int y) {     for (; x <= n; x += lowbit(x)) c[x] += y;   }   inline int get(int x, int res = 0) {     for (; x; x -= lowbit(x)) res += c[x];     return res;   }   inline void add2(int x, int y) {     for (; x <= n; x += lowbit(x)) c2[x] += y;   }   inline int get2(int x, int res = 0) {     for (; x; x -= lowbit(x)) res += c2[x];     return res;   }   signed main() {     scanf( %d , &n);     init();     for (register int i = 1; i <= n; ++i) {       int tmp = qpow(f[n][0], n - i), res = 0, num = 0;       memset(vis1, 0, sizeof(vis1)), memset(vis2, 0, sizeof(vis2));       memset(vis3, 0, sizeof(vis3));       memset(c, 0, sizeof(c)), memset(c2, 0, sizeof(c2));       for (register int j = 1; j <= n; ++j) add(j, 1), add2(j, 1);       for (register int j = 1; j <= n; ++j) {         scanf( %d , &a[i][j]);         if (i == 1) {           int cnt = get(a[i][j] - 1);           res = (res + 1ll * cnt * jie[n - j] % mod) % mod;           add(a[i][j], -1);         } else {           vis1[a[i - 1][j]] = 1;           if (vis3[a[i - 1][j]] == 0)             add2(a[i - 1][j], -1), vis3[a[i - 1][j]] = 1;           if (vis2[a[i - 1][j]]) num--;           int cnt = get(a[i][j] - 1);           if (a[i - 1][j] < a[i][j] && get(a[i - 1][j]) - get(a[i - 1][j] - 1))             cnt--;           int cnt1 = get2(a[i][j] - 1), cnt2 = 0;           if (a[i - 1][j] < a[i][j] && get2(a[i - 1][j]) - get2(a[i - 1][j] - 1))             cnt1--;           cnt2 = cnt - cnt1;           res = (res + 1ll * cnt1 * f[n - j][num + 1] % mod) % mod;           res = (res + 1ll * cnt2 * f[n - j][num] % mod) % mod;           vis2[a[i][j]] = 1;           add(a[i][j], -1);           if (!vis3[a[i][j]]) add2(a[i][j], -1), vis3[a[i][j]] = 1;           if (!vis1[a[i][j]]) num++;         }       }       ans = (ans + 1ll * res * tmp % mod) % mod;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int X[4] = {0, 0, -1, 1};   const int Y[4] = {-1, 1, 0, 0};   int dp[1001][1001], Vis[1001][1001];   char c[1001][1001];   int vis[1001][1001], i, j, m, n, p, k, ans;   bool check(int x, int y, int xx, int yy) {     if (x < 0 || y < 0 || x > n || y >= m) return false;     if (c[x][y] ==  D  && c[xx][yy] ==  A ) return true;     if (c[x][y] ==  I  && c[xx][yy] ==  D ) return true;     if (c[x][y] ==  M  && c[xx][yy] ==  I ) return true;     if (c[x][y] ==  A  && c[xx][yy] ==  M ) return true;     return false;   }   int dfs(int x, int y) {     if (Vis[x][y]) return dp[x][y];     if (vis[x][y]) {       printf( Poor Inna! n );       exit(0);     }     vis[x][y] = 1;     int i;     for (i = 0; i < 4; i++)       if (check(x + X[i], y + Y[i], x, y))         dp[x][y] = max(dp[x][y], dfs(x + X[i], y + Y[i]));     if (c[x][y] ==  A ) dp[x][y]++;     vis[x][y] = 0;     Vis[x][y] = 1;     return dp[x][y];   }   int main() {     scanf( %d%d , &n, &m);     for (i = 1; i <= n; i++) scanf( %s , &c[i]);     for (i = 1; i <= n; i++)       for (j = 0; j < m; j++)         if (c[i][j] ==  D ) ans = max(ans, dfs(i, j));     if (!ans)       printf( Poor Dima! n );     else       printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const long long int N = 1e6 + 5;   struct comp {     bool operator()(pair<int, int> &a, pair<int, int> &b) {       if (a.first == b.first) return (a.second < b.second);       return (a.first > b.first);     }   };   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, m;     cin >> n >> m;     vector<pair<int, int>> ed(m);     map<pair<int, int>, queue<int>> pos;     priority_queue<pair<int, int>, vector<pair<int, int>>, comp> p;     for (int i = 0; i < m; i++) {       cin >> ed[i].first >> ed[i].second;       p.push(ed[i]);       pos[ed[i]].push(i);     }     vector<int> q;     int i = 1, x = 2;     queue<int> avl;     q.push_back(i++);     int s = q.size();     vector<pair<int, int>> res(m);     bool check = true;     while (!p.empty()) {       pair<int, int> v = p.top();       p.pop();       int j = pos[v].front();       pos[v].pop();       if (v.second) {         res[j] = {q.back(), i};         q.push_back(i++);         if (q.size() > 2) avl.push(q.size() - 1);       } else if (!avl.empty()) {         int t = avl.front();         if (t - x >= 0) {           res[j] = {q[t], q[t - x]};           x++;         } else if (avl.size() == 1) {           check = false;           break;         } else {           avl.pop();           t = avl.front();           x = 2;           res[j] = {q[t], q[t - x]};           x++;         }       } else {         check = false;         break;       }     }     if (check) {       for (pair<int, int> t : res) cout << t.first <<     << t.second <<   n ;     } else       cout << -1;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500005;   int n, m, a[N], b[N];   priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,                  greater<pair<long long, long long> > >       Q;   long long ans;   int solve(int x) {     ans = 0;     int num = 0;     for (int i = (int)(1); i <= (int)(n); i++) {       Q.push(pair<long long, long long>(a[i], 0));       long long t = Q.top().first, now = b[i] - x;       if (now + t < 0) {         ans += now + t;         Q.pop();         Q.push(pair<long long, long long>(-now, 1));       }     }     for (; !Q.empty(); num += Q.top().second, Q.pop())       ;     return num;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = (int)(1); i <= (int)(n); i++) scanf( %d , &a[i]);     for (int i = (int)(1); i <= (int)(n); i++) scanf( %d , &b[i]);     long long l = 0, r = 2e9;     while (l <= r) {       long long mid = (l + r) / 2;       int sum = solve(mid);       if (sum == m) {         printf( %lld n , ans + 1ll * m * mid);         return 0;       }       if (sum < m)         l = mid + 1;       else         r = mid - 1;     }   }


#include <bits/stdc++.h>   using namespace std;   inline long long getnum() {     char c = getchar();     long long num, sign = 1;     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) sign = -1;     for (num = 0; c >=  0  && c <=  9 ;) {       c -=  0 ;       num = num * 10 + c;       c = getchar();     }     return num * sign;   }   pair<int, int> P[200050];   int A[200050];   stack<int> Stack;   int main() {     int d = getnum(), tot = getnum(), n = getnum() + 2;     P[1] = make_pair(0, 0);     P[2] = make_pair(d, 0);     for (int i = 3; i <= n; i++) {       int x = getnum(), y = getnum();       P[i] = make_pair(x, y);     }     sort(P + 1, P + n + 1);     Stack.push(n);     for (int i = n - 1; i >= 1; i--) {       while (P[Stack.top()].second > P[i].second) Stack.pop();       A[i] = Stack.top();       Stack.push(i);     }     long long ans = 0;     int cur = 0;     for (int i = 1; i <= n - 1; i++) {       int x = 0, y = A[i];       int to = P[y].first - P[i].first;       if (to > cur) {         int a = to - cur;         if (a + cur > tot) a = (tot - cur);         ans += (long long)a * P[i].second;         cur += a;       }       cur -= (P[i + 1].first - P[i].first);       if (cur < 0) {         puts( -1 );         return -0;       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000000;   int z[N];   void computeZ(char s[]) {     int n = strlen(s), L = 0, R = 0;     for (int i = 1; i < n; i++) {       if (i > R) {         L = R = i;         while (R < n && s[R - L] == s[R]) R++;         z[i] = R - L;         R--;       } else {         int k = i - L;         if (z[k] < R - i + 1)           z[i] = z[k];         else {           L = i;           while (R < n && s[R - L] == s[R]) R++;           z[i] = R - L;           R--;         }       }     }   }   char s[N + 1];   int suffix[N + 1], size;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> s;     computeZ(s);     int n = strlen(s), mx = 0;     int size = 0;     for (int i = 1; i < n; ++i)       if (z[i] == n - i) suffix[z[i]]++;     for (int i = 1; i < n; ++i) {       if (suffix[i])         suffix[i] = i;       else         suffix[i] = suffix[i - 1];     }     for (int i = 1; i < n; ++i) {       int len = z[i] - (z[i] == n - i);       mx = max(mx, suffix[len]);     }     if (mx == 0)       cout <<  Just a legend  << endl;     else {       s[mx] =   0 ;       cout << s << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   vector<vector<int>> v;   void sit(int x, int y) {     while (1) {       int u = max(1, x - v[x][y]), d = min(n, x + v[x][y]);       for (; u <= d; ++u) {         if (y - v[x][y] + abs(u - x) > 0) {           if (v[u][y - v[x][y] + abs(u - x)] == 0) {             cout << u <<     << y - v[x][y] + abs(u - x) <<   n ;             ++v[u][y - v[x][y] + abs(u - x)];             return;           } else if (v[x][y] < v[u][y - v[x][y] + abs(u - x)] - v[x][y]) {             v[x][y] = v[u][y - v[x][y] + abs(u - x)] - v[x][y];             break;           }         }         if (y + v[x][y] - abs(u - x) <= m) {           if (v[u][y + v[x][y] - abs(u - x)] == 0) {             cout << u <<     << y + v[x][y] - abs(u - x) <<   n ;             ++v[u][y + v[x][y] - abs(u - x)];             return;           } else if (v[x][y] < v[u][y + v[x][y] - abs(u - x)] - v[x][y]) {             v[x][y] = v[u][y + v[x][y] - abs(u - x)] - v[x][y];             break;           }         }       }       if (u > d) ++v[x][y];     }   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> m >> k;     v.resize(n + 1, vector<int>(m + 1, 0));     while (k--) {       int a, b;       cin >> a >> b;       sit(a, b);     }   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = (long long)(1e9 + 7);   const long long N = (long long)(1e5 + 7);   long long p[N];   long long h[N];   int n, m;   bool fun(long long dist) {     queue<long long> q;     for (int i = 0; i < m; i++) {       q.push(p[i]);     }     for (int i = 0; i < n; i++) {       if (q.empty()) {         return true;       }       if (q.front() >= h[i]) {         while (!q.empty() && q.front() <= h[i] + dist) {           q.pop();         }       } else {         long long x = h[i] - q.front();         if (x > dist) {           return false;         }         long long y = max(dist - (2 * x), (dist - x) / 2);         y = max(y, (long long)0);         while (!q.empty() && q.front() <= h[i]) {           q.pop();         }         while (!q.empty() && q.front() - h[i] <= y) {           q.pop();         }       }     }     if (q.empty()) {       return true;     } else {       return false;     }   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> h[i];     }     for (int i = 0; i < m; i++) {       cin >> p[i];     }     sort(h, h + n);     sort(p, p + m);     long long l = 0;     long long r = 20000000007;     long long ans = -1;     while (l <= r) {       long long dist = (l + r) / 2;       if (fun(dist)) {         ans = dist;         r = dist - 1;       } else {         l = dist + 1;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long maxm = -1e18;   long long minm = 1e18;   bool isP(long long n) {     if (n == 2LL) return 1;     if (n % 2LL == 0LL)       return 0;     else if (n == 1)       return 0;     for (long long i = 3; i * i <= n; ++i)       if (n % i == 0) return 0;     return 1;   }   void solve() {     int n;     cin >> n;     string res;     while (n > 0) {       if (n == 3) {         res = res +  7 ;         n -= 3;       } else {         res = res +  1 ;         n -= 2;       }     }     string result;     for (int i = res.length() - 1; i >= 0; i--) {       result = result + res[i];     }     cout << result << endl;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long dp[5005][5005];   deque<int> dq[5005];   int a[5005];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, k, x;     cin >> n >> k >> x;     for (int i = 1; i <= n; i++) cin >> a[i];     memset(dp, -1, sizeof(dp));     dp[0][0] = 0;     dq[0].push_back(0);     for (int i = 1; i <= n; i++) {       for (int l = x - 1; l >= 0; l--) {         while (!dq[l].empty() && i - dq[l].front() > k) dq[l].pop_front();         if (dq[l].empty()) continue;         dp[i][l + 1] = max(dp[i][l + 1], dp[dq[l].front()][l] + a[i]);         while (!dq[l + 1].empty() && dp[dq[l + 1].back()][l + 1] <= dp[i][l + 1])           dq[l + 1].pop_back();         dq[l + 1].push_back(i);       }     }     long long ans = -1;     for (int i = n; i >= n - k + 1; i--) ans = max(ans, dp[i][x]);     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int t, f, w;     bool operator<(const edge& e) const { return w > e.w; }     edge(int from, int to, int weight) {       t = to;       f = from;       w = weight;     }   };   const double EPS = (1e-5);   long long binpow(long long a, long long b) {     long long res = 1;     while (b > 0) {       if (b & 1) res = res * a;       a = a * a;       b >>= 1;     }     return res;   }   int findXOR(int n) {     switch (n % 4) {       case 0:         return n;       case 1:         return 1;       case 2:         return n + 1;       case 3:         return 0;     }   }   int rangeXOR(int l, int r) { return (findXOR(l - 1) ^ findXOR(r)); }   int getbit(int mask, int bit) { return (mask & (1 << bit)); }   void setbit(int& mask, int bit, int val) {     if (val)       mask |= (1 << bit);     else       mask &= ~(1 << bit);   }   const int N = 100 + 10;   const int M = 101;   const long long INF = 1000 * 1e9 + 1;   void solve() {     int x;     cin >> x;     if (x == 3) {       cout << 5 <<   n ;       return;     }     if (x == 1) {       cout << 1 <<   n ;       return;     }     int i = 1;     while (x > i * i / 2 + 1) {       i += 2;     }     cout << i <<   n ;   }   int main(void) {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int tc = 1;     while (tc--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, m, a, b, c;     cin >> n >> m;     vector<int> ans(n, 0);     set<int> s;     for (int i = 1; i <= n; ++i) {       s.insert(i);     }     while (m--) {       cin >> a >> b >> c;       auto lt = s.lower_bound(a), ht = prev(s.upper_bound(b));       auto ct = lt;       while (ct != next(ht)) {         if (*ct != c) {           ans[*ct - 1] = c;         }         ct = next(ct);       }       s.erase(lt, next(ht));       s.insert(c);     }     for (int x : ans) cout << x <<    ;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1e9 + 7;   long long q[500009];   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n;     cin >> n;     long long sum = 0;     long long mini = 0;     for (long long i = 1; i < n; i++) {       cin >> q[i];       sum += q[i];       if (sum < mini) mini = sum;     }     bool ok = 1;     long long p1 = 1 - mini;     set<long long> vis;     vector<long long> p;     p.push_back(p1);     vis.insert(p1);     long long prev = p1;     for (long long i = 1; i < n; i++) {       long long x = prev + q[i];       if (x < 1 or x > n or vis.count(x)) {         ok = 0;         break;       }       p.push_back(x);       vis.insert(x);       prev = x;     }     if (p[0] > n or p[0] < 1) ok = 0;     if (!ok) {       cout <<  -1 n ;       return 0;     }     for (auto i : p) cout << i <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e3 + 100;   struct node {     int a, b, c;   } p[N];   int nex[N];   vector<int> pre[N];   int dp[N][N];   int main() {     int n, m, k;     scanf( %d %d %d , &n, &m, &k);     int sum = k;     for (int i = 1; i <= n; ++i)       scanf( %d %d %d , &p[i].a, &p[i].b, &p[i].c), sum += p[i].b;     for (int i = 1; i <= n; ++i) nex[i] = i;     for (int i = 1; i <= m; ++i) {       int u, v;       scanf( %d %d , &u, &v);       nex[v] = max(nex[v], u);     }     for (int i = 1; i <= n; ++i) {       pre[nex[i]].push_back(p[i].c);     }     memset(dp, -0x3f3f3f3f, sizeof(dp));     dp[1][k] = 0;     for (int i = 1; i <= n; ++i) {       for (int j = p[i].a; j <= 5000; ++j) {         int nj = j + p[i].b;         if (dp[i][j] >= 0) dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j]);       }       for (int k = 0; k < pre[i].size(); ++k) {         int x = pre[i][k];         for (int j = 0; j <= 5000; ++j) {           dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j + 1] + x);         }       }     }     int ans = -1;     for (int i = 0; i <= sum; ++i) ans = max(ans, dp[n + 1][i]);     printf( %d , ans);     return 0;   }


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


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   #pragma GCC optimize( unroll-loops )   #pragma comment(linker,  /stack:200000000 )   using namespace std;   const int GRANDOM =       std::chrono::high_resolution_clock::now().time_since_epoch().count();   namespace vn16 {   template <class T>   struct like_array : is_array<T> {};   template <class T, size_t N>   struct like_array<array<T, N>> : true_type {};   template <class T>   struct like_array<vector<T>> : true_type {};   template <class T>   bool is_like_array(const T& a) {     return like_array<T>::value;   }   template <class T>   void _R(T& x) {     std::cin >> x;   }   inline void _R(int& x) { scanf( %d , &x); }   inline void _R(int64_t& x) { scanf( %  SCNd64, &x); }   inline void _R(double& x) { scanf( %lf , &x); }   inline void _R(char& x) { scanf(  %c , &x); }   inline void _R(char* x) { scanf( %s , x); }   template <typename T>   inline void _R(vector<T>& v, size_t ie) {     for (int i = 0; i < ie; i++) _R(v[i]);   }   template <typename T>   inline void _R(vector<T>& v) {     _R(v, v.size());   }   inline void R() {}   template <class T, class... U>   inline void R(T& head, U&... tail) {     _R(head);     R(tail...);   }   template <class T>   void _W(const T& x) {     cout << x;   }   template <typename T>   inline void _W(vector<T>& v, size_t ie) {     for (int i = 0; i < ie; i++) {       if (i > 0) _W(   );       _W(v[i]);     }     _W(  n );   }   template <typename T>   inline void _W(vector<T>& v) {     _W(v, v.size());   }   template <class T>   inline void _W(const vector<T>& x) {     for (auto i = x.begin(); i != x.end(); _W(*i++))       if (i != x.cbegin()) putchar(   );   }   inline void W() {}   template <class T, class... U>   inline void W(const T& head, const U&... tail) {     _W(head);     if (sizeof...(tail)) putchar(   ), W(tail...);   }   template <class T, class... U>   inline void WL(const T& head, const U&... tail) {     _W(head);     putchar(sizeof...(tail) ?     :   n );     W(tail...);   }   template <class T>   void _RE(T& x) {     cin >> x;   }   template <class Arg, class... Args>   void _RE(Arg& first, Args&... rest);   void _RE(double& x) {     string t;     _RE(t);     x = stod(t);   }   void _RE(long double& x) {     string t;     _RE(t);     x = stold(t);   }   template <class T>   void _RE(complex<T>& x);   template <class T1, class T2>   void _RE(pair<T1, T2>& p);   template <class T>   void _RE(vector<T>& a);   template <class T, size_t SZ>   void _RE(array<T, SZ>& a);   template <class Arg, class... Args>   void _RE(Arg& first, Args&... rest) {     _RE(first);     _RE(rest...);   }   template <class T>   void _RE(complex<T>& x) {     T a, b;     _RE(a, b);     x = cd(a, b);   }   template <class T1, class T2>   void _RE(pair<T1, T2>& p) {     _RE(p.f, p.s);   }   template <class T>   void _RE(vector<T>& a) {     for (int i = 0, ie = sz(a); i < ie; i++) _RE(a[i]);   }   template <class T, size_t SZ>   void _RE(array<T, SZ>& a) {     for (int i = 0; i < SZ; i++) _RE(a[i]);   }   };  // namespace vn16   namespace vn16 {   template <typename T>   T gcd(T m, T n) {     while (n) {       T t = m % n;       m = n;       n = t;     };     return m;   }   template <typename T>   T exgcd(T a, T b, T& sa, T& ta) {     T q, r, sb = 0, tb = 1, sc, tc;     sa = 1, ta = 0;     if (b) do         q = a / b, r = a - q * b, a = b, b = r, sc = sa - q * sb, sa = sb,         sb = sc, tc = ta - q * tb, ta = tb, tb = tc;       while (b);     return a;   }   template <typename T>   T mul_inv(T a, T b) {     T t1 = a, t2 = b, t3;     T v1 = 1, v2 = 0, v3;     T x;     while (t2 != 1)       x = t1 / t2, t3 = t1 - x * t2, v3 = v1 - x * v2, t1 = t2, t2 = t3, v1 = v2,       v2 = v3;     return (v2 + b) % b;   }   template <typename T>   T powmod(T a, T b, T MOD) {     if (b < 0) return 0;     T rv = 1;     while (b) (b % 2) && (rv = (rv * a) % MOD), a = a * a % MOD, b /= 2;     return rv;   }   template <typename T>   inline T isqrt(T k) {     T r = sqrt((double)k) + 1;     while (r * r > k) r--;     while ((r + 1) * (r + 1) <= k) r++;     return r;   }   template <typename T>   inline T icbrt(T k) {     T r = cbrt((double)k) + 1;     while (r * r * r > k) r--;     return r;   }   template <typename T>   bool mul_overflow(T& r, T a, T b) {     return __builtin_mul_overflow(a, b, &r);   }   template <long long n>   struct BitSize {     enum { Size = BitSize<n / 2>::Size + 1 };   };   template <>   struct BitSize<0> {     enum { Size = 1 };   };   template <>   struct BitSize<1> {     enum { Size = 1 };   };   template <typename TH>   void _dbg(const char* sdbg, TH h) {     cerr << sdbg <<  =  << h <<   n ;   }   template <typename TH, typename... TA>   void _dbg(const char* sdbg, TH h, TA... t) {     while (*sdbg !=  , ) cerr << *sdbg++;     cerr <<  =  << h <<  , ;     _dbg(sdbg + 1, t...);   }   }  // namespace vn16   namespace vn16 {   template <typename T>   int min_rotation(vector<T> s) {     int a = 0, N = s.size();     for (int b = 0; b < N; b++)       for (int i = 0; i < N; i++) {         if (a + i == b || s[(a + i) % N] < s[(b + i) % N]) {           b += max(0, i - 1);           break;         }         if (s[(a + i) % N] > s[(b + i) % N]) {           a = b;           break;         }       }     return a;   };   template <typename T>   class StackGuard {    private:     T x;       public:     StackGuard(T x) : x(x) {}     ~StackGuard() { x(); }   };   }  // namespace vn16   namespace vn16 {};   namespace vn16 {   const int maxn = 1e5 + 7;   const int maxm = 23;   int n, m;   char s[maxn];   int cnt2[maxm][maxm];   int cnt[maxm];   long long dp[1 << maxm];   int main() {     srand(GRANDOM);     cin >> n >> m >> s;     memset(cnt, 0, sizeof(cnt));     for (int i = 1; i < n; i++) {       int x = s[i] -  a ;       int y = s[i - 1] -  a ;       cnt2[x][y]++;       cnt2[y][x]++;     }     int maxs = (1 << m);     dp[0] = 0;     for (int x = 1; x < maxs; x++) {       dp[x] = (long long)maxn * maxm * 10;       long long c = 0;       for (int i = 0; i < m; i++) {         for (int j = i + 1; j < m; j++) {           if (((x >> i) ^ (x >> j)) & 1) c += cnt2[i][j];         }       }       for (int i = 0; i < m; i++)         if ((x >> i) & 1) dp[x] = min(dp[x], dp[x ^ (1 << i)] + c);     }     cout << dp[maxs - 1] << endl;     return 0;   }   };  // namespace vn16   int main(int argc, char* argv[]) {     std::cin.sync_with_stdio(false);     std::cin.tie(nullptr);     return vn16::main();   }


#include <bits/stdc++.h>   using namespace std;   struct point {     int x, y;   } a[5], b[5];   int ans;   int dis(int i, int j) {     int res = (a[i].x - a[j].x) * (a[i].x - a[j].x);     return res + (a[i].y - a[j].y) * (a[i].y - a[j].y);   }   int ok() {     map<int, int> mp;     for (int i = 1; i < 4; i++)       for (int j = i + 1; j <= 4; j++) mp[dis(i, j)]++;     if (mp.size() != 2) return 0;     int t;     for (auto tmp : mp) {       if (tmp.first == 0) return 0;       t = tmp.second;     }     if (t == 4 || t == 2) return 1;     return 0;   }   void Rotate(int p) {     int x = a[p].x - b[p].x, y = a[p].y - b[p].y;     a[p].x = -y + b[p].x;     a[p].y = x + b[p].y;   }   void dfs(int p, int step) {     if (p == 5) {       if (ok()) ans = min(ans, step);       return;     }     dfs(p + 1, step);     for (int i = 1; i < 4; i++) {       Rotate(p);       dfs(p + 1, step + i);     }     Rotate(p);   }   int main() {     int T;     cin >> T;     while (T--) {       ans = 100;       for (int i = 1; i <= 4; i++) cin >> a[i].x >> a[i].y >> b[i].x >> b[i].y;       dfs(1, 0);       if (ans == 100)         puts( -1 );       else         cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, u, v, w;   unordered_map<long long, vector<long long>> m;   struct Key {     long long N, p;     bool operator==(const Key& other) const {       return (N == other.N && p == other.p);     }   };   struct KeyHasher {     size_t operator()(const Key& k) const { return k.N * 59 + k.p * 61; }   };   set<pair<long long, long long>, greater<pair<long long, long long>>> sp[100010];   unordered_map<Key, long long, KeyHasher> done, wt, val;   long long dfs(long long i, vector<bool>& vis) {     vis[i] = 1;     long long mx = 0;     for (auto it : m[i]) {       if (vis[it] == 0) {         long long v = dfs(it, vis);         long long jk = val[{it, i}] = v + wt[{it, i}];         sp[i].insert({jk, it});         mx = max(mx, val[{it, i}]);       }     }     return mx;   }   void dfs2(long long i, vector<bool>& vis) {     vis[i] = 1;     for (auto it : m[i]) {       if (vis[it] == 0) {         long long mx = 0;         for (auto pt : sp[i]) {           if (it != pt.second) {             mx = pt.first;             break;           }         }         long long jk = val[{i, it}] = mx + wt[{i, it}];         sp[it].insert({jk, i});         dfs2(it, vis);       }     }   }   int main() {     cin >> n >> k;     for (long long i = 1; i < n; i++) {       cin >> u >> v >> w;       m[u].push_back(v);       m[v].push_back(u);       wt[{u, v}] = wt[{v, u}] = w;     }     vector<bool> vis(n + 1, 0);     vector<bool> vis2(n + 1, 0);     dfs(1, vis);     dfs2(1, vis2);     long long mn = LLONG_MAX, ind;     for (auto it : m) {       long long mx = 0;       for (auto i : it.second) mx = max(mx, val[{i, it.first}]);       if (mn > mx) {         mn = mx;         ind = it.first;       }     }     k--;     vector<long long> v;     v.push_back(ind);     long long s = ind, e = ind;     while (k) {       long long mx = 0, in, f = 0, first = 0;       for (auto it : m[s]) {         if (done[{it, s}] == 0) {           first = 1;           if (mx < val[{it, s}]) {             mx = val[{it, s}];             in = it;           }         }       }       if (s != e) {         for (auto it : m[e]) {           if (done[{it, e}] == 0) {             first = 1;             if (mx < val[{it, e}]) {               f = 1;               mx = val[{it, e}];               in = it;             }           }         }       }       if (first == 0) break;       if (f) {         done[{e, in}] = done[{in, e}] = 1;         e = in;       } else {         done[{s, in}] = done[{in, s}] = 1;         s = in;       }       v.push_back(in);       k--;     }     long long mx = 0;     for (auto it : v) {       for (auto i : m[it]) {         if (done[{it, i}] == 0) mx = max(mx, val[{i, it}]);       }     }     cout << mx;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   char gride[5010];   vector<vector<char> > grid;   vector<vector<int> > pesq, pesq2;   vector<vector<int> > pdir, pdir2;   vector<vector<int> > pcima, pcima2;   vector<vector<int> > pbaxo, pbaxo2;   void apaga(int i, int j) {     if (pesq2[i][j] != -1) pdir2[i][pesq2[i][j]] = pdir2[i][j];     if (pdir2[i][j] != -1) pesq2[i][pdir2[i][j]] = pesq2[i][j];     if (pcima2[i][j] != -1) pbaxo2[pcima2[i][j]][j] = pbaxo2[i][j];     if (pbaxo2[i][j] != -1) pcima2[pbaxo2[i][j]][j] = pcima2[i][j];   }   int simula(int i, int j) {     if (grid[i][j] ==  U ) {       int k = pcima2[i][j];       if (k == -1) return 1;       apaga(i, j);       return 1 + simula(k, j);     }     if (grid[i][j] ==  D ) {       int k = pbaxo2[i][j];       if (k == -1) return 1;       apaga(i, j);       return 1 + simula(k, j);     }     if (grid[i][j] ==  L ) {       int k = pesq2[i][j];       if (k == -1) return 1;       apaga(i, j);       return 1 + simula(i, k);     }     if (grid[i][j] ==  R ) {       int k = pdir2[i][j];       if (k == -1) return 1;       apaga(i, j);       return 1 + simula(i, k);     }     return 0;   }   int main() {     scanf( %d %d , &n, &m);     grid.resize(n);     for (int i = 0; i < n; i++) {       scanf( %s , gride);       grid[i].resize(m);       for (int j = 0; j < m; j++) grid[i][j] = gride[j];     }     pesq.resize(n);     pdir.resize(n);     pcima.resize(n);     pbaxo.resize(n);     for (int i = 0; i < n; i++) {       pesq[i].resize(m);       pdir[i].resize(m);       pcima[i].resize(m);       pbaxo[i].resize(m);       int atu = -1;       for (int j = 0; j < m; j++)         if (grid[i][j] !=  . ) {           pesq[i][j] = atu;           atu = j;         }       atu = -1;       for (int j = m - 1; j >= 0; j--)         if (grid[i][j] !=  . ) {           pdir[i][j] = atu;           atu = j;         }     }     for (int j = 0; j < m; j++) {       int atu = -1;       for (int i = 0; i < n; i++)         if (grid[i][j] !=  . ) {           pcima[i][j] = atu;           atu = i;         }       atu = -1;       for (int i = n - 1; i >= 0; i--)         if (grid[i][j] !=  . ) {           pbaxo[i][j] = atu;           atu = i;         }     }     int best = 0;     int qts = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (grid[i][j] !=  . ) {           pcima2 = pcima;           pbaxo2 = pbaxo;           pesq2 = pesq;           pdir2 = pdir;           int t = simula(i, j);           if (t == best)             qts++;           else if (t > best) {             best = t;             qts = 1;           }         }     printf( %d %d n , best, qts);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 3, INF = 1e9;   int main() {     ios ::sync_with_stdio(0);     cin.tie(NULL), cout.tie(NULL);     long long n, last = 0, id = 0, ans = 0, i = 0;     cin >> n;     vector<pair<int, int> > v(n);     for (auto &t : v) cin >> t.first >> t.second;     for (; i <= N; ++i) {       if (last) last--;       if (v[id].first == i) {         last += v[id].second;         id++;       }       ans = max(ans, last);       if (id == v.size()) {         break;       }     }     ans = max(ans, last);     cout << i + last <<     << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   const int INF = 1e9 + 7;   const int N = 1010;   const double pi = 3.1415926;   int n;   int t[110], d[110], p[110], pre[2010], now[2010], sum[2010], good[2010];   int dp[2010];   vector<int> v[2010];   struct node {     int t;     int d;     int p;     int id;   } e[110];   bool cmp(node a, node b) { return a.d < b.d; }   int main() {     while (~scanf( %d , &n)) {       for (int i = 0; i < 2010; i++) v[i].clear();       for (int i = 0; i < n; i++) {         scanf( %d%d%d , &e[i].t, &e[i].d, &e[i].p);         e[i].id = i + 1;       }       sort(e, e + n, cmp);       memset(dp, 0, sizeof(dp));       ;       memset(sum, 0, sizeof(sum));       ;       memset(now, 0, sizeof(now));       ;       memset(pre, 0, sizeof(pre));       ;       int ans = 0, idx = 0, s1 = 0, s2 = 0;       for (int i = 0; i < n; i++) {         for (int j = e[i].d; j > e[i].t; j--) {           s1 = 0;           if (dp[j - e[i].t] + e[i].p > dp[j]) {             dp[j] = dp[j - e[i].t] + e[i].p;             sum[j] = sum[j - e[i].t] + 1;             v[j] = v[j - e[i].t];             v[j].push_back(e[i].id);           }           if (dp[j] > ans) {             idx = j;             ans = dp[j];           }         }       }       printf( %d n , ans);       printf( %d n , sum[idx]);       for (int i = 0; i < v[idx].size(); i++) {         printf( %d  , v[idx][i]);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve();   int main() {     ios::sync_with_stdio(0);     solve();     return 0;   }   pair<pair<double, double>, pair<double, double> > rects[100];   void solve() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> rects[i].first.first >> rects[i].first.second >>           rects[i].second.first >> rects[i].second.second;       if (rects[i].first.first > rects[i].second.first)         swap(rects[i].first.first, rects[i].second.first);       if (rects[i].first.second > rects[i].second.second)         swap(rects[i].first.second, rects[i].second.second);       for (int j = 0; j < i; j++) {         double cx = 0, cy = 0, m = 0;         for (int k = j + 1; k <= i; k++) {           double l = fabs(rects[k].first.first - rects[k].second.first);           double lll = l * l * l;           m += lll;           double x = (rects[k].first.first + rects[k].second.first) / 2.0;           double y = (rects[k].first.second + rects[k].second.second) / 2.0;           cx += lll * x;           cy += lll * y;         }         cx /= m;         cy /= m;         if (rects[j].first.first > cx || rects[j].first.second > cy ||             rects[j].second.first < cx || rects[j].second.second < cy) {           cout << i;           return;         }       }     }     cout << n;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 8e5 + 1000;   long long re[maxn];   vector<long long> v;   int sum[maxn];   struct node {     long long l, r;   } a[maxn];   long long ans[maxn];   int getid(long long x) {     return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       scanf( %lld%lld , &a[i].l, &a[i].r);       v.push_back(a[i].l);       v.push_back(a[i].r);       v.push_back(a[i].l + 1);       v.push_back(a[i].r + 1);     }     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());     int sz = v.size();     for (int i = 0; i < v.size(); i++) {       re[i + 1] = v[i];     }     for (int i = 0; i < n; i++) {       int l = getid(a[i].l);       int r = getid(a[i].r);       sum[l]++;       sum[r + 1]--;     }     for (int i = 1; i <= sz; i++) {       sum[i] = sum[i - 1] + sum[i];     }     for (int i = 1; i < sz; i++) {       ans[sum[i]] += re[i + 1] - re[i];     }     for (int i = 1; i <= n; i++) {       printf( %lld  , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k, x, y, H[300005], d, ant[300005];   vector<int> v[300005], st, L;   void dfs(int nod) {     int ok = 0;     st.push_back(nod);     H[nod] = st.size();     if (H[nod] >= d) {       cout <<  PATH n ;       cout << st.size() <<   n ;       for (auto it : st) cout << it <<    ;       exit(0);     }     for (auto it : v[nod])       if (!H[it]) {         ant[it] = nod;         dfs(it);         ok = 1;       }     if (!ok) L.push_back(nod);     st.pop_back();   }   bool _try(int a, int b) {     if (H[a] < H[b]) swap(a, b);     if (H[a] - H[b] + 1 >= 3 && (H[a] - H[b] + 1) % 3 != 0) {       cout << H[a] - H[b] + 1 <<   n ;       int crt = a;       while (crt != b) {         cout << crt <<    ;         crt = ant[crt];       }       cout << crt <<   n ;       return 1;     }     return 0;   }   bool _try2(int a, int b, int c) {     if (H[a] < H[b]) swap(a, b);     if (H[a] - H[b] + 2 >= 3 && (H[a] - H[b] + 2) % 3 != 0) {       cout << H[a] - H[b] + 2 <<   n ;       int crt = a;       while (crt != b) {         cout << crt <<    ;         crt = ant[crt];       }       cout << crt <<     << c <<   n ;       return 1;     }     return 0;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> k;     d = n / k + (n % k != 0);     for (int i = 1; i <= m; i++) {       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     dfs(1);     cout <<  CYCLES n ;     for (auto it : L) {       if (k == 0) break;       k--;       if (_try(it, v[it][0])) continue;       if (_try(it, v[it][1])) continue;       if (_try(it, v[it][2])) continue;       if (_try2(v[it][0], v[it][1], it)) continue;       if (_try2(v[it][0], v[it][2], it)) continue;       if (_try2(v[it][1], v[it][2], it)) continue;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     int b[n];     for (int i = 0; i < n; i++) {       cin >> a[i];       b[i] = a[i];     }     sort(a, a + n);     int left = 0, right = 0;     for (int i = 0; i < n; i++) {       if (b[i] != a[i]) {         left = i;         break;       }     }     for (int i = n - 1; i >= 0; i--) {       if (b[i] != a[i]) {         right = i;         break;       }     }     int i = left;     int j = right;     while (left < right) {       swap(b[left], b[right]);       left++;       right--;     }     int flag = 1;     for (int i = 0; i < n; i++) {       if (a[i] != b[i]) {         flag = 0;         break;       }     }     if (flag) {       cout <<  yes  << endl;       cout << i + 1 <<     << j + 1;     } else       cout <<  no ;   }


#include <bits/stdc++.h>   using namespace std;   long long power(long long a, long long b) {     if (a == 1 || b == 0) return 1;     long long c = power(a, b / 2);     long long res = 1;     res = c * c;     if (res >= 1000000007) res %= 1000000007;     if (b % 2) res *= a;     if (res >= 1000000007) res %= 1000000007;     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n;     cin >> n;     string s;     cin >> s;     map<char, long long> count;     for (long long i = 0; i < n; i++) {       count[s[i]]++;     }     vector<long long> v;     for (auto &i : count) {       v.push_back(i.second);     }     long long ans = 0;     sort(v.begin(), v.end(), greater<long long>());     for (long long i = 0; i < v.size(); i++) {       if (v[i] == v[0]) ans++;     }     cout << power(ans, n);   }


#include <bits/stdc++.h>   using namespace std;   long long N, K, dp[501][501];   long long p2[501];   void pre() {     long long i;     p2[0] = 1;     for (i = 1; i < 501; ++i) p2[i] = (p2[i - 1] * 2) % 1000000007;   }   long long rec(long long maxinc, long long cur) {     if (maxinc > K) return 0;     if (cur > N) return (maxinc == K);     long long last = cur - 1;     long long &ret = dp[maxinc][cur];     if (ret != -1) return ret;     long long temp, mul = 1;     ret = 0;     long long cc = cur;     for (; cur <= N; ++cur) {       mul = (mul * p2[last]) % 1000000007;       temp = (rec(maxinc + 1, cur + 1) * (p2[cur - last] - 1)) % 1000000007;       ret = (ret + (temp * mul) % 1000000007) % 1000000007;     }     if (maxinc == K) ret = (ret + mul) % 1000000007;     return ret;   }   int main() {     pre();     while (scanf( %I64d %I64d , &N, &K) != EOF) {       memset(dp, -1, sizeof(dp));       printf( %I64d n , rec(0, 1));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int _Int() {     int x;     scanf( %d , &x);     return x;   }   long long int bigMod(long long int A, long long int P, int M) {     long long int R = 1;     for (A %= M; P; P >>= 1) {       if (P & 1) R = (R * A) % M;       A = (A * A) % M;     }     return R;   }   long long int bigMul(long long int A, long long int B, long long int M) {     long long int R = 0;     for (A %= M; B; B >>= 1) {       if (B & 1) R = (R + A) % M;       A = (A + A) % M;     }     return R;   }   long long int negMod(long long int A, long long int B) {     return (((A % B) + B) % B);   }   long long int invMod(long long int A, long long int M) {     return bigMod(A, M - 2, M);   }   unsigned long long int _pow(unsigned long long int A, int P) {     unsigned long long int R = 1;     for (; P; P >>= 1) {       if (P & 1) R = (R * A);       A = (A * A);     }     return R;   }   template <class T>   T GCD(T x, T y) {     while (x) x ^= y ^= x ^= y %= x;     return y;   }   template <class T>   bool inRng(T u, T v, T x) {     return u <= x && x <= v;   }   const int M = 300000 + 7;   int n, edges, source;   vector<int> graph[M];   struct edge {     int type;     int u, v;     void scan(int i) {       scanf( %d%d%d , &type, &u, &v);       graph[u].push_back(i);       if (type == 2) graph[v].push_back(i);     }   } eee[M];   char ans[M];   bool vis[M];   void bfs() {     queue<int> qe;     qe.push(source);     vis[source] = 1;     while (qe.size()) {       int u = qe.front();       qe.pop();       for (int i = graph[u].size() - 1; i >= 0; i--) {         int first = graph[u][i], v;         if (eee[first].u == u)           v = eee[first].v;         else           v = eee[first].u;         if (eee[first].type == 2 && !ans[first]) {           if (eee[first].u == u)             ans[first] =  + ;           else             ans[first] =  - ;         }         if (!vis[v]) qe.push(v);         vis[v] = 1;       }     }     for (int i = 0; i < edges; i++) {       if (eee[i].type == 2 && !ans[i]) ans[i] =  + ;     }   }   void bfs_again() {     queue<int> qe;     qe.push(source);     vis[source] = 1;     while (qe.size()) {       int u = qe.front();       qe.pop();       for (int i = graph[u].size() - 1; i >= 0; i--) {         int first = graph[u][i], v;         if (eee[first].u == u)           v = eee[first].v;         else           v = eee[first].u;         if (eee[first].type == 2 && !ans[first]) {           if (eee[first].u == u)             ans[first] =  - ;           else             ans[first] =  + ;         }         if (eee[first].type == 1 && !vis[v]) {           qe.push(v);           vis[v] = 1;         }       }     }     for (int i = 0; i < edges; i++) {       if (eee[i].type == 2 && !ans[i]) ans[i] =  + ;     }   }   void Main() {     scanf( %d%d%d , &n, &edges, &source);     for (int i = 0; i < edges; i++) eee[i].scan(i);     bfs();     int cnt = 0;     for (int i = 1; i <= n; i++) cnt += vis[i];     printf( %d n , cnt);     for (int i = 0; i < edges; i++) {       if (eee[i].type == 2) printf( %c , ans[i]);     }     putchar(10);     memset(vis, 0, sizeof vis);     memset(ans, 0, sizeof ans);     bfs_again();     cnt = 0;     for (int i = 1; i <= n; i++) cnt += vis[i];     printf( %d n , cnt);     for (int i = 0; i < edges; i++) {       if (eee[i].type == 2) printf( %c , ans[i]);     }     putchar(10);   }   int main() {     Main();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5005;   template <typename T>   inline void read(T &AKNOI) {     T x = 0, flag = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) flag = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     AKNOI = flag * x;   }   namespace ModCalculator {   const int MOD = 998244353;   inline void Inc(int &x, int y) {     x += y;     if (x >= MOD) x -= MOD;   }   inline void Dec(int &x, int y) {     x -= y;     if (x < 0) x += MOD;   }   inline int Add(int x, int y) {     Inc(x, y);     return x;   }   inline int Sub(int x, int y) {     Dec(x, y);     return x;   }   inline int Mul(int x, int y) { return 1LL * x * y % MOD; }   inline int Ksm(int x, int k) {     int ret = 1;     for (; k; k >>= 1) {       if (k & 1) ret = Mul(ret, x);       x = Mul(x, x);     }     return ret;   }   inline int Inv(int x) { return Ksm(x, MOD - 2); }   }  // namespace ModCalculator   using namespace ModCalculator;   int n, fac[N], finv[N], E[N][N];   void init() {     read(n);     fac[0] = 1;     for (int i = 1; i <= n; ++i) {       fac[i] = Mul(fac[i - 1], i);     }     finv[n] = Inv(fac[n]);     for (int i = n; i >= 1; --i) {       finv[i - 1] = Mul(finv[i], i);     }     E[0][0] = 1;     for (int i = 1; i <= n; ++i) {       E[i][0] = 1;       for (int j = 1; j <= i; ++j) {         E[i][j] = Add(Mul(E[i - 1][j], j + 1), Mul(E[i - 1][j - 1], i - j));       }     }   }   void solve() {     for (int i = 1; i <= n; ++i) {       int ans = 0;       for (int j = i; j <= n; ++j) {         Inc(ans, Mul(E[j][i - 1], Mul(fac[n], finv[j])));       }       printf( %d  , ans);     }   }   int main() {     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = 3.14159265389793238462643383279502884197169;   const long long MOD = 1e9 + 7;   const long long MOD1 = 998244353;   const long long MODo = 1e9;   const int MAXN = 2e5 + 2;   const int oo = 1e9;   const long long oo15 = 1e15;   const long long oo18 = 1e18 + 3;   const long long oooo = 9223372036854775807;   void solve() {     int n, dem1 = 0, dem0 = 0;     cin >> n;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       if (x == 1)         dem1++;       else if (x == 0)         dem0++;     }     long long res = 1;     for (int i = 1; i <= dem0; i++) res *= 2;     cout << dem1 * res <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int testcase = 1;     cin >> testcase;     while (testcase--) solve();   }


#include <bits/stdc++.h>   using namespace std;   bool sortbysec(const pair<long long, pair<long long, long long> > &a,                  const pair<long long, pair<long long, long long> > &b) {     return (a.second.first < b.second.first);   }   bool sortbyth(const pair<long long, pair<long long, long long> > &a,                 const pair<long long, pair<long long, long long> > &b) {     return (a.second.second < b.second.second);   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   long long min(long long a, long long b) { return a < b ? a : b; }   long long max(long long a, long long b) { return a > b ? a : b; }   long long max3(long long a, long long b, long long c) {     return max(max(a, b), c);   }   long long max4(long long a, long long b, long long c, long long d) {     return max(max(a, b), max(c, d));   }   long long min3(long long a, long long b, long long c) {     return min(min(a, b), c);   }   long long min4(long long a, long long b, long long c, long long d) {     return min(min(a, b), min(c, d));   }   long long po(long long x, long long n, long long mod = 1000000007) {     if (n == 0) {       return 1;     }     long long m = po(x, n / 2, mod);     m *= m;     m %= mod;     if (n % 2) {       m *= x;       m %= mod;     }     return m;   }   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(false);     cin.tie(NULL);     clock_t startTime = clock();     cout << fixed << setprecision(12);     long long n;     cin >> n;     vector<long long> v(n);     for (long long i = 0; i < n; i++) cin >> v[i];     set<long long> ans, s;     for (long long i = 0; i < n; i++) {       set<long long> temp;       temp.insert(v[i]);       auto it = s.begin();       while (it != s.end()) {         temp.insert(*it | v[i]);         it++;       }       ans.insert(temp.begin(), temp.end());       s = temp;     }     cout << ans.size() <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   bool sortcol(const vector<int>& v1, const vector<int>& v2) {     return v1[0] < v2[0];   }   int main() {     int n;     cin >> n;     vector<vector<long long> > yr;     long long max = 0, count = 0, year = 0;     for (int a = 0; a < n; a++) {       long long x, y;       cin >> x >> y;       yr.push_back({x, 1});       yr.push_back({y, -1});     }     sort(yr.begin(), yr.end());     for (int a = 0; a < 2 * n; a++) {       count += yr[a][1];       if (yr[a][1] == 1 && count > max) {         max = count;         year = yr[a][0];       }     }     cout << year <<     << max << endl;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = 2 * acos(0);   int rev(int num, int lg_n) {     int res = 0;     for (int i = 0; i < lg_n; ++i)       if (num & (1 << i)) res |= 1 << (lg_n - 1 - i);     return res;   }   void fft(vector<complex<double>>& a, bool invert) {     int n = (int)a.size();     int lg_n = 0;     while ((1 << lg_n) < n) ++lg_n;     for (int i = 0; i < n; ++i)       if (i < rev(i, lg_n)) swap(a[i], a[rev(i, lg_n)]);     for (int len = 2; len <= n; len <<= 1) {       double ang = 2 * PI / len * (invert ? -1 : 1);       complex<double> wlen(cos(ang), sin(ang));       for (int i = 0; i < n; i += len) {         complex<double> w(1);         for (int j = 0; j < len / 2; ++j) {           complex<double> u = a[i + j], v = a[i + j + len / 2] * w;           a[i + j] = u + v;           a[i + j + len / 2] = u - v;           w *= wlen;         }       }     }     if (invert)       for (int i = 0; i < n; ++i) a[i] /= n;   }   vector<int> mult(vector<int> a, vector<int> b) {     int logn = 0;     while ((1 << logn) < max(a.size(), b.size())) logn++;     while (a.size() < (1 << logn)) a.push_back(0);     while (b.size() < (1 << logn)) b.push_back(0);     vector<complex<double>> A((1 << logn)), B((1 << logn));     for (int i = 0; i < (1 << logn); ++i) {       A[i] = a[i];       B[i] = b[i];     }     fft(A, 0);     fft(B, 0);     for (int i = 0; i < (1 << logn); ++i) {       A[i] *= B[i];     }     fft(A, 1);     vector<int> ans((1 << logn));     for (int i = 0; i < (1 << logn); ++i) {       ans[i] = (int)(A[i].real() + 0.5);     }     return ans;   }   void dfs(int v, vector<int>& used, vector<vector<int>>& g) {     used[v] = 1;     for (auto u : g[v]) {       if (!used[u]) {         dfs(u, used, g);       }     }   }   void solve() {     string s, t;     cin >> s >> t;     int n = s.size(), m = t.size();     int N =  f  -  a  + 1;     vector<vector<vector<int>>> G(n - m + 1, vector<vector<int>>(N));     for (char c1 =  a ; c1 <=  f ; ++c1) {       for (int c2 =  a ; c2 <=  f ; ++c2) {         vector<int> a(n), b(m);         for (int i = 0; i < n; ++i) {           a[i] = (c1 == s[i]);         }         for (int j = 0; j < m; ++j) {           b[j] = (c2 == t[j]);         }         reverse(b.begin(), b.end());         vector<int> res = mult(a, b);         for (int k = m - 1; k < m - 1 + n - m + 1; ++k) {           if (res[k]) {             G[k - m + 1][c1 -  a ].push_back(c2 -  a );             G[k - m + 1][c2 -  a ].push_back(c1 -  a );           }         }       }     }     for (int p = 0; p < n - m + 1; ++p) {       vector<int> used(N);       int comp = 0;       for (int i = 0; i < N; ++i) {         if (!used[i]) {           dfs(i, used, G[p]);           comp++;         }       }       cout << N - comp <<    ;     }   }   int32_t main() {     std::ios_base::sync_with_stdio(0);     std::cin.tie(0);     std::cout.tie(0);     int tt = 1;     while (tt--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   string s1, s2;   int ar[200001];   int n;   int n2;   bool possible(int check) {     int posi[200001] = {0};     for (int i = 0; i < check + 1; i++) {       posi[ar[i]]++;     }     int start1 = 0;     int start2 = 0;     while (start1 < n && start2 < n2) {       if (posi[start1]) {         start1++;         continue;       }       if (s1[start1] == s2[start2]) {         start1++;         start2++;       } else         start1++;     }     if (start2 == n2) {       return true;     }     return false;   }   int main() {     cin >> s1 >> s2;     n = s1.length();     n2 = s2.length();     for (int i = 0; i < n; i++) {       scanf( %d , &ar[i]);       ar[i]--;     }     int start = 0;     int end = n;     int ans = -1;     while (start < end) {       int mid = (start + end) / 2;       if (possible(mid)) {         ans = mid;         start = mid + 1;       } else {         end = mid;       }     }     cout << ans + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void fmax(T &a, T b) {     if (a < b) a = b;   }   template <class T>   void fmin(T &a, T b) {     if (a > b) a = b;   }   const int N = 1e5 * 3 + 10;   int a[N][3], c[N], n, m, w[N];   queue<int> Q;   int main() {     cin >> n >> m;     for (int i = (int)1; i <= (int)n; i++) w[i] = -1;     for (int i = 0; i < (int)m; i++) {       int x, y;       cin >> x >> y;       a[x][c[x]++] = y;       a[y][c[y]++] = x;     }     for (int z = (int)1; z <= (int)n; z++)       if (-1 == w[z]) {         Q.push(z);         int cnt = 0;         while (!Q.empty()) {           cnt++;           if (cnt > n * 20) {             cout << -1 << endl;             return 0;           }           int i = Q.front();           Q.pop();           int t[2] = {0, 0};           for (int j = 0; j < (int)c[i]; j++)             if (-1 != w[a[i][j]]) t[w[a[i][j]]]++;           if (w[i] == (t[0] > t[1])) continue;           w[i] = (t[0] > t[1]);           for (int j = 0; j < (int)c[i]; j++) Q.push(a[i][j]);         }       }     for (int i = (int)1; i <= (int)n; i++) cout << w[i];     cout << endl;   }


#include <iostream>   #include <vector>   #include  <numeric>   #include<cstdlib>   #include <algorithm>    // std::random_shuffle   #define MODD 998244353   #define ll long long   using namespace std;      ll modpow(ll x, ll y) {       ll xs = x;       ll answer = 1;       while(y) {           if (y&1) {               answer = (answer * xs) % MODD;           }           y >>= 1;           xs = (xs * xs) % MODD ;       }       return answer;   }         ll D[22][55555];      char buf[58000000];      int pos;   ll readint() {       while(buf[pos]< 0  || buf[pos]> 9 )           pos++;       ll m=0;       while(buf[pos]>= 0  && buf[pos]<= 9 ) {           m=m*10+buf[pos]- 0 ;           pos++;       }       return m;   }         int main() {       fread(buf, 1, 51000100, stdin);       pos=0;       int n,m;       n=readint();       m=readint();       for(int i=1;i<=n;i++) {           for(int j=1;j<=m;j++) {               D[i][j]=readint();           }       }          ll fact = 1;       for(ll i=1;i<=n;i++) {           fact = (fact*i)%MODD;       }          ll factinv = modpow(fact, MODD-2);          ll counts[22];       ll ans = 0;       for(int j=1;j<=m;j++) {           for(int step=1;step<=n;step++) {               // number of cities with distance >= step+1               counts[step]=0;               for(int i=1;i<=n;i++) {                   counts[step]+=(D[i][j]>=step+1);               }           }           counts[n+1]=0;           ll prod = 1;           for(int step=n;step>=1;step--) {               prod = (prod * (counts[step]-(n-step)))%MODD;           }           prod = (prod * factinv)%MODD;              ll prob_selected = (1-prod+MODD)%MODD;           ans += prob_selected;           ans %= MODD;       }          ans = (ans + MODD)%MODD;          cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, a, b, sum = 1;     cin >> n >> a >> b;     if (n == 1) {       puts( 1 );       return 0;     }     if (b == 0 && a == n - 1) {       puts( -1 );       return 0;     }     bool fl = (b == 0);     int now = 1;     vector<long long> ans(n, 1);     while (now < ans.size() && b > 0) {       ans[now] = sum + 1;       sum += ans[now];       b--;       now++;     }     while (now < ans.size() && a > 0) {       ans[now] = ans[now - 1] + 1;       a--;       now++;     }     if (fl && ans.size() >= 2) {       sum = ans.back();       ans.insert(ans.begin(), sum);       ans.pop_back();     }     for (int i = 0; i < ans.size(); i++) {       if (ans[i] > 50000) {         puts( -1 );         return 0;       }     }     for (int i = 0; i < ans.size(); i++) {       printf( %lld  , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 1;   const long long LINF = 1e18 + 1;   int n;   int X1_1 = 1;   int Y1_1 = 1;   int X2_1 = 2;   int Y2_1 = 2;   int X1_2 = 4;   int Y1_2 = 4;   int X2_2 = 5;   int Y2_2 = 5;   int ask(int x1, int y1, int x2, int y2) {     cout <<  ?   << x1 <<     << y1 <<     << x2 <<     << y2 << endl;     int res;     cin >> res;     return res;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     int lx, rx, ly, ry;     {       int left = 0;       int right = n;       while (right - left > 1) {         int mid = (left + right) / 2;         if (ask(1, 1, mid, n) == 2) {           right = mid;         } else {           left = mid;         }       }       rx = right;     }     {       int left = 1;       int right = n + 1;       while (right - left > 1) {         int mid = (left + right) / 2;         if (ask(mid, 1, n, n) == 2) {           left = mid;         } else {           right = mid;         }       }       lx = left;     }     {       int left = 0;       int right = n;       while (right - left > 1) {         int mid = (left + right) / 2;         if (ask(1, 1, n, mid) == 2) {           right = mid;         } else {           left = mid;         }       }       ry = right;     }     {       int left = 1;       int right = n + 1;       while (right - left > 1) {         int mid = (left + right) / 2;         if (ask(1, mid, n, n) == 2) {           left = mid;         } else {           right = mid;         }       }       ly = left;     }     int bord_x;     {       int left = lx - 1;       int right = rx;       while (right - left > 1) {         int mid = (left + right) / 2;         if (ask(lx, ly, mid, ry) == 1) {           right = mid;         } else {           left = mid;         }       }       bord_x = right;     }     int bord_y;     {       int left = ly - 1;       int right = ry;       while (right - left > 1) {         int mid = (left + right) / 2;         if (ask(lx, ly, rx, mid) == 1) {           right = mid;         } else {           left = mid;         }       }       bord_y = right;     }     if (bord_x == rx || ask(bord_x + 1, ly, rx, ry) != 1) {       if (bord_y == ry || ask(lx, bord_y + 1, rx, ry) != 1) {         assert(false);       }       int y1_1 = ly;       int y2_1 = bord_y;       int x1_1, x2_1;       {         int left = lx - 1;         int right = rx;         while (right - left > 1) {           int mid = (left + right) / 2;           if (ask(lx, y1_1, mid, y2_1) == 1) {             right = mid;           } else {             left = mid;           }         }         x2_1 = right;       }       {         int left = lx;         int right = rx + 1;         while (right - left > 1) {           int mid = (left + right) / 2;           if (ask(mid, y1_1, rx, y2_1) == 1) {             left = mid;           } else {             right = mid;           }         }         x1_1 = left;       }       int y1_2;       int y2_2 = ry;       int x1_2, x2_2;       {         int left = bord_y + 1;         int right = ry + 1;         while (right - left > 1) {           int mid = (left + right) / 2;           if (ask(lx, mid, rx, ry) == 1) {             left = mid;           } else {             right = mid;           }         }         y1_2 = left;       }       {         int left = lx - 1;         int right = rx;         while (right - left > 1) {           int mid = (left + right) / 2;           if (ask(lx, y1_2, mid, y2_2) == 1) {             right = mid;           } else {             left = mid;           }         }         x2_2 = right;       }       {         int left = lx;         int right = rx + 1;         while (right - left > 1) {           int mid = (left + right) / 2;           if (ask(mid, y1_2, rx, y2_2) == 1) {             left = mid;           } else {             right = mid;           }         }         x1_2 = left;       }       cout <<  !   << x1_1 <<     << y1_1 <<     << x2_1 <<     << y2_1 <<                << x1_2 <<     << y1_2 <<     << x2_2 <<     << y2_2 << endl;     } else {       int x1_1 = lx;       int x2_1 = bord_x;       int y1_1, y2_1;       {         int left = ly - 1;         int right = ry;         while (right - left > 1) {           int mid = (left + right) / 2;           if (ask(x1_1, ly, x2_1, mid) == 1) {             right = mid;           } else {             left = mid;           }         }         y2_1 = right;       }       {         int left = ly;         int right = ry + 1;         while (right - left > 1) {           int mid = (left + right) / 2;           if (ask(x1_1, mid, x2_1, ry) == 1) {             left = mid;           } else {             right = mid;           }         }         y1_1 = left;       }       int x1_2;       int x2_2 = rx;       int y1_2, y2_2;       {         int left = bord_x + 1;         int right = rx + 1;         while (right - left > 1) {           int mid = (left + right) / 2;           if (ask(mid, ly, rx, ry) == 1) {             left = mid;           } else {             right = mid;           }         }         x1_2 = left;       }       {         int left = ly - 1;         int right = ry;         while (right - left > 1) {           int mid = (left + right) / 2;           if (ask(x1_2, ly, x2_2, mid) == 1) {             right = mid;           } else {             left = mid;           }         }         y2_2 = right;       }       {         int left = ly;         int right = ry + 1;         while (right - left > 1) {           int mid = (left + right) / 2;           if (ask(x1_2, mid, x2_2, ry) == 1) {             left = mid;           } else {             right = mid;           }         }         y1_2 = left;       }       cout <<  !   << x1_1 <<     << y1_1 <<     << x2_1 <<     << y2_1 <<                << x1_2 <<     << y1_2 <<     << x2_2 <<     << y2_2 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     char ch = getchar();     long long nega = 1;     while (!isdigit(ch)) {       if (ch ==  - ) nega = -1;       ch = getchar();     }     long long ans = 0;     while (isdigit(ch)) {       ans = ans * 10 + ch - 48;       ch = getchar();     }     if (nega == -1) return -ans;     return ans;   }   inline long long min(long long x, long long y, long long z) {     return min(x, min(y, z));   }   inline long long max(long long x, long long y, long long z) {     return max(x, max(y, z));   }   inline long long add(long long x, long long y) {     return x + y >= 998244353 ? x + y - 998244353 : x + y;   }   inline long long add(long long x, long long y, long long z) {     return add(add(x, y), z);   }   inline long long sub(long long x, long long y) {     return x - y < 0 ? x - y + 998244353 : x - y;   }   inline long long mul(long long x, long long y) {     return 1LL * x * y % 998244353;   }   inline long long mul(long long x, long long y, long long z) {     return mul(mul(x, y), z);   }   signed main() {     long long a, b, c, d;     cin >> a >> b >> c >> d;     long long ans = 0;     for (long long i = c; i <= d; i++) {       long long m = i - b + 1;       if (m >= a && c - b + 1 > 0) {         long long l = max(a, i - c);         long long d = c - i + l, e = c - i + min(b, m);         if (e >= d) ans += (e + d) * (e - d + 1) / 2;       }       long long L = b - max(m, a - 1);       if (c - b + 1 >= 0 && b > m) ans += L * (c - b + 1);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long N;   int sol;   int main() {     scanf( %I64d , &N);     while (N % 3 == 0) N /= 3;     printf( %I64d n , (N + 2) / 3);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using i64 = long long;   constexpr int MAX = 400010;   constexpr int MOD = 1000000007;   i64 fac[MAX], inv[MAX];   int l[MAX], r[MAX], x[MAX], cnt[MAX];   i64 ModPow(i64 a, i64 b, i64 p = MOD) {     i64 ret = 1;     for (a %= p; b; b >>= 1) {       if (b & 1) ret = ret * a % p;       a = a * a % p;     }     return ret;   }   inline i64 Inv(i64 x, i64 p = MOD) { return ModPow(x, p - 2, p); }   void Init() {     fac[0] = 1;     inv[0] = 1;     for (int i = 1; i < MAX; ++i) {       fac[i] = fac[i - 1] * i % MOD;       inv[i] = Inv(fac[i]);     }   }   inline i64 C(int n, int m) {     return n < m ? 0 : fac[n] * inv[m] % MOD * inv[n - m] % MOD;   }   i64 Solve(int n, int k) {     int m = 0;     for (int i = 0; i < n; ++i) {       l[i] <<= 1;       r[i] = ((r[i] << 1) | 1);       x[m++] = l[i];       x[m++] = r[i];     }     sort(x, x + m);     int a = x[0], b = x[m - 1];     x[m++] = a - 1;     x[m++] = b + 1;     sort(x, x + m);     m = unique(x, x + m) - x;     memset(cnt, 0, sizeof(cnt));     for (int i = 0; i < n; ++i) {       int u = lower_bound(x, x + m, l[i]) - x;       int v = lower_bound(x, x + m, r[i]) - x;       ++cnt[u];       --cnt[v];     }     i64 ret = 0;     for (int i = 1; i < m - 1; ++i) {       cnt[i] += cnt[i - 1];       if (cnt[i] >= k) {         int u = ((x[i + 1] + 1) >> 1), v = ((x[i] + 1) >> 1);         int d = u - v;         i64 cur = C(cnt[i], k) * d % MOD;         ret = (ret + cur) % MOD;       }     }     return ret;   }   int main() {     Init();     int n, k;     while (~scanf( %d%d , &n, &k)) {       for (int i = 0; i < n; ++i) {         scanf( %d%d , &l[i], &r[i]);       }       printf( %d n , (int)Solve(n, k));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[2555], t[2555];   int n, ans[2555 * 3], o;   int main() {     scanf( %d , &n);     scanf( %s , s);     scanf( %s , t);     o = 0;     for (int i = n - 1; i >= 0; i--) {       int j = n - 1 - i;       while (j < n && s[j] != t[i]) j++;       if (j == n) {         printf( -1 n );         return 0;       }       ans[o++] = n;       ans[o++] = j;       ans[o++] = 1;       reverse(s + j, s + n);       reverse(s, s + n);       reverse(s + 1, s + n);     }     cout << o << endl;     for (int i = 0; i < o; i++) printf( %d  , ans[i]);     printf(  n );   }


#include <bits/stdc++.h>   using namespace std;   vector<int> a;   void init(int x) {     vector<int> b;     while (x > 0) {       int tmp = x % 10;       b.push_back(tmp);       x /= 10;     }     for (int i = b.size() - 1; i >= 0; i--) {       a.push_back(b[i]);     }   }   int main() {     for (int i = 0; i <= 1000; ++i) init(i);     int n;     while (~scanf( %d , &n)) cout << a[n - 1] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int _seive_size;   bitset<1000000> bs;   vector<long long int> primes;   void seive(long long int upperbound) {     _seive_size = upperbound + 1;     bs.set();     bs[0] = bs[1] = 0;     for (long long int i = 2; i <= _seive_size; i++) {       if (bs[i]) {         for (long long int j = i * i; j <= _seive_size; j += i) {           bs[j] = 0;         }         primes.push_back((int)i);       }     }   }   bool isTrime(long long int N) {     long long int temp;     long long int start = 0;     long long int end = primes.size() - 1;     while (start <= end) {       long long int mid = start + (end - start) / 2;       temp = primes[mid] * primes[mid];       if (temp == N) {         return true;       } else if (temp < N) {         start = mid + 1;       } else {         end = mid - 1;       }     }     return false;   }   int main() {     seive(1000000);     long long int n;     cin >> n;     long long int arr[n];     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     for (int i = 0; i < n; i++) {       if (isTrime(arr[i])) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = (1LL << 22) + 2;   long long n, m;   long long a[N];   bool vis[2][N];   void dfs(long long k, long long cur) {     if (vis[cur][k]) return;     vis[cur][k] = 1;     if (cur == 1) return dfs(k, cur ^ 1);     for (long long i = 0; i < n; i++) {       if ((k & (1LL << i)) == 0) dfs(k | (1LL << i), 0);     }     dfs(((1LL << n) - 1) ^ k, 1);   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> m;     for (long long i = 0; i < N; i++) vis[1][i] = 1;     for (long long i = 1; i <= m; i++) {       cin >> a[i];       vis[1][a[i]] = 0;     }     long long ans = 0;     for (long long i = 1; i <= m; i++) {       if (vis[1][a[i]]) continue;       ans++;       dfs(a[i], 1);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 998244353;   const int MAXN = 1000010;   int get() {     int x;     bool f = 0;     char c;     while (!isdigit(c = getchar()))       if (c ==  - ) f = 1;     x = c - 48;     while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c - 48;     if (f)       return -x;     else       return x;   }   int N, A[MAXN];   int add(int x, int v) {     x += v;     if (x >= MOD) x -= MOD;     return x;   }   int main() {     N = get();     for (int i = 1; i <= N; ++i) A[i] = get();     int F = 0, G = 0, v = 0;     for (int i = 1; i <= N; ++i) {       v = add(1LL * v * 2 % MOD, A[i]);       F = add(G, v);       G = add(G, F);     }     cout << F << endl;     return 0;   }


#include <iostream>   #include <algorithm>   #include <string.h>   #include <queue>   #include <vector>      #define INF 987654321      using namespace std;      int main() {       int t;       scanf( %d ,&t);       while(t--){           int a,b,flag=0;           scanf( %d %d ,&a,&b);           if(a==b)printf( YES n );           else if(a>b)printf( NO n );           else{               //여기가 실제로 계산해야되는 부분.               //2^n꼴은 무조건 1->2->4->8->16으로만 이동이 가능함.               // 3=11으로서, 3+1, 3+10, 3+11 3개가 가능함. 3->4,5,6다 가능하다.               // 7=111으로서, 7->8,9,10,11,12,13,14다 가능하다.               // 결국 우리가 그럼 찾아야 되는 값은 5,6과 같은 중간에 비는값이 있는 값들.                  // 5 = 101으로, 5+1, 5+100, 5+101.  5->6,9,10로 이동이 가능.               // 6 = 110으로, 6+10, 6+100, 6+110. 6->8,10,12로 이동이 가능.               // 결론. 3은 11,13,14를 못감. 8 =1000, 9=1001, 10=1010, 11=1011               // 와 그럼 3=11 숫자가 2개다보니, 11과 같이 3개가 필요한 곳은 못간다.               for(int i=0;i<30;i++)               {                 if((a>>i)&1)flag++;                 if((b>>i)&1)flag--;                 if(flag<0)                 {                     printf( NO n );                     break;                 }               }               if(flag>=0)               {                 printf( YES n );               }           }       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long f[1200];   long long mod = 1000000007;   int arr[1100];   long long pw(long long x, long long sum) {     long long res = 1, power = sum, val = x;     while (power) {       if (power & 1) {         res *= val;         res %= mod;       }       val *= val;       val %= mod;       power >>= 1ll;     }     return res;   }   int main() {     f[0] = 1;     for (int i = 1; i <= 1000; i++) f[i] = (f[i - 1] * i) % mod;     int n, m;     cin >> n >> m;     long long ans = f[n - m];     vector<int> vec;     for (int i = 0; i < m; i++) {       scanf( %d , &arr[i]);     }     sort(arr, arr + m);     if (!m)       vec.push_back(n);     else {       vec.push_back(arr[0] - 1);       for (int i = 1; i < m; i++) vec.push_back(arr[i] - arr[i - 1] - 1);       vec.push_back(n - arr[m - 1]);     }     long long sum = 1;     for (int i = 0; i < vec.size(); i++) sum = (sum * f[vec[i]]) % mod;     ans = (ans * pw(sum, mod - 2)) % mod;     for (int i = 1; i < vec.size() - 1; i++)       if (vec[i] - 1 && vec[i]) ans = (ans * pw(2, vec[i] - 1)) % mod;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 10;   const long long int INFLL = 1e18 + 10;   const long double EPS = 1e-8;   const long double EPSLD = 1e-14;   const long long int MOD = 1000000007;   template <class T>   T &chmin(T &a, const T &b) {     return a = min(a, b);   }   template <class T>   T &chmax(T &a, const T &b) {     return a = max(a, b);   }   struct Unionfind {     vector<int> size;     vector<int> par;     Unionfind() {}     Unionfind(int n) : size(n, 1), par(n) {       for (int i = (0); i < (int)(n); i++) par[i] = i;     }     void init(int n) {       size.resize(n);       par.resize(n);       for (int i = (0); i < (int)(n); i++) {         size[i] = 1;         par[i] = i;       }     }     int find(int x) {       if (par[x] == x) return x;       return par[x] = find(par[x]);     }     bool unite(int x, int y) {       x = find(x);       y = find(y);       if (x == y) return false;       if (size[y] < size[x]) swap(x, y);       par[x] = y;       size[y] += size[x];       return true;     }     bool same(int x, int y) { return find(x) == find(y); }   };   vector<long long int> compress(vector<long long int> a) {     vector<long long int> ord = a, res;     sort((ord).begin(), (ord).end());     ord.erase(unique((ord).begin(), (ord).end()), ord.end());     for (auto &w : a)       res.emplace_back(lower_bound((ord).begin(), (ord).end(), w) - ord.begin());     return res;   }   Unionfind uf;   struct Segtree {     vector<long long int> cnt, an;     int size;     void init(int n) {       size = 1;       while (size < n) size *= 2;       cnt = vector<long long int>(size * 2 - 1, 0);       an = vector<long long int>(size * 2 - 1, -2);     }     void add(int k, int id) {       k += size - 1;       cnt[k] = 1;       an[k] = id;       while (k > 0) {         k = (k - 1) / 2;         cnt[k] = cnt[k * 2 + 1] + cnt[k * 2 + 2];         if (an[k] == -2)           an[k] = id;         else           an[k] = -1;       }     }     void del(int k) {       k += size - 1;       cnt[k] = 0;       an[k] = -2;       while (k > 0) {         k = (k - 1) / 2;         cnt[k] = cnt[k * 2 + 1] + cnt[k * 2 + 2];         an[k] = -1;         if (an[k * 2 + 1] == -2 && an[k * 2 + 2] == -2) an[k] = -2;       }     }     long long int query(int a, int b, int id, int k, int l, int r) {       if (an[k] == -2) return 0;       if (a <= l && r <= b) {         if (an[k] != -1) {           if (uf.same(an[k], id)) return cnt[k];           uf.unite(an[k], id);           return cnt[k] - 1;         }         an[k] = id;       } else if (b <= l || r <= a) {         return 0;       }       long long int chl = query(a, b, id, k * 2 + 1, l, (l + r) / 2);       long long int chr = query(a, b, id, k * 2 + 2, (l + r) / 2, r);       return chl + chr;     }     long long int query(int a, int b, int id) {       return query(a, b, id, 0, 0, size);     }   };   const int MAX_N = 200010;   int n;   long long int x1[MAX_N], x2[MAX_N];   long long int z1[MAX_N], z2[MAX_N];   vector<long long int> ix, iy;   vector<long long int> xs, ys;   Segtree seg;   vector<pair<pair<long long int, long long int>,               pair<long long int, long long int> > >       qu;   long long int res[MAX_N];   long long int ans;   int main() {     scanf( %d , &n);     for (int i = (0); i < (int)(n); i++) {       scanf( %lld %lld %lld %lld , &x1[i], &z1[i], &x2[i], &z2[i]);       ix.emplace_back(x1[i]);       ix.emplace_back(x2[i]);       iy.emplace_back(z1[i]);       iy.emplace_back(z2[i]);     }     xs = compress(ix);     ys = compress(iy);     for (int i = (0); i < (int)(n); i++) {       if (xs[i * 2] == xs[i * 2 + 1])         qu.emplace_back(pair<long long int, long long int>(xs[i * 2], ys[i * 2]),                         pair<long long int, long long int>(ys[i * 2 + 1], i));       else {         qu.emplace_back(pair<long long int, long long int>(xs[i * 2], -1),                         pair<long long int, long long int>(ys[i * 2], i));         qu.emplace_back(pair<long long int, long long int>(xs[i * 2 + 1], INFLL),                         pair<long long int, long long int>(ys[i * 2], i));         res[i] = x2[i] - x1[i];       }     }     sort((qu).begin(), (qu).end());     uf.init(n);     seg.init(3 * n);     for (auto &q : qu) {       int id = q.second.second;       if (q.first.second == -1)         seg.add(q.second.first, id);       else if (q.first.second == INFLL)         seg.del(q.second.first);       else         res[id] =             z2[id] - z1[id] - seg.query(q.first.second, q.second.first + 1, id);     }     for (int i = (0); i < (int)(n); i++)       if (uf.find(i) != i) res[uf.find(i)] += res[i];     for (int i = (0); i < (int)(n); i++)       if (uf.find(i) == i) chmax(ans, res[i]);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   const int oo = 0x3f3f3f3f;   template <typename T>   inline bool chkmax(T &a, T b) {     return a < b ? a = b, true : false;   }   template <typename T>   inline bool chkmin(T &a, T b) {     return a > b ? a = b, true : false;   }   template <typename T>   T read(T &first) {     int f = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) f = -1;     for (first = 0; isdigit(ch); ch = getchar()) first = 10 * first + ch -  0 ;     return first *= f;   }   template <typename T>   void write(T first) {     if (first < 0) {       putchar( - );       first = -first;     }     static char s[20];     int top = 0;     for (; first; first /= 10) s[++top] = first % 10 +  0 ;     if (top == 0)       putchar( 0 );     else       while (top) putchar(s[top--]);   }   const int MAXN = 1e5 + 5, MAXK = 6;   struct Line {     int a, b, c;     int id;     bool on(const std::pair<double, double> &p) {       static double EPS = 1e-8;       return std::fabs(a * p.first + b * p.second + c) < EPS;     }   };   int N, K;   Line A[MAXN];   std::pair<int, int> ans[MAXN];   int anssize;   int st[MAXN];   inline bool intersection(const Line &a, const Line &b,                            std::pair<double, double> &p) {     long long d = (long long)a.b * b.a - (long long)b.b * a.a;     if (d == 0) return false;     p.first = (double)((long long)b.b * a.c - (long long)a.b * b.c) / d;     p.second = (double)((long long)b.c * a.a - (long long)a.c * b.a) / d;     return true;   }   bool dfs(int n, int k) {     if (n <= k) {       for (int i = 0; i < n; ++i) {         ans[anssize++] = std::make_pair(A[i].id + 1, -1);       }       return true;     }     if (k == 0) return false;     for (int i = n - k - 1; i < n; ++i) {       for (int j = i + 1; j < n; ++j) {         std::pair<double, double> p;         if (intersection(A[i], A[j], p) == false) continue;         ans[anssize++] = std::make_pair(A[i].id + 1, A[j].id + 1);         int m = n, top = 0;         for (int k = n - 1; k >= 0; --k) {           if (A[k].on(p)) {             st[++top] = k;             std::swap(A[k], A[--m]);           }         }         if (dfs(m, k - 1) == true) return true;         --anssize;         while (top > 0) {           std::swap(A[st[top--]], A[m++]);         }       }     }     return false;   }   void input() {     read(N);     read(K);     for (int i = 0; i < N; ++i) {       read(A[i].a);       read(A[i].b);       read(A[i].c);       A[i].id = i;     }   }   void solve() {     srand(time(NULL));     if (N >= 5 * K * K) {       int times = 100;       while (times-- && N > 0 && K > 0) {         int i = rand() % N;         int j = rand() % N;         std::pair<double, double> p;         if (i == j || intersection(A[i], A[j], p) == false) continue;         int top = 0;         for (int k = N - 1; k >= 0; --k) {           if (A[k].on(p)) st[++top] = k;         }         if (top > K) {           ans[anssize++] = std::make_pair(A[i].id + 1, A[j].id + 1);           for (int k = 1; k <= top; ++k) {             A[st[k]] = A[--N];           }           --K;           break;         }       }     }     bool haveAns = dfs(N, K);     if (haveAns) {       puts( YES );       write(anssize);       putchar(  n );       for (int i = 0; i < anssize; ++i) {         write(ans[i].first);         putchar(   );         write(ans[i].second);         putchar(  n );       }     } else       puts( NO );   }   int main() {     input();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e6 + 5;   const int inf = 1e9 + 5;   const double pi = acos(-1);   const int dx[] = {1, -1, 0, 0};   const int dy[] = {0, 0, 1, -1};   const int mod = 1e9 + 7;   int p[N], cnt[N];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     ;     string s;     cin >> s;     int n = (int)(s).size();     memset(p, 0, sizeof p);     for (int i = 1; i < n; i++) {       int prev = p[i - 1];       while (prev > 0 && s[prev] != s[i]) prev = p[prev - 1];       if (s[prev] == s[i]) prev++;       p[i] = prev;     }     if (p[n - 1] == 0) {       if (p[p[n - 1] - 1] == 0) {         cout <<  Just a legend ;       } else {         cout << s.substr(0, p[p[n - 1] - 1]);       }     } else {       int cnt = 0;       for (int i = 0; i < n; i++) {         if (p[i] == p[n - 1]) {           ++cnt;         }       }       if (cnt >= 2) {         cout << s.substr(0, p[n - 1]);       } else {         if (p[p[n - 1] - 1] == 0) {           cout <<  Just a legend ;         } else {           cout << s.substr(0, p[p[n - 1] - 1]);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s[10100];   string sep;   bool u[10100] = {false};   vector<string> str;   string make(pair<string, string> p) {     return ((p.first + sep + p.second) < (p.second + sep + p.first))                ? (p.first + sep + p.second)                : (p.second + sep + p.first);   }   int main() {     int n = 0, sumLeng = 0, len = 0;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> s[i];       sumLeng += s[i].length();     }     cin >> sep;     len = sumLeng / (n / 2);     sort(s, s + n);     for (int i = 0; i < n; ++i) {       if (u[i]) continue;       int leng = s[i].length();       for (int j = 0; j < n; ++j)         if (!u[j] && leng + s[j].length() == len && i != j) {           str.push_back(make(make_pair(s[i], s[j])));           u[i] = true;           u[j] = true;           break;         }     }     sort(str.begin(), str.end());     for (int i = 0; i < n / 2; ++i) cout << str[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   using minpq = priority_queue<T, vector<T>, greater<T>>;   int n, m, k;   int arr[70][70];   int dp[71][71][36][71];   int func(int s, int e, int cnd, int rem) {     if (cnd >= m / 2) {       return -1e9;     }     if (s == n) {       if (rem == 0) {         return 0;       }       return -1e9;     }     if (e == m) {       return -1e9;     }     if (dp[s][e][cnd][rem] != -1) {       return dp[s][e][cnd][rem];     }     int ans = -1e9;     int nc = (rem + arr[s][e]) % k;     ans =         max({func(s, e + 1, cnd, rem), arr[s][e] + func(s, e + 1, cnd + 1, nc)});     ans = max({ans, func(s + 1, 0, 0, rem), arr[s][e] + func(s + 1, 0, 0, nc)});     return dp[s][e][cnd][rem] = ans;   }   void solve() {     memset(dp, -1, sizeof(dp));     cin >> n >> m >> k;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> arr[i][j];       }     }     int ans = func(0, 0, 0, 0);     if (ans < 0) {       ans = 0;     }     cout << ans << endl;   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0, f = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       x = (x << 3) + (x << 1) + (c ^ 48);       c = getchar();     }     return f == -1 ? ~x + 1 : x;   }   long long n;   vector<long long> ve[11];   long long sum[11];   const long long N = 1e6, mod1 = 998244353, mod2 = 1e9 + 7;   long long cnt, tmp, ans;   long long m1(long long x) { return (x % mod1 + mod1) % mod1; }   void dfs1(long long x, long long v) {     if (v > n) return;     if (x > 6) return;     ++cnt;     ve[x].push_back(m1(cnt - v));     sum[x] += m1(cnt - v);     for (long long i = 0; i < 10; ++i) dfs1(x + 1, v * 10 + i);   }   void dfs2(long long x, long long v) {     if (v > n) return;     if (x >= 1) {       if (v * N * 10 > n && v * N + N - 1 <= n) {         for (long long i = 0, t = 1; i <= 6; ++i, t *= 10) {           long long val = m1(cnt - v * t);           long long pos =               lower_bound(ve[i].begin(), ve[i].end(), mod1 - val) - ve[i].begin();           (ans += val * ve[i].size() + sum[i] - mod1 * (ve[i].size() - pos)) %=               mod2;         }         cnt += tmp;         return;       }       ++cnt;       (ans += m1(cnt - v)) %= mod2;     }     for (long long i = (x == 0 ? 1 : 0); i <= 9; ++i) dfs2(x + 1, v * 10 + i);   }   signed main() {     n = read();     dfs1(0, 0);     tmp = cnt;     cnt = 0;     for (long long i = 0; i <= 6; ++i) sort(ve[i].begin(), ve[i].end());     dfs2(0, 0);     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 10;   const int inf = 2000000000;   int f(int first, int second, int t[N]) {     int a[N], b[N];     for (int i = 0; i < N; i++) {       a[i] = b[i] = t[i];     }     a[first]--;     b[second]--;     if (a[first] < 0 || b[second] < 0) {       return -1;     }     if ((first + second) != 10) {       return 0;     }     int cur = 1;     for (int i = 0; i < N; i++) {       cur += min(a[i], b[N - i - 1]);     }     return cur;   }   pair<string, string> get(int a[N], pair<pair<int, int>, int> code) {     int q[N], w[N];     for (int i = 0; i < N; i++) {       q[i] = w[i] = a[i];     }     string s, t;     for (int i = 0; i < code.second; i++) {       s +=  0 ;       t +=  0 ;       q[0]--;       w[0]--;     }     int first = code.first.first;     int second = code.first.second;     s += char(first +  0 );     t += char(second +  0 );     q[first]--;     w[second]--;     for (int i = 0; i < N; i++) {       int val = min(q[i], w[N - i - 1]);       for (int j = 0; j < val; j++) {         s += char(i +  0 );         t += char(N - i - 1 +  0 );       }       q[i] -= val;       w[N - i - 1] -= val;     }     for (int i = 0; i < N; i++) {       for (int j = 0; j < q[i]; j++) {         s += char(i +  0 );       }       for (int j = 0; j < w[i]; j++) {         t += char(i +  0 );       }     }     reverse((s).begin(), (s).end());     reverse((t).begin(), (t).end());     return {s, t};   }   int main() {     cin.tie(0);     ios_base::sync_with_stdio(0);     string s;     cin >> s;     int a[N];     memset(a, 0, sizeof(a));     int n = s.length();     for (int i = 0; i < n; i++) {       a[s[i] -  0 ]++;     }     int best = -1;     pair<pair<int, int>, int> code;     for (int i = 0; i <= a[0]; i++) {       int b[N];       for (int j = 0; j < N; j++) {         b[j] = a[j];       }       b[0] -= i;       for (int first = 0; first < N; first++) {         for (int second = 0; second < N; second++) {           int cur = f(first, second, b);           if (cur + i > best) {             best = cur + i;             code = {{first, second}, i};           }         }       }     }     pair<string, string> ans = get(a, code);     cout << ans.first <<   n  << ans.second;   }


#include <bits/stdc++.h>   using namespace std;   const string nameFile =  test ;   ifstream in(nameFile +  .in );   ofstream out(nameFile +  .out );   const int nmax = 4e3;   int n, nrGond;   char ch;   int a[nmax + 2][nmax + 2];   int dp[802][nmax + 2];   char buffer[10000];   inline int getCost(int i, int j) {     return (a[j][j] + a[i - 1][i - 1] - a[j][i - 1] - a[i - 1][j]) / 2;   }   void divideDp(int ll, int rr, int limLeftSrc, int limRightSrc, int layer) {     if (ll > rr) return;     int mij = (ll + rr) / 2;     pair<int, int> ob = {1e9, 0};     for (int i = limLeftSrc; i <= min(mij, limRightSrc); i++)       ob = min(ob, {getCost(i, mij) + dp[layer - 1][i - 1], i});     dp[layer][mij] = ob.first;     divideDp(ll, mij - 1, limLeftSrc, ob.second, layer);     divideDp(mij + 1, rr, ob.second, limRightSrc, layer);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     scanf( %d%d n , &n, &nrGond);     for (int i = 1; i <= n; i++) {       gets(buffer);       for (int j = 1; j <= n; j++)         a[i][j] = buffer[2 * (j - 1)] -  0  + a[i - 1][j] + a[i][j - 1] -                   a[i - 1][j - 1];     }     for (int i = 1; i <= n; i++) dp[0][i] = 1e9;     for (int i = 1; i <= nrGond; i++) divideDp(i, n, i, n, i);     cout << dp[nrGond][n];     return 0;   }