#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5, inf = 1e9 + 7;   long long n, k;   string s, t;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long i, j, a, b, ans = 0;     cin >> n >> k >> s >> t;     ans = n;     i = 0;     while (i < n && s[i] == t[i]) i++;     long long t1 = 0, t2 = 0, a1 = 1, a2 = 1;     k--;     if (i < n && k) {       ans += n - i;       k--;       i++;     }     for (; i < n; i++) {       if (s[i] ==  a ) {         ans += min(k, t1 + 1) * (n - i);         k -= min(k, t1 + 1);         t1 = t1 * 2 + 1;       } else {         ans += min(k, t1) * (n - i);         k -= min(k, t1);         t1 *= 2;       }       if (t[i] ==  a ) {         ans += min(k, t2) * (n - i);         k -= min(k, t2);         t2 *= 2;       } else {         ans += min(k, t2 + 1) * (n - i);         k -= min(k, t2 + 1);         t2 = t2 * 2 + 1;       }       if (!k) break;     }     cout << ans;   }


#include <bits/stdc++.h>   int main() { puts( Karen ); }


#include <bits/stdc++.h>   using namespace std;   long long n, m, k;   vector<vector<pair<int, int> > > v2;   long long costs[(int)1e5 + 5];   int parent[(int)1e5 + 5];   vector<int> answer;   void dikstra() {     priority_queue<pair<long long, int>, vector<pair<long long, int> >,                    greater<pair<long long, int> > >         pq;     pq.push({0, 1});     parent[1] = -1;     costs[1] = 0;     while (!pq.empty()) {       int node = pq.top().second;       long long cost = pq.top().first;       pq.pop();       for (auto I : v2[node]) {         if (costs[I.second] > cost + I.first) {           costs[I.second] = cost + I.first;           parent[I.second] = node;           pq.push({costs[I.second], I.second});         }       }     }   }   int main() {     string s;     cin >> s;     cin >> n;     for (int i = 0; i < n; i++) {       s.push_back( ! );     }     int mx = 0;     for (int i = 0; i < s.size(); i++) {       for (int j = i + 1; j < s.size(); j++) {         int l = 0;         for (int k = i; k < i + ((j - i + 1) / 2); k++) {           if (s[k] ==  !  || s[k + (j - i + 1) / 2] ==  ! ) {             l++;           } else if (s[k] == s[k + (j - i + 1) / 2]) {             l++;           } else             break;         }         mx = max(mx, l * 2);       }     }     cout << mx << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long mpow(long long a, long long b) {     long long c = 1;     while (b) {       if (b & 1) c = c * a % 998244353;       a = a * a % 998244353;       b /= 2;     }     return c;   }   long long minv(long long a) { return mpow(a, 998244353 - 2); }   int n;   long long ans;   vector<int> g[500010];   long long f(int u, int p) {     long long prob = 1;     for (int v : g[u])       if (v != p) {         prob *= 1 - f(v, u) * ((998244353 + 1) / 2) % 998244353;         prob %= 998244353;       }     ans += 1 - prob;     ans %= 998244353;     return prob;   }   int main() {     scanf( %d , &n);     for (int i = 1, u, v; i < n; i++) {       scanf( %d %d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     f(1, 0);     printf( %I64d n , (ans + 998244353) * mpow(2, n) % 998244353);   }


#include <bits/stdc++.h>   using namespace std;   int n;   struct hh {     int t, nxt;   } edge[1000020 << 1];   int head[1000020], ecnt;   void make_edge(int f, int t) {     edge[++ecnt] = (hh){t, head[f]};     head[f] = ecnt;     edge[++ecnt] = (hh){f, head[t]};     head[t] = ecnt;   }   int dep[1000020], size[1000020], son[1000020], fa[1000020];   void dfs1(int x, int fa) {     dep[x] = dep[fa] + 1;     size[x] = 1;     for (int i = head[x]; i; i = edge[i].nxt)       if (edge[i].t != fa) {         dfs1(edge[i].t, x);         size[x] += size[edge[i].t];         if (size[edge[i].t] > size[son[x]]) son[x] = edge[i].t;       }   }   map<string, int> S[1000020];   int sum[1000020];   string a[1000020];   bool s[1000020];   void add(string s, int d, int t) {     if (!S[d][s]) ++sum[d];     S[d][s] += t;     if (!S[d][s]) --sum[d];   }   void add(int x, int fa, int t) {     add(a[x], dep[x], t);     for (int i = head[x]; i; i = edge[i].nxt)       if (edge[i].t != fa && !s[edge[i].t]) add(edge[i].t, x, t);   }   vector<pair<int, int> > q[1000020];   int ans[1000020];   void dfs2(int x, int fa, bool keep) {     for (int i = head[x]; i; i = edge[i].nxt)       if (edge[i].t != fa && edge[i].t != son[x]) dfs2(edge[i].t, x, 0);     s[son[x]] = 1;     if (son[x]) dfs2(son[x], x, 1);     add(x, fa, 1);     for (int i = 0; i < (int)q[x].size(); i++)       ans[q[x][i].first] = sum[q[x][i].second];     s[son[x]] = 0;     if (!keep) add(x, fa, -1);   }   int main() {     ios::sync_with_stdio(false);     int i, x, y, m;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> a[i] >> fa[i];       if (fa[i]) make_edge(fa[i], i);     }     for (i = 1; i <= n; i++)       if (!fa[i]) dfs1(i, 0);     cin >> m;     for (i = 1; i <= m; i++) {       cin >> x >> y;       q[x].push_back(make_pair(i, dep[x] + y));     }     for (i = 1; i <= n; i++)       if (!fa[i]) dfs2(i, 0, 0);     for (i = 1; i <= m; i++) printf( %d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)5e5 + 5;   int n, m, k, dp[1 << 15][110], nxt[N][16];   vector<int> a;   map<string, int> mp;   void update(int &x, int y) {     if (x == -1 || x > y) x = y;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) {       string s;       cin >> s;       mp[s] = i;     }     cin >> m;     int idx = 0, cnt = 0, lmt = n * (n - 1) / 2;     for (int i = 1; i <= m; ++i) {       cin >> k;       string s;       a.clear();       for (int j = 1; j <= k; ++j) {         cin >> s;         if (mp.find(s) != mp.end())           a.push_back(mp[s]);         else           a.push_back(0);       }       for (int j = 1; j <= n; ++j) nxt[k + 1][j] = k + 1;       for (int j = k; j >= 1; --j) {         for (int x = 1; x <= n; ++x) {           if (a[j - 1] == x)             nxt[j][x] = j;           else             nxt[j][x] = nxt[j + 1][x];         }       }       memset(dp, -1, sizeof(dp));       dp[0][0] = 0;       for (int x = 0; x < (1 << n) - 1; ++x) {         for (int y = 0; y <= lmt; ++y)           if (dp[x][y] != -1) {             int tmp = 0;             for (int z = n - 1; z >= 0; --z) {               if ((x >> z) & 1)                 ++tmp;               else {                 int pos = nxt[dp[x][y] + 1][z + 1];                 if (pos <= k && y + tmp <= lmt)                   update(dp[x | (1 << z)][y + tmp], pos);               }             }           }       }       for (int y = 0; y <= lmt; ++y)         if (dp[(1 << n) - 1][y] != -1) {           if (lmt - y + 1 > cnt) idx = i, cnt = lmt - y + 1;           break;         }     }     if (!idx) return puts( Brand new problem! ), 0;     printf( %d n[: , idx);     for (int i = 1; i <= cnt; ++i) printf( | );     printf( :] );     return 0;   }


