#include <bits/stdc++.h>   using namespace std;   int dx[] = {-1, 0, 1, 0};   int dy[] = {0, 1, 0, -1};   int dx2[] = {1, -1, -1, 1, 0, 0, -1, 1};   int dy2[] = {1, -1, 1, -1, 1, -1, 0, 0};   int kmx[] = {-1, -1, 1, 1, 2, -2, 2, -2};   int kmy[] = {2, -2, 2, -2, -1, -1, 1, 1};   int bishopX[] = {-1, 1, -1, 1};   int bishopY[] = {-1, -1, 1, 1};   class Timer {    public:     clock_t T;     Timer() { T = clock(); }     ~Timer() {       fprintf(stderr,   n%.3f n , double(clock() - T) / CLOCKS_PER_SEC);     }   };   int read() {     int x;     scanf( %d , &x);     return x;   }   long long readL() {     long long x;     scanf( %lld , &x);     return x;   }   const int N = 1e5 + 5;   int visX[N][4], visY[N][4];   vector<int> v1[4 * N], v2[4 * N];   int n, m, k;   long long ans[N];   pair<int, int> a[N];   int main() {     n = read();     m = read();     k = read();     for (int i = 0; i < k; i++) {       int x = read();       int y = read();       ans[i] = -1;       v1[x - y + N].push_back(i);       v2[x + y].push_back(i);       a[i] = make_pair(x, y);     }     int x = 0;     int y = 0;     int d = 0;     long long gt = 0;     int mn;     while (1) {       switch (d) {         case 0:           mn = min(n - x, m - y);           x = x + mn;           y = y + mn;           gt += mn;           break;         case 1:           mn = min(x, y);           x = x - mn;           y = y - mn;           gt += mn;           break;         case 2:           mn = min(x, m - y);           x = x - mn;           y = y + mn;           gt += mn;           break;         case 3:           mn = min(n - x, y);           x = x + mn;           y = y - mn;           gt += mn;           break;       }       int dif = x - y + N;       int sum = x + y;       if (d == 0 || d == 1)         for (int i = 0; i < v1[dif].size(); i++) {           int j = v1[dif][i];           if (ans[j] == -1) {             int dddd = abs(x - a[j].first);             ans[j] = gt - dddd;           }         }       else         for (int i = 0; i < v2[sum].size(); i++) {           int j = v2[sum][i];           if (ans[j] == -1) {             int dddd = abs(x - a[j].first);             ans[j] = gt - dddd;           }         }       if (x == 0 && y == 0 || x == 0 && y == m || x == n && y == 0 ||           x == n && y == m) {         break;       }       if (x == n) {         if (d == 0)           d = 2;         else if (d == 3)           d = 1;         continue;       }       if (y == m) {         if (d == 2)           d = 1;         else if (d == 0)           d = 3;         continue;       }       if (x == 0) {         if (d == 2)           d = 0;         else if (d == 1)           d = 3;         continue;       }       if (y == 0) {         if (d == 1)           d = 2;         else if (d == 3)           d = 0;         continue;       }     }     for (int i = 0; i < k; i++) {       printf( %lld n , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     int n, i, j, k, c, v, l = 1, r = 1, maxm = 0, minm;     cin >> n;     int A[n];     string s;     cin >> s;     for (i = 0; i < n; i++) A[i] = 0;     for (i = 0; i < n; i++) {       for (j = i; j < n; j++) {         swap(s[i], s[j]);         v = 0;         c = 0;         for (k = 0; k < n; k++) {           if (s[k] ==  ( )             v++;           else             v--;           A[k] = v;         }         if (v == 0) {           minm = 0;           for (k = 0; k < n; k++) {             if (A[k] < minm) {               minm = A[k];               c = 1;             } else if (A[k] == minm)               c++;           }           if (c > maxm) {             maxm = c;             l = i + 1;             r = j + 1;           }         }         swap(s[i], s[j]);       }     }     cout << maxm << endl;     cout << l <<     << r << endl;   }


#include <bits/stdc++.h>   using namespace std;   int div1(int a, int b) {     if (a % b == 0)       return a / b;     else       return a / b + 1;   }   int main() {     int k, n, s, p;     while (scanf( %d%d%d%d , &k, &n, &s, &p) != EOF) {       int ever_need = div1(n, s);       int ans = div1(ever_need * k, p);       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 105;   const int oo = 1e9;   int g[N][N], n, m;   int col[N];   vector<pair<int, int> > ans;   pair<int, int> get(int val) {     for (int i = 1; i <= m; ++i) {       if (g[1][i] < val) return {oo, val};     }     memset(col, 0, sizeof(col));     ans.clear();     int ret = val;     for (int i = 1; i <= m; ++i) col[i] = g[1][i] - val;     ans.push_back({1, val});     for (int i = 2; i <= n; ++i) {       int mn = oo, mx = -oo;       for (int j = 1; j <= m; ++j) {         if (g[i][j] < col[j]) return {oo, val};         mn = min(mn, g[i][j] - col[j]);         mx = max(mx, g[i][j] - col[j]);       }       if (mn != mx) return {oo, val};       ans.push_back({i, mn});       ret += mn;     }     for (int i = 1; i <= m; ++i) {       ans.push_back({-i, col[i]});       ret += col[i];     }     return {ret, val};   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         scanf( %d , &g[i][j]);       }     }     pair<int, int> ret = {oo, -1};     for (int i = 0; i <= 500; ++i) ret = min(ret, get(i));     if (ret.first == oo) {       printf( -1 n );       return 0;     }     get(ret.second);     printf( %d n , ret.first);     for (auto u : ans) {       if (u.first < 0) {         for (int i = 1; i <= u.second; ++i) printf( col %d n , -u.first);       } else {         for (int i = 1; i <= u.second; ++i) printf( row %d n , u.first);       }     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;   #define ll long long    #define pb push_back   #define inf 1e18   #define nn    n    #define vll vector<long long int>   #define vl vector<long int>   #define ff first   #define ss second                     int main()   {       #ifndef ONLINE_JUDGE       freopen( input.txt ,  r , stdin);       freopen( output.txt ,  w , stdout);       #endif          int t;       cin>>t;          while(t--)       {        ll int hero_attack,hero_health,n;        cin>>hero_attack>>hero_health>>n;           vector<pair<ll int,ll int>> v;        ll int attack[n];        for(int i=0;i<n;i++)        {         cin>>attack[i];              }           ll int health[n];                for(int i=0;i<n;i++)        {         cin>>health[i];              }           for(int i=0;i<n;i++)        {         v.pb({attack[i],health[i]});        }           sort(v.begin(),v.end());               int flag=1;           for(auto &it:v)           {                ll int x,y;            if(hero_health<=0)            {             flag=0;             cout<< NO <<nn;             break;            }               if(it.second%hero_attack!=0)            {            x=(it.second/hero_attack)+1;            }            if(it.second%hero_attack==0)            {            x=it.second/hero_attack;            }            if(hero_health%it.first!=0)            {            y=(hero_health/it.first)+1;            }            if(hero_health%it.first==0)            {            y=hero_health/it.first;            }                  if(x<=y)            {             hero_health=hero_health-x*it.first;               }            else            {             cout<< NO <<nn;             flag=0;             break;               }                    }              if(flag==1)            cout<< YES <<nn;                              }          return 0;      }