#include <bits/stdc++.h>   int main() {     char old_h[1001][21], new_h[1001][21];     int q, i, j;     char temp[21];     scanf( %d , &q);     for (i = 0; i < q; i++) {       scanf( %s %s , old_h[i], new_h[i]);     }     for (i = 0; i < q; i++) {       for (j = i + 1; j < q; j++) {         if (strcmp(new_h[i], old_h[j]) == 0) {           strcpy(temp, new_h[i]);           strcpy(new_h[i], new_h[j]);           strcpy(new_h[j], temp);         }       }     }     int count = 0;     for (i = 0; i < q; i++) {       if (strcmp(old_h[i], new_h[i]) != 0) count++;     }     printf( %d n , count);     for (i = 0; i < q; i++) {       if (strcmp(old_h[i], new_h[i]) != 0) {         printf( %s %s n , old_h[i], new_h[i]);       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, d, ans = 0;   vector<long long> a;   int main() {     ios::sync_with_stdio(false);     cin >> n >> d;     while (n--) {       long long temp;       cin >> temp;       a.push_back(temp);     }     for (long long i = 0; i < a.size(); i++) {       long long idx = upper_bound(a.begin(), a.end(), a[i] + d) - a.begin();       idx--;       if (idx > i + 1) ans += ((idx - i) * (idx - i - 1)) / 2;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(0.0) * 2.0;   const double eps = 1e-12;   const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1},  {0, -1},                           {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};   template <class T>   inline T abs1(T a) {     return a < 0 ? -a : a;   }   template <class T>   inline T max1(T a, T b) {     return a > b ? a : b;   }   template <class T>   inline T max1(T a, T b, T c) {     return max1(max1(a, b), c);   }   template <class T>   inline T max1(T a, T b, T c, T d) {     return max1(max1(a, b, c), d);   }   template <class T>   inline T max1(T a, T b, T c, T d, T e) {     return max1(max1(a, b, c, d), e);   }   template <class T>   inline T min1(T a, T b) {     return a > b ? a : b;   }   template <class T>   inline T min1(T a, T b, T c) {     return min1(min1(a, b), c);   }   template <class T>   inline T min1(T a, T b, T c, T d) {     return min1(min1(a, b, c), d);   }   template <class T>   inline T min1(T a, T b, T c, T d, T e) {     return min1(min1(a, b, c, d), e);   }   template <class T>   inline T lowb(T num) {     return num & (-num);   }   inline int jud(double a, double b) {     if (abs(a) < eps && abs(b) < eps)       return 0;     else if (abs1(a - b) / abs1(a) < eps)       return 0;     if (a < b) return -1;     return 1;   }   template <typename t>   inline int jud(t a, t b) {     if (a < b) return -1;     if (a == b) return 0;     return 1;   }   template <typename it, typename t1>   inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {     int be = 0, en = na - 1;     if (*a <= *(a + na - 1)) {       if (f_lb == 0)         while (be < en) {           int mid = (be + en + 1) / 2;           if (jud(*(a + mid), val) != 1)             be = mid;           else             en = mid - 1;         }       else         while (be < en) {           int mid = (be + en) / 2;           if (jud(*(a + mid), val) != -1)             en = mid;           else             be = mid + 1;         }       if (f_small && jud(*(a + be), val) == 1) be--;       if (!f_small && jud(*(a + be), val) == -1) be++;     } else {       if (f_lb)         while (be < en) {           int mid = (be + en + 1) / 2;           if (jud(*(a + mid), val) != -1)             be = mid;           else             en = mid - 1;         }       else         while (be < en) {           int mid = (be + en) / 2;           if (jud(*(a + mid), val) != 1)             en = mid;           else             be = mid + 1;         }       if (!f_small && jud(*(a + be), val) == -1) be--;       if (f_small && jud(*(a + be), val) == 1) be++;     }     return be;   }   inline int bitnum(unsigned int nValue) { return __builtin_popcount(nValue); }   inline int bitnum(int nValue) { return __builtin_popcount(nValue); }   inline int bitnum(unsigned long long nValue) {     return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);   }   inline int bitnum(long long nValue) {     return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);   }   inline int bitmaxl(unsigned int a) {     if (a) return 0;     return 32 - __builtin_clz(a);   }   inline int bitmaxl(int a) {     if (a) return 0;     return 32 - __builtin_clz(a);   }   inline int bitmaxl(unsigned long long a) {     int temp = a >> 32;     if (temp) return 32 - __builtin_clz(temp) + 32;     return bitmaxl(int(a));   }   inline int bitmaxl(long long a) {     int temp = a >> 32;     if (temp) return 32 - __builtin_clz(temp) + 32;     return bitmaxl(int(a));   }   long long pow(long long n, long long m, long long mod = 0) {     if (m < 0) return 0;     long long ans = 1;     long long k = n;     while (m) {       if (m & 1) {         ans *= k;         if (mod) ans %= mod;       }       k *= k;       if (mod) k %= mod;       m >>= 1;     }     return ans;   }   const int maxn = (1 << 24) + 10;   int cnt[maxn], n;   int arr[10010];   int main() {     ios_base::sync_with_stdio(0);     scanf( %d , &n);     for (int i = 0; i < n; i++) {       char str[10];       scanf( %s , str);       int rmask = 0;       for (int j = 0; j < 3; j++) rmask |= 1 << (str[j] -  a );       rmask = (1 << 24) - 1 - rmask;       arr[i] = rmask;       cnt[rmask]++;     }     long long ans = 0;     for (int i = 23; i >= 0; i--) {       int mask = 1 << 24;       mask -= 1 + (1 << i);       for (int j = mask; j >= 0; j = (j - 1) & mask) {         cnt[j] += cnt[j + (1 << i)];         if (j == 0) break;       }     }     for (int i = (1 << 24) - 1; i >= 0; i--) {       ans ^= (n - cnt[i]) * (n - cnt[i]);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   int getbit(T s, int i) {     return (s >> i) & 1;   }   template <class T>   T onbit(T s, int i) {     return s | (T(1) << i);   }   template <class T>   T offbit(T s, int i) {     return s & (~(T(1) << i));   }   template <class T>   int cntbit(T s) {     return __builtin_popcount(s);   }   template <class T>   T gcd(T a, T b) {     T r;     while (b != 0) {       r = a % b;       a = b;       b = r;     }     return a;   }   template <class T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   int n;   string s;   long long u[2000005], d[2000005];   long long nu[2000005], nd[2000005];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> s;     memset(nu, 0, sizeof(nu));     memset(nd, 0, sizeof(nd));     memset(u, 0, sizeof(u));     memset(d, 0, sizeof(d));     for (int i = (1); i <= (n); ++i) {       char ch = s[i - 1];       nu[i] = nu[i - 1];       nd[i] = nd[i - 1];       if (ch ==  U ) {         nu[i]++;         u[nu[i]] = u[nu[i] - 1] + i;       } else {         nd[i]++;         d[nd[i]] = d[nd[i] - 1] + i;       }     }     for (int i = (1); i <= (n); ++i) {       char ch = s[i - 1];       long long res = 0;       if (ch ==  U ) {         long long numd = nd[n] - nd[i];         long long numu = nu[i];         if (numd < numu) {           res += (d[nd[i] + numd] - d[nd[i]]) * 2 + n + 1;           res -= (u[nu[i]] - u[nu[i] - numd - 1]) * 2 - i;         } else {           res += (d[nd[i] + numu] - d[nd[i]]) * 2;           res -= (u[nu[i]]) * 2 - i;         }       } else {         long long numd = nd[n] - nd[i - 1];         long long numu = nu[i];         if (numd <= numu) {           res += (d[nd[i - 1] + numd] - d[nd[i - 1]]) * 2 + n + 1 - i;           res -= (u[nu[i]] - u[nu[i] - numd]) * 2;         } else {           res += (d[nd[i] + numu] - d[nd[i - 1]]) * 2 - i;           res -= (u[nu[i]]) * 2;         }       }       cout << res <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dx[] = {1, 0, -1, 0, -1, 1, -1, 1};   int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};   const double eps = 1e-6;   const int N = 1e9;   void moha() {}   bool vis[1505][1505];   int comp[1505][1505][2];   char x[1505][1505];   int n, m;   int row(int x) { return (x + n * 1500) % n; }   int col(int y) { return (y + m * 1500) % m; }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     int a, b;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> x[i][j];         if (x[i][j] ==  S ) {           a = i;           b = j;         }       }     queue<pair<int, int> > q;     q.push({a, b});     vis[a][b] = 1;     comp[row(a)][col(b)][0] = a;     comp[row(a)][col(b)][1] = b;     while (!q.empty()) {       pair<int, int> p = q.front();       q.pop();       for (int i = 0; i < 4; i++) {         int xx = p.first + dx[i];         int yy = p.second + dy[i];         if (x[row(xx)][col(yy)] ==  # ) continue;         if (vis[row(xx)][col(yy)]) {           if (comp[row(xx)][col(yy)][0] != xx ||               comp[row(xx)][col(yy)][1] != yy) {             cout <<  Yes  << endl;             return 0;           }           continue;         }         q.push({xx, yy});         vis[row(xx)][col(yy)] = 1;         comp[row(xx)][col(yy)][0] = xx;         comp[row(xx)][col(yy)][1] = yy;       }     }     cout <<  No  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   int a[101];   signed main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     sort(a + 1, a + 1 + n);     int ans = 0;     for (int i = 1; i <= n; i++) {       if (!a[i]) continue;       ans++;       for (int j = i + 1; j <= n; j++) {         if (a[j]) {           if (a[j] % a[i] == 0) a[j] = 0;         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   template <typename T>   inline void read(T &x) {     char c;     bool nega = 0;     while ((!isdigit(c = getchar())) && (c !=  - ))       ;     if (c ==  - ) {       nega = 1;       c = getchar();     }     x = c - 48;     while (isdigit(c = getchar())) x = x * 10 + c - 48;     if (nega) x = -x;   }   template <typename T>   inline void writep(T x) {     if (x > 9) writep(x / 10);     putchar(x % 10 + 48);   }   template <typename T>   inline void write(T x) {     if (x < 0) {       putchar( - );       x = -x;     }     writep(x);   }   template <typename T>   inline void writeln(T x) {     write(x);     putchar(  n );   }   int n, s;   int a[2000];   deque<int> pos[2001];   map<int, int> m;   int f[2001];   int trace[2001];   int g[2001];   int dist_inc(int u, int v) {     if (v >= u) return v - u;     return v + n - u;   }   int dist_dec(int u, int v) {     if (v <= u) return u - v;     return u + n - v;   }   int dist(int u, int v) { return min(abs(u - v), n - abs(u - v)); }   void output(int x) {     putchar( +- [x < 0]);     writeln(abs(x));   }   void rebuild(int u) {     if (a[u] > 1) rebuild(trace[u]);     if ((a[u] == 1) && (a[s] == 1)) {       output(0);       return;     }     int x = a[u];     int sz = pos[x].size();     int i = trace[u];     if (pos[x].size() == 1) {       if (dist(i, u) == dist_inc(i, u)) {         output(dist(i, u));       } else {         output(-dist(trace[u], u));       }     } else {       while (dist_inc(pos[x].back(), i) + dist_inc(i, pos[x].front()) !=              dist_inc(pos[x].back(), pos[x].front())) {         pos[x].push_back(pos[x].front());         pos[x].pop_front();       }       if (i == s) {         if (a[s] == 0)           g[s] = 0;         else           g[s] = f[s];       }       int maxs = dist_inc(i, pos[x].front());       int tmax = pos[x].front();       for (int j = (0); j < (sz); j++) {         if (j) {           if (maxs < dist_inc(pos[x][j - 1], pos[x][j])) {             maxs = dist_inc(pos[x][j - 1], pos[x][j]);             tmax = pos[x][j];           }         }         if (pos[x][j] == u) break;       }       if (f[u] == g[i] + n + dist_inc(i, u) - maxs * 2) {         int last = i;         for (int j = (0); j < (sz); j++) {           if (pos[x][j] == tmax) break;           last = pos[x][j];           output(dist_inc((j == 0) ? i : pos[x][j - 1], pos[x][j]));         }         for (int j = (sz - 1); j >= (0); j--) {           output(-dist_dec((j == sz - 1) ? last : pos[x][j + 1], pos[x][j]));           if (pos[x][j] == tmax) {             if (tmax == u) break;             output(dist_inc(tmax, u));             break;           }         }         return;       }       maxs = dist_dec(i, pos[x].back());       tmax = pos[x].back();       for (int j = (sz - 1); j >= (0); j--) {         if (j < sz - 1) {           if (maxs < dist_dec(pos[x][j + 1], pos[x][j])) {             maxs = dist_dec(pos[x][j + 1], pos[x][j]);             tmax = pos[x][j];           }         }         if (pos[x][j] == u) break;       }       if (f[u] == g[i] + n + dist_dec(i, u) - maxs * 2) {         int last = i;         for (int j = (sz - 1); j >= (0); j--) {           if (pos[x][j] == tmax) break;           last = pos[x][j];           output(-dist_dec((j == sz - 1) ? i : pos[x][j + 1], pos[x][j]));         }         for (int j = (0); j < (sz); j++) {           output(dist_inc((j == 0) ? last : pos[x][j - 1], pos[x][j]));           if (pos[x][j] == tmax) {             if (tmax == u) break;             output(-dist_dec(tmax, u));             break;           }         }         return;       }     }   }   int main() {     read(n);     read(s);     s--;     for (int i = (0); i < (n); i++) read(a[i]);     if (a[s] == *min_element(a, a + n)) {       a[s]--;     }     for (int i = (0); i < (n); i++) m[a[i]] = 0;     {       int cnt = 0;       for (auto &t : m) t.second = ++cnt;     }     for (int i = (0); i < (n); i++) {       a[i] = m[a[i]];       pos[a[i]].push_back(i);     }     if (a[s] != 1) pos[0].push_back(s);     for (int x = (1 + (a[s] == 1)); x <= (m.size()); x++) {       for (int i : pos[x - 1]) g[i] = f[i];       for (int i : pos[x]) f[i] = 1e9;       int sz = pos[x].size();       for (int i : pos[x - 1]) {         if (pos[x].size() == 1) {           if (f[pos[x][0]] > g[i] + dist(i, pos[x][0])) {             f[pos[x][0]] = g[i] + dist(i, pos[x][0]);             trace[pos[x][0]] = i;           }         } else {           while (dist_inc(pos[x].back(), i) + dist_inc(i, pos[x].front()) !=                  dist_inc(pos[x].back(), pos[x].front())) {             pos[x].push_back(pos[x].front());             pos[x].pop_front();           }           int maxs = dist_inc(i, pos[x].front());           for (int j = (0); j < (sz); j++) {             if (j) maxs = max(maxs, dist_inc(pos[x][j - 1], pos[x][j]));             if (f[pos[x][j]] > g[i] + n + dist_inc(i, pos[x][j]) - maxs * 2) {               f[pos[x][j]] = g[i] + n + dist_inc(i, pos[x][j]) - maxs * 2;               trace[pos[x][j]] = i;             }           }           maxs = dist_dec(i, pos[x].back());           for (int j = (sz - 1); j >= (0); j--) {             if (j + 1 < sz) maxs = max(maxs, dist_dec(pos[x][j + 1], pos[x][j]));             if (f[pos[x][j]] > g[i] + n + dist_dec(i, pos[x][j]) - maxs * 2) {               f[pos[x][j]] = g[i] + n + dist_dec(i, pos[x][j]) - maxs * 2;               trace[pos[x][j]] = i;             }           }         }       }     }     int best = pos[m.size()][0];     for (int i : pos[m.size()])       if (f[i] < f[best]) best = i;     writeln(f[best]);     rebuild(best);   }


#include <bits/stdc++.h>   long long tt, x, y, z, sum, n, i, j, h, k, t, d, m, l, ll, rr, r, a[1000006],       b[1000006], f[1000006];   using namespace std;   long long xl(long long i) {     long long res = 0;     for (j = 1; j <= d; j++) b[j] = a[j];     l = 1;     r = i;     tt = 1;     z = b[1];     x = 1;     while (r <= n) {       if (b[x] > i) {         l += b[x] - b[x] % i - i;         r += b[x] - b[x] % i - i;         b[x] -= (b[x] - b[x] % i - i);       }       y = 0;       t = (l + r) / 2;       while (z < t) tt++, z += a[tt];       while (y < i) {         if (b[x] > i - y) {           res += (i - y) * abs(f[x] - f[tt]);           b[x] -= (i - y);           y = i;         } else {           res += b[x] * abs(f[x] - f[tt]);           y += b[x];           x++;         }       }       l += i;       r += i;     }     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cin >> n;     for (i = 1; i <= n; i++) {       cin >> j;       sum += j;       if (j > 0) d++, a[d] = j, f[d] = i;     }     if (sum == 1)       cout << -1;     else {       n = sum;       i = 2;       m = 1e18;       while (i <= sqrt(sum)) {         if (sum % i == 0) m = min(m, xl(i));         while (sum % i == 0) sum /= i;         i++;       }       if (sum > 1) m = min(m, xl(sum));       cout << m;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = LLONG_MAX;   const int inf = INT_MAX;   const int mod = 1e9 + 7;   const int pi = acos(-1);   const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};   const int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};   const int N = 2e5 + 5;   const int MAXN = 1e5 + 1;   int a[N], dp[N][6], p[N][6];   vector<int> v;   void ans(int n, int val) {     v.push_back(val);     if (n == 1) {       reverse(v.begin(), v.end());       for (int i = 0; i < (int)v.size(); i++) {         cout << v[i] <<    ;       }       exit(0);     }     int to = p[n][val];     ans(n - 1, to);   }   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i <= 5; i++) {       dp[1][i] = 1;     }     for (int i = 2; i <= n; i++) {       for (int j = 1; j <= 5; j++) {         int w = INT_MIN;         for (int k = 1; k <= 5; k++) {           if (k == j) continue;           if (a[i] > a[i - 1]) {             if (j > k) w = max(w, dp[i - 1][k]);           }           if (a[i] < a[i - 1]) {             if (j < k) w = max(w, dp[i - 1][k]);           }           if (a[i] == a[i - 1]) {             w = max(w, dp[i - 1][k]);           }         }         if (w == 1) {           dp[i][j] = 1;           int ans;           for (int k = 1; k <= 5; k++) {             if (k == j) continue;             if (a[i] > a[i - 1]) {               if (j > k) {                 if (dp[i - 1][k] == 1) {                   ans = k;                   break;                 }               }             } else if (a[i] < a[i - 1]) {               if (j < k) {                 if (dp[i - 1][k] == 1) {                   ans = k;                   break;                 }               }             } else {               if (dp[i - 1][k] == 1) {                 ans = k;                 break;               }             }           }           p[i][j] = ans;         } else {           dp[i][j] = INT_MIN;         }       }     }     for (int i = 1; i <= 5; i++) {       if (dp[n][i] == 1) {         ans(n, i);         return 0;       }     }     cout << -1;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 8;   const int mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       long long n, m;       cin >> n >> m;       long long arr[n][m];       long long x = 0;       for (long long i = 0; i < (n); i++) {         for (long long j = 0; j < (m); j++) {           cin >> arr[i][j];           if (arr[i][j] < 0) {             x++;           }         }       }       long long ans = 0;       long long minval = 101;       for (long long i = 0; i < (n); i++) {         for (long long j = 0; j < (m); j++) {           ans += abs(arr[i][j]);           minval = min(minval, abs(arr[i][j]));         }       }       if (x % 2 != 0) {         ans -= 2 * minval;       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, colour[105], p[105];     memset(colour, -1, sizeof colour);     bool flag = 0;     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> p[i];       colour[p[i]] = 2;     }     for (int i = 0; i < 101; i++) {       if (colour[i] == 2) {         colour[i] = flag;         flag = !flag;       }     }     for (int i = 0; i < n; i++) {       if (i) cout <<    ;       cout << colour[p[i]];     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     vector<vector<long long int>> grid(51, vector<long long int>(51, 0));     vector<pair<long long int, long long int>> a;     int t1 = 4 * n + 1;     while (t1--) {       int u, v;       cin >> u >> v;       a.push_back({u, v});       grid[u][v] = 1;     }     t1 = 4 * n + 1;     for (int stx = 0; stx < 50; stx++) {       for (int enx = stx + 1; enx < 51; enx++) {         for (int sty = 0; sty < 50; sty++) {           for (int eny = sty + 1; eny < 51; eny++) {             long long int cnt = 0, c1 = 0, c2 = 0, c3 = 0, c4 = 0;             for (int j = sty; j <= eny; j++) {               if (grid[stx][j] == 1) {                 cnt++;                 c1++;                 if (j == sty) {                   c2++;                 }                 if (j == eny) {                   c4++;                 }               }               if (grid[enx][j] == 1) {                 cnt++;                 c3++;                 if (j == sty) {                   c2++;                 }                 if (j == eny) {                   c4++;                 }               }             }             for (int j = stx + 1; j <= enx - 1; j++) {               if (grid[j][sty] == 1) {                 cnt++;                 c2++;               }               if (grid[j][eny] == 1) {                 cnt++;                 c4++;               }             }             if (cnt == 4 * n && c1 >= n && c2 >= n && c3 >= n && c4 >= n) {               for (int i = 0; i < a.size(); i++) {                 if (a[i].first != stx && a[i].first != enx &&                     a[i].second != sty && a[i].second != eny) {                   cout << a[i].first <<     << a[i].second;                   return 0;                 }                 if (a[i].first == stx || a[i].first == enx) {                   if (a[i].second < sty || a[i].second > eny) {                     cout << a[i].first <<     << a[i].second;                     return 0;                   }                 }                 if (a[i].second == sty || a[i].second == eny) {                   if (a[i].first < stx || a[i].first > enx) {                     cout << a[i].first <<     << a[i].second;                     return 0;                   }                 }               }             }           }         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m, dp[3005][3005], a[1000005];   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; ++i) scanf( %d , a + i);     if (n <= m) {       dp[1][a[1] % m] = 1;       for (int i = 2; i <= n; ++i) {         for (int j = 0; j < m; ++j) {           a[i] = (a[i] % m);           dp[i][j] = (dp[i - 1][j] | dp[i - 1][(j - a[i] + m) % m]);         }         dp[i][a[i] % m] = 1;       }       if (dp[n][0])         return !printf( YES n );       else         return !printf( NO n );     } else       printf( YES n );     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   struct Point {     int x;     int y;     int index;     double angle;     void Translate(int dx, int dy) {       x += dx;       y += dy;     }     void CalculateAngle() { angle = atan2(y, x); }     bool operator<(const Point& p) const { return angle < p.angle; }   };   std::ostream& operator<<(std::ostream& stream, const Point& p) {     stream <<  (  << p.x <<  ,  << p.y <<  ) ;   }   int n, m;   Point points[250];   Point ccPoints[250];   queue<int> Q[250];   vector<int> visibleOut[250], visibleIn[250];   long long dp[250][250][51];   long long maxArea[51];   long long sol = 0;   bool IsLeftTurn(Point p1, Point p2, Point p3) {     Point v1;     v1.x = p2.x - p1.x;     v1.y = p2.y - p1.y;     Point v2;     v2.x = p3.x - p2.x;     v2.y = p3.y - p2.y;     return (long long)v1.x * v2.y - (long long)v1.y * v2.x > 0;   }   inline long long TriangleArea(Point p1, Point p2) {     return (long long)p1.x * p2.y - (long long)p1.y * p2.x;   }   void Proceed(int i, int j) {     while (Q[i].size() > 0 &&            IsLeftTurn(ccPoints[Q[i].front()], ccPoints[i], ccPoints[j])) {       Proceed(Q[i].front(), j);       Q[i].pop();     }     visibleOut[i].push_back(j);     visibleIn[j].push_back(i);     Q[j].push(i);   }   inline void MakeVisibilityGraph(int rightCount) {     for (int i = 0; i < rightCount; i++) {       while (!Q[i].empty()) {         Q[i].pop();       }       visibleIn[i].clear();       visibleOut[i].clear();     }     for (int i = 0; i < rightCount - 1; i++) {       Proceed(i, i + 1);     }   }   inline void MaxArea(int rightCount) {     for (int i = 0; i < rightCount; i++) {       for (int j = 0; j < visibleOut[i].size(); j++) {         for (int k = 0; k <= m; k++) dp[i][visibleOut[i][j]][k] = 0;       }     }     for (int i = rightCount - 1; i >= 0; i--) {       for (int j = 0; j <= m; j++) maxArea[j] = 0;       int outIndex = visibleOut[i].size() - 1;       for (int inIndex = visibleIn[i].size() - 1; inIndex >= 0; inIndex--) {         dp[visibleIn[i][inIndex]][i][0] = 0;         dp[visibleIn[i][inIndex]][i][1] =             TriangleArea(ccPoints[visibleIn[i][inIndex]], ccPoints[i]);         for (int j = 2; j <= m; j++)           dp[visibleIn[i][inIndex]][i][j] =               maxArea[j - 1] > 0                   ? maxArea[j - 1] +                         TriangleArea(ccPoints[visibleIn[i][inIndex]], ccPoints[i])                   : 0;         while (outIndex >= 0 &&                IsLeftTurn(ccPoints[visibleIn[i][inIndex]], ccPoints[i],                           ccPoints[visibleOut[i][outIndex]])) {           for (int j = 1; j <= 50; j++) {             if (dp[i][visibleOut[i][outIndex]][j - 1] > maxArea[j - 1]) {               maxArea[j - 1] = dp[i][visibleOut[i][outIndex]][j - 1];               dp[visibleIn[i][inIndex]][i][j] =                   maxArea[j - 1] +                   TriangleArea(ccPoints[visibleIn[i][inIndex]], ccPoints[i]);             }           }           outIndex--;         }       }     }     for (int i = 0; i < rightCount; i++) {       for (int j = 0; j < visibleOut[i].size(); j++) {         sol = max(sol, dp[i][visibleOut[i][j]][m - 2]);       }     }   }   inline void ProcessLeftmostPoint(int leftIndex) {     int rightCount = 0;     for (int i = 0; i < n; i++) {       if (i != leftIndex && points[i].x >= points[leftIndex].x) {         ccPoints[rightCount] = points[i];         ccPoints[rightCount].Translate(-points[leftIndex].x,                                        -points[leftIndex].y);         ccPoints[rightCount].CalculateAngle();         ++rightCount;       }     }     sort(ccPoints, ccPoints + rightCount);     MakeVisibilityGraph(rightCount);     MaxArea(rightCount);   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %d %d , &points[i].x, &points[i].y);       points[i].index = i;     }     for (int i = 0; i < n; i++) {       ProcessLeftmostPoint(i);     }     cout << sol / 2 << (sol % 2 == 1 ?  .50  :  .00 ) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long oo = 1e18;   const long long MAXN = 110;   long long N;   vector<string> query(long long a, long long b) {     cout <<  ?           <<     << a <<     << b <<   n ;     fflush(stdout);     long long sz = b - a + 1;     vector<string> vect;     for (long long i = 0; i < sz * (sz + 1) / 2; i++) {       string cad;       cin >> cad;       vect.push_back(cad);     }     return vect;   }   void res(string cad) {     cout <<  !           <<     << cad <<   n ;     fflush(stdout);   }   struct edge {     long long nodo;     char a, b;   };   vector<edge> L[MAXN];   char xx, yy;   void solve(vector<string> vect, long long des) {     vector<string> arr[MAXN];     long long sz = 0;     for (long long i = 0; i < vect.size(); i++) {       arr[vect[i].size()].push_back(vect[i]);       sz = max(sz, (long long)vect[i].size());     }     map<char, long long> cont;     for (long long i = 0; i < sz; i++) {       cont[arr[sz][0][i]]++;     }     vector<edge> vv;     for (long long i = 2; i <= sz / 2 + 1; i++) {       map<char, long long> mp;       map<char, long long> temp;       for (auto x : cont) {         temp[x.first] = x.second * i;       }       for (long long i = 0; i < vv.size(); i++) {         temp[vv[i].a] += i + 1;         temp[vv[i].b] += i + 1;       }       for (auto cad : arr[i]) {         for (long long x = 0; x < i; x++) {           mp[cad[x]]++;         }       }       char a, b;       for (auto x : temp) {         if (x.second > mp[x.first]) {           a = x.first;           break;         }       }       temp[a]--;       for (auto x : temp) {         if (x.second > mp[x.first]) {           b = x.first;           break;         }       }       cont[a]--;       cont[b]--;       vv.push_back({0, a, b});       L[i - 1 + des].push_back({sz - i + 2 + des, a, b});       L[sz - i + 2 + des].push_back({i - 1 + des, a, b});       if (i == 2 && des == 0) {         xx = a;         yy = b;       }     }   }   char ss[MAXN];   void DFS(long long nodo, long long padre, char c) {     ss[nodo] = c;     for (auto v : L[nodo]) {       if (v.nodo != padre) {         char temp;         if (v.a == c)           temp = v.b;         else           temp = v.a;         DFS(v.nodo, nodo, temp);       }     }   }   int32_t main() {     cin >> N;     vector<string> vect = query(1, N);     if (N == 1) {       res(vect[0]);       return 0;     }     solve(vect, 0);     vect = query(2, N);     solve(vect, 1);     if (xx != yy) {       vect = query(1, 1);       DFS(1, -1, vect[0][0]);       string st;       for (long long i = 1; i <= N; i++) st += ss[i];       res(st);     } else {       DFS(1, -1, xx);       string st;       for (long long i = 1; i <= N; i++) st += ss[i];       for (long long i = 0; i < N; i++) {         if (st[i] != st[N - i - 1]) {           vect = query(i + 1, i + 1);           char cc = vect[0][0];           if (st[i] != cc) {             reverse(st.begin(), st.end());           }           break;         }       }       res(st);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void task();   int main() {     srand(time(0));     task();     return 0;   }   const int INF = 0x3f3f3f3f;   const int N = 1e5 + 10;   const int M = 1e5 + 10;   int n, x;   int l[N];   int r[N];   long long int dp[2 * N + 10];   vector<int> v;   void task() {     scanf( %d %d , &n, &x);     v.push_back(x);     for (int i = 0; i < n; ++i) {       scanf( %d %d , &l[i], &r[i]);       v.push_back(l[i]);       v.push_back(r[i]);     }     sort((v.begin()), (v.end()));     int m = ((int)((v).size()));     memset(dp, 0x3f, sizeof(dp));     for (int i = 0; i < m; ++i)       if (v[i] == x) dp[i] = 0;     for (int i = 0; i < n; ++i) {       for (int j = 1; j < m; ++j) dp[j] = min(dp[j], dp[j - 1] + v[j] - v[j - 1]);       for (int j = m - 2; j >= 0; --j)         dp[j] = min(dp[j], dp[j + 1] + v[j + 1] - v[j]);       for (int j = 0; j < m; ++j)         if (v[j] < l[i])           dp[j] += l[i] - v[j];         else if (v[j] > r[i])           dp[j] += v[j] - r[i];     }     cout << *min_element(dp, dp + m);   }