#include <bits/stdc++.h>   using namespace std;   const int size = 400007;   const long long modulo = 1000000007;   const int INF = 2e9;   const double EPS = 1e-8;   const double PI = acos(-1.0);   int d[2][size];   char board[2][size];   int compo[size];   int l[size], r[size];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     cin.ignore();     gets(board[0]);     gets(board[1]);     for (int i = 0; i < 2; i++)       for (int j = 0; j < size; j++) d[i][j] = 2e9;     int j = 0, com = -1;     vector<bool> start(n, false);     while (j < n) {       while (j < n && board[0][j] ==  X  && board[1][j] ==  X ) ++j;       if (j < n) {         start[j] = true;         ++com;         compo[j] = com;       }       d[0][j] = (board[0][j] ==  X  ? 2e9 : 0);       d[0][j + n] = (board[1][j] ==  X  ? 2e9 : 1);       d[1][j] = (board[0][j] ==  X  ? 2e9 : 1);       d[1][j + n] = (board[1][j] ==  X  ? 2e9 : 0);       int col = j + 1;       while (col < n) {         bool ok = false;         for (int st = 0; st < 2; st++)           for (int cell = col; cell < 2 * n; cell += n)             if (board[cell / n][col] !=  X  &&                 d[st][cell] > d[st][cell - 1] + 1) {               ok = true;               d[st][cell] = d[st][cell - 1] + 1;             }         for (int st = 0; st < 2; st++)           for (int cell = col; cell < 2 * n; cell += n)             if (board[cell / n][col] !=  X  &&                 d[st][cell] > d[st][(cell + n) % (2 * n)] + 1) {               d[st][cell] = d[st][(cell + n) % (2 * n)] + 1;             }         if (ok) {           compo[col] = com;           ++col;         } else {           break;         }       }       j = col;     }     if (board[0][0] ==  .  && board[1][0] ==  . )       l[0] = -1;     else       l[0] = 0;     for (int i = 1; i < n; i++) {       if (board[0][i] ==  .  && board[1][i] ==  . )         l[i] = l[i - 1];       else         l[i] = i;     }     if (board[0][n - 1] ==  .  && board[1][n - 1] ==  . )       r[n - 1] = n;     else       r[n - 1] = n - 1;     for (int i = n - 2; i >= 0; i--) {       if (board[0][i] ==  .  && board[1][i] ==  . )         r[i] = r[i + 1];       else         r[i] = i;     }     while (m--) {       int a, b;       cin >> a >> b;       if (a > b) swap(a, b);       if (a + n == b)         cout << 1 << endl;       else {         --a;         --b;         int a_row = a / n, a_col = a % n, b_row = b / n, b_col = b % n;         if (a_col > b_col) {           swap(a_row, b_row);           swap(a_col, b_col);           swap(a, b);         }         if (compo[a_col] != compo[b_col]) {           cout << -1 << endl;           continue;         }         int ans = min(d[0][b], d[1][b]);         if (l[a_col] == a_col)           ans -= min(d[0][a], d[1][a]);         else if (start[a_col])           ans = d[a_row][b];         else {           if (r[a_col] > b_col)             ans = b_col - a_col + (a_row != b_row);           else {             int right_exit = (board[0][r[a_col]] ==  .  ? 0 : 1);             ans = min(d[0][b] - d[0][a], d[1][b] - d[1][a]);             int pref = (d[0][b] - d[0][a] < d[1][b] - d[1][a] ? 0 : 1);             int lind = l[a_col];             int left_exit = -1;             if (lind < 0 || start[lind + 1])               left_exit = pref;             else if (board[0][lind] ==  . )               left_exit = 0;             else               left_exit = 1;             if (right_exit != a_row && left_exit != a_row) ans += 2;           }         }         cout << ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   static const int INF = 0x3f3f3f3f;   static const long long INFL = 0x3f3f3f3f3f3f3f3f;   static const double EPS = 1e-10;   static const double PI = acos(-1.0);   inline int read() {     int X = 0;     bool flag = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) flag = 0;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       X = (X << 1) + (X << 3) + ch -  0 ;       ch = getchar();     }     if (flag) return X;     return ~(X - 1);   }   inline void write(int X) {     if (X < 0) {       X = ~(X - 1);       putchar( - );     }     if (X > 9) write(X / 10);     putchar(X % 10 +  0 );   }   static const int MAXN = 500 + 10;   vector<int> g[MAXN];   int d[1 << 19], pre[1 << 19];   int n, m, u, v;   int main() {     scanf( %d%d , &n, &m);     while (m--) {       scanf( %d%d , &u, &v);       u--, v--;       g[u].push_back(v);       g[v].push_back(u);     }     memset(d, -1, sizeof(d));     d[n - 1] = 0;     queue<int> Q;     Q.push(n - 1);     while (!Q.empty()) {       int t = Q.front();       Q.pop();       bool flag = false;       if (t & (1 << 18)) {         flag = true;         t ^= (1 << 18);       }       int u = t >> 9, v = t & 511;       if (flag) t ^= (1 << 18);       if (flag) {         for (int i = 0; i < g[v].size(); i++)           if (g[v][i] != u && d[(u << 9) | g[v][i]] == -1) {             d[(u << 9) | g[v][i]] = d[t] + 1;             pre[(u << 9) | g[v][i]] = t;             Q.push((u << 9) | g[v][i]);           }       } else {         for (int i = 0; i < g[u].size(); i++)           if (d[(1 << 18) | (g[u][i] << 9) | v] == -1) {             d[(1 << 18) | (g[u][i] << 9) | v] = d[t];             pre[(1 << 18) | (g[u][i] << 9) | v] = t;             Q.push((1 << 18) | (g[u][i] << 9) | v);           }       }     }     int pos = (n - 1) << 9;     if (d[pos] == -1)       puts( -1 );     else {       printf( %d n , d[pos]);       vector<int> ans1, ans2;       ans1.push_back(n - 1);       ans2.push_back(0);       while (pos != n - 1) {         pos = pre[pre[pos]];         ans1.push_back(pos >> 9);         ans2.push_back(pos & 511);       }       reverse(ans1.begin(), ans1.end());       reverse(ans2.begin(), ans2.end());       for (auto it : ans1) printf( %d  , it + 1);       puts(  );       for (auto it : ans2) printf( %d  , it + 1);       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   pair<int, int> joker[2] = {{-1, -1}, {-1, -1}};   pair<int, int> s1 = {-1, -1}, s2 = {-1, -1};   string ranks =  23456789TJQKA , suits =  CDHS , a[64][64];   set<string> deck;   vector<string> left_cards;   void gen_deck() {     for (char r : ranks)       for (char s : suits) {         string res =   ;         res = res + r;         res = res + s;         deck.insert(res);       }   }   bool can(int ii, int jj) {     set<char> cur_ranks;     set<char> cur_suits;     for (int i = 0; i < 3; i++)       for (int j = 0; j < 3; j++) {         cur_ranks.insert(a[ii + i][jj + j][0]);         cur_suits.insert(a[ii + i][jj + j][1]);       }     return ((((int)(cur_ranks).size()) == 9) || (((int)(cur_suits).size()) == 1));   }   bool check() {     for (int lr = 0; lr < n - 2; lr++)       for (int lc = 0; lc < m - 2; lc++)         for (int rr = 0; rr < n - 2; rr++)           for (int rc = 0; rc < m - 2; rc++) {             if (rr < lr + 3 && rc < lc + 3) continue;             if (can(lr, lc) && can(rr, rc)) {               s1 = {lr, lc};               s2 = {rr, rc};               return 1;             }           }     return 0;   }   int main() {     ios::sync_with_stdio(false);     gen_deck();     cin >> n >> m;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> a[i][j];         if (a[i][j][0] ==  J ) joker[a[i][j][1] -  1 ] = {i, j};         deck.erase(a[i][j]);       }     for (auto it : deck) left_cards.push_back(it);     for (int i = 0; i < ((int)(left_cards).size()); i++)       for (int j = 0; j < ((int)(left_cards).size()); j++) {         if (i == j) continue;         if (joker[0].first != -1)           a[joker[0].first][joker[0].second] = left_cards[i];         if (joker[1].first != -1)           a[joker[1].first][joker[1].second] = left_cards[j];         if (check()) {           cout <<  Solution exists.  << endl;           if (joker[0].first < 0 && joker[1].first < 0) {             cout <<  There are no jokers.  << endl;           } else if (!((joker[0].first < 0) || (joker[1].first < 0))) {             cout <<  Replace J1 with   << left_cards[i] <<   and J2 with                    << left_cards[j] <<  .  << endl;           } else {             if (joker[0].first < 0) {               cout <<  Replace J2 with   << left_cards[j] <<  .  << endl;             } else {               cout <<  Replace J1 with   << left_cards[i] <<  .  << endl;             }           }           cout <<  Put the first square to (  << s1.first + 1 <<  ,                  << s1.second + 1 <<  ).  << endl;           cout <<  Put the second square to (  << s2.first + 1 <<  ,                  << s2.second + 1 <<  ).  << endl;           return 0;         }       }     cout <<  No solution.  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 25;   string str[maxn];   int a[maxn][maxn];   long long dp[1 << 20];   int mask[26], tot[26], big[26], cnt[26], candi[26];   int Min[maxn];   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> str[i];     }     memset(Min, 0x3f, sizeof(Min));     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> a[i][j];         Min[i] = min(Min[i], a[i][j]);       }     }     for (int i = 0; i < (1 << n); i++) {       int tmp = 0;       for (int j = 0; j < n; j++) {         if (i >> j & 1) tmp += Min[j + 1];       }       dp[i] = tmp;     }     for (int i = 1; i <= m; i++) {       memset(mask, 0, sizeof(mask));       memset(tot, 0, sizeof(tot));       memset(big, 0, sizeof(big));       memset(cnt, 0, sizeof(cnt));       for (int j = 1; j <= n; j++) {         int c = str[j][i - 1] -  a ;         cnt[c]++;         mask[c] |= (1 << (j - 1));         tot[c] += a[j][i];         big[c] = max(big[c], a[j][i]);       }       int idx = 0;       for (int j = 0; j < 26; j++) {         if (cnt[j]) candi[idx++] = j;       }       for (int j = 0; j < (1 << n); j++) {         for (int k = 0; k < idx; k++) {           int u = candi[k];           dp[j | mask[u]] = min(dp[j | mask[u]], dp[j] + (tot[u] - big[u]));         }       }     }     cout << dp[(1 << n) - 1] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000006;   int t, n, m, x, y;   stack<int> s;   int rg[N], rs[N], ls[N], lg[N], a[N];   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     a[0] = a[n + 1] = 0;     for (int i = 1; i <= n + 1; i++) {       while (!s.empty() && a[s.top()] > a[i]) {         t = s.top();         s.pop();         rs[t] = i - 1;       }       s.push(i);     }     s.pop();     for (int i = n; i >= 0; i--) {       while (!s.empty() && a[s.top()] >= a[i]) {         t = s.top();         s.pop();         ls[t] = i + 1;       }       s.push(i);     }     s.pop();     a[0] = a[n + 1] = INT_MAX;     for (int i = 1; i <= n + 1; i++) {       while (!s.empty() && a[s.top()] < a[i]) {         t = s.top();         s.pop();         rg[t] = i - 1;       }       s.push(i);     }     s.pop();     for (int i = n; i >= 0; i--) {       while (!s.empty() && a[s.top()] <= a[i]) {         t = s.top();         s.pop();         lg[t] = i + 1;       }       s.push(i);     }     s.pop();     long long ans = 0;     for (int i = 1; i <= n; i++) {       ans += 1LL * ((rg[i] - lg[i]) + 1LL * (rg[i] - i) * (i - lg[i])) * a[i];       ans -= 1LL * ((rs[i] - ls[i]) + 1LL * (rs[i] - i) * (i - ls[i])) * a[i];     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3FFFFFFF;   char arr[1001][1001];   int main() {     int n, m, x, y, z, p;     scanf( %d%d%d%d%d%d , &n, &m, &x, &y, &z, &p);     x %= 4;     y %= 2;     z %= 4;     vector<int> a(p), b(p);     for (int i = 0; i < p; i++) {       scanf( %d%d , &a[i], &b[i]);     }     if (x == 1) {       for (int i = 0; i < p; i++) {         int at = b[i];         int bt = n - a[i] + 1;         a[i] = at;         b[i] = bt;       }       swap(n, m);     } else if (x == 2) {       for (int i = 0; i < p; i++) {         int at = n - a[i] + 1;         int bt = m - b[i] + 1;         a[i] = at;         b[i] = bt;       }     } else if (x == 3) {       for (int i = 0; i < p; i++) {         int at = m - b[i] + 1;         int bt = a[i];         a[i] = at;         b[i] = bt;       }       swap(n, m);     }     if (y == 1) {       for (int i = 0; i < p; i++) {         int at = a[i];         int bt = m - b[i] + 1;         a[i] = at;         b[i] = bt;       }     }     if (z == 3) {       for (int i = 0; i < p; i++) {         int at = b[i];         int bt = n - a[i] + 1;         a[i] = at;         b[i] = bt;       }       swap(n, m);     } else if (z == 2) {       for (int i = 0; i < p; i++) {         int at = n - a[i] + 1;         int bt = m - b[i] + 1;         a[i] = at;         b[i] = bt;       }     } else if (z == 1) {       for (int i = 0; i < p; i++) {         int at = m - b[i] + 1;         int bt = a[i];         a[i] = at;         b[i] = bt;       }       swap(n, m);     }     for (int i = 0; i < p; i++) {       printf( %d %d n , a[i], b[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     if (a < b) return gcd(b, a);     while (a > b) {       a %= b;     }     return gcd(b, a);   }   long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }   const double EPS = 10e-9;   const double PI = acos(-1.0);   const double INF = (1LL << 29);   using namespace std;   template <typename T>   string toString(T t) {     stringstream ss;     ss << t;     return ss.str();   }   const long long prime = 1000000007;   long long power(long long n, long long a, long long prime) {     long long res = 1LL;     while (a > 0) {       if (a & 1LL) {         res *= n;         res %= prime;       }       n *= n;       n %= prime;       a >>= 1;     }     return res;   }   int main() {     string x;     cin >> x;     long long total = 0;     for (int i = 0; i < (x.size()); ++i) {       if (x[i] ==  1 ) {         int len = i, left = x.size() - len - 1;         long long part = power(2, len, prime) * power(4, left, prime);         total += part % prime;         total %= prime;       }     }     cout << total << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, cnt[21][21], pos[21][400001], dp[1 << 20];   vector<long long> v[21];   signed main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(false);     cin >> n;     for (long long i = 1; i <= n; i++) {       long long a;       cin >> a;       a--;       v[a].push_back(i);       pos[a][i]++;     }     for (long long i = 0; i < 20; i++) {       for (long long j = 1; j <= n; j++) {         pos[i][j] += pos[i][j - 1];       }     }     for (long long i = 0; i < 20; i++) {       for (long long j = 0; j < 20; j++) {         if (i == j) continue;         for (auto x : v[i]) {           cnt[i][j] += pos[j][x];         }       }     }     dp[0] = 0;     for (long long i = 1; i < (1 << 20); i++) {       dp[i] = 1000000000000000000;       for (long long j = 0; j < 20; j++) {         if (i >> j & 1) {           long long ans1 = 0;           for (long long k = 0; k < 20; k++) {             if (k == j) continue;             if (i >> k & 1) ans1 += cnt[j][k];           }           dp[i] = min(dp[i], dp[i ^ (1 << j)] + ans1);         }       }     }     cout << dp[(1 << 20) - 1];   }


#include <bits/stdc++.h>   using namespace std;   void _fill_int(int* p, int val, int rep) {     int i;     for (i = 0; i < rep; i++) p[i] = val;   }   int N, M, K;   signed long long mo = 1000000007;   int L, R;   string LL, RR;   vector<string> S;   int V[201];   vector<vector<int> > TR;   vector<int> AC;   signed long long dpdp[2][201][501][2][2];   int find(string s) {     int cur = 0;     for (__typeof(s.begin()) it = s.begin(); it != s.end(); it++)       if ((cur = TR[cur][*it]) == 0) return -1;     return cur;   }   void create(vector<string> S) {     int i;     TR.push_back(vector<int>(256));     for (__typeof(S.begin()) it = S.begin(); it != S.end(); it++) {       int cur = 0;       for (__typeof((*it).begin()) c = (*it).begin(); c != (*it).end(); c++) {         if (TR[cur][*c] == 0)           TR.push_back(vector<int>(256)), TR[cur][*c] = TR.size() - 1;         cur = TR[cur][*c];       }       AC.resize(TR.size());       AC[cur] += V[it - S.begin()];     }     queue<int> Q;     for (i = 0; i < 255; i++)       if (TR[0][i]) TR[TR[0][i]][255] = 0, Q.push(TR[0][i]);     while (!Q.empty()) {       int k = Q.front();       Q.pop();       for (i = 0; i < 255; i++)         if (TR[k][i]) {           Q.push(TR[k][i]);           int pre = TR[k][255];           while (pre != 0 && TR[pre][i] == 0) pre = TR[pre][255];           TR[TR[k][i]][255] = TR[pre][i];           AC[TR[k][i]] += AC[TR[pre][i]];         }     }   }   signed long long dodo(string SS) {     signed long long ret = 0, rr;     int i, j, k, x, y, pos, val, b1, b2;     memset(dpdp, 0, sizeof(dpdp));     dpdp[0][0][0][1][1] = 1;     for (i = 0; i < SS.size(); i++) {       int cur = i % 2, tar = cur ^ 1;       memset(dpdp[tar], 0, sizeof(dpdp[tar]));       for (b1 = 0; b1 < 2; b1++)         for (b2 = 0; b2 < 2; b2++)           for (val = 0; val < K + 1; val++)             for (pos = 0; pos < TR.size(); pos++)               if ((rr = dpdp[cur][pos][val][b1][b2])) {                 for (j = 0; j < M; j++) {                   if (b1 && j > SS[i]) break;                   y = (b1 && j == SS[i]);                   if (b2 && (j == 0)) {                     dpdp[tar][pos][val][y][1] += rr;                     dpdp[tar][pos][val][y][1] %= mo;                     continue;                   }                   x = pos;                   while (x && TR[x][j] == 0) x = TR[x][255];                   x = TR[x][j];                   if (val + AC[x] <= K) {                     dpdp[tar][x][val + AC[x]][y][0] += rr;                     dpdp[tar][x][val + AC[x]][y][0] %= mo;                   }                 }               }     }     x = SS.size() % 2;     for (i = 0; i < 201; i++)       for (j = 0; j < K + 1; j++)         ret += dpdp[x][i][j][0][0] + dpdp[x][i][j][0][1] + dpdp[x][i][j][1][0] +                dpdp[x][i][j][1][1];     return ret % mo;   }   void solve() {     int f, i, j, k, l, x, y;     cin >> N >> M >> K;     cin >> L;     for (i = 0; i < L; i++) cin >> x, LL += x;     cin >> R;     for (i = 0; i < R; i++) cin >> x, RR += x;     for (i = 0; i < N; i++) {       cin >> j;       string s;       while (j--) cin >> x, s += x;       S.push_back(s);       cin >> V[i];     }     create(S);     i = LL.size() - 1;     LL[i] -= 1;     while (LL[i] < 0) LL[i] += M, LL[--i] -= 1;     while (LL.size() > 0 && LL[0] == 0) LL.erase(LL.begin());     cout << (dodo(RR) - dodo(LL) + mo) % mo << endl;   }   int main(int argc, char** argv) {     string s;     if (argc == 1) ios::sync_with_stdio(false);     for (int i = 1; i < argc; i++) s += argv[i], s +=   n ;     for (int i = s.size() - 1; i >= 0; i--) ungetc(s[i], stdin);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, ans = 0;     cin >> n;     vector<int> v;     int dat[101] = {};     for (int i = 0; i < n; ++i) {       cin >> a;       if (a == 1) break;       v.push_back(a);     }     if (a != 1) {       sort(v.begin(), v.end());       for (int i = 0; i < v.size(); ++i) {         if (dat[v[i]] == 0) {           for (int j = v[i]; j < 101; j += v[i]) {             dat[j] = 1;           }           ++ans;         }       }     } else       ans = 1;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long p = 998244353;   long long po(long long a, long long b) {     if (b == 0) return 1;     if (b == 1) return a;     if (b % 2 == 0) {       long long u = po(a, b / 2);       return (u * u) % p;     } else {       long long u = po(a, b - 1);       return (a * u) % p;     }   }   long long inv(long long x) { return po(x, p - 2); }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     long long a[n];     for (long long i = 0; i < n; ++i) cin >> a[i];     sort(a, a + n);     reverse(a, a + n);     long long pr[n + 1];     pr[0] = 0;     for (long long i = 0; i < n; ++i) pr[i + 1] = pr[i] + a[i];     for (long long k = 1; k <= n; ++k) {       long long ans = 0;       for (long long l = k; l <= n; l += k) {         ans += (pr[l] - pr[((l - 1) / k) * k]) * ((l - 1) / k);         ans %= p;         if (l > (n - k) && l < n) l = (n - k);       }       ans *= inv(n);       ans %= p;       ans += p;       ans %= p;       cout << ans <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool solve(vector<int> ps, vector<int> v) {     if (ps.empty()) return v[0] == 0;     int n = v.size();     vector<int> nps(ps.begin() + 1, ps.end());     vector<vector<int> > g(ps[0], vector<int>(n / ps[0]));     bool ans = true;     for (int i = 0; i < n; ++i) {       g[i % ps[0]][i % (n / ps[0])] = v[i];     }     for (int i = 1; i < ps[0]; ++i) {       for (int j = 0; j < n / ps[0]; ++j) {         g[i][j] -= g[0][j];       }       if (!solve(nps, g[i])) ans = false;     }     return ans;   }   int main() {     int N;     string S;     cin >> N >> S;     vector<int> ps;     int rad = 1, x = N;     for (int i = 2; i * i <= x; ++i) {       if (x % i == 0) {         rad *= i;         ps.push_back(i);         while (x % i == 0) x /= i;       }     }     if (x > 1) rad *= x, ps.push_back(x);     bool ans = true;     for (int i = 0; i < N / rad; ++i) {       vector<int> v;       for (int j = i; j < N; j += N / rad) {         v.push_back(S[j] -  0 );       }       bool res = solve(ps, v);       if (!res) ans = false;     }     cout << (ans ?  YES  :  NO ) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int N = 77;   int n;   char s[N];   vector<int> v, k, x;   int dp[N][N][N][2];   int count(vector<int> &list, int from, int w) {     int cnt = 0;     for (int i = from; i < list.size(); i++) {       if (list[i] < w) cnt++;     }     return cnt;   }   int cost(int vv, int kk, int xx, int w) {     return count(v, vv, w) + count(k, kk, w) + count(x, xx, w);   }   int main() {     scanf( %d , &n);     scanf( %s , s);     for (int i = 0; i < n; i++) {       if (s[i] ==  V )         v.push_back(i);       else if (s[i] ==  K )         k.push_back(i);       else         x.push_back(i);     }     for (int i = 0; i < N; i++)       for (int j = 0; j < N; j++)         for (int k = 0; k < N; k++)           for (int l = 0; l < 2; l++) dp[i][j][k][l] = 1e9;     dp[0][0][0][0] = 0;     for (int i = 0; i <= v.size(); i++) {       for (int j = 0; j <= k.size(); j++) {         for (int p = 0; p <= x.size(); p++) {           for (int c = 0; c < 2; c++) {             if (i < v.size())               dp[i + 1][j][p][1] =                   min(dp[i + 1][j][p][1], cost(i, j, p, v[i]) + dp[i][j][p][c]);             if (c == 0 && j < k.size())               dp[i][j + 1][p][0] =                   min(dp[i][j + 1][p][0], cost(i, j, p, k[j]) + dp[i][j][p][c]);             if (p < x.size())               dp[i][j][p + 1][0] =                   min(dp[i][j][p + 1][0], cost(i, j, p, x[p]) + dp[i][j][p][c]);           }         }       }     }     printf( %d n , min(dp[v.size()][k.size()][x.size()][0],                        dp[v.size()][k.size()][x.size()][1]));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, flag = 0;     int a[5005];     cin >> n;     for (i = 1; i <= n; i++) {       cin >> a[i];     }     for (i = 1; i <= n; i++) {       if (a[a[a[i]]] == i) {         cout <<  YES ;         return 0;       }     }     cout <<  NO ;   }