#include <bits/stdc++.h>   #define pb push_back   #define mp make_pair   #define fr first   #define sc second   #define MOD 1000000007   #define len(x) x.size()   #define min3(a, b, c) min(a, min(b, c))   #define max3(a, b, c) max(a, max(b, c))   #define all(v) v.begin(), v.end()   #define alla(a,n) a, a + n      using namespace std;      typedef long long ll;   typedef unsigned long long ull;   typedef pair<ll, ll> pll;   typedef vector<ll> vll;   typedef vector<pll> vpll;   typedef vector<vll> vvll;   typedef vector<string> vs;         void run()   {   #ifndef ONLINE_JUDGE    freopen( input.txt ,  r , stdin) ;    freopen( output.txt ,  w , stdout) ;   #endif    ios_base::sync_with_stdio(false);    cin.tie(NULL) ; cout.tie(NULL) ;   }      void solve()   {    int t;    cin >> t;    while (t--)    {     int n, d;     cin >> n >> d;     int arr[n];     for (int i = 0; i < n; i++)     {      cin >> arr[i];     }        int flag = 0, min_val = 0;        for (int i = 0; i < n; i++)     {      if (arr[i] > d)      {       flag = 1;      }     }        if (flag == 0)     {      cout <<  YES  << endl;     }     else     {      sort(arr, arr + n);      // cout << arr[0] << arr[1];      min_val = arr[0] + arr[1];         if (min_val <= d)      {       cout <<  YES  << endl;      }      else      {       cout <<  NO  << endl;      }     }    }   }      int32_t main()   {    run();    solve();       return 0 ;   }


#include <bits/stdc++.h>   using namespace std;   long long Answer = 0, idx[300100], edges[300100], dis[300100];   int n, m, x, y, w, u;   vector<pair<pair<int, int>, int> > v[300100];   priority_queue<pair<int, int>, vector<pair<int, int> >,                  greater<pair<int, int> > >       Q;   void dik() {     for (int i = 1; i <= n; i++) dis[i] = 1e18;     Q.push({0, u});     dis[u] = 0;     while (!Q.empty()) {       pair<int, int> x = Q.top();       Q.pop();       for (int i = 0; i < v[x.second].size(); i++) {         int V = v[x.second][i].first.first;         int cost = v[x.second][i].first.second;         int id = v[x.second][i].second;         if (dis[V] > dis[x.second] + cost ||             (dis[V] == dis[x.second] + cost && edges[idx[V]] > edges[id])) {           dis[V] = dis[x.second] + cost;           idx[V] = id;           Q.push({dis[V], V});         }       }     }   }   int main() {     memset(idx, -1, sizeof(idx));     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> x >> y >> w;       v[x].push_back({{y, w}, i + 1});       v[y].push_back({{x, w}, i + 1});       edges[i + 1] = w;     }     cin >> u;     dik();     for (int i = 1; i <= n; i++) {       if (idx[i] != -1) {         Answer += edges[idx[i]];       }     }     cout << Answer << endl;     for (int i = 1; i <= n; i++) {       if (idx[i] != -1) cout << idx[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int nxt() {     int x;     cin >> x;     return x;   }   int gettm(char c) {     if (c ==  G ) {       return 5;     } else if (c ==  W ) {       return 3;     } else if (c ==  L ) {       return 1;     } else {       assert(false);     }   }   int main() {     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());     cin.sync_with_stdio(0);     int n = nxt();     vector<long long> a(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     string s;     cin >> s;     deque<pair<int, long long>> water_shit, ground_shit;     bool was_water = false;     long long ans = 0;     for (int i = 0; i < n; ++i) {       if (s[i] ==  G ) {         ground_shit.push_back({i, a[i]});       } else if (s[i] ==  W ) {         was_water = true;         water_shit.push_back({i, a[i]});       } else {         long long need = a[i];         while (need && !water_shit.empty()) {           long long tmp = min(need, water_shit.back().second);           need -= tmp;           water_shit.back().second -= tmp;           ans += 4 * tmp;           if (water_shit.back().second == 0) {             water_shit.pop_back();           }         }         while (need && !ground_shit.empty()) {           long long tmp = min(need, ground_shit[0].second);           need -= tmp;           ground_shit[0].second -= tmp;           ans += 6 * tmp;           if (ground_shit[0].second == 0) {             ground_shit.pop_front();           }         }         if (was_water) {           ans += 4 * need;         } else {           ans += 6 * need;         }       }     }     long long cur = 0;     long long totally_used = 0;     for (int i = 0, j = 0; i < (int)ground_shit.size(); ++i) {       while (j < (int)water_shit.size() &&              water_shit[j].first < ground_shit[i].first) {         cur += water_shit[j].second;         ++j;       }       long long tmp = min(cur, ground_shit[i].second);       ground_shit[i].second -= tmp;       cur -= tmp;       totally_used += tmp;       ans += 4 * tmp;       ans += ground_shit[i].second * 3;     }     totally_used *= -1;     for (auto p : water_shit) {       totally_used += p.second;     }     ans += 2 * totally_used;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     scanf( %d %d , &n, &m);     vector<int> vec(n + 1, 0);     vector<pair<int, pair<int, int>>> arr;     for (int i = 0; i < m; i++) {       int si, di, ci;       scanf( %d %d %d , &si, &di, &ci);       arr.push_back(make_pair(di, make_pair(si, ci)));     }     bool is_possible = true;     for (int i = 1; i <= n; i++) {       bool exam_time = false;       for (int j = 0; j < m; j++) {         if (arr[j].first == i) {           exam_time = true;           if (arr[j].second.second != 0) {             is_possible = false;           } else {             vec[i] = m + 1;           }           break;         }       }       if (exam_time) continue;       int best = -1, mini = (int)1e9;       for (int j = 0; j < m; j++) {         if (arr[j].first > i && arr[j].second.first <= i &&             arr[j].second.second != 0) {           if (arr[j].first < mini) {             mini = arr[j].first;             best = j;           }         }       }       if (best != -1) {         vec[i] = best + 1;         arr[best].second.second--;       }     }     if (is_possible) {       for (int i = 1; i <= n; i++) {         printf( %d  , vec[i]);       }     } else {       printf( -1 n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[1005], half;   pair<int, int> info[1005];   bool cmp(pair<int, int> A, pair<int, int> B) {     if (A.first == B.first)       return A.second < B.second;     else       return A.first < B.first;   }   int main() {     int n, k;     scanf( %d %d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %d , arr + i);       half = arr[i] / 2 + (arr[i] % 2);       info[i] = pair<int, int>(half, arr[i]);     }     sort(info, info + n, cmp);     int lvl = k, cnt = 0;     for (int i = 0; i < n; i++) {       if (info[i].first <= lvl) {         lvl = max(k, info[i].second);       } else {         int num = info[i].second;         while (lvl < num) {           num = (num / 2) + (num % 2);           if (lvl < num) cnt++;         }         lvl = info[i].second;       }     }     printf( %d , cnt);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n, m, i, min, count, a[101], sum, count0;     cin >> t;     while (t--) {       cin >> n >> m;       count = 0;       sum = 0;       count0 = 0;       for (i = 0; i < (n * m); i++) {         cin >> a[i];         if (a[i] < 0) {           count++;           a[i] = -1 * a[i];         }         if (a[i] == 0) count0 = 1;         sum += a[i];       }       if (count % 2 != 0 && count0 != 1) {         min = a[0];         for (i = 0; i < (n * m); i++) {           if (a[i] < min) min = a[i];         }         cout << sum - (2 * min) << endl;       } else         cout << sum << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long> > v;   bool cmp(pair<long long, long long> &a, pair<long long, long long> &b) {     if (a.first == b.first) return a.second < b.second;     return b.first < a.first;   }   long long func(pair<long long, long long> &a, pair<long long, long long> &b,                  long long d) {}   int main() {     long long n;     long long m, d;     scanf( %lld %lld %lld , &n, &m, &d);     pair<long long, long long> a[n];     pair<long long, long long> b[m];     for (long long i = 0; i < n; i++) {       cin >> a[i].first >> a[i].second;     }     sort(a, a + n, cmp);     for (long long i = 0; i < m; i++) {       cin >> b[i].first >> b[i].second;     }     sort(b, b + m, cmp);     long long posi = 1, totalw = 0, posj = 1, totals = 0;     totalw = a[0].second + b[0].second;     totals = a[0].first + b[0].first;     if (totalw > d) return cout << 0, 0;     while (posj < m && totalw + b[posj].second <= d) {       totalw += b[posj].second;       totals += b[posj].first;       posj++;     }     long long mx = totals;     while (posi < n) {       totalw += a[posi].second;       totals += a[posi].first;       posi++;       while (totalw > d) {         if (posj <= 1) return cout << mx, 0;         posj--;         totals -= b[posj].first;         totalw -= b[posj].second;       }       mx = max(mx, totals);     }     cout << mx << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 10;   const long long inf = 1e15;   const int mod = 1e6;   void faster() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   const long long N = 3e5 + 10;   int n, m;   long long a[N];   int main() {     faster();     cin >> n;     if (n % 10 < 10 - (n % 10))       cout << n - n % 10 << endl;     else       cout << n + (10 - (n % 10)) << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000000 + 10, MOD = (int)1e+9 + 7;   long long fac[MAXN], Ifac[MAXN], T;   int k, w;   long long Pow(long long a, long long b) {     long long d = 1;     for (; b; d = (b & 1) ? d * a % MOD : d, a = a * a % MOD, b /= 2)       ;     return d;   }   long long A(int x, int y) { return fac[x] * Ifac[x - y] % MOD; }   int main() {     cin >> k >> w;     fac[0] = Ifac[0] = 1;     for (int i = 1; i <= k; ++i)       fac[i] = fac[i - 1] * i % MOD,       Ifac[i] = Ifac[i - 1] * Pow(i, MOD - 2) % MOD;     long long CC = 0, t = Pow(k, w - 2);     for (int i = 1; i <= k; ++i) {       if (i < w)         CC = (CC + A(k, i) * A(k, i) % MOD * t % MOD * i % MOD) % MOD;       else         CC = (CC + A(k, i) * A(k + w - 1 - i, w - 1) % MOD * w) % MOD;       if (i < k) {         t = t * Pow(k, MOD - 2) % MOD;         if (i + 2 <= w)           CC = (CC + A(k, i) * A(k, i + 1) % MOD * t % MOD * i % MOD) % MOD;         else           CC =               (CC + A(k, i + 1) * A(k - i + w - 2, w - 2) % MOD * (w - 1) % MOD) %               MOD;       }     }     cout << (CC + MOD) % MOD << endl;     fclose(stdin);     fclose(stdout);     return 0;   }