#include <bits/stdc++.h>   const double PI = 3.1415926535897932384626433;   const int KL = 1e4 + 10;   const long long MOD = 1e9 + 7;   using namespace std;   int n;   struct point {     long double x, y, z;   };   long double dis(point a, point b) {     long double dx = a.x - b.x;     long double dy = a.y - b.y;     long double dz = a.z - b.z;     return sqrt(dx * dx + dy * dy + dz * dz);   }   point a[KL], p;   long double t[KL], vp, vs;   bool ok(long double mid) {     long double ttt = 0.0;     int pos;     for (int i = 1; i <= n; i++) {       ttt += t[i];       if (ttt >= mid) {         pos = i;         break;       }     }     long double dx = a[pos].x - a[pos - 1].x;     long double dy = a[pos].y - a[pos - 1].y;     long double dz = a[pos].z - a[pos - 1].z;     long double d = dis(a[pos], a[pos - 1]);     long double dc = (mid - (ttt - t[pos])) * vs;     long double sm = dc / d;     dx = dx * sm;     dy = dy * sm;     dz = dz * sm;     point c = {dx + a[pos - 1].x, dy + a[pos - 1].y, dz + a[pos - 1].z};     long double ds = dis(c, p);     return (ds / mid <= vp);   }   void prnt(long double nm) { cout << fixed << setprecision(10) << nm <<    ; }   int main() {     cin >> n;     for (int i = 0; i <= n; i++) {       cin >> a[i].x >> a[i].y >> a[i].z;     }     cin >> vp >> vs >> p.x >> p.y >> p.z;     long double tt = 0.0;     for (int i = 1; i <= n; i++) {       t[i] = dis(a[i - 1], a[i]) / vs;       tt += t[i];     }     if (!ok(tt))       cout <<  NO  << endl;     else {       long double lo = 0;       long double hi = tt;       for (int i = 0; i < 100; i++) {         long double mid = (lo + hi) / 2;         if (ok(mid))           hi = mid;         else           lo = mid;       }       cout <<  YES  << endl;       cout << fixed << setprecision(10) << lo << endl;       if (lo == 0) {         prnt(a[0].x);         prnt(a[0].y);         prnt(a[0].z);         cout << endl;         return 0;       }       long double ttt = 0.0;       int pos;       for (int i = 0; i <= n; i++) {         ttt += t[i];         if (ttt >= lo) {           pos = i;           break;         }       }       long double dx = a[pos].x - a[pos - 1].x;       long double dy = a[pos].y - a[pos - 1].y;       long double dz = a[pos].z - a[pos - 1].z;       long double d = dis(a[pos], a[pos - 1]);       long double dc = (lo - (ttt - t[pos])) * vs;       long double sm = dc / d;       dx = dx * sm;       dy = dy * sm;       dz = dz * sm;       point c = {dx + a[pos - 1].x, dy + a[pos - 1].y, dz + a[pos - 1].z};       prnt(c.x);       prnt(c.y);       prnt(c.z);       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Node {     vector<Node*> next;     Node() : next(26, nullptr) {}   };   Node* root = new Node;   void insert(string& s, int i, Node* c) {     if (i >= s.length()) return;     if (c->next[s[i] -  a ] == nullptr) {       c->next[s[i] -  a ] = new Node;     }     insert(s, i + 1, c->next[s[i] -  a ]);   }   bool traverseWin(Node* c) {     bool curWin = false;     for (auto n : c->next)       if (n != nullptr) {         if (!traverseWin(n)) curWin = true;       }     return curWin;   }   bool traverseLose(Node* c) {     bool curLose = false;     int i = 0;     for (auto n : c->next)       if (n != nullptr) {         ++i;         if (!traverseLose(n)) curLose = true;       }     return curLose || (i == 0);   }   int main(int, char**) {     std::ios::sync_with_stdio(false);     int n, k;     cin >> n >> k;     for (int i = 0; i < n; ++i) {       string s;       cin >> s;       insert(s, 0, root);     }     bool w = traverseWin(root);     bool l = traverseLose(root);     if (!w) {       cout <<  Second  << endl;     } else {       if (!l) {         cout << (k % 2 == 0 ?  Second  :  First ) << endl;       } else {         cout <<  First  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long ax, ay, bx, by, cx, cy;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> ax >> ay >> bx >> by >> cx >> cy;     if ((by - ay) * (cx - bx) == (cy - by) * (bx - ax))       cout <<  NO n ;     else if ((by - ay) * (by - ay) + (bx - ax) * (bx - ax) ==              (by - cy) * (by - cy) + (bx - cx) * (bx - cx))       cout <<  YES n ;     else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   int main() {     int n(0);     scanf( %d , &n);     char first =  x , last =  y ;     int count(0);     if (n % 2 == 0) {       for (int k = 0; k < n / 2; k++) {         printf( %c%c , (count) % 2 +  a , (count) % 2 +  a );         ++count;       };       puts(  );       for (int k = 0; k < n / 2; k++) {         printf( %c%c , (count) % 2 +  c , (count) % 2 +  c );         ++count;       };       puts(  );       printf( %c , first);       for (int k = 0; k < n / 2 - 1; k++) {         printf( %c%c , (count) % 2 +  f , (count) % 2 +  f );         ++count;       };       printf( %c n , last);       printf( %c , first);       for (int k = 0; k < n / 2 - 1; k++) {         printf( %c%c , (count) % 2 +  m , (count) % 2 +  m );         ++count;       };       printf( %c n , last);     } else if (n % 2 == 1) {       char count =  b ;       printf( %c , first);       for (int k = 0; k < n / 2; k++) {         printf( %c%c , (count) % 2 +  a , (count) % 2 +  a );         ++count;       };       puts(  );       printf( %c , first);       for (int k = 0; k < n / 2; k++) {         printf( %c%c , (count) % 2 +  c , (count) % 2 +  c );         ++count;       };       puts(  );       for (int k = 0; k < n / 2; k++) {         printf( %c%c , (count) % 2 +  f , (count) % 2 +  f );         ++count;       };       printf( %c n , last);       for (int k = 0; k < n / 2; k++) {         printf( %c%c , (count) % 2 +  m , (count) % 2 +  m );         ++count;       };       printf( %c n , last);     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:102400000,102400000 )   using namespace std;   const double PI = acos(-1.0);   int f_max(int a, int b) { return a > b ? a : b; }   int f_min(int a, int b) { return a < b ? a : b; }   int f_abs(int a) { return a > 0 ? a : -a; }   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   int n, m;   int u, d, l, r;   int x, y;   int main() {     int q, w, e;     int i, j, k;     int ans, len;     while (~scanf( %d%d , &n, &m)) {       len = 0x7fffffff;       r = u = -0x7fffffff;       l = d = 0x7fffffff;       scanf( %d , &n);       for (i = 0; i < n; i++) {         scanf( %d%d , &x, &y);         u = f_max(u, y - x);         d = f_min(d, y - x);         l = f_min(l, x + y);         r = f_max(r, x + y);       }       scanf( %d , &m);       int mm;       for (i = 1; i <= m; i++) {         scanf( %d%d , &x, &y);         mm = 0;         mm = f_max(mm, u - (y - x));         mm = f_max(mm, (y - x) - d);         mm = f_max(mm, r - (x + y));         mm = f_max(mm, (x + y) - l);         if (mm < len) {           ans = i;           len = mm;         }       }       printf( %d n%d n , len, ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   long long mod = 1e9 + 7;   long long fmod(long long b, long long exp) {     long long res = 1;     while (exp) {       if (exp & 1ll) res = (res * b) % mod;       b = (b * b) % mod;       exp /= 2ll;     }     return res;   }   long long buc[101];   long long fac[N], inv[N];   long long dp[N], temp_dp[N];   long long ans[55][55];   string s, s1, s2;   int find(char c) {     if (c >=  A  && c <=  Z )       return (int)(c -  A  + 26);     else       return (c -  a );   }   inline void add(long long &a, long long b) {     a += b;     if (a >= mod) a -= mod;   }   inline void sub(long long &a, long long b) {     a -= b;     if (a < 0) a += mod;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1, n, i, j, m, q;     cin >> s;     n = s.length();     for (int i = 0; i < n; i++) buc[find(s[i])]++;     fac[0] = 1;     for (i = 1; i <= n; i++) fac[i] = (fac[i - 1] * 1ll * i) % mod;     inv[n] = fmod(fac[n], mod - 2);     for (int i = n - 1; i >= 0; i--) inv[i] = (inv[i + 1] * 1ll * (i + 1)) % mod;     long long num = (fac[n / 2] * fac[n / 2]) % mod;     for (int i = 0; i < 52; i++) num = (num * inv[buc[i]]) % mod;     dp[0] = 1;     for (int i = 0; i < 52; i++) {       if (!buc[i]) continue;       for (int j = n; j >= buc[i]; j--) add(dp[j], dp[j - buc[i]]);     }     for (i = 0; i < 52; i++) ans[i][i] = dp[n / 2];     for (i = 0; i < 52; i++) {       if (!buc[i]) continue;       for (int k = 0; k <= n; k++) temp_dp[k] = dp[k];       for (int k = buc[i]; k <= n; k++) sub(temp_dp[k], temp_dp[k - buc[i]]);       for (int j = i + 1; j < 52; j++) {         if (!buc[j]) continue;         for (int k = buc[j]; k <= n; k++) sub(temp_dp[k], temp_dp[k - buc[j]]);         ans[i][j] = (2ll * temp_dp[n / 2]) % mod;         ans[j][i] = ans[i][j];         for (int k = n; k >= buc[j]; k--) add(temp_dp[k], temp_dp[k - buc[j]]);       }     }     cin >> q;     int x, y;     while (q--) {       cin >> x >> y;       int l = find(s[x - 1]);       int r = find(s[y - 1]);       cout << (num * ans[l][r]) % mod <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fastInOut();   unsigned long long power(unsigned long long b, unsigned long long pow) {     if (b == 0)       return 0;     else if (pow == 1)       return b;     else if (pow % 2 == 0)       return power(b * b, pow / 2);     else       return b * power(b * b, pow / 2);   }   int power(int z, int i) {     int zz = 1;     for (int k = 0; k < i; k++) {       zz *= z;     }     return zz;   }   long long power(long long x, long long y) {     long long z = 1;     for (long long i = 0; i < y; i++) z *= x;     return z;   }   bool right(int arr[]) {     int a[3];     a[0] = power(arr[3] - arr[1], 2) + power(arr[2] - arr[0], 2);     a[1] = power(arr[5] - arr[1], 2) + power(arr[4] - arr[0], 2);     a[2] = power(arr[5] - arr[3], 2) + power(arr[4] - arr[2], 2);     sort(a, a + 3);     if (a[0] + a[1] == a[2] && (sqrt(a[0]) * sqrt(a[1]) * sqrt(a[2])) / 2)       return true;     return false;   }   bool fed(int k, int f, int test) {     if (f < test) swap(f, test);     int c = 0;     while (f) {       if ((f & 1) != (test & 1)) c++;       f = f >> 1;       test = test >> 1;       if (c > k) return false;     }     if (c > k) return false;     return true;   }   unsigned long long fact(long long a) {     unsigned long long res = 1;     for (long long i = a; i > 0; i--) res *= i;     return res;   }   unsigned long long comb(long long a, long long b) {     if ((a - b) < b) b = a - b;     if (a <= 19) return fact(a) / (fact(a - b) * fact(b));     if (a == b || b == 0) return 1;     if (b == 1) return a;     return comb(a - 1, b - 1) + comb(a - 1, b);   }   bool palind(string s) {     for (unsigned int i = 0, f = s.size() - 1; i < s.size() / 2; i++, f--)       if (s[i] != s[f]) return false;     return true;   }   int div(long long x) {     double SqrtOfnumber;     SqrtOfnumber = sqrt(x);     int counter = 1;     int i;     for (i = 2; i < SqrtOfnumber; i++) {       if (x % i == 0) counter++;     }     counter = counter * 2;     if (i * i == x) counter++;     return counter;   }   bool isprim(int x) {     for (int i = x - 1; i > 1; i--) {       if (x % i == 0) return false;     }     return true;   }   set<long long> prim(int n) {     n++;     bool *arr = new bool[n];     for (int i = 0; i < n; i++) arr[i] = true;     set<long long> se;     for (int i = 2; i * i < n; i++)       if (arr[i])         for (int k = i * i; k < n; k += i) arr[k] = false;     for (int i = 2; i < n; i++)       if (arr[i]) se.insert(i);     return se;   }   int n, m;   char arr[1010][1010];   bool pash(int x, int y) {     int s;     s = 1;     if (x + 1 < n && arr[x + 1][y] ==  b ) s++;     if (x + 1 < n && y + 1 < m && arr[x + 1][y + 1] ==  b ) s++;     if (y + 1 < m && arr[x][y + 1] ==  b ) s++;     if (s == 4) return true;     s = 1;     if (x - 1 >= 0 && arr[x - 1][y] ==  b ) s++;     if (x - 1 >= 0 && y + 1 < m && arr[x - 1][y + 1] ==  b ) s++;     if (y + 1 < m && arr[x][y + 1] ==  b ) s++;     if (s == 4) return true;     s = 1;     if (x - 1 >= 0 && arr[x - 1][y] ==  b ) s++;     if (x - 1 >= 0 && y - 1 >= 0 && arr[x - 1][y - 1] ==  b ) s++;     if (y - 1 >= 0 && arr[x][y - 1] ==  b ) s++;     if (s == 4) return true;     s = 1;     if (x + 1 < n && arr[x + 1][y] ==  b ) s++;     if (x + 1 < n && y - 1 >= 0 && arr[x + 1][y - 1] ==  b ) s++;     if (y - 1 >= 0 && arr[x][y - 1] ==  b ) s++;     if (s == 4) return true;     return false;   }   int main() {     fastInOut();     int n, m, x;     string res =  YES ;     cin >> n >> m;     vector<int> v;     for (int i = 0; i < m; i++) {       cin >> x;       v.push_back(x);     }     sort(v.begin(), v.end());     if (v.size()) {       if (*min_element(v.begin(), v.end()) == 1 ||           *max_element(v.begin(), v.end()) == n)         res =  NO ;       else         for (int i = 0; i < m - 2; i++)           if (v[i + 1] - v[i] == 1 && v[i + 2] - v[i] == 2) {             res =  NO ;             break;           }     }     cout << res;     return 0;   }   void fastInOut() {     ios_base::sync_with_stdio(0);     cin.tie(NULL), cout.tie(NULL);   }