#include <bits/stdc++.h>   int main() {     long long int a, b, c, d, x;     int t;     scanf( %d , &t);     while (t--) {       scanf( %lld%lld%lld%lld , &a, &b, &c, &d);       x = a / d + (((a / d) / b) * c);       printf( %lld n , x);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int arr[4];     int t = 0;     int s = 0;     for (int i = 0; i < 4; i++) cin >> arr[i];     for (int m = 0; m < 4; m++) {       for (int j = 0; j < 4; j++) {         for (int k = 0; k < 4; k++) {           if (arr[m] < arr[j] + arr[k] && arr[j] < arr[m] + arr[k] &&               arr[k] < arr[m] + arr[j] && m != j && j != k && k != m) {             t += 1;           } else if (arr[m] == arr[j] + arr[k] && m != j && j != k && k != m) {             s += 1;           }         }       }     }     if (t == 0 && s != 0)       cout <<  SEGMENT ;     else if (t != 0)       cout <<  TRIANGLE ;     else if (t == 0 && s == 0)       cout <<  IMPOSSIBLE ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9 + 7;   const double EPS = 1e-9;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     long long x, y, z;     cin >> x >> y >> z;     long long sasha = x % z;     long long masha = y % z;     if ((sasha + masha) >= z) {       cout << ((x + y) / z) <<     << (z - max(sasha, masha));     } else       cout << (x / z + y / z) <<     << 0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   struct state {     int l, r, v, d;   } node[N];   int n, m, cnt, bit[2][N * 2];   vector<int> g[N];   void add(int x, int val, int *b) {     while (x <= n * 2) {       b[x] += val;       x += (x & (-x));     }   }   int get(int x, int *b) {     int ans = 0;     while (x > 0) {       ans += b[x];       x -= (x & (-x));     }     return ans;   }   void dfs(int c, int fa, int d) {     node[c].l = cnt++;     node[c].d = d;     for (int i = 0; i < g[c].size(); i++) {       if (g[c][i] == fa) continue;       dfs(g[c][i], c, 1 - d);     }     node[c].r = cnt++;   }   void init() {     cnt = 1;     scanf( %d%d , &n, &m);     memset(bit, 0, sizeof(bit));     for (int i = 1; i <= n; i++) scanf( %d , &node[i].v);     int a, b;     for (int i = 1; i < n; i++) {       scanf( %d%d , &a, &b);       g[a].push_back(b);       g[b].push_back(a);     }     dfs(1, -1, 0);   }   void solve() {     int o, a, b;     for (int i = 0; i < m; i++) {       scanf( %d , &o);       if (o == 1) {         scanf( %d%d , &a, &b);         add(node[a].l, b, bit[node[a].d]);         add(node[a].r + 1, -b, bit[node[a].d]);       } else {         scanf( %d , &a);         printf( %d n , node[a].v + get(node[a].l, bit[node[a].d]) -                            get(node[a].l, bit[1 - node[a].d]));       }     }   }   int main() {     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> v;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long t, n, x;     cin >> t;     while (t--) {       cin >> n;       long long m[n + 5];       bool flag[n + 5];       memset(m, 0, sizeof(m));       memset(flag, false, sizeof(flag));       for (long long i = 0; i < n; i++) {         cin >> x;         v.push_back(x);         m[x]++;       }       long long mx = *max_element(v.begin(), v.end());       for (long long i = 0; i < n; i++) {         long long sum = v[i];         for (long long j = i + 1; j < n; j++) {           sum += v[j];           if (sum > mx) break;           if (m[sum] && !flag[sum]) flag[sum] = true;         }       }       long long ans = 0;       for (long long i = 0; i <= n; i++) {         if (flag[i]) ans += m[i];       }       cout << ans << endl;       v.clear();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   const int MAX_N = 100005;   long long cost[MAX_N];   int r[MAX_N];   struct dish {     int idx, cnt;     long long c;     bool operator<(const dish &d) const {       if (c != d.c) return c < d.c;       return idx < d.idx;     }   };   int main() {     cin >> n >> m;     for (int i = 0; i < n; ++i) cin >> r[i];     for (int i = 0; i < n; ++i) cin >> cost[i];     multiset<dish> s;     for (int i = 0; i < n; ++i) {       s.insert({i, r[i], cost[i]});     }     vector<long long> ans(m);     for (int i = 0; i < m; ++i) {       long long tempCost = 0;       int dIdx, dCnt;       cin >> dIdx >> dCnt;       --dIdx;       auto it = s.find({dIdx, r[dIdx], cost[dIdx]});       if (it != s.end()) {         dish d = *it;         s.erase(it);         int buy = min(dCnt, r[dIdx]);         d.cnt -= buy;         r[dIdx] -= buy;         dCnt -= buy;         tempCost += d.c * buy;         if (d.cnt) s.insert(d);       }       while (!s.empty() && dCnt) {         auto it = s.begin();         dish d = *it;         s.erase(it);         int buy = min(dCnt, d.cnt);         d.cnt -= buy;         r[d.idx] -= buy;         dCnt -= buy;         tempCost += d.c * buy;         if (d.cnt) s.insert(d);       }       if (dCnt) break;       ans[i] = tempCost;     }     for (int i = 0; i < m; ++i) cout << ans[i] <<   n ;     return 0;   }


/* -> Written by <-      -----------     |K_A_Z_A_M_A|     |___________|     |    ___    |     |   (^_^)   |     |  /( | )  |     |____|_|____|       H O A N G   */      #include <bits/stdc++.h>      #define Task             running    #define F               first   #define S               second   #define pb              push_back   #define bit(x, i)       ((x >> (i)) & 1)   #define inf             1e9 + 7   #define INF             1e18 + 7   #define ll              long long   #define pii             pair <int, int>   #define debug(x)        cerr << #x <<   is   << x <<   n ;      using namespace std;      const int MOD = 1e9 + 7;   const int maxn = 1e5 + 5;   const int base = 3e5 + 1;      int n, q;   int a[300005];   vector <int> p[300005];      void Solve(int test_case) {       cin >> n >> q;       for (int i = 1; i <= n; ++ i) {           cin >> a[i];           p[a[i]].pb(i);       }       mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());       for (int i = 1; i <= q; ++ i) {           int l, r; cin >> l >> r;           int ans = 1;           for (int times = 1; times <= 40; ++ times) {               int u = a[uniform_int_distribution<int>(l,r)(rng)];               int f = upper_bound(p[u].begin(), p[u].end(), r) - lower_bound(p[u].begin(), p[u].end(), l);               ans = max(ans, 2 * f - (r - l + 1));           }           cout << ans <<   n ;       }   }         int main(){       ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);       if (fopen(Task .inp ,  r )) {           freopen(Task .inp ,  r , stdin);           freopen(Task .out ,  w , stdout);       }   //    Prepare();       int test_case = 1;   //     cin >> test_case;       for (int i = 1; i <= test_case; ++ i) {           Solve(i);       }       return 0;   }


#include <bits/stdc++.h>   int main(void) {     int m, n;     char a[100001], b[100001];     scanf( %s , a);     scanf( %s , b);     n = strlen(a);     m = strlen(b);     if (n == m) {       if (strcmp(a, b) == 0)         printf( -1 );       else         printf( %d , m);     } else if (n > m) {       printf( %d , n);     } else       printf( %d , m);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     map<int, int> m;     int sum = 0, a;     for (int i = 0; i < 5; i++) {       cin >> a;       m[a]++;       sum += a;     }     int temp = 0;     for (auto i : m) {       if (i.second == 2) temp = max(temp, i.first * 2);       if (i.second >= 3) temp = max(temp, i.first * 3);     }     cout << sum - temp;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 5005, inf = 1011111111;   int n, p[max_n], b[max_n];   int pos[max_n];   vector<pair<vector<int>, pair<vector<int>, vector<int>>>> all;   vector<pair<vector<int>, pair<vector<int>, vector<int>>>> my_all;   void find_b() {     for (int i = 0; i < n; ++i) {       pos[p[i]] = i;     }     for (int i = 0; i < n; ++i) {       b[i] = pos[i];     }   }   int ans[2][max_n];   int res[max_n], tm, used[max_n];   int main() {     cin >> n;     for (int i = 0; i + 1 < n; ++i) {       for (int j = 0; j < 2; ++j) {         cout <<  ?   << i + j <<     << i << endl;         cout.flush();         cin >> ans[j][i];       }     }     int cnt = 0;     for (int i = 0; i < n; ++i) {       p[0] = i;       for (int j = 0; j + 1 < n; ++j) {         int x = ans[0][j] ^ ans[1][j];         p[j + 1] = p[j] ^ x;       }       find_b();       int f = 1;       for (int j = 0; j + 1 < n; ++j) {         for (int k = 0; k < 2; ++k) {           if (ans[k][j] != (p[j + k] ^ b[j])) {             f = 0;             break;           }         }       }       ++tm;       for (int j = 0; j < n; ++j) {         if (p[j] >= n) {           f = 0;           break;         }         if (used[p[j]] == tm) {           f = 0;           break;         }         used[p[j]] = tm;       }       if (0) {         for (int j = 0; j < n; ++j) {           cout << p[j] <<    ;         }         cout << endl;         for (int j = 0; j < n; ++j) {           cout << b[j] <<    ;         }         cout <<  f =   << f << endl;       }       cnt += f;       if (f) {         for (int j = 0; j < n; ++j) {           res[j] = p[j];         }       }     }     cout <<  !  << endl;     cout << cnt << endl;     for (int i = 0; i < n; ++i) {       cout << res[i] <<    ;     }     cout << endl;     cout.flush();     return 0;   }


#include<bits/stdc++.h>   using namespace std;   typedef long long int ll;   int main()   {       ll t,i,maxi,sumi,mini;       cin>>t;       while(t--)       {           ll n;           cin>>n;           ll a[n];           for(i=0;i<n;i++)           cin>>a[i];           maxi=-1;           mini=a[0];           sumi=0;           for(i=0;i<n;i++)           {               sumi=sumi+a[i];               if(maxi<a[i])               maxi=a[i];               if(mini>a[i])               mini=a[i];           }           mini=0;           if(maxi*(n-1)>sumi)               {                   mini+=maxi*(n-1)-sumi;                   sumi=maxi*(n-1);               }                      mini+=((n-1)-sumi%(n-1))%(n-1);           cout<<mini<<endl;          }   }