#include <bits/stdc++.h>   const double PI = acos(-1.0);   const int INF = 0x3f3f3f3f;   const double eps = 1e-8;   const int MAXN = 1e3 + 10;   const long long MOD = 1e9 + 7;   int cases = 0;   using std::string;   const int dir[][2] = {{-1, 0}, {-1, -1}, {-1, 1}, {0, -1},                         {0, 1},  {1, -1},  {1, 0},  {1, 1}};   int next[MAXN];   int main() {     int n, m;     scanf( %d%d , &n, &m);     while (m--) {       int t, k, d;       std::vector<int> tmp;       scanf( %d%d%d , &t, &k, &d);       int cnt = 0;       int res = 0;       for (int i = 1; i <= n; ++i) {         if (next[i] <= t) {           tmp.push_back(i);         }         if (tmp.size() == k) {           for (int u : tmp) {             next[u] = t + d;             res += u;           }           break;         }       }       printf( %d n , res == 0 ? -1 : res);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using namespace std;   const int maxn = 3e5 + 5;   int level[maxn * 2], num[maxn * 2], par[maxn * 2];   char s[maxn * 2];   stack<int> stk;   int main() {     int n, cnt = 0;     scanf( %d , &n);     scanf( %s , s + 1);     for (int i = 1; i <= n; i++) {       if (stk.empty())         stk.push(i);       else {         int c = stk.top();         if (s[i] ==  )  && s[c] ==  ( )           stk.pop();         else           stk.push(i);       }       s[i + n] = s[i];       cnt = cnt + ((s[i] ==  ( ) ? (1) : (-1));     }     if (cnt) return 0 & printf( 0 n1 1 n );     int j = 1;     while (!stk.empty()) {       int c = stk.top();       if (s[c] ==  ) ) break;       stk.pop();       j = c;     }     for (int i = 1; i <= n * 2; i++) level[i] = -1, num[i] = 0;     while (!stk.empty()) stk.pop();     for (int i = j; i <= j + n - 1; i++) {       if (stk.empty()) {         stk.push(i);         level[i] = 1;         cnt++;       } else {         int c = stk.top();         if (s[i] ==  )  && s[c] ==  ( ) {           stk.pop();           par[c] = i;         } else {           stk.push(i);           level[i] = stk.size();         }       }     }     int i = j;     while (i <= j + n - 1) {       if (level[i] == 1) {         int r = par[i], k = i;         while (i < r) {           if (level[i] == 2) num[k]++;           i++;         }       }       i++;     }     i = j;     while (i <= j + n - 1) {       if (level[i] == 2) {         int r = par[i], k = i;         while (i < r) {           if (level[i] == 3) num[k]++;           i++;         }       }       i++;     }     int ans = 0, l, r;     for (int i = j; i <= j + n - 1; i++) {       if (level[i] == 1) {         if (ans < (num[i] + 1)) {           ans = num[i] + 1;           l = i % n;           r = par[i] % n;         }       } else if (level[i] == 2) {         if (ans < (cnt + num[i] + 1)) {           ans = cnt + num[i] + 1;           l = i % n;           r = par[i] % n;         }       }     }     if (ans < cnt) ans = cnt, l = 1, r = 1;     if (l == 0) l = n;     if (r == 0) r = n;     printf( %d n%d %d n , ans, l, r);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 300005;   int n, k, i, a, b, da, db, u, v, head[N], Next[N * 2], adj[N * 2], j, dp[N],       ans, dd, t;   void Push(int u, int v) {     Next[++k] = head[u];     head[u] = k;     adj[k] = v;   }   void dfs(int i, int fa) {     int j, mxx = 0;     dp[i] = 0;     for (j = head[i]; j; j = Next[j])       if (adj[j] != fa) {         dfs(adj[j], i);         if (dp[i] < dp[adj[j]] + 1) {           mxx = max(mxx, dp[i]);           dp[i] = dp[adj[j]] + 1;         } else           mxx = max(mxx, dp[adj[j]] + 1);       }     ans = max(ans, mxx + dp[i]);   }   void dis(int i, int fa, int d) {     int j;     for (j = head[i]; j; j = Next[j])       if (adj[j] != fa) dis(adj[j], i, d + 1);     if (i == b) dd = d;   }   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d %d %d %d %d , &n, &a, &b, &da, &db);       for (i = 1; i <= n; ++i) head[i] = 0;       k = 0;       for (i = 1; i < n; ++i) {         scanf( %d %d , &u, &v);         Push(u, v);         Push(v, u);       }       if (db <= da * 2) {         puts( Alice );         continue;       }       ans = 0;       dfs(1, 0);       dis(a, 0, 0);       if (dd <= da || ans <= da * 2)         puts( Alice );       else         puts( Bob );     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cerr.tie(0);     int n, tmp;     vector<pair<int, int> > a;     cin >> n;     for (int i = 0; i < n; i++) cin >> tmp, a.push_back(make_pair(tmp, i + 1));     sort(a.begin(), a.end());     int c1 = 0, c2 = 0, s1 = 0, s2 = 0;     vector<int> t1, t2;     int l = 0, r = n - 1;     while (l <= r) {       if (s1 > s2) {         if (c2 - c1 <= 0) {           c2++;           s2 += a[r].first;           t2.push_back(a[r].second);           r--;         } else {           c1++;           s1 += a[l].first;           t1.push_back(a[l].second);           l++;         }       } else {         if (c2 - c1 <= 0) {           c2++;           s2 += a[l].first;           t2.push_back(a[l].second);           l++;         } else {           c1++;           s1 += a[r].first;           t1.push_back(a[r].second);           r--;         }       }     }     cout << c1 << endl;     for (int i = 0; i < c1; i++) cout << t1[i] <<    ;     cout << endl;     cout << c2 << endl;     for (int i = 0; i < c2; i++) cout << t2[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200010;   int c[maxn], A[maxn], B[maxn];   int n, k, a, b, q, cmd;   void add(int *c, int p, int val) {     for (; p <= n; p += p & -p) c[p] += val;   }   int sum(int *c, int p) {     int ret = 0;     for (; p; p -= p & -p) ret += c[p];     return ret;   }   int main() {     cin >> n >> k >> a >> b >> q;     while (q--) {       int x, y;       scanf( %d , &cmd);       if (cmd == 1) {         scanf( %d%d , &x, &y);         add(A, x, -min(c[x], a));         add(B, x, -min(c[x], b));         c[x] += y;         add(A, x, min(c[x], a));         add(B, x, min(c[x], b));       } else {         scanf( %d , &x);         int ans = 0;         ans += sum(B, x - 1);         ans += sum(A, n) - sum(A, x + k - 1);         printf( %d n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2 * 1e6 + 10;   long long a[maxn], b[maxn], q[maxn], sz[maxn], n;   inline long long read() {     long long x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 1) + (x << 3) + c -  0 ;       c = getchar();     }     return x * f;   }   long long b1, b2, n1;   signed main() {     n = read();     long long p = 0;     for (long long i = 1; i <= n; i++) {       a[i] = read();       if (a[i] > a[p]) p = i;       if (a[i] > b1)         b2 = b1, b1 = a[i], n1 = 1;       else if (a[i] == b1)         n1++;       else if (a[i] > b2)         b2 = a[i];     }     for (long long i = 1; i <= n; i++) b[i] = a[(i + p - 1) % n + 1];     long long l = 1, r = 0, ans = 0;     for (long long i = 1; i <= n; i++) {       while (l <= r && b[i] > b[q[r]]) r--, ans++;       if (r >= l) {         if (b[i] == b[q[r]])           ans += min(r, sz[r] + 1);         else           ans++;       }       q[++r] = i;       if (b[i] == b[q[r - 1]])         sz[r] = sz[r - 1] + 1;       else         sz[r] = 1;     }     p = 0;     for (long long i = 1; i < n; i++)       if (b[i] >= b[p]) {         p = i;         if (n1 > 1 && b[p] == b1)           ;         else if (n1 == 1 && b[p] == b2)           ;         else           ans++;       }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Max = 50;   int row[Max][Max] = {0}, dp[Max][Max][Max][Max] = {0}, a[Max][Max];   int main() {     int n, m, q;     cin >> n >> m >> q;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         scanf( %1d , &a[i][j]);         row[i][j] = row[i][j - 1] + 1;         if (a[i][j] == 1) row[i][j] = 0;       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         for (int k = i; k <= n; k++) {           for (int l = j; l <= m; l++) {             dp[i][j][k][l] =                 dp[i][j][k - 1][l] + dp[i][j][k][l - 1] - dp[i][j][k - 1][l - 1];             int r = l - j + 1;             for (int z = k; z >= i; z--) {               r = min(r, row[z][l]);               dp[i][j][k][l] += r;             }           }         }       }     }     while (q--) {       int a, b, c, d;       scanf( %d%d%d%d , &a, &b, &c, &d);       cout << dp[a][b][c][d] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t;   int n, k;   int siz[100086];   int c, x;   int id[100086];   int mx;   char s[100086];   inline int get(int l, int r) {     printf( ? %d , r - l + 1), fflush(stdout);     for (int i = l; i <= r; i++) printf(  %d , i), fflush(stdout);     puts(  );     fflush(stdout);     int x;     scanf( %d , &x);     return x;   }   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d%d , &n, &k);       for (int i = 1; i <= k; i++) siz[i] = 0;       for (int i = 1; i <= n; i++) id[i] = 0;       for (int i = 1; i <= k; i++) {         scanf( %d , &c);         while (c--) {           scanf( %d , &x);           id[x] = i;           siz[i]++;         }       }       mx = get(1, n);       int l = 1, r = n, mid;       while (l < r) {         mid = l + r >> 1;         x = get(l, mid);         if (x == mx)           r = mid;         else           l = mid + 1;       }       if (!id[l])         x = mx;       else {         printf( ? %d , n - siz[id[l]]), fflush(stdout);         for (int i = 1; i <= n; i++)           if (id[i] != id[l]) printf(  %d , i), fflush(stdout);         puts(  );         fflush(stdout);         scanf( %d , &x);       }       printf( ! ), fflush(stdout);       for (int i = 1; i <= k; i++) {         if (i == id[l])           printf(  %d , x);         else           printf(  %d , mx);         fflush(stdout);       }       puts(  );       fflush(stdout);       scanf( %s , s);       if (s[0] ==  I ) return 0;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long N = 3e5 + 5;   const long long INF = 1e14;   vector<long long> adj[33];   int dirx[] = {-1, 1, 0, 0};   int diry[] = {0, 0, -1, 1};   long long ans[33][2];   void dfs(int u, int p, int dir, long long dist) {     int d = 0;     for (long long i = 0; i < adj[u].size(); i++) {       int v = adj[u][i];       if (v == p) continue;       if (dir == d) d++;       ans[v][0] = ans[u][0] + dist * dirx[d];       ans[v][1] = ans[u][1] + dist * diry[d];       dfs(v, u, d ^ 1, (dist >> 1));       d++;     }   }   int main() {     ios::sync_with_stdio(false);     int n;     int a, b;     cin >> n;     for (long long i = 0; i < n - 1; i++) {       cin >> a >> b;       a--;       b--;       adj[a].push_back(b);       adj[b].push_back(a);     }     for (long long i = 0; i < n; i++)       if (adj[i].size() > 4) {         cout <<  NO n ;         return 0;       }     ans[0][0] = 0;     ans[0][1] = 0;     dfs(0, -1, -1, (1 << 30));     cout <<  YES n ;     for (long long i = 0; i < n; i++)       cout << ans[i][0] <<     << ans[i][1] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int Max(int a, int b) { return (a > b ? a : b); }   inline int Min(int a, int b) { return (a > b ? b : a); }   int dp[45][65][65];   int cnt[65], K;   void Init() {     for (int i = 0; i < 65; i++) {       int n = i;       int x = 0;       while (n) {         x++;         n &= (n - 1);       }       cnt[i] = x;     }   }   int get(int num) {     int k = K - 1;     return (num | ((num >> 1)) | ((num << 1) & k));   }   int main() {     int i, j, k, t;     int a, b, c, x, y, z;     int n, m;     Init();     while (scanf( %d%d , &n, &m) != EOF) {       if (n > m) swap(n, m);       K = (1 << n);       if (m == 1 && n == 1) {         puts( 0 );         continue;       }       for (i = 0; i < K; i++)         for (j = 0; j < K; j++) {           if ((i | get(j)) == K - 1) {             dp[2][i][j] = cnt[i] + cnt[j];           } else             dp[2][i][j] = 100000000;         }       for (i = 3; i <= m; i++)         for (j = 0; j < K; j++)           for (k = 0; k < K; k++) {             dp[i][j][k] = 100000000;             for (t = 0; t < K; t++) {               int my = j | get(k) | t;               if (dp[i - 1][k][t] == 100000000 || my != K - 1) continue;               dp[i][j][k] = Min(dp[i][j][k], dp[i - 1][k][t] + cnt[j]);             }           }       int ans = 100000000;       for (j = 0; j < K; j++)         for (k = 0; k < K; k++)           for (t = 0; t < K; t++) {             if ((get(t | j) | k) != K - 1) continue;             ans = Min(ans, dp[m][j][k] + cnt[t]);           }       printf( %d n , n * m - ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct ms {     string s;     int id;   };   bool operator<(const ms &a, const ms &b) { return a.s < b.s; }   vector<ms> sv, msv[11];   string s;   int n, lenmin = 11, lenmax = 0;   bool used[10000] = {false};   int main(void) {     int i;     cin >> n;     sv.reserve(n);     for (i = 0; i < n; ++i) {       cin >> s;       if ((int)s.length() > lenmax) lenmax = s.length();       if ((int)s.length() < lenmin) lenmin = s.length();       ms sid = {s, i};       sv.push_back(sid);       msv[s.length()].push_back(sid);     }     cin >> s;     for (i = 0; i < n; ++i) sv[i].s.append(s);     sort(sv.begin(), sv.end());     for (i = 1; i <= 10; ++i) {       sort(msv[i].begin(), msv[i].end());       reverse(msv[i].begin(), msv[i].end());     }     lenmax = lenmax + lenmin;     for (i = 0; i < n; ++i)       if (!used[sv[i].id]) {         cout << sv[i].s;         used[sv[i].id] = true;         lenmin = lenmax - sv[i].s.length() + 1;         while (used[msv[lenmin].back().id]) msv[lenmin].pop_back();         cout << msv[lenmin].back().s <<   n ;         used[msv[lenmin].back().id] = true;         msv[lenmin].pop_back();       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Szukaj(vector<int>& moje, vector<bool>& jest, int x) {     int nr = -1;     for (int i = 0; i < moje.size(); ++i) {       if (moje[i] >= x && jest[i]) {         if (nr == -1)           nr = i;         else {           if (moje[i] < moje[nr]) nr = i;         }       }     }     return nr;   }   int main() {     ios_base::sync_with_stdio(0);     int n, m;     cin >> n >> m;     vector<int> atk, def;     for (int i = 0; i < n; ++i) {       string op;       int s;       cin >> op >> s;       if (op ==  ATK )         atk.push_back(s);       else         def.push_back(s);     }     vector<int> moje(m);     for (int i = 0; i < m; ++i) cin >> moje[i];     int odp = 0;     sort(atk.begin(), atk.end());     sort(moje.begin(), moje.end());     reverse(moje.begin(), moje.end());     for (int i = 1; i <= min(atk.size(), moje.size()); ++i) {       vector<int> a, b;       for (int j = 0; j < i; ++j) a.push_back(moje[j]);       for (int j = i - 1; j >= 0; --j) b.push_back(atk[j]);       int s = 0;       bool ok = true;       for (int j = 0; j < i; ++j) {         if (a[j] >= b[j])           s += a[j] - b[j];         else           ok = false;       }       if (ok) odp = max(odp, s);     }     bool ok = true;     int s = 0;     vector<bool> jest(moje.size(), true);     sort(def.begin(), def.end());     for (int i = 0; i < def.size(); ++i) {       int nr = Szukaj(moje, jest, def[i] + 1);       if (nr != -1)         jest[nr] = false;       else         ok = false;     }     for (int i = 0; i < atk.size(); ++i) {       int nr = Szukaj(moje, jest, atk[i]);       if (nr != -1) {         jest[nr] = false;         s += moje[nr] - atk[i];       } else         ok = false;     }     for (int i = 0; i < moje.size(); ++i)       if (jest[i]) s += moje[i];     if (ok) odp = max(odp, s);     cout << odp;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int d, s;     int id;   };   node a[100009], b[100009];   map<int, int> Map;   vector<int> e[100009], vec;   int match[100009];   int vis[100009];   int n, m;   bool cmp(node x, node y) { return x.d > y.d; }   bool find(int x) {     for (int i = 0; i < e[x].size(); i++) {       if (match[e[x][i]] == x) continue;       if (match[e[x][i]] == 0 || find(match[e[x][i]])) {         match[e[x][i]] = x;         return true;       }     }     return false;   }   int main() {     int d, s;     scanf( %d , &n);     memset(match, 0, sizeof(match));     for (int i = 1; i <= n; i++) {       scanf( %d%d , &a[i].d, &a[i].s);       a[i].id = i;       Map[a[i].s] = i;     }     cin >> m;     for (int i = 1; i <= m; i++) {       scanf( %d%d , &d, &s);       if (Map.find(s) != Map.end()) {         if (a[Map[s]].d <= d) e[Map[s]].push_back(i);       }       if (Map.find(s + 1) != Map.end()) {         if (a[Map[s + 1]].d <= d) e[Map[s + 1]].push_back(i);       }     }     sort(a + 1, a + 1 + n, cmp);     long long ans1 = 0, ans2 = 0;     for (int i = 1; i <= n; i++) {       if (find(a[i].id)) {         ans1++;         ans2 += a[i].d;       }     }     cout << ans2 << endl;     cout << ans1 << endl;     for (int i = 1; i <= m; i++) {       if (match[i] != 0) cout << i <<     << match[i] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int MAXN = (1 << 18) + 5;   const int MAXM = 1005;   bitset<MAXM> STree[MAXN], Prime, mask;   int Lazy[MAXN], a[MAXN], in[MAXN], out[MAXN], time_tag, n, m, H[MAXN];   vector<int> G[MAXN];   void dfs(int i, int fa) {     in[i] = ++time_tag;     H[in[i]] = i;     for (int v : G[i]) {       if (v == fa) continue;       dfs(v, i);     }     out[i] = time_tag;   }   void init(int k, int l, int r) {     if (r == l) return;     if (r - l == 1) {       STree[k][a[H[l]]] = 1;       return;     }     int m = l + r >> 1;     init(k << 1, l, m);     init(k << 1 | 1, m, r);     STree[k] = STree[k << 1] | STree[k << 1 | 1];     return;   }   void inline push_down(int k) {     if (Lazy[k]) {       (Lazy[k << 1] += Lazy[k]) %= m;       (Lazy[k << 1 | 1] += Lazy[k]) %= m;       STree[k << 1] = ((STree[k << 1] << Lazy[k]) & mask) |                       ((STree[k << 1] >> (m - Lazy[k])) & mask);       STree[k << 1 | 1] = ((STree[k << 1 | 1] << Lazy[k]) & mask) |                           ((STree[k << 1 | 1] >> (m - Lazy[k])) & mask);       Lazy[k] = 0;     }   }   void inline maintain(int k) {     STree[k] = STree[k << 1] | STree[k << 1 | 1];     return;   }   void modify(int k, int l, int r, int ql, int qr, int val) {     if (ql >= r || qr <= l) return;     if (ql <= l && r <= qr) {       (Lazy[k] += val) %= m;       STree[k] = ((STree[k] << val) & mask) | ((STree[k] >> (m - val)) & mask);       return;     }     push_down(k);     int m = l + r >> 1;     modify(k << 1, l, m, ql, qr, val);     modify(k << 1 | 1, m, r, ql, qr, val);     maintain(k);   }   bitset<MAXM> query(int k, int l, int r, int ql, int qr) {     if (ql >= r || qr <= l) return bitset<MAXM>();     if (ql <= l && r <= qr) return STree[k];     push_down(k);     int m = l + r >> 1;     bitset<MAXM> ret =         query(k << 1, l, m, ql, qr) | query(k << 1 | 1, m, r, ql, qr);     maintain(k);     return ret;   }   int main() {     Prime = ~Prime;     Prime[0] = Prime[1] = 0;     for (int i = (2); i < (1001); i++) {       if (Prime[i]) {         for (int j = i + i; j <= 1000; j += i) {           Prime[j] = 0;         }       }     }     scanf( %d%d , &n, &m);     for (int i = (0); i < (m); i++) mask[i] = 1;     for (int i = (1); i < (n + 1); i++) scanf( %d , a + i), a[i] %= m;     for (int i = (1); i < (n); i++) {       int u, v;       scanf( %d%d , &u, &v);       G[u].push_back(v), G[v].push_back(u);     }     dfs(1, -1);     init(1, 1, time_tag + 1);     int q;     scanf( %d , &q);     for (int cas = (0); cas < (q); cas++) {       int op, v, x;       scanf( %d , &op);       if (op == 1) {         scanf( %d%d , &v, &x);         modify(1, 1, time_tag + 1, in[v], out[v] + 1, x % m);       } else {         scanf( %d , &v);         bitset<MAXM> res = Prime & query(1, 1, time_tag + 1, in[v], out[v] + 1);         printf( %d n , res.count());       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 600 + 5;   const int M = 2e5 + 5;   template <class T>   inline void getin(T& num) {     char c;     bool flag = 0;     num = 0;     while ((c = getchar()) <  0  || c >  9 )       if (c ==  - ) flag = 1;     while (c >=  0  && c <=  9 ) {       num = num * 10 + c - 48;       c = getchar();     }     if (flag) num = -num;   }   int n, m, u, v, In[N];   int tot, x[N], y[N], Out[N];   int cnt, fir[N], tar[M], nxt[M];   long long p, f[N][N], a[N][N];   inline void link(int a, int b) {     tar[++cnt] = b, In[b]++, Out[a]++;     nxt[cnt] = fir[a], fir[a] = cnt;   }   template <class T>   inline void add(T& a, T b) {     a += b;     if (a >= p) a -= p;   }   inline void Dp() {     queue<int> q;     int c = 0;     for (int i = 1; i <= n; i++) {       if (!In[i]) x[++tot] = i;       if (!Out[i]) y[++c] = i;     }     for (int i = 1; i <= n; i++) f[i][i] = 1;     for (int i = 1; i <= n; i++)       if (!In[i]) q.push(i);     while (!q.empty()) {       int u = q.front();       q.pop();       for (int i = fir[u]; i; i = nxt[i]) {         int v = tar[i];         for (int i = 1; i <= n; i++) add(f[i][v], f[i][u]);         In[v]--;         if (!In[v]) q.push(v);       }     }     for (int i = 1; i <= tot; i++)       for (int j = 1; j <= tot; j++) a[i][j] = f[x[i]][y[j]];   }   inline int calc(int n) {     int ret = 1;     for (int i = 1; i <= n; i++) {       if (a[i][i] < 0) {         ret = -ret;         for (int k = i; k <= n; k++) a[i][k] = -a[i][k];       }       for (int j = i + 1; j <= n; j++) {         for (int k = i; k <= n; k++) a[i][k] %= p, a[j][k] %= p;         while (a[j][i]) {           if (a[j][i] < 0) {             ret = -ret;             for (int k = i; k <= n; k++) a[j][k] = -a[j][k];           }           int t = a[i][i] / a[j][i];           for (int k = i; k <= n; k++) a[i][k] = (a[i][k] - t * a[j][k]) % p;           for (int k = i; k <= n; k++) swap(a[i][k], a[j][k]);           ret = -ret;         }       }       if (a[i][i] == 0) return 0;       ret = 1ll * ret * a[i][i] % p;     }     return (ret + p) % p;   }   int main() {     getin(n), getin(m), getin(p);     for (int i = 1; i <= m; i++) getin(u), getin(v), link(u, v);     Dp();     cout << calc(tot) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   bool is_right(complex<long long> p1, complex<long long> p2,                 complex<long long> p3) {     return (conj(p2 - p1) * (p3 - p1)).imag() < 0;   }   const int N = 1e5 + 5;   vector<complex<long long> > U;   complex<long long> a[N];   int n;   void read_input() {     cin >> n;     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       a[i] = complex<long long>(x, y - 1LL * x * x);     }   }   void solve() {     sort(a, a + n, [](complex<long long> a, complex<long long> b) {       return make_pair(a.real(), a.imag()) < make_pair(b.real(), b.imag());     });     for (int i = 0; i < n; U.push_back(a[i++]))       while (U.size() > 1 && !is_right(U[U.size() - 2], U.back(), a[i]))         U.pop_back();     cout << U.size() - (U.size() > 1 && U[0].real() == U[1].real()) - 1;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     read_input(), solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline bool chkmin(T& x, T y) {     return x > y ? x = y, true : false;   }   template <class T>   inline bool chkmax(T& x, T y) {     return x < y ? x = y, true : false;   }   inline int read(void) {     int x, f = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) f = -1;     for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ;     return x * f;   }   template <typename T>   struct Graph {     enum { maxn = 305, maxm = 200000 };     struct Edge {       int to, nxt;       T w, cap, flow;     } v[maxm];     int h[maxn], cnt;     Graph() { memset(h, -1, sizeof(h)); }     void add1(int x, int y, T w, T cap) {       v[cnt] = Edge{y, h[x], w, cap, 0};       h[x] = cnt++;     }     void add(int x, int y, T w, T cap) {       add1(x, y, w, cap);       add1(y, x, -w, 0);     }   };   Graph<int> G;   int Ss, Se;   static int f[305];   int dfs(int x, int re) {     if (x == Se || re <= 0) return re;     int s1 = 0, t;     for (int i = G.h[x]; i != -1; i = G.v[i].nxt) {       int to = G.v[i].to;       if (f[to] != f[x] + 1) continue;       t = dfs(to, min(re, G.v[i].cap - G.v[i].flow));       G.v[i].flow += t;       G.v[i ^ 1].flow -= t;       s1 += t;       re -= t;       if (re == 0) break;     }     return s1;   }   bool bfs(int x) {     queue<int> Q;     memset(f, -1, sizeof(f));     Q.push(x);     f[x] = 0;     while (!Q.empty()) {       int u = Q.front();       Q.pop();       for (int i = G.h[u]; i != -1; i = G.v[i].nxt) {         if (G.v[i].flow >= G.v[i].cap) continue;         int to = G.v[i].to;         if (f[to] != -1) continue;         f[to] = f[u] + 1;         Q.push(to);       }     }     return f[Se] != -1;   }   int dinic() {     int ans = 0;     while (bfs(Ss)) ans += dfs(Ss, 1000000000);     return ans;   }   bool vi[305];   int n, lk[305], v, x, ans, w;   vector<int> V[305];   bool dfs(int x) {     vi[x] = 1;     for (int i = 0; i <= V[x].size() - 1; ++i) {       int to = V[x][i];       if (!lk[to] || (!vi[lk[to]] && dfs(lk[to]))) {         lk[to] = x;         return 1;       }     }     return 0;   }   int main(void) {     n = read();     for (int i = 1; i <= n; ++i) {       v = read();       while (v--) {         x = read();         V[i].push_back(x);       }     }     for (int i = 1; i <= n; ++i) {       memset(vi, 0, sizeof(vi));       dfs(i);     }     for (int i = 1; i <= n; ++i)       for (int j = 0; j <= V[i].size() - 1; ++j)         G.add(i, lk[V[i][j]], 0, 1000000000);     Ss = n + 1;     Se = n + 2;     for (int i = 1; i <= n; ++i) {       w = -read();       if (w < 0)         G.add(i, Se, 0, -w);       else         G.add(Ss, i, 0, w), ans += w;     }     printf( %d n , -(ans - dinic()));     return 0;   }


#include <bits/stdc++.h>   int valid(char prev, char next) {     char s[] =  aeioun ;     for (int i = 0; i < 6; i++)       if (prev == s[i]) return 1;     for (int i = 0; i < 5; i++)       if (next == s[i]) return 1;     return 0;   }   int main() {     char s[300];     scanf( %s , s);     int v = 1;     for (int i = 0; s[i] !=   0 ; i++) v &= valid(s[i], s[i + 1]);     printf( %s n , v ?  Yes  :  No );   }


#include <bits/stdc++.h>   using namespace std;   map<pair<long long, long long>, pair<long long, long long> > mp;   long long n;   pair<long long, long long> solve(pair<long long, long long> a) {     if (a.second < 10)       return make_pair(a.first || a.second, a.second - max(a.first, a.second));     if (mp[a].first) return mp[a];     long long ans = 0, p = 1;     while (a.second >= p * 10) p *= 10;     pair<long long, long long> b =         solve(make_pair(max(a.first, a.second / p), a.second % p));     ans += b.first;     pair<long long, long long> c =         solve(make_pair(a.first, a.second / p * p + b.second));     ans += c.first;     return mp[a] = make_pair(ans, c.second);   }   int main() {     cin >> n;     cout << solve(make_pair(0, n)).first;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int k;   int n1, n2, n3;   int t1, t2, t3;   int v1[3000];   int v2[3030];   int v3[3030];   int main() {     ios::sync_with_stdio(false);     cin >> k >> n1 >> n2 >> n3 >> t1 >> t2 >> t3;     int time = t1 + t2 + t3 - 1;     int pos = 0;     int res = 0;     for (int i = 0; i <= time + 1; i++) {       v1[i] = 0;       v2[i] = 0;       v3[i] = 0;     }     for (int i = 1; i <= k; i++) {       int posnew = -1;       for (int j = pos; j <= pos + time + 1; j++) {         int kk = j - pos;         if ((v1[kk] + 1 <= n1) &&             ((kk + t1 > time + 1) || (v2[kk + t1] + 1 <= n2)) &&             ((kk + t1 + t2 > time + 1) || (v3[kk + t1 + t2] + 1 <= n3))) {           posnew = j;           break;         }       }       int t = min(pos + time + 1, posnew + time);       for (int j = posnew; j <= t; j++) {         if (j <= posnew + t1 - 1)           v1[j - pos]++;         else if (j <= posnew + t1 + t2 - 1)           v2[j - pos]++;         else           v3[j - pos]++;         v1[j - posnew] = v1[j - pos];         v2[j - posnew] = v2[j - pos];         v3[j - posnew] = v3[j - pos];       }       for (int j = t + 1; j <= posnew + time; j++) {         if (j <= posnew + t1 - 1) {           v1[j - posnew] = 1;           v2[j - posnew] = 0;           v3[j - posnew] = 0;         } else if (j <= posnew + t1 + t2 - 1) {           v2[j - posnew] = 1;           v1[j - posnew] = 0;           v3[j - posnew] = 0;         } else {           v3[j - posnew] = 1;           v1[j - posnew] = 0;           v2[j - posnew] = 0;         }       }       v1[time + 1] = 0;       v2[time + 1] = 0;       v3[time + 1] = 0;       res = posnew + time;       pos = posnew;     }     cout << res + 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;      #define int int64_t   #define MOD 998244353   #define inf 9e18   #define tikoo (int) (1e9 + 7)      template<typename T, typename T1> T max(T a, T1 b) { if (b > a) a = b; return a; }   template<typename T, typename T1> T min(T a, T1 b) { if (b < a) a = b; return a; }   int fastpow(int a, int b, int m = tikoo) { int res = 1; a %= m;       while (b > 0) { if (b & 1) res = (res * a) % m; a = (a * a) % m; b >>= 1; } return res; }      signed main() {       ios_base::sync_with_stdio(0);       cin.tie(0), cout.tie(0);       int t = 1;       cin >> t;       while (t--) {           int n, k;           cin >> n >> k;           cout << fastpow(n, k) <<   n ;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   void cppio() {     ios_base::sync_with_stdio(0);     cin.tie(0);   }   const int maxn = int(2e5) + 10;   int n;   int a[maxn];   int p, q;   long long first, second;   int gcd(int a, int b) {     if (!b) {       first = 1, second = 0;       return a;     }     int q = a / b;     int ret = gcd(b, a % b);     tie(first, second) = make_pair(second, first - second * q);     return ret;   }   int ans[maxn][3];   int main() {     cppio();     cin >> n;     for (int i = 1; i <= (n); ++i) cin >> a[i];     if (n == 1) {       cout <<  1 1 n  << (-a[1]) <<   n1 1 n0 n1 1 n0 n ;       return 0;     }     cout << 1 <<     << n <<   n0  ;     for (int i = 2; i <= n; ++i) {       cout << (-n * 1ll * a[i]) <<    ;     }     cout <<   n ;     cout << 1 <<     << 1 <<   n ;     cout << (-a[1]) <<   n ;     cout << 2 <<     << n <<   n ;     for (int i = 2; i <= n; ++i) {       int first = a[i];       cout << ((n - 1) * 1ll * first) <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[100005], dist[100005], sad[100005], subt[100005], ans = 0LL;   vector<pair<long long, long long> > adj[100005];   void dfs(long long node, long long par, long long min_vertex) {     if (par != -1) sad[node] = (a[node] < dist[node] - dist[min_vertex]);     min_vertex = (dist[node] < dist[min_vertex]) ? node : min_vertex;     for (auto i : adj[node])       if (par != i.first) {         dist[i.first] += (dist[node] + i.second);         dfs(i.first, node, min_vertex);         subt[node] += subt[i.first];       }     subt[node]++;   }   void dfs1(long long node, long long par) {     if (sad[node]) {       ans += subt[node];       return;     }     for (auto i : adj[node])       if (i.first != par) dfs1(i.first, node);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int TESTS = 1;     while (TESTS--) {       long long n, i, j, k;       cin >> n;       for (i = 1; i <= n; i++) cin >> a[i];       for (i = 1; i <= n - 1; i++) {         long long x, wt;         cin >> x >> wt;         adj[i + 1].push_back(make_pair(x, wt));         adj[x].push_back(make_pair(i + 1, wt));       }       dfs(1, -1, 1);       dfs1(1, -1);       cout << ans;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e4 + 10;   const int M = 128;   const int inf = 0x7fffffff;   const long long INF = 9E18;   const int mod = 1e9 + 7;   int n, m;   int pos[M][maxn], len[M];   int dp[M][maxn];   char s[M][maxn];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %s , s[i] + 1);       for (int j = 1; j <= m; j++) {         if (s[i][j] ==  1 ) pos[i][len[i]++] = j;       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (len[i] == 0) {           puts( -1 );           return 0;         }         int cnt = len[i];         int loc = lower_bound(pos[i], pos[i] + cnt, j) - pos[i];         int movl = inf, movr = inf;         if (loc == len[i]) {           loc--;           movr = j - pos[i][loc];           movl = m - (j - pos[i][0]);         } else if (loc == 0) {           movl = pos[i][loc] - j;           movr = m - (pos[i][len[i] - 1] - j);         } else {           movl = pos[i][loc] - j;           movr = j - pos[i][--loc];         }         dp[i][j] = min(movl, movr);       }     }     int ans = inf;     for (int i = 1; i <= m; i++) {       int sum = 0;       for (int j = 1; j <= n; j++) {         sum += dp[j][i];       }       ans = min(ans, sum);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string x;     deque<char> vec;     int c;     bool flag;     int max = 0;     for (int i = 0; i < 8; i++) {       cin >> x;       for (int j = 0; j < x.length(); j++) vec.push_back(x[j]);       if (i % 2 == 0) {         c = 0;         while (true) {           c++;           flag = true;           for (int j = 1; j < x.length(); j++) {             if (x[j] == x[j - 1]) {               flag = false;               char temp = vec.back();               vec.pop_back();               vec.push_front(temp);               break;             }           }           if (c == 9 || flag == true) break;         }       } else if (i % 2 != 0) {         c = 0;         while (true) {           c++;           flag = true;           for (int j = 1; j < x.length(); j++) {             if (x[j] == x[j - 1]) {               flag = false;               char temp = vec.back();               vec.pop_back();               vec.push_front(temp);               break;             }           }           if (c == 9 || flag == true) break;         }       }       if (c > max) max = c;     }     if (flag == false || max == 9)       cout <<  NO ;     else       cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n = 3, i, j, a, b;     vector<pair<int, int> > v, v2;     for (i = 0; i < 3; i++) {       scanf( %d%d , &a, &b);       v.push_back(make_pair(a, b));       v2.push_back(make_pair(b, a));     }     int cnt, c2, c3;     sort(v.begin(), v.end());     sort(v2.begin(), v2.end());     if (v[0].first == v[1].first) {       if (v[1].first == v[2].first)         cnt = 1;       else if (v[1].second == v[2].second)         cnt = 2;       else         cnt = 3;     } else {       if (v[1].first == v[2].first)         cnt = 2;       else if (v[1].second == v[2].second)         cnt = 3;       else         cnt = 3;     }     if (v[0].first == v[2].first) {       if (v[1].first == v[2].first)         c2 = 1;       else if (v[1].second == v[2].second)         c2 = 2;       else         c2 = 3;     } else {       if (v[1].first == v[2].first)         c2 = 2;       else if (v[1].second == v[2].second)         c2 = 3;       else         c2 = 3;     }     if (v[0].first == v[2].first) {       if (v[1].first == v[0].first)         c3 = 1;       else if (v[1].second == v[0].second)         c3 = 2;       else         c3 = 3;     } else {       if (v[1].first == v[0].first)         c3 = 2;       else if (v[1].second == v[0].second)         c3 = 3;       else         c3 = 3;     }     cnt = min(cnt, min(c2, c3));     int c1;     if (v2[0].first == v2[1].first) {       if (v2[1].first == v2[2].first)         c1 = 1;       else if (v2[1].second == v2[2].second)         c1 = 2;       else         c1 = 3;     } else {       if (v2[1].first == v2[2].first)         c1 = 2;       else if (v2[1].second == v2[2].second)         c1 = 3;       else         c1 = 3;     }     if (v2[0].first == v2[2].first) {       if (v2[1].first == v2[2].first)         c2 = 1;       else if (v2[1].second == v2[2].second)         c2 = 2;       else         c2 = 3;     } else {       if (v2[1].first == v2[2].first)         c2 = 2;       else if (v2[1].second == v2[2].second)         c2 = 3;       else         c2 = 3;     }     if (v2[0].first == v2[2].first) {       if (v2[1].first == v2[0].first)         c3 = 1;       else if (v2[1].second == v2[0].second)         c3 = 2;       else         c3 = 3;     } else {       if (v2[1].first == v2[0].first)         c3 = 2;       else if (v2[1].second == v2[0].second)         c3 = 3;       else         c3 = 3;     }     if (v[0].first == v[1].first) {       if (v[2].second < v[1].second && v[2].second > v[0].second) {         cout <<  3  << endl;         exit(0);       } else if (v[2].second < v[0].second && v[2].second > v[1].second) {         cout <<  3  << endl;         exit(0);       } else {         cnt = min(cnt, min(c1, min(c2, c3)));       }     } else if (v2[0].first == v2[1].first) {       if (v2[2].second < v2[1].second && v2[2].second > v2[0].second) {         cout <<  3  << endl;         exit(0);       } else if (v2[2].second < v2[0].second && v2[2].second > v2[1].second) {         cout <<  3  << endl;         exit(0);       } else {         cnt = min(cnt, min(c1, min(c2, c3)));       }     } else       cnt = min(cnt, min(c1, min(c2, c3)));     reverse(v.begin(), v.end());     reverse(v2.begin(), v2.end());     int cnt1;     if (v[0].first == v[1].first) {       if (v[1].first == v[2].first)         cnt1 = 1;       else if (v[1].second == v[2].second)         cnt1 = 2;       else         cnt1 = 3;     } else {       if (v[1].first == v[2].first)         cnt1 = 2;       else if (v[1].second == v[2].second)         cnt1 = 3;       else         cnt1 = 3;     }     if (v[0].first == v[2].first) {       if (v[1].first == v[2].first)         c2 = 1;       else if (v[1].second == v[2].second)         c2 = 2;       else         c2 = 3;     } else {       if (v[1].first == v[2].first)         c2 = 2;       else if (v[1].second == v[2].second)         c2 = 3;       else         c2 = 3;     }     if (v[0].first == v[2].first) {       if (v[1].first == v[0].first)         c3 = 1;       else if (v[1].second == v[0].second)         c3 = 2;       else         c3 = 3;     } else {       if (v[1].first == v[0].first)         c3 = 2;       else if (v[1].second == v[0].second)         c3 = 3;       else         c3 = 3;     }     cnt1 = min(cnt1, min(c2, c3));     if (v2[0].first == v2[1].first) {       if (v2[1].first == v2[2].first)         c1 = 1;       else if (v2[1].second == v2[2].second)         c1 = 2;       else         c1 = 3;     } else {       if (v2[1].first == v2[2].first)         c1 = 2;       else if (v2[1].second == v2[2].second)         c1 = 3;       else         c1 = 3;     }     if (v2[0].first == v2[2].first) {       if (v2[1].first == v2[2].first)         c2 = 1;       else if (v2[1].second == v2[2].second)         c2 = 2;       else         c2 = 3;     } else {       if (v2[1].first == v2[2].first)         c2 = 2;       else if (v2[1].second == v2[2].second)         c2 = 3;       else         c2 = 3;     }     if (v2[0].first == v2[2].first) {       if (v2[1].first == v2[0].first)         c3 = 1;       else if (v2[1].second == v2[0].second)         c3 = 2;       else         c3 = 3;     } else {       if (v2[1].first == v2[0].first)         c3 = 2;       else if (v2[1].second == v2[0].second)         c3 = 3;       else         c3 = 3;     }     if (v[0].first == v[1].first) {       if (v[2].second < v[1].second && v[2].second > v[0].second) {         cout <<  3  << endl;         exit(0);       } else if (v[2].second < v[0].second && v[2].second > v[1].second) {         cout <<  3  << endl;         exit(0);       } else {         cnt1 = min(cnt1, min(c1, min(c2, c3)));       }     } else if (v2[0].first == v2[1].first) {       if (v2[2].second < v2[1].second && v2[2].second > v2[0].second) {         cout <<  3  << endl;         exit(0);       } else if (v2[2].second < v2[0].second && v2[2].second > v2[1].second) {         cout <<  3  << endl;         exit(0);       } else {         cnt1 = min(cnt1, min(c1, min(c2, c3)));       }     } else       cnt1 = min(cnt1, min(c1, min(c2, c3)));     cnt = min(cnt, cnt1);     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     long long ans = 0;     bool flag = false;     if (n == 2) {       cout << 1 <<   n ;       return 0;     }     for (int i = 2; i <= sqrt(n); i++) {       if (n % i == 0) {         flag = true;         break;       }     }     if (!flag) {       cout << 1 <<   n ;       return 0;     }     if (n & 1) {       flag = false;       long long x = n - 2;       for (int i = 2; i <= sqrt(x); i++) {         if (x % i == 0) {           flag = true;           break;         }       }       if (!flag) {         cout << 2 <<   n ;         return 0;       } else         cout << 3 <<   n ;     } else {       cout << 2 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 56;   int n, a[N];   int cnt(int x) {     int ans = 0;     for (int i = 1; i <= n; ++i) ans += (a[i] == x);     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cin >> n;     for (int i = 1; i <= n; ++i) cin >> a[i];     int k = *min_element(a + 1, a + n + 1);     cout << (cnt(k) <= n / 2 ?  Alice  :  Bob );   }