#include <bits/stdc++.h>   using namespace std;   long long PosMod(long long a) { return a >= 0 ? a : a + 998244353; }   long long AddMod(long long a, long long b) {     return PosMod((a + b) % 998244353);   }   long long MultMod(long long a, long long b) {     return PosMod((a * b) % 998244353);   }   long long powll(long long a, long long n) {     long long r = 1;     for (long long b = n; b > 0; b >>= 1, a = MultMod(a, a))       if (b & 1) r = MultMod(r, a);     return r;   }   long long InvMod(long long a) { return powll(a, 998244353 - 2); }   long long Frac(long long a, long long b) { return MultMod(a, InvMod(b)); }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   int n, m, k;   int COL[2][2][1000100];   int ROW[2][2][1000100];   map<std::pair<int, int>, int> writtenValue;   long long col_posCnt[3];   long long row_posCnt[3];   long long valid1Cnt[2];   long long valid2Cnt[2];   int calcRPos(int i) {     int pos = 0;     pos += ROW[1][1][i] == 0 && ROW[0][0][i] == 0;     pos += ROW[0][1][i] == 0 && ROW[1][0][i] == 0;     return pos;   }   int calcCPos(int i) {     int pos = 0;     pos += COL[1][1][i] == 0 && COL[0][0][i] == 0;     pos += COL[0][1][i] == 0 && COL[1][0][i] == 0;     return pos;   }   std::pair<int, int> calcValid(int i) {     bool valid1 = ROW[1][1][i] == 0 && ROW[0][0][i] == 0;     bool valid2 = ROW[0][1][i] == 0 && ROW[1][0][i] == 0;     if (i % 2 == 0) swap(valid1, valid2);     return {(int)valid1, (int)valid2};   }   int main() {     cin.tie(0);     ios::sync_with_stdio(0);     cin >> n >> m >> k;     col_posCnt[2] = m;     row_posCnt[2] = n;     valid1Cnt[1] = n;     valid2Cnt[1] = n;     for (int i = 0; i < k; i++) {       int r, c, t;       cin >> r >> c >> t;       int prevRPos = calcRPos(r);       int prevCPos = calcCPos(c);       std::pair<int, int> prevValid = calcValid(r);       row_posCnt[prevRPos]--;       col_posCnt[prevCPos]--;       valid1Cnt[prevValid.first]--;       valid2Cnt[prevValid.second]--;       std::pair<int, int> p = {r, c};       if (writtenValue.count(p) > 0) {         int val = writtenValue[p];         COL[r % 2][val][c]--;         ROW[c % 2][val][r]--;         writtenValue.erase(p);       }       if (t == 0 || t == 1) {         writtenValue[p] = t;         COL[r % 2][t][c]++;         ROW[c % 2][t][r]++;       }       int curRPos = calcRPos(r);       int curCPos = calcCPos(c);       std::pair<int, int> curValid = calcValid(r);       row_posCnt[curRPos]++;       col_posCnt[curCPos]++;       valid1Cnt[curValid.first]++;       valid2Cnt[curValid.second]++;       long long ansR = row_posCnt[0] == 0 ? powll(2, row_posCnt[2]) : 0;       long long ansC = col_posCnt[0] == 0 ? powll(2, col_posCnt[2]) : 0;       long long dup =           (long long)(valid1Cnt[0] == 0) + (long long)(valid2Cnt[0] == 0);       cout << AddMod(AddMod(ansR, ansC), -dup) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int lcd = 2 * 2 * 2 * 3 * 3 * 5 * 7 * 11 * 13;   long long a, b, k, d[lcd], y = 0, l, i;   int main() {     cin >> a >> b >> k;     while (a > b) {       for (i = 2, l = 1; i <= k; i++)         if (a % i > l && a - a % i >= b) l = a % i;       y++;       a -= l;       if (d[a % lcd]) {         int c = y - d[a % lcd];         y += c * ((a - b) / lcd);         a -= ((a - b) / lcd) * lcd;       }       d[a % lcd] = y;     }     cout << y;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int cla[110];   int main() {     int n;     cin >> n;     int cnt = 0;     int c;     int cnt0, cnt1;     int flag = 0;     for (int i = 0; i < n; i++) {       cin >> c;       if (c == 1) {         if (cnt0 == 1 && flag == 1) cnt++;         flag = 1;         cnt0 = 0;         cnt++;       } else {         cnt0++;       }     }     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N, K;     int O = 0, E = 0;     scanf( %d%d , &N, &K);     for (int i = 0; i < N; i++) {       int t;       scanf( %d , &t);       if (t % 2 == 0)         E++;       else         O++;     }     if (N == K) {       if (O % 2)         puts( Stannis );       else         puts( Daenerys );       return 0;     }     if (K % 2 == 0 && (N - K) % 2 == 0) {       puts( Daenerys );       return 0;     }     if (K % 2 == 0 && (N - K) % 2 == 1) {       if ((N - K) / 2 >= min(O, E))         puts( Daenerys );       else         puts( Stannis );       return 0;     }     if (K % 2 == 1 && (N - K) % 2 == 1) {       if ((N - K) / 2 >= O)         puts( Daenerys );       else         puts( Stannis );       return 0;     }     if (K % 2 == 1 && (N - K) % 2 == 0) {       if ((N - K) / 2 >= E)         puts( Stannis );       else         puts( Daenerys );       return 0;     }   }


#include <bits/stdc++.h>   using namespace std;   int v[1000001];   int main() {     int n;     cin >> n;     for (int i = 2; i * i <= 1000000; i++)       if (v[i] == 0)         for (int j = i * i; j <= 1000000; j += i) v[j] = 1;     if (n > 2)       cout <<  2  << endl;     else       cout << 1 << endl;     for (int i = 2; i <= n + 1; i++)       if (v[i] == 0)         cout << 1 <<    ;       else         cout <<  2  ;     return 0;   }


#include <bits/stdc++.h>   using ll = long long;   using namespace std;   const long double PI = acos(-1);   const long long M = 998244353;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   inline long long mod(long long n, long long m = M) {     long long ret = n % m;     if (ret < 0) ret += m;     return ret;   }   long long exp(long long n, long long k, long long m = M) {     if (k == 0) return 1;     if (k == 1) return n;     long long ax = exp(n, k / 2, m);     ax = mod(ax * ax, m);     if (k % 2) ax = mod(ax * n, m);     return ax;   }   long long gcd(long long a, long long b) {     if (a == 0)       return b;     else       return gcd(b % a, a);   }   void solution() {     list<long long> l;     long long n, k;     cin >> n >> k;     vector<list<long long>::iterator> elems(n + 1);     for (long long i = 0; i < n; i++) {       long long x;       cin >> x;       l.push_back(x);       auto ed = l.end();       ed--;       elems[x] = ed;     }     long long ans = 1;     vector<bool> mark(n + 1, true);     vector<long long> b(k);     for (long long i = 0; i < k; i++) {       cin >> b[i];       mark[b[i]] = false;     }     for (long long i = 0; i < k; i++) {       auto it = elems[b[i]];       long long mult = 0;       if (it != l.begin()) {         auto it2 = it;         it2--;         mult += mark[*it2];       }       auto it2 = it;       it2++;       if (it2 != l.end()) mult += mark[*it2];       ans *= mult;       ans = mod(ans);       mark[b[i]] = true;     }     cout << ans <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     while (t--) solution();   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)5e6 + 100;   const int maxm = (int)1e6 + 100;   const int mod = (int)1e9 + 7;   const int P = (int)1e6 + 7;   const double pi = acos(-1.0);   int n, t[2][maxn], id = 1;   long long dp[2][30];   vector<int> g[maxn];   void add(int x, int pos) {     int v = 0;     for (int i = (29); i >= (0); --i) {       int bit = ((x >> i) & 1);       if (!t[bit][v]) t[bit][v] = id++;       v = t[bit][v];       g[v].push_back(pos);     }   }   void go(int v, int b = 29) {     int l = t[0][v], r = t[1][v];     if (l) go(l, b - 1);     if (r) go(r, b - 1);     if (!l || !r) return;     long long res = 0;     int ptr = 0;     for (auto x : g[l]) {       while (ptr < ((int)(g[r]).size()) && g[r][ptr] < x) ptr++;       res += ptr;     }     dp[0][b] += res;     dp[1][b] += ((int)(g[l]).size()) * 1ll * ((int)(g[r]).size()) - res;   }   void solve() {     scanf( %d , &n);     for (int i = (1); i <= (n); ++i) {       int x;       scanf( %d , &x);       add(x, i);     }     go(0);     long long inv = 0;     int res = 0;     for (int i = (0); i <= (29); ++i) {       inv += min(dp[0][i], dp[1][i]);       if (dp[1][i] < dp[0][i]) res += (1 << i);     }     printf( %lld %d , inv, res);   }   int main() {     int t = 1;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     int x[n], x1[m], x2[m], y[m];     for (int i = 0; i < n; i++) cin >> x[i];     sort(x, x + n);     vector<int> v;     for (int i = 0; i < m; i++) {       cin >> x1[i] >> x2[i] >> y[i];       if (x1[i] == 1) {         v.push_back(x2[i]);       }     }     sort(v.rbegin(), v.rend());     if (v.size() == 0) {       cout <<  0 ;       return 0;     }     int answer = 1000000000;     for (int i = 0; i <= n; i++) {       int right = 1000000000;       if (i != n) right = x[i];       if (v[0] < right)         answer = min(answer, i);       else {         int l = 0;         int r = (int)v.size() - 1;         while (r - l > 1) {           int sr = (r + l) / 2;           if (v[sr] >= right)             l = sr;           else             r = sr;         }         if (v[r] < right) r = l;         answer = min(answer, i + r + 1);       }     }     cout << answer;     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, result, count = 0, i;     scanf( %d , &n);     char arr[n];     for (i = 0; i < n; i++) {       scanf(  n%c , &arr[i]);     }     i = 0;     for (i = 0; i < n; i++) {       if (arr[i] != arr[i - 1]) {         count++;       }     }     if (count + 2 < n) {       result = count + 2;     }     if (count + 2 >= n) {       result = n;     }     printf( %d , result);   }


#include <bits/stdc++.h>   using namespace std;   typedef struct x {     int cor;     int power;     bool operator<(x T) const { return cor < T.cor; }   } beacon;   beacon arr[2000009];   vector<int> tmp;   int dp[2000009];   int main() {     int n, m, k;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d%d , &arr[i].cor, &arr[i].power);     }     sort(arr, arr + n);     for (int i = 0; i < n; i++) tmp.push_back(arr[i].cor);     dp[0] = 0;     for (int i = 1; i < n; i++) {       int p = arr[i].power;       int left = arr[i].cor - p;       vector<int>::iterator it = lower_bound(tmp.begin(), tmp.end(), left);       int l = it - tmp.begin();       dp[i] = (i - l);       if (l > 0) dp[i] += dp[l - 1];     }     int ans = INT_MAX;     for (int i = 0; i < n; i++) {       ans = min(ans, dp[i] + (n - i - 1));     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   template <class X = int>   inline X min(X a, X b, X c) {     return min(min(a, b), c);   }   template <class X = int>   inline X min(X a, X b, X c, X d) {     return min(min(a, b, c), d);   }   template <class X = int>   inline X max(X a, X b, X c) {     return max(max(a, b), c);   }   template <class X = int>   inline X max(X a, X b, X c, X d) {     return max(max(a, b, c), d);   }   template <class X = int>   inline X mid(X s, X e) {     return (s + (e - s) / 2);   }   template <class X = int>   inline X len(X s, X e) {     return (e - s + 1);   }   const int MOD = 1e9 + 7;   const int INF = 987654321;   const bool TESTCASES = 0;   const bool CODEJAM = 0;   void solve() {     int n;     string s;     cin >> n;     cin >> s;     int ans = 0;     int temp = 0, br = 0;     for (int i = 0; i < n; i++) {       br += s[i] ==  )  ? -1 : 1;       if (br < 0)         temp++;       else if (br == 0 and temp != 0) {         ans += temp + 1;         temp = 0;       }     }     if (br != 0)       cout <<  -1 n ;     else       cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int T = 1;     if (TESTCASES) cin >> T;     for (int t = 1; t <= T; t++) {       if (CODEJAM) cout <<  Case #  << t <<  :  ;       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char s1[50], s2[50];     cin >> s1 >> s2;     long long left = 0, right = 0, mid = 0, l;     l = strlen(s1);     for (long long i = 0; i < l; i++)       if (s1[i] ==  | ) {         mid = i;         break;       }     left = mid;     right = l - (mid + 1);     if ((strlen(s1) + strlen(s2) - 1) % 2 == 1) {       printf( Impossible );       return 0;     }     if (left == right) {       if (strlen(s2) % 2 == 0) {         for (int j = 0; j < left; j++) printf( %c , s1[j]);         for (int j = 0; j < strlen(s2) / 2; j++) printf( %c , s2[j]);         printf( | );         for (int j = left + 1; j < l; j++) printf( %c , s1[j]);         for (int j = strlen(s2) / 2; j < strlen(s2); j++) printf( %c , s2[j]);         return 0;       } else {         printf( Impossible );         return 0;       }     }     if (left < right) {       if (right - left > strlen(s2)) {         printf( Impossible );         return 0;       } else {         long long dif = right - left;         dif += (strlen(s2) - dif) / 2;         for (int j = 0; j < left; j++) printf( %c , s1[j]);         for (int j = 0; j < dif; j++) printf( %c , s2[j]);         printf( | );         for (int j = left + 1; j < strlen(s1); j++) printf( %c , s1[j]);         for (int j = dif; j < strlen(s2); j++) printf( %c , s2[j]);         return 0;       }     }     if (right < left) {       if (left - right > strlen(s2)) {         printf( Impossible );         return 0;       } else {         long long dif = left - right;         dif = (strlen(s2) - dif) / 2;         for (int j = 0; j < left; j++) printf( %c , s1[j]);         for (int j = 0; j < dif; j++) printf( %c , s2[j]);         printf( | );         for (int j = left + 1; j < strlen(s1); j++) printf( %c , s1[j]);         for (int j = dif; j < strlen(s2); j++) printf( %c , s2[j]);         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, x, y, z, ans = 0;     cin >> n >> x >> y >> z;     for (long long i = 0; i <= x / 2; i++) {       for (long long j = 0; j <= y; j++) {         if ((n - (i + j)) % 2 == 0 && (n - (i + j)) / 2 <= z &&             (n - (i + j)) / 2 >= 0)           ans++;       }     }     cout << ans;   }   int main() {     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int n, a, b, k, p[N];   int main() {     cin >> n >> a >> b >> k;     int sum = a + b;     for (int i = 1; i <= n; i++) {       cin >> p[i];       if (p[i] % sum == 0)         p[i] = sum;       else         p[i] %= sum;       p[i] -= a;     }     int ans = 0;     sort(p + 1, p + 1 + n);     for (int i = 1; i <= n; i++) {       if (p[i] <= 0)         ans++;       else {         int t = ceil(p[i] * 1.0 / a);         if (t <= k) k -= t, ans++;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int64_t maxn = 1e3 + 42;   vector<int64_t> g[maxn];   int64_t par[maxn], sz[maxn];   basic_string<int64_t> leaves[maxn];   void dfs(int64_t v) {     sz[v] = 1;     for (auto u : g[v]) {       if (u != par[v]) {         par[u] = v;         dfs(u);         sz[v] += sz[u];         leaves[v] += leaves[u][0];       }     }     if (g[v].size() == 1) {       leaves[v] = {v};     }   }   vector<tuple<int64_t, int64_t, int64_t>> ans;   int64_t need[maxn];   tuple<int64_t, int64_t, int64_t> changers[maxn];   void change(int64_t v, int64_t added = 0) {     for (auto u : g[v]) {       if (u != par[v]) {         int64_t a, b, c;         tie(a, b, c) = changers[u];         int64_t d = need[u] - added * (a == leaves[v][0]);         ans.emplace_back(a, b, d / 2);         ans.emplace_back(a, c, d / 2);         ans.emplace_back(b, c, -d / 2);         change(u, added * (a == leaves[v][0]) + d);       }     }   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     int64_t n;     cin >> n;     int64_t u[n], v[n], val[n];     for (int64_t i = 1; i < n; i++) {       cin >> u[i] >> v[i] >> val[i];       u[i]--, v[i]--;       g[u[i]].push_back(v[i]);       g[v[i]].push_back(u[i]);     }     bool ok = true;     for (int64_t i = 0; i < n; i++) {       ok &= g[i].size() == 1 || g[i].size() >= 3;     }     if (!ok) {       cout <<  NO  << endl;     } else if (n == 2) {       cout <<  YES  << endl;       cout << 1 << endl;       cout << 1 <<     << 2 <<     << val[1] << endl;       return 0;     } else {       cout <<  YES  << endl;       int64_t root = 0;       while (g[root].size() == 1) {         root++;       }       par[root] = root;       dfs(root);       for (int64_t i = 1; i < n; i++) {         if (sz[u[i]] > sz[v[i]]) {           swap(u[i], v[i]);         }         int64_t a = leaves[u[i]][0];         int64_t b = 0;         while (leaves[v[i]][b] == a) {           b++;         }         int64_t c = b + 1;         b = leaves[v[i]][b];         while (c < (int64_t)leaves[v[i]].size() && leaves[v[i]][c] == a) {           c++;         }         if (c == (int64_t)leaves[v[i]].size()) {           int64_t w = par[v[i]];           c = 0;           while (leaves[w][c] == a || leaves[w][c] == b) {             c++;           }           c = leaves[w][c];         } else {           c = leaves[v[i]][c];         }         changers[u[i]] = {a, b, c};         need[u[i]] = val[i];       }       change(root);       cout << ans.size() << endl;       for (auto it : ans) {         int64_t a, b, c;         tie(a, b, c) = it;         cout << a + 1 <<     << b + 1 <<     << c << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, d, h, i, j;   int main() {     cin >> n >> d >> h;     if (d > h * 2) {       cout << -1 << endl;       return 0;     }     if (d == 1 && h == 1 && n > 2) {       cout << -1 << endl;       return 0;     }     for (i = 1; i <= h; i++) cout << i <<     << i + 1 << endl;     if (d != 1 && d != h) {       cout << 1 <<     << i + 1 << endl;       i++;     }     int k = 1;     if (d == h) k++;     for (j = i; j <= d; j++) cout << j <<     << j + 1 << endl;     for (i = j + 1; i <= n; i++) cout << k <<     << i << endl;   }