#include <bits/stdc++.h>   using namespace std;   long long num[30], pre[100010], cnt;   int main() {     int n, l, lr, ll;     char s[100010];     cin >> n;     for (int i = 0; i < n; i++) {       cin >> s + 1;       l = strlen(s + 1);       for (int j = 0; j < 26; j++)         if (num[j] && j +  a  != s[1] && j +  a  != s[l]) num[j] = 1;       lr = 1;       while (s[lr + 1] == s[lr]) lr++;       ll = l;       while (lr != l && s[ll] == s[ll - 1]) ll--;       ll = l - ll + 1;       if (s[1] == s[l])         if (lr == l)           num[s[1] -  a ] = (l + 1) * num[s[1] -  a ] + l;         else           num[s[1] -  a ] = lr + ll + 1;       else {         num[s[1] -  a ] = num[s[1] -  a ] > 0 ? lr + 1 : lr;         num[s[l] -  a ] = num[s[l] -  a ] > 0 ? ll + 1 : ll;       }       for (int j = lr + 1; j <= l - ll + 1; j++)         if (s[j] == s[j - 1])           pre[j] = pre[j - 1] + 1;         else {           pre[j] = 1;           num[s[j - 1] -  a ] = max(num[s[j - 1] -  a ], pre[j - 1]);         }     }     cnt = 0;     for (int j = 0; j < 26; j++) cnt = max(cnt, num[j]);     cout << cnt;   }