#include <bits/stdc++.h>   using namespace std;   int dp[1000001];   int a[1000001];   vector<int> v[1000001];   vector<int> an;   int main() {     memset((dp), (0), sizeof(dp));     memset((a), (0), sizeof(a));     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) {       int x;       scanf( %d , &x);       if (x <= m) {         a[x]++;         v[x].push_back(i + 1);       }     }     for (int i = 1; i <= m; i++) {       int x = i;       while (x <= m) {         dp[x] += a[i];         x += i;       }     }     int ans = -1, ind = -1;     for (int i = 1; i < 1000001; i++)       if (ans < dp[i]) ans = dp[i], ind = i;     printf( %d , ind);     printf(   );     printf( %d , dp[ind]);     printf(  n );     for (int i = 1; i <= m; i++)       if (ind % i == 0 && a[i] > 0)         for (int j : v[i]) an.push_back(j);     sort(an.begin(), an.end());     for (int i : an) printf( %d , i), printf(   );     printf(  n );   }


#include <bits/stdc++.h>   using namespace std;   long long int n, k, pres[300005], cont[300005];   vector<long long int> fcts[300005];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> k;     long long int tot = 0;     pres[1] = 1;     for (long long int i = 1; i <= n; ++i)       for (int j = 2 * i; j <= n; j += i) fcts[j].push_back(i);     for (long long int i = 2; i <= n; ++i) {       if (tot >= k) {         n = i - 1;         break;       }       tot += fcts[i].size();       cont[i] += fcts[i].size();       for (long long int v : fcts[i]) cont[v]++;       pres[i] = 1;     }     if (tot < k) {       cout <<  No n ;       return 0;     }     set<pair<long long int, long long int> > conts;     for (long long int i = 1; i <= n; ++i) conts.insert(make_pair(cont[i], i));     long long int rmv = tot - k;     while (rmv) {       auto it = conts.lower_bound(make_pair(rmv + 1, -1LL));       if (it == conts.begin()) {         cout <<  No n ;         return 0;       }       it--;       pair<long long int, long long int> xp = *it;       pres[xp.second] = 0;       rmv -= xp.first;       conts.erase(it);       for (long long int v : fcts[xp.second]) {         if (pres[v]) {           conts.erase(make_pair(cont[v], v));           cont[v]--;           conts.insert(make_pair(cont[v], v));         }       }       for (long long int v = 2 * xp.second; v <= n; v += xp.second)         if (pres[v]) {           conts.erase(make_pair(cont[v], v));           cont[v]--;           conts.insert(make_pair(cont[v], v));         }     }     cout <<  Yes n ;     vector<long long int> ans;     for (long long int i = 1; i <= n; ++i)       if (pres[i]) ans.push_back(i);     cout << ans.size() <<   n ;     for (long long int v : ans) cout << v <<    ;     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   struct Point {     double x, y;     Point(double x = 0, double y = 0) : x(x), y(y) {}   };   Point operator+(Point a, Point b) { return Point(a.x + b.x, a.y + b.y); }   Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); }   Point operator*(Point a, double p) { return Point(a.x * p, a.y * p); }   Point operator/(Point a, double p) { return a * (1 / p); }   bool operator<(const Point& a, const Point& b) {     return a.x < b.x || (a.x == b.x && a.y < b.y);   }   const double EPS = 1e-10;   int dcmp(double x) {     if (fabs(x) < EPS)       return 0;     else       return x < 0 ? -1 : 1;   }   bool operator==(const Point& a, const Point& b) {     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;   }   double ang(Point v) { return atan2(v.y, v.x); }   double Dot(Point A, Point B) { return A.x * B.x + A.y * B.y; }   double Length(Point A) { return sqrt(Dot(A, A)); }   double Angle(Point A, Point B) {     return acos(Dot(A, B) / Length(A) / Length(B));   }   double Cross(Point A, Point B) { return A.x * B.y - A.y * B.x; }   Point CirCenter(Point a, Point b, Point c) {     double a1 = b.x - a.x, b1 = b.y - a.y, c1 = (a1 * a1 + b1 * b1) / 2;     double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2 * a2 + b2 * b2) / 2;     double d = a1 * b2 - a2 * b1;     return Point(a.x + (c1 * b2 - c2 * b1) / d, a.y + (a1 * c2 - a2 * c1) / d);   }   vector<Point> ConvexHull(vector<Point> p) {     sort(p.begin(), p.end());     p.erase(unique(p.begin(), p.end()), p.end());     int n = p.size();     vector<Point> ch(n + 1);     int m = 0;     for (int i = 0; i < n; i++) {       while (m > 1 && dcmp(Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) <= 0)         m--;       ch[m++] = p[i];     }     int k = m;     for (int i = n - 2; i >= 0; i--) {       while (m > k && dcmp(Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) <= 0)         m--;       ch[m++] = p[i];     }     if (n > 1) m--;     ch.resize(m);     return ch;   }   bool judge(const vector<Point>& a, const vector<Point>& b, int i, int j) {     double minr = -DBL_MAX, maxr = DBL_MAX;     int mid = -1;     int na = a.size(), nb = b.size();     bool flag = true;     for (int k = i + 1; k < j; k++) {       if (dcmp(Cross(a[k] - a[i], a[k] - a[j])) == 0) {         flag = false;         break;       }       Point center = CirCenter(a[i], a[j], a[k]);       double r = Length(center - (a[i] + a[j]) / 2.0);       if (dcmp(Cross(center - a[i], a[j] - a[i])) *               dcmp(Cross(a[k] - a[i], a[j] - a[i])) <           0)         r = -r;       if (dcmp(r - minr) > 0) minr = r, mid = k;     }     if (flag) {       for (int k = (j + 1) % na;; k = (k + 1) % na) {         if (k == i) break;         if (dcmp(Cross(a[k] - a[i], a[k] - a[j])) == 0) {           flag = false;           break;         }         Point center = CirCenter(a[i], a[j], a[k]);         double r = Length(center - (a[i] + a[j]) / 2.0);         if (dcmp(Cross(center - a[i], a[j] - a[i])) *                 dcmp(Cross(a[k] - a[i], a[j] - a[i])) >             0)           r = -r;         if (dcmp(r - maxr) < 0) maxr = r;       }     }     bool ok = true;     if (flag) {       for (int k = 0; k < nb; k++) {         if (dcmp(Cross(b[k] - a[i], a[j] - a[i])) == 0 &&             dcmp(Dot(a[i] - b[k], a[j] - b[k])) <= 0) {           ok = false;           break;         }         Point center = CirCenter(a[i], a[j], b[k]);         double r = Length(center - (a[i] + a[j]) / 2.0);         if (dcmp(Cross(center - a[i], a[j] - a[i])) *                 dcmp(Cross(b[k] - a[i], a[j] - a[i])) <             0)           r = -r;         if (dcmp(Cross(b[k] - a[i], a[j] - a[i])) > 0) {           if (dcmp(r - maxr) < 0) maxr = r;         } else if (dcmp(Cross(b[k] - a[i], a[j] - a[i])) < 0) {           r = -r;           if (dcmp(minr - r) < 0) minr = r;         }         if (dcmp(maxr - minr) <= 0) {           ok = false;           break;         }       }     } else       ok = false;     return ok || (mid >= 0 && (judge(a, b, i, mid) || judge(a, b, mid, j)));   }   int n, m;   int main() {     scanf( %d%d , &n, &m);     vector<Point> ina(n), inb(m);     int x, y;     for (int i = 0; i < n; i++)       scanf( %d%d , &x, &y), ina[i].x = (double)x, ina[i].y = (double)y;     for (int i = 0; i < m; i++)       scanf( %d%d , &x, &y), inb[i].x = (double)x, inb[i].y = (double)y;     vector<Point> a = ConvexHull(ina), b = ConvexHull(inb);     puts((judge(a, inb, 0, a.size() - 1) || judge(b, ina, 0, b.size() - 1))              ?  YES               :  NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int qtd[200007];   int vogal(char ch) {     return ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u  ||            ch ==  A  || ch ==  E  || ch ==  I  || ch ==  O  || ch ==  U ;   }   map<int, int> prim;   int main() {     string s;     cin >> s;     int resp = 0;     int qtdr = 0;     for (int i = 1; i <= s.length(); i++) {       qtd[i] = qtd[i - 1];       qtd[i] += vogal(s[i - 1]) ? -1 : 2;       if (qtd[i] >= 0)         resp = i;       else {         if (!prim.count(qtd[i])) prim[qtd[i]] = i;         resp = max(resp, i - prim[qtd[i]]);       }     }     if (resp == 0) {       cout <<  No solution n ;       return 0;     }     for (int i = resp; i <= s.length(); i++) {       if (qtd[i - resp] <= qtd[i]) qtdr++;     }     cout << resp <<     << qtdr <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const long long inf = 1e18;   const long double pie = acos((long double)-1);   int dx[] = {1, -1, 0, 0};   int dy[] = {0, 0, 1, -1};   unordered_map<int, vector<int>> mp;   int indegree[1001];   void addedge(int x, int y) {     mp[x].push_back(y);     mp[y].push_back(x);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     auto start = std::chrono::high_resolution_clock::now();     int n, m;     cin >> n >> m;     while (m--) {       int x, y;       cin >> x >> y;       x--;       y--;       addedge(x, y);     }     int cnt = 0;     for (auto x : mp) {       vector<int> v = x.second;       for (auto y : v) {         indegree[y]++;       }     }     while (true) {       int c = 0;       vector<int> ans;       for (int i = 0; i < n; ++i) {         if (indegree[i] == 1) {           ans.push_back(i);           indegree[i] = 0;           c++;         }       }       if (c != 0) {         cnt++;       }       for (auto x : ans) {         for (auto y : mp[x]) {           if (indegree[y] > 0) indegree[y]--;         }       }       int flag = 0;       for (int i = 0; i < n; ++i) {         if (indegree[i] == 1) {           flag++;           break;         }       }       if (flag == 0) {         break;       }     }     cout << cnt <<   n ;     auto stop = std::chrono::high_resolution_clock::now();     auto duration =         std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxm = 1.5e7;   const int maxn = 1e5 + 5;   int lson[maxm], rson[maxm];   long long sum[maxm];   int T[maxn];   int clk, n;   void modity(int &rt, int L, int R, int pos, int val) {     if (!rt) rt = ++clk;     if (L == R) {       sum[rt] += val;     } else {       int mid = L + R >> 1;       if (pos <= mid)         modity(lson[rt], L, mid, pos, val);       else         modity(rson[rt], mid + 1, R, pos, val);       sum[rt] = sum[lson[rt]] + sum[rson[rt]];     }   }   long long query(int rt, int L, int R, int l, int r) {     if (!rt) return 0;     if (l <= L && R <= r)       return sum[rt];     else {       int mid = L + R >> 1;       long long ret = 0;       if (l <= mid) ret += query(lson[rt], L, mid, l, r);       if (mid < r) ret += query(rson[rt], mid + 1, R, l, r);       return ret;     }   }   int lowbit(int x) { return x & -x; }   void modity(int pos, pair<int, int> val) {     for (int i = pos; i <= n; i += lowbit(i)) {       modity(T[i], 1, n, val.first, val.second);     }   }   long long query(int l, int r) {     long long ret = 0;     for (int i = r; i > 0; i -= lowbit(i)) ret += query(T[i], 1, n, l, n);     for (int i = l - 1; i > 0; i -= lowbit(i)) ret -= query(T[i], 1, n, l, n);     return ret;   }   set<int> s[maxn];   int a[maxn];   int main() {     int m;     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       if (s[a[i]].size()) {         int prev = *s[a[i]].rbegin();         modity(i, make_pair(prev, i - prev));       }       s[a[i]].insert(i);     }     while (m--) {       int opt;       scanf( %d , &opt);       if (opt == 1) {         int pos, val;         scanf( %d %d , &pos, &val);         if (val == a[pos]) continue;         auto it = s[a[pos]].find(pos);         auto is = it;         it++;         if (it != s[a[pos]].end()) {           modity(*it, make_pair(*is, *is - *it));           if (is != s[a[pos]].begin()) {             auto ix = is;             ix--;             modity(*it, make_pair(*ix, *it - *ix));           }         }         if (is != s[a[pos]].begin()) {           auto ix = is;           ix--;           modity(*is, make_pair(*ix, *ix - *is));         }         s[a[pos]].erase(pos);         a[pos] = val;         is = s[a[pos]].insert(pos).first;         if (is != s[a[pos]].begin()) {           auto ix = is;           ix--;           modity(*is, make_pair(*ix, *is - *ix));         }         it = is;         it++;         if (it != s[a[pos]].end()) {           if (is != s[a[pos]].begin()) {             auto ix = is;             ix--;             modity(*it, make_pair(*ix, *ix - *it));           }           modity(*it, make_pair(*is, *it - *is));         }       } else {         int l, r;         scanf( %d %d , &l, &r);         long long ret = query(l, r);         printf( %lld n , ret);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const long long maxn = 1e5 + 10;   vector<long long> a[maxn];   long long lo[maxn], hi[maxn];   const long long inf = 1e15;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout << fixed << setprecision(9);     long long n;     cin >> n;     long long cnt = 0;     map<long long, long long> mp;     vector<long long> keep;     for (long long i = 0; i < n; ++i) {       long long len;       cin >> len;       bool ok = false;       long long mn = inf;       long long mx = -inf;       for (long long j = 0; j < len; ++j) {         long long cur;         cin >> cur;         a[i].push_back(cur);         if (cur > mn) ok = true;         mn = min(mn, cur);         mx = max(mx, cur);       }       lo[i] = mn;       hi[i] = mx;       if (ok) {         ++cnt;         hi[i] = inf;       } else {         keep.push_back(mx);       }     }     sort(keep.begin(), keep.end());     long long res = 0;     for (long long i = 0; i < n; ++i) {       if (hi[i] == inf) {         res += n;       } else {         long long cur = cnt;         auto it = upper_bound(keep.begin(), keep.end(), lo[i]);         if (it != keep.end()) {           long long ind = it - keep.begin();           long long is = (long long)keep.size() - ind;           cur += is;         }         res += cur;       }     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 100;   int a[maxn], ans[maxn];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m, c;     cin >> n >> m >> c;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) {       int val;       cin >> val;       ans[i] += val;       ans[i + n - m + 1] -= val;     }     ans[0] %= c;     for (int i = 1; i < n; i++) ans[i] = (ans[i] + ans[i - 1]) % c;     for (int i = 0; i < n; i++) cout << ((a[i] + ans[i]) % c + c) % c <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int i, j, k, l, n, m, s, tot, ss, t;   const int pp = 1000000007;   long long a[100010], an;   void dfs(int x, bitset<120> y, long long s) {     if (x > m / 2) {       an = (an + s) % pp;       return;     }     dfs(x + 1, y, s);     if ((a[x]) && (!y[x]))       dfs(x + 1, y | (y << x) | (y >> x) | (y << (m - x)) | (y >> (m - x)),           (s * a[x]) % pp);   }   int main() {     scanf( %d%d%d , &n, &m, &t);     for (int i = 1; i <= t; i++) {       int x;       scanf( %d , &x);       x %= m;       a[min(x, m - x)]++;     }     dfs(0, bitset<120>(1), 1);     printf( %I64d , an);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t;     cin >> t;     while (t--) {       long long int n, k;       cin >> n >> k;       vector<set<long long int>> v(k);       for (long long int i = 0; i < n; ++i) {         char c;         cin >> c;         if (c ==  ? ) continue;         if (c ==  1 )           v[i % k].insert(1);         else           v[i % k].insert(2);       }       bool ans = true;       long long int count_0 = 0, count_1 = 0, c_q = 0;       for (auto s : v) {         if (s.size() > 1) ans = false;       }       for (auto s : v) {         if (s.empty()) {           c_q++;           continue;         }         if (*s.begin() == 1) {           count_1++;           continue;         } else {           count_0++;           continue;         }       }       if (c_q < abs(count_0 - count_1)) {         ans = false;       }       if (!ans) {         cout <<  NO               <<   n ;       } else {         cout <<  YES               <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   #define ll long long            int main()   {          ios_base::sync_with_stdio(false);       cin.tie(NULL);       int t;       cin >> t;       int n, k;       while (t--)       {           char a[3] = { a , b , c };           cin >> n >> k;            int temp = k;            while(k--) {               cout<<a[0];           }           int i = 1;           n = n - temp;           //cout << n;           while(n--) {                              if(i == 3){                   i = 0;               }               cout<<a[i];               i++;           }              cout << endl;       }          return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool check(long long mid, vector<long long> &d, vector<long long> &prep) {     long long n = d.size();     long long m = prep.size() - 1;     vector<long long> pos(m + 1, -1);     for (long long i = 0; i <= mid; i++) pos[d[i]] = i;     for (long long i = 1; i <= m; i++)       if (pos[i] == -1) return false;     long long prep_days = 0;     for (long long i = 0; i <= mid; i++) {       if (d[i] == 0)         prep_days++;       else if (pos[d[i]] == i) {         if (prep_days < prep[d[i]])           return false;         else           prep_days -= prep[d[i]];       } else         prep_days++;     }     return true;   }   int32_t main() {     long long n, m;     cin >> n >> m;     vector<long long> d(n);     for (long long &i : d) cin >> i;     vector<long long> prep(m + 1);     for (long long i = 1; i <= m; i++) cin >> prep[i];     long long l = 0, r = n - 1;     while (l < r) {       long long mid = (l + r) / 2;       if (check(mid, d, prep)) {         r = mid;       } else {         l = mid + 1;       }     }     if (r == n - 1) {       if (check(r, d, prep))         cout << n <<   n ;       else         cout << -1 <<   n ;     } else       cout << r + 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   int sz(const T &a) {     return (int)a.size();   }   long long int am[101];   int main() {     int n;     cin >> n;     long long int trip;     long long int str;     cin >> trip >> str;     long long int a, b;     bool lastinc = false;     cout <<  +   << 1 << endl;     cin >> trip >> str;     cout <<  +   << 2 << endl;     cin >> a >> b;     if (b > str)       lastinc = true;     else       lastinc = false;     trip = a, str = b;     cout <<  +   << 3 << endl;     cin >> a >> b;     if (a > trip) {       am[3] = (long long int)sqrt(2 * (a - trip)) + 2;     } else {       if (lastinc)         am[3] = 2;       else         am[3] = 1;     }     long long int diff = b - str;     trip = a, str = b;     cout <<  +   << 1 << endl;     cin >> a >> b;     am[1] = (long long int)sqrt(2 * (a - trip)) + 2;     am[2] = (b - str) / am[3];     if (diff > (am[1] - 1) * (am[2]))       lastinc = true;     else       lastinc = false;     trip = a, str = b;     for (int i = 4; i < n; i++) {       cout <<  +   << i << endl;       cin >> a >> b;       if (a > trip) {         am[i] = (long long int)sqrt(2 * (a - trip)) + 1;       } else {         if (lastinc)           am[i] = 1;         else           am[i] = 0;       }       am[i]++;       if (b - str > (am[i - 2]) * (am[i - 1]))         lastinc = true;       else         lastinc = false;       trip = a, str = b;     }     for (int i = 3; i < n; i++) {       str -= am[i] * am[i - 1] * am[i - 2];     }     am[n] = (str / am[n - 1] / am[n - 2]) + 1;     am[1]--;     cout <<  !  ;     for (int i = 1; i <= n; i++) cout << am[i] - 1 <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Ss[10005], Ee[10005];   int pre[10005 * 200][26];   char A[10005], s[10005], e[10005];   int main() {     scanf( %s%s%s , A, s, e);     int n = strlen(A);     int lens = strlen(s);     int lene = strlen(e);     int i, j;     memset(Ss, 0, sizeof(Ss));     memset(Ee, 0, sizeof(Ee));     memset(pre, -1, sizeof(pre));     for (i = 0; i <= n - lens; i++) {       for (j = 0; j < lens; j++)         if (A[i + j] != s[j]) break;       if (j >= lens) Ss[i] = 1;     }     for (i = 0; i <= n - lene; i++) {       for (j = 0; j < lene; j++)         if (A[i + j] != e[j]) break;       if (j >= lene) Ee[i + lene - 1] = 1;     }     int m = (lens > lene ? lens : lene);     int cnt = 0;     int alloc = 1;     for (i = 0; i <= n - m; i++) {       if (!Ss[i]) continue;       int cur = 0;       for (j = i; j < i + m - 1; j++) {         if (pre[cur][A[j] -  A ] == -1) pre[cur][A[j] -  A ] = alloc++;         cur = pre[cur][A[j] -  A ];       }       for (j = i + m - 1; j < n; j++) {         if (Ee[j])           if (pre[cur][A[j] -  A ] == -1) cnt++;         if (pre[cur][A[j] -  A ] == -1) pre[cur][A[j] -  A ] = alloc++;         cur = pre[cur][A[j] -  A ];       }     }     printf( %d n , cnt);     return 0;   }


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