#include <bits/stdc++.h>   using namespace std;   long long sub(long long a, long long b) {     return (a - b + 1000000007) % 1000000007;   }   long long add(long long a, long long b) { return (a + b) % 1000000007; }   long long mul(long long a, long long b) { return (a * b) % 1000000007; }   vector<long long> adj[100005];   bool vis[100005];   long long siz, edges;   void dfs(long long i) {     vis[i] = 1;     for (long long j = 0; j < adj[i].size(); j++) {       edges++;       if (!vis[adj[i][j]]) dfs(adj[i][j]);     }   }   long long mpow(long long base, long long exp) {     base %= 1000000007;     long long result = 1;     while (exp > 0) {       if (exp & 1) result = ((long long)result * base) % 1000000007;       base = ((long long)base * base) % 1000000007;       exp >>= 1;     }     return result;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long n, sum = 0, t = 1, m, ans;     while (t--) {       string s1, s =   ;       cin >> s1;       for (long long i = 0; i < s1.length(); i++)         if (s1[i] ==  a  || s1[i] ==  b ) s += s1[i];       n = s.length();       vector<long long> dp(n + 1, 1);       dp[0] = 1;       ans = 1;       for (long long i = 1; i < n + 1; i++) {         if (s[i - 1] ==  a )           dp[i] = dp[i - 1] + 1;         else {           dp[i] = 1;           ans = (ans * (dp[i - 1])) % 1000000007;         }       }       ans = (ans * dp[n]) % 1000000007;       cout << ans - 1 <<   n ;     }     cerr <<   nTime elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   s. n ;     ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> v, v1;     int a, b, c, d, e, f, g, h, i, j, k, l;     cin >> a;     for (i = 0; i < a; i++) {       v.clear();       v1.clear();       cin >> c >> d >> b;       map<int, int> m;       for (j = 0; j < b; j++) {         cin >> e;         m[e]++;         v.push_back(e);       }       f = 0;       g = -1;       h = -1;       int min1 = INT_MAX;       for (j = 0; j < v.size(); j++) {         for (k = d; k <= c; k++) {           if (!m.count(k)) {             min1 = min(k - d, min1);             break;           }         }         for (k = d; k >= 1; k--) {           if (!m.count(k)) {             min1 = min(d - k, min1);             break;           }         }       }       cout << min1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int t, n;   string s;   int main() {     ios_base::sync_with_stdio(false);     cin >> t;     while (t--) {       cin >> s;       int mask = 0;       n = s.size();       int ok100001 = (s[0] ==  1 );       for (int i = 1; i + 1 < n; ++i) ok100001 &= (s[i] ==  0 );       ok100001 &= (s.back() <=  1 );       if (ok100001) {         for (int i = 0; i < n - 2; ++i) cout << 9;         cout <<   n ;         continue;       }       for (int i = 0; i < n; ++i) mask ^= 1 << (s[i] - 48);       for (int i = n - 1; i >= 0; --i) {         mask ^= 1 << (s[i] - 48);         if (s[i] ==  0 ) continue;         int popcnt = __builtin_popcount(mask);         if (popcnt > n - i) continue;         if (popcnt % 2 != (n - i) % 2) continue;         if (popcnt < n - i ||             (popcnt == n - i && __builtin_ctz(mask) < s[i] - 48)) {           int curdigit = s[i] - 48 - 1;           if (popcnt == n - i)             for (; curdigit >= 0; --curdigit)               if (mask & (1 << curdigit)) break;           s[i] = curdigit +  0 ;           mask ^= 1 << curdigit;           for (int j = i + 1; j < n; ++j) {             curdigit = 9;             if (__builtin_popcount(mask) == n - j)               for (; curdigit >= 0; --curdigit)                 if (mask & (1 << curdigit)) break;             s[j] = curdigit +  0 ;             mask ^= 1 << curdigit;           }           break;         }       }       cout << s <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   long long a[30], b[30], dp[30][30][30];   struct node {     long long dis[30][30];     node operator*(const node &t) const {       node rep;       memset(rep.dis, 0x3f, sizeof rep.dis);       for (int i = 0; i <= n; i++)         for (int j = 0; j <= n; j++)           for (int k = 0; k <= n; k++)             rep.dis[i][j] = min(rep.dis[i][j], dis[i][k] + t.dis[k][j]);       return rep;     }   } key;   node pow(node rep, int n) {     if (n == 1) return rep;     node req = pow(rep, n / 2);     if (n % 2 == 0) return req * req;     return req * req * rep;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) scanf( %d , &b[i]);     memset(dp, 0x3f, sizeof dp);     for (int i = 0; i <= n; i++) dp[i][i][0] = 0;     for (int k = 1; k <= n; k++)       for (int i = 0; i <= n; i++)         for (int j = 0; j <= n; j++) {           if (j - 1 >= 0)             dp[i][j][k] = min(dp[i][j][k], dp[i][j - 1][k - 1] + a[k]);           if (j + 1 <= n)             dp[i][j][k] = min(dp[i][j][k], dp[i][j + 1][k - 1] + b[k]);         }     for (int i = 0; i <= n; i++)       for (int j = 0; j <= n; j++) key.dis[i][j] = dp[i][j][n];     key = pow(key, m);     printf( %d n , key.dis[0][0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, p = 0, q = 0;     cin >> n;     string str;     cin >> str;     for (int i = 0; i < n; i++) {       if (str[i] ==  F  && str[i + 1] ==  S ) p = p + 1;       if (str[i] ==  S  && str[i + 1] ==  F ) q = q + 1;     }     if (q > p)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   const int nMax = 200001;   struct Segment {     int left;     int right;     long long sum;     long long forced;     Segment* lChild;     Segment* rChild;   };   Segment* initTree(int left, int right, long long forced) {     Segment* segment = new Segment;     segment->left = left;     segment->right = right;     segment->forced = forced;     if (left == right) {       segment->lChild = nullptr;       segment->rChild = nullptr;       segment->sum = left;       segment->forced = left;     } else {       segment->lChild = initTree(left, (left + right) / 2, forced);       segment->rChild = initTree((left + right) / 2 + 1, right, forced);       segment->sum = ((segment->rChild == nullptr) ? 0 : segment->rChild->sum) +                      ((segment->lChild == nullptr) ? 0 : segment->lChild->sum);     }     return segment;   }   int customMax(int fd, int sd) { return (fd > sd) ? fd : sd; }   int getValue(Segment* root, int item, int value = -1) {     if (root->left == root->right) {       if (value == -1) value = root->forced;       return value;     }     if (value == -1)       if (root->forced != -1) value = root->forced;     if (item <= (root->left + root->right) / 2)       return getValue(root->lChild, item, value);     else       return getValue(root->rChild, item, value);   }   int safeForceCheck(Segment* root, int left, int right, int threshold) {     if (left > right) return -1;     if (left == right)       if (getValue(root, left) >= threshold)         return -1;       else         return left;     int result;     if (getValue(root, (left + right) / 2) >= threshold) {       result = safeForceCheck(root, left, (left + right) / 2 - 1, threshold);     } else {       result = safeForceCheck(root, (left + right) / 2 + 1, right, threshold);       if (result == -1) result = (left + right) / 2;     }     return result;   }   void forceToSegment(Segment* root, int left, int right, long long forced,                       long long was_forced = -1) {     if (left < 0) return;     if (right < left) return;     if ((left == root->left) && (right == root->right)) {       root->forced = forced;       root->sum = (right - left + 1) * forced;       return;     }     if (was_forced == -1) was_forced = root->forced;     if (was_forced != -1) {       if (was_forced >= forced) return;       root->lChild->forced = was_forced;       root->lChild->sum =           (root->lChild->right - root->lChild->left + 1) * was_forced;       root->rChild->forced = was_forced;       root->rChild->sum =           (root->rChild->right - root->rChild->left + 1) * was_forced;       root->forced = -1;     }     if (right <= (root->left + root->right) / 2) {       forceToSegment(root->lChild, left, right, forced, was_forced);     } else if (left > (root->left + root->right) / 2) {       forceToSegment(root->rChild, left, right, forced, was_forced);     } else {       forceToSegment(root->lChild, left, (root->left + root->right) / 2, forced,                      was_forced);       forceToSegment(root->rChild, (root->left + root->right) / 2 + 1, right,                      forced, was_forced);     }     root->sum = ((root->rChild == nullptr) ? 0 : root->rChild->sum) +                 ((root->lChild == nullptr) ? 0 : root->lChild->sum);   }   int main() {     long n;     Segment* root;     scanf( %ld , &n);     std::vector<int> a(n);     std::vector<int> used(nMax, -1);     int maxA = 0;     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);       used[a[i]] = i;       if (a[i] > maxA) maxA = a[i];     }     maxA++;     std::vector<long long> downCount(maxA, 0);     std::vector<int> rowTail(n, 0);     std::vector<std::vector<int> > affective(maxA);     downCount[maxA - 1] = ((long long)n * (n + 1)) / 2;     for (int i = 1; i < maxA; ++i)       for (int j = i; j < maxA; j += i)         if (used[j] != -1) affective[i].push_back(used[j]);     root = initTree(0, n - 1, -1);     for (int i = maxA - 2; i >= 1; i--) {       if (affective[i].size() <= 1) {         downCount[i] = downCount[i + 1];       } else {         std::sort(affective[i].begin(), affective[i].end());         forceToSegment(root, affective[i][1] + 1,                        safeForceCheck(root, affective[i][1] + 1, n - 1, n), n);         forceToSegment(root, affective[i][0] + 1,                        safeForceCheck(root, affective[i][0] + 1, affective[i][1],                                       affective[i][affective[i].size() - 1]),                        affective[i][affective[i].size() - 1]);         forceToSegment(root, 0,                        safeForceCheck(root, 0, affective[i][0],                                       affective[i][affective[i].size() - 2]),                        affective[i][affective[i].size() - 2]);         downCount[i] = ((long long)n * (n)) - root->sum;       }     }     long long result = 0LL;     for (int i = maxA - 2; i >= 1; i--) {       result += (downCount[i + 1] - downCount[i]) * i;     }     printf( %I64d , result);     return 0;   }