#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   int nxt[4][100001] = {0};   bool ok[4];   void getnext(int m, char s2[], int t) {     int j = 0;     for (int i = 2; i <= m; i++) {       while (j != 0 && s2[j + 1] != s2[i]) j = nxt[t][j];       if (s2[j + 1] == s2[i]) j++;       nxt[t][i] = j;     }     return;   }   int KMP(int n, int m, char s1[], char s2[], int t) {     int j = 0, cnt = 0;     for (int i = 1; i <= n; i++) {       while (j != 0 && s2[j + 1] != s1[i]) j = nxt[t][j];       if (s2[j + 1] == s1[i]) j++;       if (j == m) {         return -1;         j = nxt[t][j];       }     }     return j;   }   char s[4][100001];   int len[4];   int K[4][4];   int main() {     scanf( %s%s%s , s[1] + 1, s[2] + 1, s[3] + 1);     len[1] = strlen(s[1] + 1);     len[2] = strlen(s[2] + 1);     len[3] = strlen(s[3] + 1);     for (int i = 1; i <= 3; i++) {       getnext(len[i], s[i], i);       for (int j = 1; j <= 3; j++) {         if (i == j) continue;         K[j][i] = KMP(len[j], len[i], s[j], s[i], i);       }     }     int ans = INF;     for (int i = 1; i <= 3; i++) {       for (int j = 1; j <= 3; j++) {         for (int k = 1; k <= 3; k++) {           if (i == j || j == k || i == k) continue;           int sum = len[i] + len[j] + len[k] - K[i][j] - K[j][k];           if (K[i][j] >= 0 && K[j][k] >= 0)             ans = min(ans, sum);           else {             if (K[i][j] < 0 && K[i][k] < 0)               ans = min(ans, len[i]);             else if (K[i][j] < 0)               ans = min(ans, sum + K[i][j] + K[j][k] - len[j] - K[i][k]);             if (K[j][k] < 0) ans = min(ans, sum + K[j][k] - len[k]);           }         }       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void file(string s) {     freopen((s +  .in ).c_str(),  r , stdin);     freopen((s +  .out ).c_str(),  w , stdout);   }   template <typename Tp>   void read(Tp &x) {     long long fh = 1;     char c = getchar();     x = 0;     while (c >  9  || c <  0 ) {       if (c ==  - ) {         fh = -1;       }       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 1) + (x << 3) + (c & 15);       c = getchar();     }     x *= fh;   }   long long n, m, T;   vector<long long> ft[1000005];   long long pos[1000005];   namespace FTree {   long long dfnl[1000005], dfnr[1000005], tot;   struct BIT {     long long b[1000005];     void add(long long x, long long v) {       for (; x <= tot; x += (x & (-x))) b[x] += v;     }     long long ask(long long x) {       long long ret = 0;       for (; x; x -= (x & (-x))) ret += b[x];       return ret;     }   } Tr;   void dfs(long long x) {     dfnl[x] = ++tot;     for (unsigned i = 0; i < ft[x].size(); ++i) {       long long y = ft[x][i];       dfs(y);     }     dfnr[x] = tot;   }   void prep() {     dfs(0);     for (long long i = 1; i <= n; ++i) {       Tr.add(dfnl[pos[i]], 1);       Tr.add(dfnr[pos[i]] + 1, -1);     }   }   void upd(long long x, long long v) {     Tr.add(dfnl[x], v);     Tr.add(dfnr[x] + 1, -v);   }   long long qry(long long x) { return Tr.ask(dfnl[x]); }   }  // namespace FTree   namespace ACAM {   signed ch[1000005][26], fail[1000005], tot;   long long ins(const char *str) {     long long p = 0, nn = strlen(str), dir;     for (long long i = 0; i < nn; ++i) {       dir = str[i] -  a ;       if (!ch[p][dir]) ch[p][dir] = ++tot;       p = ch[p][dir];     }     return p;   }   void build() {     queue<long long> q;     for (long long i = 0; i < 26; ++i)       if (ch[0][i]) q.push(ch[0][i]);     while (!q.empty()) {       long long x = q.front();       q.pop();       for (long long i = 0; i < 26; ++i) {         if (ch[x][i]) {           fail[ch[x][i]] = ch[fail[x]][i];           q.push(ch[x][i]);         } else {           ch[x][i] = ch[fail[x]][i];         }       }     }     for (long long i = 1; i <= tot; ++i) ft[fail[i]].push_back(i);     FTree::prep();   }   void mtch(const char *str) {     long long p = 0, nn = strlen(str), ans = 0;     for (long long i = 0; i < nn; ++i) {       p = ch[p][str[i] -  a ];       ans += FTree::qry(p);     }     printf( %lld n , ans);   }   }  // namespace ACAM   char sstr[1000005];   bool vis[1000005];   signed main() {     read(T);     read(n);     for (long long i = 1; i <= n; ++i) {       scanf( %s , sstr);       pos[i] = ACAM::ins(sstr);       vis[i] = 1;     }     ACAM::build();     char op;     for (long long i = 1, x; i <= T; ++i) {       scanf(  n%c , &op);       if (op ==  ? ) {         scanf( %s , sstr);         ACAM::mtch(sstr);       }       if (op ==  + ) {         read(x);         if (!vis[x]) {           FTree::upd(pos[x], 1), vis[x] = 1;         }       }       if (op ==  - ) {         read(x);         if (vis[x]) {           FTree::upd(pos[x], -1), vis[x] = 0;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};   vector<int> next(vector<int>& a, int p, int k) {     vector<int> result;     for (long long mul = 1; mul <= (long long)k; mul *= p) {       for (int i = 0; i < a.size(); ++i) {         long long newN = (long long)a[i] * mul;         if (newN <= k) {           result.push_back((int)newN);         }       }     }     return result;   }   int main() {     ios_base::sync_with_stdio(false);     int k;     cin >> k;     int treshold = 2 * k * k;     vector<int> p;     vector<int> a;     a.push_back(1);     int need = (k / 2);     if (k % 2 == 1) need++;     p.push_back(prime[0]);     int ppp = 1;     for (int i = 0; i < p.size(); ++i) {       int old = a.size();       a = next(a, p[i], 2 * k * k);       if (a.size() - old >= need && a.size() >= k)         break;       else         p.push_back(prime[ppp++]);     }     vector<int> result;     vector<int> h(p.size());     int i = a.size() - 1;     for (int ch = (int)p.size() - 1; ch >= 0; ch--) {       for (; h[ch] < need && i >= 0; --i) {         if (a[i] % p[ch] == 0) {           result.push_back(a[i]);           for (int pp = 0; pp < p.size(); ++pp) {             if (a[i] % p[pp] == 0) ++h[pp];           }           a[i] = 31;         }       }     }     while (result.size() < k) {       if (i < 0) i = (a.size() - 1);       if (a[i] != 31) {         result.push_back(a[i]);         a[i] = 31;       }       i--;     }     for (int i = 0; i < result.size(); ++i) cout << result[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   using namespace ::std;   const int maxn = 5300;   const int inf = 1e9 + 800;   const int mod = 1e9 + 7;   bool ro[maxn][maxn];   bool cu[maxn][maxn];   bool mosro[maxn];   bool moscu[maxn];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       for (int j = 0; j < n; j += 4) {         char c = s[j / 4];         if ( A  <= c && c <=  Z ) {           c -=  A ;           c += 10;         } else {           c -=  0 ;         }         ro[i][j] = ((c >> 3) & 1);         ro[i][j + 1] = ((c >> 2) & 1);         ro[i][j + 2] = ((c >> 1) & 1);         ro[i][j + 3] = ((c >> 0) & 1);       }     }     for (int i = 0; i < n; i++) {       for (int j = n - 1; j >= 0; j--) {         cu[i][j] = ro[j][i];       }     }     for (int i = 0; i + 1 < n; i++) {       mosro[i] = 1;       moscu[i] = 1;       for (int j = 0; j < n; j++) {         if (ro[i][j] != ro[i + 1][j]) {           mosro[i] = 0;         }         if (cu[i][j] != cu[i + 1][j]) {           moscu[i] = 0;         }       }     }     for (int x = n; x >= 1; x--) {       if (n % x == 0) {         bool good = 1;         for (int j = 0; j + 1 < n; j++) {           if (j % x != x - 1 && (mosro[j] == 0 || moscu[j] == 0)) {             good = 0;             break;           }         }         if (good) {           cout << x;           return 0;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int p[100010];   const int md = 1000000009;   int an = 1;   int sz[100010];   void make_set(int v) {     p[v] = v;     sz[v] = 1;   }   int find_set(int v) {     if (p[v] == v) return v;     return p[v] = find_set(p[v]);   }   void union_sets(int a, int b) {     a = find_set(a);     b = find_set(b);     if (a != b) {       if (sz[a] < sz[b]) swap(a, b);       p[b] = a;       sz[a] += sz[b];     } else       an = (an * 2) % md;   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) make_set(i);     for (int i = 0; i < m; i++) {       int a, b;       scanf( %d %d , &a, &b);       union_sets(a, b);       printf( %d n , an - 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dp[100][100];   struct point {    public:     long long x, y;     point(long long a, long long b) {       x = a;       y = b;     }     point() {}     void setPoint(long long a, long long b) {       x = a;       y = b;     }     double dist(point p) { return sqrt(pow(x - p.x, 2.0) + pow(y - p.y, 2.0)); }     long long dot(point A, point B) {       point OA(A.x - x, A.y - y), OB(B.x - x, B.y - y);       return (OA.x * OB.x + OA.y * OB.y);     }     long long cross(point A, point B) {       point OA(A.x - x, A.y - y), OB(B.x - x, B.y - y);       return (OA.x * OB.y - OA.y * OB.x);     }     bool operator<(const point &p) const {       return x < p.x || (x == p.x && y < p.y);     }   };   int main() {     long long ans = 0;     int X, Y;     string str;     cin >> str;     Y = str.size();     cin >> X;     int val;     vector<pair<int, int>> a;     for (int i = 0; i < (X); i++) {       cin >> val;       a.push_back(make_pair(val - 1, 1));       a.push_back(make_pair(Y - val, -1));     }     sort((a).begin(), (a).end());     int start = 0, open = 0, stop = 0, dist, last = 1;     for (int i = 0; i <= a.size() / 2; i++) {       stop = a[i].first;       dist = stop;       if (last != a[i].second) {         dist = stop - start + 1;         dist = start + dist / 2;         last = a[i].second;       }       for (int j = (start); j < (dist); j++) {         if (open > 0) {           if (open % 2) {             str[j] ^= str[Y - 1 - j] ^= str[j] ^= str[Y - 1 - j];           }         }       }       start = stop;       open += a[i].second;     }     cout << str << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int n;   int main() {     cin >> n;     cin >> s;     for (int i = 0; i < n; i++) {       if (i + 2 < n && s[i] ==  o  && s[i + 1] ==  g  && s[i + 2] ==  o ) {         while (i + 2 < n && s[i + 1] ==  g  && s[i + 2] ==  o ) i += 2;         cout <<  *** ;       } else         cout << s[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(NULL);     cout.tie(NULL);     ios_base::sync_with_stdio(false);     int n;     cin >> n;     vector<string> arr;     for (int i = 0; i < n; i++) {       string x;       cin >> x;       arr.push_back(x);     }     int chocolate = 0;     for (int i = 0; i < n; i++) {       int temp = 0;       temp = count(arr[i].begin(), arr[i].end(),  C );       chocolate += (temp * (temp - 1)) / 2;     }     for (int i = 0; i < n; i++) {       int temp = 0;       for (int j = 0; j < n; j++) {         if (arr[j][i] ==  C ) {           temp += 1;         }       }       chocolate += (temp * (temp - 1)) / 2;     }     cout << chocolate << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<pair<int, int>, int> > a;   int main() {     int L, b, f, n, m, cmd, x;     cin >> L >> b >> f;     cin >> n;     for (int cs = (1); cs <= (n); ++cs) {       sort((a).begin(), (a).end());       cin >> cmd >> x;       if (cmd == 1) {         if ((a.empty() && x <= L) || (!a.empty() && x + f <= a[0].first.first)) {           cout << 0 << endl;           a.push_back(make_pair(make_pair(0, x), cs));           goto C;         }         m = (int)(a).size();         for (int i = 0; i < (m - 1); ++i)           if (a[i + 1].first.first - a[i].first.second >= b + f + x) {             cout << a[i].first.second + b << endl;             a.push_back(make_pair(                 make_pair(a[i].first.second + b, a[i].first.second + b + x), cs));             goto C;           }         if (m && L - a.back().first.second >= b + x) {           cout << a.back().first.second + b << endl;           a.push_back(make_pair(               make_pair(a.back().first.second + b, a.back().first.second + b + x),               cs));           goto C;         }         cout << -1 << endl;       } else {         for (__typeof((a).begin()) i = ((a).begin()); i != (a).end(); ++i)           if (i->second == x) {             swap(*i, a.back());             a.pop_back();             goto C;           }       }     C:;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int maxn = 2e6 + 7;   char str[maxn];   int ans[10][10], used[10];   int mp[10][10][10][10];   struct NODE {     int num, step;   };   int bfs(int numa, int numb, int a, int b) {     queue<NODE> que;     memset(used, 0, sizeof(used));     que.push((NODE){numa, 0});     while (!que.empty()) {       NODE now = que.front();       que.pop();       int x = (now.num + a) % 10;       int y = (now.num + b) % 10;       if (x == numb || y == numb) return now.step;       if (!used[x]) {         used[x] = 1;         que.push((NODE){x, now.step + 1});       }       if (!used[y]) {         used[y] = 1;         que.push((NODE){y, now.step + 1});       }     }     return -1;   }   int main() {     for (auto i = 0; i < 10; i++) {       for (auto j = i; j < 10; j++) {         for (auto numa = 0; numa < 10; numa++) {           for (auto numb = 0; numb < 10; numb++) {             int Insert = bfs(numa, numb, i, j);             if (Insert == -1) {               mp[i][j][numa][numb] = mp[j][i][numa][numb] = -1;               continue;             } else               mp[i][j][numa][numb] = mp[j][i][numa][numb] = Insert;           }         }       }     }     scanf( %s , str);     int len = strlen(str);     for (auto i = 0; i < 10; i++) {       for (auto j = i; j < 10; j++) {         for (auto p = 1; p < len; p++) {           int numa = str[p - 1] -  0 ;           int numb = str[p] -  0 ;           int Insert = mp[i][j][numa][numb];           if (Insert == -1) {             ans[i][j] = ans[j][i] = -1;             break;           } else {             ans[i][j] += Insert;             if (i != j) ans[j][i] += Insert;           }         }       }     }     for (auto i = 0; i < 10; i++) {       for (auto j = 0; j < 10; j++) {         printf( %d  , ans[i][j]);       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string st;     cin >> st;     for (int i = 0; i < st.size(); i++) {       if (st[i] ==  0  || st[i] ==  8 ) {         cout <<  YES n ;         cout << st[i] << endl;         return 0;       }       for (int j = i + 1; j < st.size(); j++) {         int num = st[i] -  0 ;         num *= 10;         num += (st[j] -  0 );         if (num % 8 == 0) {           cout <<  YES n ;           cout << num << endl;           return 0;         }         for (int k = j + 1; k < st.size(); k++) {           num = st[i] -  0 ;           num *= 10;           num += (st[j] -  0 );           num *= 10;           num += (st[k] -  0 );           if (num % 8 == 0) {             cout <<  YES n ;             cout << num << endl;             return 0;           }         }       }     }     cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<char, long long> mp;   map<pair<char, long long>, long long> cnt;   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     for (char c =  a ; c <=  z ; c++) cin >> mp[c];     string s;     cin >> s;     long long n = s.length();     long long sum = mp[s[0]];     cnt[{s[0], mp[s[0]]}]++;     long long ans = 0;     for (long long i = 1; i < n; i++) {       ans += cnt[{s[i], sum}];       sum += mp[s[i]];       cnt[{s[i], sum}]++;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     map<int, int> mp;     long long ans = 0, mx = 1e6;     for (int i = 0; i < n; i++) {       int first;       scanf( %d , &first);       int second = 1;       long long yy = 1;       for (int j = 2; j * j <= first; j++)         if (first % j == 0) {           int cnt = 0;           while (first % j == 0) cnt++, first /= j;           cnt %= k;           if (cnt) {             for (int t = 0; t < cnt; t++) second *= j;             for (int t = cnt; t < k; t++) yy = min(mx, yy * j);           }         }       if (first > 1) {         int cnt = 1, j = first;         for (int t = 0; t < cnt; t++) second *= j;         for (int t = cnt; t < k; t++) yy = min(mx, yy * j);       }       if (mp.count(yy)) ans += mp[yy];       mp[second]++;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   int R, C;   int ER, EC;   int SR, SC;   char field[1010][1010];   int dist[1010][1010];   int Q[1010 * 1010], Qb, Qe;   int dr[4] = {-1, +1, 0, 0};   int dc[4] = {0, 0, -1, +1};   int main(void) {     scanf( %d %d , &R, &C);     for (int r = 0; r < R; ++r) {       scanf( %s , field[r]);       for (int c = 0; c < C; ++c) {         if (field[r][c] ==  E ) {           ER = r;           EC = c;         }         if (field[r][c] ==  S ) {           SR = r;           SC = c;         }       }     }     memset(dist, -1, sizeof dist);     Q[0] = ER * 10000 + EC;     Qe = 1;     dist[ER][EC] = 0;     while (Qb != Qe) {       int r = Q[Qb] / 10000;       int c = Q[Qb] % 10000;       Qb += 1;       for (int d = 0; d < 4; ++d) {         int rr = r + dr[d];         int cc = c + dc[d];         if (rr < 0 || cc < 0 || rr >= R || cc >= C) continue;         if (field[rr][cc] ==  T ) continue;         if (dist[rr][cc] != -1) continue;         dist[rr][cc] = dist[r][c] + 1;         Q[Qe++] = rr * 10000 + cc;       }     }     int battles = 0;     int mydist = dist[SR][SC];     for (int r = 0; r < R; ++r) {       for (int c = 0; c < C; ++c) {         if (!isdigit(field[r][c])) continue;         if (dist[r][c] == -1 || dist[r][c] > mydist) continue;         int cnt = field[r][c] -  0 ;         battles += cnt;       }     }     printf( %d n , battles);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, M;   vector<long long> cards;   map<int, vector<int>> m;   int sodd = 1, seven = 2;   int oddbalance;   int nextOdd() {     for (int i = sodd;; i += 2) {       if (m[i].empty()) {         sodd = i + 2;         if (i > M) {           cout << -1 << endl;           exit(0);         }         return i;       }     }   }   int nextEven() {     for (int i = seven;; i += 2) {       if (m[i].empty()) {         seven = i + 2;         if (i > M) {           cout << -1 << endl;           exit(0);         }         return i;       }     }   }   int cnt;   int main() {     cin >> n >> M;     cards.resize(n);     for (int i = 0; i < n; i++) {       cin >> cards[i];       if (cards[i] % 2)         oddbalance++;       else         oddbalance--;       m[cards[i]].push_back(i);     }     for (auto p : m) {       while (p.second.size() > 1) {         cnt++;         if (p.first % 2 == 1)           oddbalance--;         else           oddbalance++;         int pos = p.second.back();         p.second.pop_back();         int val;         if (oddbalance > 0) {           val = nextEven();           oddbalance--;         } else {           val = nextOdd();           oddbalance++;         }         m[val].push_back(pos);         cards[pos] = val;       }     }     for (int i = 0; i < n && oddbalance; i++) {       if (oddbalance > 0 && cards[i] % 2 == 1) {         cnt++;         cards[i] = nextEven();         oddbalance -= 2;       } else if (oddbalance < 0 && cards[i] % 2 == 0) {         cnt++;         cards[i] = nextOdd();         oddbalance += 2;       }     }     if (oddbalance != 0) {       cout << -1 << endl;       exit(0);     }     cout << cnt << endl;     for (int c : cards) {       cout << c <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize  -O3    bool prime(long long int n) {     if (n == 1) return false;     if (n == 2 || n == 3) return true;     long long int k = sqrt(n) + 1;     for (long long int i = 2; i < k; i++) {       if (n % i == 0) return false;     }     return true;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int x;     cin >> x;     long long int b[x];     for (long long int i = 0; i < x; i++) {       cin >> b[i];     }     long long int y;     cin >> y;     long long int g[y];     for (long long int i = 0; i < y; i++) {       cin >> g[i];     }     sort(b, b + x);     sort(g, g + y);     long long int ans = 0;     for (long long int i = 0; i < x; i++) {       for (long long int j = 0; j < y; j++) {         long long int val = abs(b[i] - g[j]);         if (val <= 1) {           g[j] = 99999;           ans++;           break;           ;         }       }     }     cout << ans;     cout << endl;     ;     ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, s, inp;   int c[10];   int best = -1;   int b3, b4, b5;   struct bezout {     long long d, a, b;   };   bezout extgcd(long long x, long long y) {     if (y == 0) return (bezout){x, 1, 0};     bezout s = extgcd(y, x % y);     return (bezout){s.d, s.b, s.a - x / y * s.b};   }   void check(int k3, int k4, int k5) {     int a = k3 * c[3], b = k4 * c[4], z = k5 * c[5];     if (a + b + z != s) return;     if (k3 < 0 || k3 > k4 || k4 > k5 || k3 > k5) return;     int ct = abs(a - b) + abs(z - b);     if (best == -1 || best > ct) {       best = ct;       b3 = k3;       b4 = k4;       b5 = k5;     }   }   int main() {     scanf( %d %d , &n, &s);     for (int i = 0; i < n; i++) {       scanf( %d , &inp);       c[inp]++;     }     for (int k4 = 0; c[4] * k4 <= s; k4++) {       int C = s - c[4] * k4;       bezout t = extgcd(c[3], c[5]);       if (C % t.d != 0) continue;       long long k3 = t.a * C / t.d;       long long k5 = t.b * C / t.d;       int lcm = c[3] * c[5] / t.d;       int inc3 = lcm / c[3], inc5 = lcm / c[5];       if (k3 >= 0) {         int mod = k3 / inc3;         k3 = k3 % inc3;         k5 = k5 + mod * inc5;       }       if (k3 < 0) {         int mod = -k3 / inc3;         if ((-k3) % inc3 != 0) mod++;         k3 = k3 + inc3 * mod;         k5 = k5 - inc5 * mod;       }       check(k3, k4, k5);       int st = 0, ed = s;       while (st < ed) {         int m = (st + ed + 1) / 2;         int nk5 = k5 - m * inc5;         if (k3 + m * inc3 <= k4 && nk5 >= k4 && c[5] * nk5 >= c[4] * k4)           st = m;         else           ed = m - 1;       }       int maxk3 = k3 + inc3 * st;       int maxk5 = k5 - inc5 * st;       check(maxk3, k4, maxk5);       check(maxk3 + inc3, k4, maxk5 - inc5);     }     if (best == -1)       printf( %d n , best);     else       printf( %d %d %d n , b3, b4, b5);   }


#include <bits/stdc++.h>   using namespace std;   int res;   int a[100], ins[100];   int IsLucky(int x) {     for (; x; x /= 10)       if (x % 10 != 4 && x % 10 != 7) return 0;     return 1;   }   void Dfs(int now, int lim) {     res++;     if ((long long)now * 10 + 4 <= lim) Dfs(now * 10 + 4, lim);     if ((long long)now * 10 + 7 <= lim) Dfs(now * 10 + 7, lim);   }   int main() {     int n, m, k;     scanf( %d%d , &n, &m);     long long cnt = 1;     for (int i = 1;; i++) {       cnt *= i;       if (cnt >= m) {         k = i;         break;       }     }     if (k > n) {       puts( -1 );       return 0;     }     memset(ins, 0, sizeof(ins));     for (int i = 0; i < k; i++) {       cnt = 1;       for (int j = 1; j < k - i; j++) cnt *= j;       int ret = (m - 1) / cnt;       m -= ret * cnt;       for (int j = 0; j < k; j++)         if (!ins[j]) {           if (ret == 0) {             ins[j] = 1;             a[i] = j;             break;           } else             ret--;         }     }     res = 0;     for (int i = 0; i < k; i++)       if (IsLucky(n - k + 1 + i) && IsLucky(n - k + 1 + a[i])) res++;     if (4 <= n - k) Dfs(4, n - k);     if (7 <= n - k) Dfs(7, n - k);     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 100007;   long long n, x[N], y[N];   pair<long long, long long> mid(long long a, long long b) {     long long xx = (x[a] + x[b]) / 2;     long long yy = (y[a] + y[b]) / 2;     return {xx, yy};   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     for (long long i = 0; i < n; ++i) {       cin >> x[i] >> y[i];       x[i] *= 2;       y[i] *= 2;     }     if (n % 2) {       cout <<  NO n ;       return 0;     }     pair<long long, long long> midpoint = mid(0, n / 2);     for (long long i = 1; i < n / 2; ++i) {       pair<long long, long long> cur = mid(i, i + n / 2);       if (cur != midpoint) {         cout <<  NO n ;         return 0;       }     }     cout <<  YES n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int tt;     cin >> tt;     while (tt--) {       int c, m, x;       cin >> c >> m >> x;       cout << min(c, min(m, ((c + m + x) / 3))) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long n;   int main() {     int T;     scanf( %d , &T);     while (T--) {       scanf( %I64d , &n);       for (long long i = n;; i += i)         if (i * (n + 1) % (4 * n) == 0) {           printf( %I64d n , i + 1);           break;         }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 550, mod = 998244353, inv2 = 499122177, g = 3;   long long mul[N], inv[N];   long long ksm(long long f, long long x) {     long long tot = 1;     while (x) {       if (x & 1ll) tot = tot * f % mod;       f = f * f % mod;       x >>= 1ll;     }     return tot;   }   void pre() {     long long lim = N - 5;     mul[0] = inv[0] = 1;     for (long long i = 1; i <= lim; i++) mul[i] = mul[i - 1] * i % mod;     inv[lim] = ksm(mul[lim], mod - 2);     for (long long i = lim - 1; i >= 1; i--) inv[i] = inv[i + 1] * (i + 1) % mod;   }   namespace NTT {   void solve(long long* s, long long* f, long long* g, long long n, long long m) {     long long A[N];     for (long long i = 0; i <= n + m; i++) A[i] = 0;     for (long long i = 0; i <= n; i++) {       for (long long j = 0; j <= m; j++) {         A[i + j] = (A[i + j] + 1ll * f[i] * g[j] % mod) % mod;       }     }     for (long long i = 0; i <= n + m; i++) s[i] = A[i];     return;   }   }  // namespace NTT   long long A(long long m, long long n) {     if (n == 0) return 1;     if (m == -1) return ksm(-1, n) * mul[n] % mod;     if (m < n) return 0;     return mul[m] * inv[m - n] % mod;   }   long long n;   long long p[N], q[N], mp[N];   long long cnt1, cnt2, cnt0, cntcir;   long long S[N][N], C[N][N], F[N], G[N], H[N];   bool vis[N];   int main() {     pre();     cin >> n;     S[0][0] = C[0][0] = 1;     for (long long i = 1; i <= n; i++) {       C[i][0] = 1;       for (long long j = 1; j <= i; j++) {         S[i][j] = (S[i - 1][j - 1] + S[i - 1][j] * (i - 1) % mod) % mod;         C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;       }     }     for (long long i = 1; i <= n; i++) cin >> p[i], mp[p[i]] = i;     for (long long i = 1; i <= n; i++) cin >> q[i];     for (long long i = 1; i <= n; i++) {       if (!p[i]) {         long long x = i;         while (1) {           vis[x] = 1;           if (!q[x]) {             cnt0++;             break;           }           if (!mp[q[x]]) {             cnt1++;             break;           }           x = mp[q[x]];         }       }     }     for (long long i = 1; i <= n; i++) {       if (p[i] && !vis[i]) {         long long x = i;         while (1) {           if (vis[x]) {             if (x == i) cntcir++;             break;           }           vis[x] = 1;           if (!q[x]) {             cnt2++;             break;           }           if (!mp[q[x]]) break;           x = mp[q[x]];         }       }     }     for (long long i = 0; i <= n; i++) {       for (long long j = i; j <= cnt1; j++) {         F[i] = (F[i] + C[cnt1][j] * S[j][i] % mod *                            A(cnt0 + cnt1 - j - 1, cnt1 - j) % mod) %                mod;       }     }     for (long long i = 0; i <= n; i++) {       for (long long j = i; j <= cnt2; j++) {         G[i] = (G[i] + C[cnt2][j] * S[j][i] % mod *                            A(cnt0 + cnt2 - j - 1, cnt2 - j) % mod) %                mod;       }     }     for (long long i = 0; i <= n; i++) {       H[i] = S[cnt0][i] * mul[cnt0] % mod;     }     NTT::solve(F, F, G, n, n);     NTT::solve(F, F, H, n, n);     for (long long i = 0; i < n; i++) cout << F[n - cntcir - i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   char s[100005];   int cnt[30];   int main() {     int k, i;     scanf( %s , s);     scanf( %d , &k);     for (i = 0; s[i]; i++) cnt[s[i] -  a ]++;     if (k >= i) {       puts( 0 n );       return 0;     }     while (1) {       int minx = 1 << 30, mini;       for (i = 0; i < 30; i++) {         if (cnt[i]) {           minx = min(cnt[i], minx);           if (minx == cnt[i]) mini = i;         }       }       if (k >= cnt[mini]) {         k -= cnt[mini];         cnt[mini] = 0;       } else         break;     }     int ans = 0;     for (i = 0; i < 30; i++)       if (cnt[i]) ans++;     printf( %d n , ans);     for (i = 0; s[i]; i++) {       if (cnt[s[i] -  a ]) putchar(s[i]);     }   }


#include <bits/stdc++.h>   using namespace std;   int n, p;   vector<int> adj[100005];   vector<int> ans;   vector<int> vis;   int dfs(int s) {     if (s >= n) return 0;     vis[s] = 1;     int flag = 0;     for (int i = 0; i < adj[s].size(); i++) {       if (!vis[adj[s][i]]) {         flag = 1;         ans[s] += dfs(adj[s][i]);       }     }     if (!flag) {       ans[s] = 1;     }     return ans[s];   }   int main() {     cin >> n;     ans.resize(n, 0);     vis.resize(n, 0);     for (int i = 0; i < n - 1; i++) {       cin >> p;       adj[i + 1].push_back(p - 1);       adj[p - 1].push_back(i + 1);     }     dfs(0);     sort(ans.begin(), ans.end());     for (int i = 0; i < n; i++) {       cout << ans[i] <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; ++i) cin >> a[i];     vector<int> ind;     int num = 0, add = 0;     for (int i = 0; i < n; ++i) {       if (a[i] == i)         ++num;       else if (a[a[i]] == i)         add = 2;     }     if (!add) add = 1;     if (num == n) add = 0;     cout << num + add << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename... Args>   void read(Args &...args) {     ((cin >> args), ...);   }   template <typename... Args>   void show(Args... args) {     ((cout << args <<    ), ...);   }   void solve() {     int n;     read(n);     vector<int> v(n);     for (auto &it : v) cin >> it;     int l = 0, r = n - 1, x = 0;     string s;     while (l <= r) {       if (min(v[l], v[r]) > x) {         if (v[l] < v[r]) {           s.push_back( L );           x = v[l++];         } else if (v[r] < v[l]) {           s.push_back( R );           x = v[r--];         } else {           int ll = l + 1, rr = r - 1;           while (ll < r && v[ll] > v[ll - 1]) ll++;           while (rr > l && v[rr] > v[rr + 1]) rr--;           if (ll - l > r - rr) {             x = ll - l;             while (x--) s.push_back( L );           } else {             x = r - rr;             while (x--) s.push_back( R );           }           break;         }       } else if (max(v[l], v[r]) > x) {         if (v[l] > v[r]) {           s.push_back( L );           x = v[l++];         } else {           s.push_back( R );           x = v[r--];         }       } else {         break;       }     }     cout << int(s.size()) <<   n  << s;   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int T = 1;     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9 + 10;   const int MOD = (int)1e9 + 7;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, a, b, c, t;     cin >> n >> a >> b >> c >> t;     vector<int> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     if (c < b) return cout << n * a, 0;     long long ans = 0;     for (int i = 0; i < n; i++) ans += a + (t - v[i]) * (c - b);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e18;   const int N = 510000, M = N << 1;   int n, hd[N], to[M], nxt[M], noedg = 1, size[N];   long long buc[N], f[N], ans = inf;   vector<int> app[N];   template <class T>   inline void read(T &x) {     x = 0;     char ch = getchar(), w = 0;     while (!isdigit(ch)) w = (ch ==  - ), ch = getchar();     while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();     x = w ? -x : x;     return;   }   inline void addEdg(int x, int y) {     nxt[++noedg] = hd[x], hd[x] = noedg, to[noedg] = y;     nxt[++noedg] = hd[y], hd[y] = noedg, to[noedg] = x;     return;   }   inline long long sq(long long x) { return x * x; }   void dfs(int now, int fa) {     size[now] = 1, f[now] = inf;     for (register int i = hd[now], v; i; i = nxt[i]) {       if ((v = to[i]) == fa) continue;       dfs(v, now), size[now] += size[v];     }     buc[0] = 0, app[now].push_back(0);     for (register int i = hd[now], v; i; i = nxt[i]) {       if ((v = to[i]) == fa) continue;       for (register int j = 0, sz = app[now].size(); j < sz; ++j)         ans = min(ans, buc[app[now][j]] + f[v] + sq(n - app[now][j] - size[v]));       if (buc[size[v]] == inf) app[now].push_back(size[v]);       buc[size[v]] = min(buc[size[v]], f[v]);       f[now] = min(f[now], f[v] + sq(size[now] - size[v]));     }     for (register int i = 0, sz = app[now].size(); i < sz; ++i)       buc[app[now][i]] = inf;     if (f[now] == inf) f[now] = 1;     return;   }   int main() {     int x, y;     read(n);     for (register int i = 1; i < n; ++i)       read(x), read(y), addEdg(x, y), buc[i] = inf;     dfs(1, 0);     cout << ((long long)n * (2 * n - 1) - ans) / 2;     return 0;     int qwq;   }


#include <bits/stdc++.h>   #pragma optimization_level 3   #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math,O3 )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx )   using namespace std;   const long long N = 5e5 + 3;   const long long mod = 998244353;   const long long INF = 0x7f7f7f7f7f7f7f7f;   const int INFi = 0x7f7f7f7f;   long long test = 1, n, m, x, y, k, b[N], a[N];   vector<long long> v;   long long ch(int l, int r) {     long long mx = max(a[l], a[r]), res = 0;     set<long long> s1, s2;     for (int i = l + 1; i < r; i++) {       if (a[i] > mx) s1.insert(i);       s2.insert(i);     }     if (s2.size() < k and s1.size()) {       cout << -1 <<   n ;       exit(0);     }     if (k * x > y) {       res = (s2.size() / k) * y + (s2.size() % k) * x;     } else {       if (s1.size())         res = y + (s2.size() - k) * x;       else         res = s2.size() * x;     }     return res;   }   void solve() {     cin >> n >> m >> y >> k >> x;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= m; i++) cin >> b[i];     a[0] = a[n + 1] = b[0] = b[m + 1] = 0;     int j = 1;     v.push_back(0);     for (int i = 1; i <= n; i++)       if (a[i] == b[j]) j++, v.push_back(i);     v.push_back(n + 1);     if (j != m + 1) {       cout << -1 <<   n ;       return;     }     long long ans = 0;     for (int i = 1; i <= m + 1; i++) {       ans += ch(v[i - 1], v[i]);     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout << fixed;     cout << setprecision(15);     ;     for (int i = 1; i <= test; i++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = (long long)1e9 + 999999;   const long long Mod = (long long)1e9 + 7;   const long long MaXN = (long long)1e18;   const int N = (int)1e6 + 7;   const int MaXI = (int)1e9;   const int Mass = (int)2e5;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     int n;     cin >> n;     string s;     cin >> s;     int b = 0, g = 0, r = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  B ) {         b++;       }       if (s[i] ==  G ) {         g++;       }       if (s[i] ==  R ) {         r++;       }     }     if (b == n) {       cout <<  B ;       return 0;     } else if (g == n) {       cout <<  G ;       return 0;     } else if (r == n) {       cout <<  R ;       return 0;     }     if (r > 1 || g > 1) {       cout <<  B ;     } else if (r == 1 && g == 1) {       cout <<  B ;     }     if (b > 1 || r > 1) {       cout <<  G ;     } else if (b == 1 && r == 1) {       cout <<  G ;     }     if (b > 1 || g > 1) {       cout <<  R ;     } else if (b == 1 && g == 1) {       cout <<  R ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   ostream &operator<<(ostream &out, vector<long long> &a) {     for (auto i : a) out << i <<    ;     return out;   }   istream &operator>>(istream &in, vector<long long> &a) {     for (auto &i : a) in >> i;     return in;   }   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     long long q;     cin >> q;     while (q--) {       string a, b, c;       cin >> a >> b >> c;       bool boo = 0;       for (long long i = 0; i < a.size(); ++i) {         if (c[i] != b[i] && c[i] != a[i]) {           cout <<  NO                 <<   n ;           boo = 1;           break;         }       }       if (!boo)         cout <<  YES               <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, q;     char c;     vector<int> a(10);     cin >> n;     for (int i = 0; i < n; i++) {       cin >> c;       if (c ==  L ) {         q = 0;         while (a[q] == 1) {           q++;         }         a[q] = 1;       } else if (c ==  R ) {         q = 9;         while (a[q] == 1) {           q--;         }         a[q] = 1;       } else {         q = (int)c - 48;         a[q] = 0;       }     }     for (int i = 0; i < 10; i++) {       cout << a[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int bits[32];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     vector<long long> v(n);     for (int i = 0; i < n; i++) {       cin >> v[i];     }     for (int i = 0; i < m; i++) {       int x;       cin >> x;       bits[x]++;     }     sort(v.begin(), v.end());     long long ans = 0;     for (int j = 0; j < 31; j++) {       for (auto &x : v) {         if ((x & 1) && bits[j]) {           x--;           ans++;           bits[j]--;         }       }       for (auto &x : v) {         while (x && bits[j]) {           x--;           ans++;           bits[j]--;         }       }       for (auto &x : v) {         x >>= 1;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 1009;   pair<int, pair<int, int> > sr[Maxn * Maxn];   int n, m;   int check(int k) {     bitset<Maxn> row[Maxn];     for (int i = 0; i <= k; i++) {       int x = sr[i].second.first, y = sr[i].second.second;       row[x].set(y);     }     for (int i = 0; i < n; i++)       for (int j = i + 1; j < n; j++)         if ((row[i] & row[j]).count() >= 2) return 1;     return 0;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         int p;         scanf( %d , &p);         sr[i * m + j] = make_pair(-p, make_pair(i, j));       }     sort(sr, sr + n * m);     int l = 0, r = n * m - 1;     while (l < r - 1) {       int mid = l + r >> 1;       if (check(mid))         r = mid;       else         l = mid;     }     cout << -sr[r].first << endl;   }


#include <bits/stdc++.h>   using namespace std;   double dp[128][128];   int x[128], y[128];   int N, K, x_0, y_0;   double eps;   double dist(double x1, double y1) {     return sqrt((x_0 - x1) * (x_0 - x1) + (y_0 - y1) * (y_0 - y1));   }   double hit(double R, double D) {     return (D <= R) ? 1. : exp(1 - D * D / R / R);   }   double P(double R) {     fill(*dp, *dp + 128 * 128, 0.);     dp[0][1] = hit(R, dist(x[0], y[0]));     dp[0][0] = 1 - dp[0][1];     for (int i = 1; i < N; i++)       for (int j = 0; j <= N; j++) {         double ph = hit(R, dist(x[i], y[i]));         dp[i][j] += (1 - ph) * dp[i - 1][j];         if (j >= 1) dp[i][j] += ph * dp[i - 1][j - 1];       }     double ret = 0.;     for (int j = 0; j < K; j++) ret += dp[N - 1][j];     return ret;   }   int main() {     cin >> N >> K >> eps >> x_0 >> y_0;     eps /= 1000.;     for (int i = 0; i < N; i++) cin >> x[i] >> y[i];     double lo = 0., hi = 1000000.;     for (int x = 0; x < 50; x++) {       double ave = (lo + hi) / 2.;       if (P(ave) <= eps)         hi = ave;       else         lo = ave;     }     cout << setprecision(10) << fixed << hi << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long inf;   const double eps = 1e-8;   const double pi = acos(-1.0);   template <class T>   long long chkmin(T &a, T b) {     return a > b ? a = b, 1 : 0;   }   template <class T>   long long chkmax(T &a, T b) {     return a < b ? a = b, 1 : 0;   }   template <class T>   T sqr(T a) {     return a * a;   }   template <class T>   T mmin(T a, T b) {     return a < b ? a : b;   }   template <class T>   T mmax(T a, T b) {     return a > b ? a : b;   }   template <class T>   T aabs(T a) {     return a < 0 ? -a : a;   }   template <class T>   long long dcmp(T a, T b) {     return a > b;   }   template <long long *a>   long long cmp_a(long long first, long long second) {     return a[first] < a[second];   }   struct __INIT__ {     __INIT__() { memset(&inf, 0x3f, sizeof(inf)); }   } __INIT___;   namespace io {   const long long SIZE = (1 << 21) + 1;   char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,                                          qu[55];   long long f, qr;   inline void flush() {     fwrite(obuf, 1, oS - obuf, stdout);     oS = obuf;   }   inline void putc(char first) {     *oS++ = first;     if (oS == oT) flush();   }   template <typename A>   inline bool read(A &first) {     for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                                 (iS == iT ? EOF : *iS++))                               : *iS++);          c <  0  || c >  9 ;          c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                          (iS == iT ? EOF : *iS++))                        : *iS++))       if (c ==  - )         f = -1;       else if (c == EOF)         return 0;     for (first = 0; c <=  9  && c >=  0 ;          c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                          (iS == iT ? EOF : *iS++))                        : *iS++))       first = first * 10 + (c & 15);     first *= f;     return 1;   }   inline bool read(char &first) {     while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                                  (iS == iT ? EOF : *iS++))                               : *iS++)) ==     ||            first ==   n  || first ==   r )       ;     return first != EOF;   }   inline bool read(char *first) {     while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                                   (iS == iT ? EOF : *iS++))                                : *iS++)) ==   n  ||            *first ==     || *first ==   r )       ;     if (*first == EOF) return 0;     while (!(*first ==   n  || *first ==     || *first ==   r ))       *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                                 (iS == iT ? EOF : *iS++))                              : *iS++);     *first = 0;     return 1;   }   template <typename A, typename... B>   inline bool read(A &first, B &...second) {     return read(first) && read(second...);   }   template <typename A>   inline bool write(A first) {     if (!first) putc( 0 );     if (first < 0) putc( - ), first = -first;     while (first) qu[++qr] = first % 10 +  0 , first /= 10;     while (qr) putc(qu[qr--]);     return 0;   }   inline bool write(char first) {     putc(first);     return 0;   }   inline bool write(const char *first) {     while (*first) {       putc(*first);       ++first;     }     return 0;   }   inline bool write(char *first) {     while (*first) {       putc(*first);       ++first;     }     return 0;   }   template <typename A, typename... B>   inline bool write(A first, B... second) {     return write(first) || write(second...);   }   struct Flusher_ {     ~Flusher_() { flush(); }   } io_flusher_;   }  // namespace io   using io ::putc;   using io ::read;   using io ::write;   char strs[1000005];   char *s[100005];   long long to[100005][26], t = 1, fail[100005];   long long c[100005];   long long ed[100005];   long long q[100005], *l, *r;   long long cnt[201][100005];   long long is[100005];   long long ans[100005];   vector<pair<long long, long long> > ask[100005];   const long long S = 500;   long long dfn[100005], low[100005], su[100005];   long long cl;   void add(long long first, long long second) {     for (; first <= cl; first += first & -first) su[first] += second;   }   long long sum(long long first) {     long long second = 0;     for (; first; first ^= first & -first) second += su[first];     return second;   }   vector<long long> son[100005];   void dfs(long long first) {     dfn[first] = ++cl;     for (auto i : son[first]) dfs(i);     low[first] = cl;   }   signed main() {     long long n, m;     read(n, m);     s[1] = strs;     for (long long i = 1; i <= n; ++i) {       read(s[i]);       s[i + 1] = s[i] + strlen(s[i]);       long long at = 1;       for (char *j = s[i]; j < s[i + 1]; ++j) {         if (!to[at][*j -  a ]) to[at][*j -  a ] = ++t;         at = to[at][*j -  a ];       }       ed[i] = at;     }     *(l = r = q) = 1;     fail[1] = 1;     while (l <= r) {       for (long long i = 0; i < 26; ++i)         if (to[*l][i]) {           *(++r) = to[*l][i];           if (*l == 1) {             fail[*r] = 1;           } else {             long long at = fail[*l];             while (at != 1 && !to[at][i]) at = fail[at];             if (to[at][i])               fail[*r] = to[at][i];             else               fail[*r] = 1;           }           son[fail[*r]].push_back(*r);         }       ++l;     }     l = q;     while (l <= r) {       for (long long i = 0; i < 26; ++i)         if (!to[*l][i]) {           if (*l == 1)             to[*l][i] = 1;           else             to[*l][i] = to[fail[*l]][i];         }       ++l;     }     long long ct = 0;     for (long long i = 1; i <= n; ++i) {       if (s[i + 1] - s[i] > S) {         is[i] = ++ct;         long long at = 1;         for (char *j = s[i]; j < s[i + 1]; ++j) {           at = to[at][*j -  a ];           ++c[at];         }         for (long long *j = r;; --j) {           c[fail[*j]] += c[*j];           if (j == q) break;         }         for (long long j = 1; j <= n; ++j) cnt[ct][j] = cnt[ct][j - 1] + c[ed[j]];         for (long long j = 1; j <= t; ++j) c[j] = 0;       }     }     for (long long i = 1; i <= m; ++i) {       long long l, r, k;       read(l, r, k);       if (s[k + 1] - s[k] > S) {         ans[i] = cnt[is[k]][r] - cnt[is[k]][l - 1];       } else {         ask[r].push_back(make_pair(k, i));         ask[l - 1].push_back(make_pair(k, -i));       }     }     dfs(1);     for (long long i = 1; i <= n; ++i) {       add(dfn[ed[i]], 1);       add(low[ed[i]] + 1, -1);       for (auto j : ask[i]) {         long long aid = j.second, tp = 1;         if (aid < 0) {           aid = -aid;           tp = -1;         }         long long at = 1;         for (char *l = s[j.first]; l < s[j.first + 1]; ++l) {           at = to[at][*l -  a ];           ans[aid] += tp * sum(dfn[at]);         }       }     }     for (long long i = 1; i <= m; ++i) write(ans[i],   n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int a, b;       cin >> a >> b;       if (a % b == 0) {         cout << 0 <<   n ;       } else {         long long int g = a / b;         long long int ans = (b * (g + 1) - a);         cout << ans <<   n ;       }     }     return 0;   }


#include <iostream>   #include <cstdio>   #include <cassert>   #include <cstring>   #include <cmath>   #include <functional>   #include <algorithm>   #include <utility>   #include <vector>   #include <string>   #include <map>   #include <set>   using namespace std;   using ll = long long;   using PII = pair<int,int>;   const ll inf = 1ll << 60;   const int maxn = 200000 + 5;      int n, s;      struct DS {     ll base = 0;     map<int, ll> f;     set<pair<ll,int> > bag;        int size() {       return f.size();     }        bool count(int x) {       return f.count(x);     }        ll get(int x) {       if (f.count(x)) {         return f[x] + base;       } else {         return inf;       }     }        void set(int x, ll v) {       if (f.count(x)) {         v = std::min(v, erase(x));       }       v -= base;       f[x] = v;       bag.emplace(v, x);     }        ll erase(int x) {       if (f.count(x)) {         ll val = f[x];         f.erase(x);         bag.erase({ val, x });         return val + base;       } else {         return inf;       }     }        ll min() {       return bag.begin()->first + base;     }   };      int main() {     scanf( %d%d , &n, &s);     vector<DS> stk { {} };     vector<int> cond;     stk.back().set(0, 0);     for (int i = 1; i <= n; i++) {       char op[8];       scanf( %s , op);       if (op[0] ==  s ) {         int x, v;         scanf( %d%d , &x, &v);         ll mn = stk.back().min();         stk.back().base += v;         if (x != s) {           stk.back().set(x, mn);         }       } else if (op[0] ==  i ) {         int x;         scanf( %d , &x);         DS u;         u.set(x, stk.back().get(x));         stk.back().erase(x);         stk.push_back(u);         cond.push_back(x);       } else if (op[0] ==  e ) {         int c = cond.back();         cond.pop_back();            int sz = stk.size();         auto& x = stk[sz - 1];         auto& y = stk[sz - 2];            if (x.size() > y.size()) swap(x, y);         for (auto u: x.f) {           y.set(u.first, x.get(u.first));         }         stk.pop_back();       }     }     printf( %lld n , stk.back().min());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, ans;   vector<long long> a;   int main() {     cin >> n;     a.resize(n);     long long min_A = 1 << 31 - 1;     long long max_A = -1;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] > max_A) {         max_A = a[i];       }       if (a[i] < min_A) {         min_A = a[i];       }     }     for (int i = 0; i < n; i++) {       if (a[i] < max_A && a[i] > min_A) {         ans++;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long sol[200000];   long long solve(int in, vector<int> v[], int visited[], int a[]) {     if (sol[in] != -2e16) {       return sol[in];     }     long long ans = a[in];     visited[in] = 1;     for (int i = 0; i < v[in].size(); i++) {       if (visited[v[in][i]] == 0) {         ans += solve(v[in][i], v, visited, a);       }     }     visited[in] = 0;     sol[in] = ans;     return ans;   }   long long max1[200000];   long long maxx(int in, vector<int> v[], int visited[], long long sol[]) {     if (max1[in] != -2e16) {       return max1[in];     }     long long ans = sol[in];     visited[in] = 1;     for (int i = 0; i < v[in].size(); i++) {       if (visited[v[in][i]] == 0) {         long long xx = maxx(v[in][i], v, visited, sol);         if (xx > ans) {           ans = xx;         }       }     }     visited[in] = 0;     max1[in] = ans;     return ans;   }   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     vector<int> v[n];     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       v[x - 1].push_back(y - 1);       v[y - 1].push_back(x - 1);     }     int vis[n];     for (int i = 0; i < n; i++) {       vis[i] = 0;     }     for (int i = 0; i < 200000; i++) {       sol[i] = -2e16;       max1[i] = -2e16;     }     long long xx = solve(0, v, vis, a);     long long xy = maxx(0, v, vis, sol);     queue<int> q;     q.push(0);     long long ans = -2e16;     while (!q.empty()) {       int p = q.front();       q.pop();       vis[p] = 1;       priority_queue<long long> pq;       for (int i = 0; i < v[p].size(); i++) {         if (vis[v[p][i]] == 0) {           pq.push(max1[v[p][i]]);           q.push(v[p][i]);         }       }       if (pq.size() < 2) {         continue;       } else {         long long val = pq.top();         pq.pop();         val += pq.top();         if (val > ans) {           ans = val;         }       }     }     if (ans == -2e16) {       cout <<  Impossible  << endl;       return 0;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long G[500][500], d[500][500];   long long x[500], n, m;   void floyd(long long k) {     for (long long i = 0; i < n; i++)       if (i != k) {         for (long long j = 0; j < n; j++)           if (i != j && j != k)             if (G[i][j] > G[i][k] + G[k][j]) {               G[i][j] = G[i][k] + G[k][j];             }       }   }   void build_graph() {     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < n; j++) {         scanf( %I64d , &G[i][j]);       }     }     for (long long i = 0; i < n; i++) {       scanf( %I64d , &x[n - i - 1]);       x[n - i - 1]--;     }   }   void solve() {     m = 0;     long long ans[500] = {0};     memcpy(d, G, sizeof(G));     for (long long i = 0; i < n; i++) {       m++;       floyd(x[i]);       for (long long j = 0; j < m; j++) {         for (long long k = 0; k < m; k++) {           ans[n - i - 1] += G[x[j]][x[k]];         }       }     }     printf( %I64d , ans[0]);     for (long long i = 1; i < n; i++) printf(  %I64d , ans[i]);     putchar(10);   }   int main() {     while (~scanf( %I64d , &n)) {       build_graph();       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char inp[200005];   int main() {     bool ok;     queue<int> zeroes, ones;     vector<int> vec[200005];     int n_vec = 0, n_z = 0, n_o = 0, tam;     scanf( %s , inp);     tam = strlen(inp);     if (inp[0] ==  1 )       cout << -1 << endl;     else {       vec[n_vec++].push_back(0);       zeroes.push(0);       n_z++;       ok = true;       for (int i = 1; i < tam; i++) {         if (inp[i] ==  0 ) {           if (n_o == 0) {             vec[n_vec++].push_back(i);             zeroes.push(n_vec - 1);             n_z++;           } else {             int q = ones.front();             vec[q].push_back(i);             zeroes.push(q);             n_z++;             ones.pop();             n_o--;           }         } else {           if (n_z == 0) {             ok = false;             break;           } else {             int q = zeroes.front();             vec[q].push_back(i);             ones.push(q);             n_o++;             zeroes.pop();             n_z--;           }         }       }       if (n_o != 0) ok = false;       if (!ok)         printf( -1 n );       else {         printf( %d n , n_vec);         vector<int>::iterator vit;         for (int i = 0; i < n_vec; i++) {           printf( %d , vec[i].size());           vit = vec[i].begin();           while (vit != vec[i].end()) {             printf(  %d , (*vit) + 1);             vit++;           }           printf(  n );         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int x1[200010];   int x2[200010];   int yz1[200010];   int yz2[200010];   int n;   pair<pair<int, int>, pair<int, int> > pp[200010];   pair<pair<int, int>, pair<int, int> > qq[200010];   bool isValid(int x1, int y1, int x2, int y2) {     if (x1 <= x2 && y1 <= y2) {       return true;     }     return false;   }   pair<pair<int, int>, pair<int, int> > fn(int x1, int y1, int x2, int y2, int x3,                                            int y3, int x4, int y4) {     return make_pair(make_pair(max(x1, x3), max(y1, y3)),                      make_pair(min(x2, x4), min(y2, y4)));   }   int main() {     int INF = INT_MAX;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x1[i] >> yz1[i] >> x2[i] >> yz2[i];     }     pp[0].first.first = x1[0];     pp[0].first.second = yz1[0];     pp[0].second.first = x2[0];     pp[0].second.second = yz2[0];     for (int i = 1; i < n; i++) {       pair<pair<int, int>, pair<int, int> > uu;       uu = fn(pp[i - 1].first.first, pp[i - 1].first.second,               pp[i - 1].second.first, pp[i - 1].second.second, x1[i], yz1[i],               x2[i], yz2[i]);       if (isValid(uu.first.first, uu.first.second, uu.second.first,                   uu.second.second)) {         pp[i] = uu;       } else {         pp[i] = make_pair(make_pair(INF, INF), make_pair(-INF, -INF));       }     }     qq[n - 1].first.first = x1[n - 1];     qq[n - 1].first.second = yz1[n - 1];     qq[n - 1].second.first = x2[n - 1];     qq[n - 1].second.second = yz2[n - 1];     for (int i = n - 2; i >= 0; i--) {       pair<pair<int, int>, pair<int, int> > uu;       uu = fn(qq[i + 1].first.first, qq[i + 1].first.second,               qq[i + 1].second.first, qq[i + 1].second.second, x1[i], yz1[i],               x2[i], yz2[i]);       if (isValid(uu.first.first, uu.first.second, uu.second.first,                   uu.second.second)) {         qq[i] = uu;       } else {         qq[i] = make_pair(make_pair(INF, INF), make_pair(-INF, -INF));       }     }     for (int i = 0; i < n; i++) {       if (i == 0) {         if (isValid(qq[1].first.first, qq[1].first.second, qq[1].second.first,                     qq[1].second.second)) {           cout << qq[1].first.first <<     << qq[1].first.second;           return 0;         }       } else if (i == n - 1) {         if (isValid(pp[n - 2].first.first, pp[n - 2].first.second,                     pp[n - 2].second.first, pp[n - 2].second.second)) {           cout << pp[n - 2].first.first <<     << pp[n - 2].first.second;           return 0;         }       } else {         pair<pair<int, int>, pair<int, int> > uu;         uu = fn(qq[i + 1].first.first, qq[i + 1].first.second,                 qq[i + 1].second.first, qq[i + 1].second.second,                 pp[i - 1].first.first, pp[i - 1].first.second,                 pp[i - 1].second.first, pp[i - 1].second.second);         if (isValid(uu.first.first, uu.first.second, uu.second.first,                     uu.second.second)) {           cout << uu.first.first <<     << uu.first.second;           return 0;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7, siz = 1e6 + 5;   long long t, n, m, k, a[siz], inp[siz], cnt, co[siz], less_right[siz],       less_left[siz], tree[siz][2];   pair<long long, long long> p[siz];   long long get(long long ind, long long id) {     long long ret = 0;     for (; ind; ind -= ind & -ind) ret += tree[ind][id];     return ret;   }   void upd(long long id, long long ind, long long val) {     for (; ind <= cnt; ind += ind & -ind) tree[ind][id] += val;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (long long i = 1; i <= n; i++) cin >> p[i].first, p[i].second = i;     sort(p + 1, p + 1 + n);     for (long long i = 1; i <= n; i++) {       a[p[i].second] = ++cnt;       inp[p[i].second] = p[i].first;     }     for (long long i = 1; i <= n; i++) {       less_left[i] = get(a[i], 0);       upd(0, a[i], i);     }     for (long long i = n; i >= 1; i--) {       less_right[i] = get(a[i], 1);       upd(1, a[i], n - i + 1);     }     long long ans = 0;     for (long long i = 1; i <= n; i++) {       co[i] = i * (n - i + 1);       co[i] += less_left[i] * (n - i + 1) + less_right[i] * i;       co[i] %= mod;       long long toadd = (co[i] * inp[i]) % mod;       (ans += toadd) %= mod;     }     cout << ans << endl;   }


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


#include <bits/stdc++.h>   using namespace std;   const int N = 310;   int r[N];   char g[N][N];   int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};   int main() {     int n, m, k;     scanf( %d %d %d , &n, &m, &k);     for (int i = 0; i < n; i++) scanf( %s , g[i]);     int cnt = 0;     for (int x = 1; x < 150; x++) {       for (int i = x; i + x - 1 < n; i++) {         for (int j = x; j + x - 1 < m; j++) {           if (g[i][j] !=  * ) continue;           bool ok = true;           for (int d = 0; d < 4 and ok; d++) {             int ni = i + dir[d][0] * x;             int nj = j + dir[d][1] * x;             if (g[ni][nj] !=  * ) ok = false;           }           if (ok) cnt++;           if (cnt == k) {             printf( %d %d n , i + 1, j + 1);             for (int d = 0; d < 4; d++) {               int ni = i + dir[d][0] * x;               int nj = j + dir[d][1] * x;               printf( %d %d n , ni + 1, nj + 1);             }             return 0;           }         }       }     }     printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = INT_MAX;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, m;     cin >> n >> m;     if (n > m) {       swap(n, m);     }     if (n == 1) {       if (m % 6 == 0) {         cout << n * m;       } else if (m % 6 == 5) {         cout << n * m - 1;       } else if (m % 6 == 4) {         cout << n * m - 2;       } else {         cout << n * m - m % 6;       }     } else if (n == 2) {       if (m == 2) {         cout << 0;       } else if (m == 3) {         cout << 4;       } else if (m == 7) {         cout << 12;       } else {         cout << m * 2;       }     } else {       cout << n * m / 2 * 2;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int vp, vd, time_s, time_b, dist;   int main() {     scanf( %d%d%d%d%d , &vp, &vd, &time_s, &time_b, &dist);     if (vd <= vp) {       puts( 0 );       return 0;     }     int ans = 0;     double now = 1.0 * vp * time_s;     while (now < dist) {       double Overtake = now / (vd - vp);       double loc = now + Overtake * vp;       if (loc >= dist) break;       ++ans;       now = loc;       double Back = now / vd + 1.0 * time_b;       now += Back * vp;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 106;   int g[MAX_N][MAX_N];   int h[MAX_N][MAX_N];   bool not_empty_row[MAX_N];   bool not_empty_col[MAX_N];   bool used_row[MAX_N];   bool used_col[MAX_N];   void coppy(int n, int m) {     for (int i = 1; n >= i; i++) {       for (int j = 1; m >= j; j++) {         h[i][j] = g[i][j];       }     }     memset(used_row, 0, sizeof(used_row));     memset(used_col, 0, sizeof(used_col));   }   bool okayy(int n, int m) {     bool ret = true;     for (int i = 1; n >= i; i++) {       for (int j = 1; m >= j; j++) {         ret &= (h[i][j] == 0);       }     }     return ret;   }   bool deleted(int type, int x, int val, int n, int m) {     if (type == 1) {       int i = x;       for (int j = 1; m >= j; j++) {         h[i][j] -= val;         if (h[i][j] < 0) return false;       }       return true;     }     if (type == 2) {       int j = x;       for (int i = 1; n >= i; i++) {         h[i][j] -= val;         if (h[i][j] < 0) return false;       }       return true;     }   }   int main() {     int n, m;     scanf( %d %d , &n, &m);     bool not_empty = false;     for (int i = 1; n >= i; i++) {       for (int j = 1; m >= j; j++) {         scanf( %d , &g[i][j]);         if (g[i][j]) {           not_empty_row[i] = 1;           not_empty_col[j] = 1;           not_empty = true;         }       }     }     if (!not_empty) {       puts( 0 );       return 0;     }     int rrow = -1;     for (int i = 1; n >= i; i++) {       if (not_empty_row[i]) {         rrow = i;         break;       }     }     if (rrow == -1) cout << 0 / 0 << endl;     vector<pair<int, pair<int, int> > > ans;     vector<pair<int, pair<int, int> > > real_ans;     int meruru = 123456789;     int xx = 0;     for (int cnt = 0; g[rrow][1] >= cnt; cnt++) {       ans.clear();       coppy(n, m);       bool ret = deleted(1, rrow, cnt, n, m);       xx = 0;       xx += cnt;       ans.push_back(make_pair(1, make_pair(rrow, cnt)));       for (int j = 1; m >= j; j++) {         xx += h[rrow][j];         ans.push_back(make_pair(2, make_pair(j, h[rrow][j])));         ret &= deleted(2, j, h[rrow][j], n, m);       }       for (int i = 1; n >= i; i++) {         xx += h[i][1];         ans.push_back(make_pair(1, make_pair(i, h[i][1])));         ret &= deleted(1, i, h[i][1], n, m);       }       if (ret && okayy(n, m)) {         if (xx < meruru) {           real_ans = ans;           meruru = xx;         }       } else         ans.clear();     }     ans = real_ans;     if (ans.size() == 0)       puts( -1 );     else {       int cnt = 0;       for (pair<int, pair<int, int> > i : ans) {         cnt += i.second.second;       }       if (cnt != meruru) cout << 0 / 0 << endl;       printf( %d n , cnt);       for (pair<int, pair<int, int> > i : ans) {         while (i.second.second--) {           if (i.first == 1)             printf( row  );           else             printf( col  );           printf( %d n , i.second.first);         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const int nax = 3e5 + 10;   long long w[nax];   long long dp[nax], ans;   vector<pair<int, long long> > v[nax];   void dfs(int id, int par) {     vector<long long> temp;     for (auto i : v[id]) {       if (i.first == par) continue;       dfs(i.first, id);       temp.emplace_back(dp[i.first] - i.second);       dp[id] = max(dp[id], dp[i.first] - i.second);     }     dp[id] += w[id];     ans = max(ans, dp[id]);     sort(temp.begin(), temp.end());     reverse(temp.begin(), temp.end());     if ((int)temp.size() == 1) ans = max(ans, temp[0] + w[id]);     if ((int)temp.size() > 1) ans = max(ans, temp[1] + temp[0] + w[id]);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n;     cin >> n;     for (int i = 1; i <= n; ++i) cin >> w[i];     for (int i = 0; i < n - 1; ++i) {       int x, y, t;       cin >> x >> y >> t;       v[x].emplace_back(y, t);       v[y].emplace_back(x, t);     }     dfs(1, -1);     cout << ans <<   n ;     ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   const double eps = 1e-11;   template <class T>   inline void checkmin(T &a, T b) {     if (b < a) a = b;   }   template <class T>   inline void checkmax(T &a, T b) {     if (b > a) a = b;   }   template <class T>   inline T sqr(T x) {     return x * x;   }   template <class T>   inline void bin(T n, int pos) {     T i, one = 1, two = 2;     for (i = (one << (pos - 1)); i > 0; i = (i >> 1))       cout << ((n & i) ?  1  :  0 );     cout << endl;   }   int lsb(unsigned int v) {     int r;     static const int MultiplyDeBruijnBitPosition[32] = {         0,  1,  28, 2,  29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4,  8,         31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6,  11, 5,  10, 9};     r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];     return r + 1;   }   template <class T>   inline int msb(T n) {     return (n == 0) ? 0 : (1 + msb(n >> 1));   }   template <class T>   inline T getbit(T n, int pos) {     return ((n & (1 << pos)) ? 1 : 0);   }   template <class T>   inline int countbit(T n) {     return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));   }   template <class T>   inline void setbit(T &n, int p) {     n = n | (1LL << p);   }   template <class T>   inline T gcd(T a, T b) {     if (a < 0) return gcd(-a, b);     if (b < 0) return gcd(a, -b);     return (b == 0) ? a : gcd(b, a % b);   }   bool isequal(vector<int> a, vector<int> b) {     for (int i = 0; i < (a.size()); i++)       if (a[i] != b[i]) return false;     return true;   }   template <class T>   bool allequal(vector<T> a) {     sort(a.begin(), a.end());     return a[0] == a[a.size() - 1];   }   template <class T>   void printarray(vector<T> a) {     for (int i = 0; i < (a.size()); i++) cout << a[i] <<    ;     cout << endl;   }   void printmatrix(vector<vector<int>> a, int n, int m) {     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cout << setw(4) << a[i][j];       }       cout << endl;     }   }   void readmatrix(vector<vector<int>> &a, int n, int m) {     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> a[i][j];       }     }   }   template <class T>   void printsubarray(vector<T> a, int start, int end) {     if (start < 0 or end > a.size() - 1) cout <<  Out of bounds  << endl;     for (int i = start; i <= end; i++) cout << a[i] <<    ;     cout << endl;   }   bool isprime(int n) {     if (n < 2) return false;     if (n == 2) return true;     for (int i = 2; i * i <= n; i++) {       if (n % i == 0) return false;     }     return true;   }   void split(const string &s, char c, vector<string> &result) {     stringstream ss(s);     string x;     while (getline(ss, x, c)) result.push_back(x);   }   void err(vector<string>::iterator it) {}   template <typename T, typename... Args>   void err(vector<string>::iterator it, T a, Args... args) {     cerr << it->substr((*it)[0] ==    , it->length()) <<   =   << a <<   n ;     err(++it, args...);   }   void yup() { cout <<  Yes  << endl; }   void nope() { cout <<  No  << endl; }   void Yup() { cout <<  YES  << endl; }   void Nope() { cout <<  NO  << endl; }   void readarray(vector<int> &a, int size) {     int tmp;     a.clear();     for (int i = 0; i < (size); i++) {       cin >> tmp;       a.push_back(tmp);     }   }   void solvecase() {     int n, m;     int k;     cin >> n;     pair<int, int> g[111];     for (int i = 0; i < n; i++) {       cin >> g[i].first >> g[i].second;     }     bool ok = true;     if (g[0].first < g[0].second) ok = false;     for (int i = 1; i < n; i++) {       if ((g[i].first < g[i - 1].first) or (g[i].second < g[i - 1].second))         ok = false;     }     for (int i = 1; i < n; i++) {       int jumpc = g[i].second - g[i - 1].second;       int jumpp = g[i].first - g[i - 1].first;       if (jumpc > jumpp) ok = false;     }     ok ? Yup() : Nope();   }   int main() {     int cases = 1;     cin >> cases;     while (cases--) {       solvecase();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> tree[2 * 100000 + 4];   long long dp[2 * 100000 + 3][2];   int value[2 * 100000 + 4];   void dfs(int x) {     dp[x][0] = 0;     dp[x][1] = -200000000005;     for (int i = 0, sz = tree[x].size(); i < sz; i++) {       int v = tree[x][i];       dfs(v);       long long tmp1 = dp[x][1], tmp0 = dp[x][0];       dp[x][0] = max(tmp0 + dp[v][0], tmp1 + dp[v][1]);       dp[x][1] = max(tmp1 + dp[v][0], tmp0 + dp[v][1]);     }     dp[x][1] = max(dp[x][0] + value[x], dp[x][1]);   }   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       int x;       cin >> x >> value[i];       if (i == 1) continue;       tree[x].push_back(i);     }     dfs(1);     cout << dp[1][1] << endl;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   void makedata() {     freopen( input.txt ,  w , stdout);     fclose(stdout);   }   long long a[250000];   int n, q;   template <typename T>   class SegmentTree {    private:     T *data, *lazy;     int type;     void pushup(int rt) {       if (type == 0)         data[rt] = data[rt << 1] + data[rt << 1 | 1];       else if (type == 1)         data[rt] = max(data[rt << 1], data[rt << 1 | 1]);     }     void pushdown(int rt, T m) {       if (lazy[rt] == 0) return;       lazy[rt << 1] += lazy[rt];       lazy[rt << 1 | 1] += lazy[rt];       if (type == 0) {         data[rt << 1] += (m - (m >> 1)) * lazy[rt];         data[rt << 1 | 1] += (m >> 1) * lazy[rt];       } else if (type == 1) {         data[rt << 1] += lazy[rt];         data[rt << 1 | 1] += lazy[rt];       }       lazy[rt] = 0;     }       public:     SegmentTree(int n, int t)         : data((T *)malloc((n << 3) * sizeof(T))),           lazy((T *)malloc((n << 3) * sizeof(T))),           type(t) {}     void Build(T *base, int l, int r, int rt) {       lazy[rt] = 0;       if (l == r)         data[rt] = base[l];       else {         int mid = (l + r) >> 1;         Build(base, l, mid, rt << 1);         Build(base, mid + 1, r, rt << 1 | 1);         pushup(rt);       }     }     void Modify(int l, int r, int rt, int L, int R, T v) {       if (L <= l && R >= r) {         lazy[rt] += v;         if (type == 0)           data[rt] += v * (r - l + 1);         else if (type == 1)           data[rt] += v;         return;       }       pushdown(rt, r - l + 1);       int mid = (l + r) >> 1;       if (L <= mid) Modify(l, mid, rt << 1, L, R, v);       if (R > mid) Modify(mid + 1, r, rt << 1 | 1, L, R, v);       pushup(rt);     }     T QueryPoint(int l, int r, int rt, int val) {       if (l == r) return data[rt];       pushdown(rt, r - l + 1);       int mid = (l + r) >> 1;       T ret = 0;       if (val <= mid)         ret = QueryPoint(l, mid, rt << 1, val);       else         ret = QueryPoint(mid + 1, r, rt << 1 | 1, val);       pushup(rt);       return ret;     }     T QuerySegment(int l, int r, int rt, int L, int R) {       pushdown(rt, r - l + 1);       if (L == l && R == r) return data[rt];       int mid = (l + r) >> 1;       if (R <= mid) return QuerySegment(l, mid, rt << 1, L, R);       if (mid < L) return QuerySegment(mid + 1, r, rt << 1 | 1, L, R);       if (type == 0)         return QuerySegment(l, mid, rt << 1, L, mid) +                QuerySegment(mid + 1, r, rt << 1 | 1, mid + 1, R);       else if (type == 1)         return max(QuerySegment(l, mid, rt << 1, L, mid),                    QuerySegment(mid + 1, r, rt << 1 | 1, mid + 1, R));     }     int find(int l, int r, int rt, int p, T x) {       if (r <= p) return -1;       if (data[rt] < x) return -1;       if (l == r) return l;       int mid = (l + r) >> 1;       if (l > p) {         if (data[rt << 1] >= x)           return find(l, mid, rt << 1, p, x);         else           return find(mid + 1, r, rt << 1 | 1, p, x);       }       int rtn = find(l, mid, rt << 1, p, x);       if (rtn != -1) return rtn;       return find(mid + 1, r, rt << 1 | 1, p, x);     }   };   SegmentTree<long long> Tsum(250000, 0), Tmax(250000, 1);   int main() {     std::ios::sync_with_stdio(0), cin.tie(0);     cin >> n >> q;     for (int i = 1; i <= n; i++) cin >> a[i];     Tsum.Build(a, 1, n, 1);     Tmax.Build(a, 1, n, 1);     while (q--) {       int p;       long long x;       cin >> p >> x;       Tsum.Modify(1, n, 1, p, p, x - a[p]);       Tmax.Modify(1, n, 1, p, p, x - a[p]);       a[p] = x;       if (a[1] == 0) {         cout << 1 << endl;         continue;       }       int m = 1;       while (1) {         long long sum = Tsum.QuerySegment(1, n, 1, 1, m);         int nex = Tmax.find(1, n, 1, m, sum);         if (nex == -1) {           cout << -1 << endl;           break;         }         if (a[nex] == Tsum.QuerySegment(1, n, 1, 1, nex - 1)) {           cout << nex << endl;           break;         }         m = nex;       }     }     return 0;   }


#define _CRT_SECURE_NO_WARNINGS      #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma GCC target_tbl( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )         //#include  stdafx.h    #include  iostream    #include  fstream    #include  algorithm    #include  iomanip    #include  stack    #include  queue    #include  string    #include  vector    #include  map    #include  set    #include  list    #include  deque    #include  complex    #include  bitset    #include  cmath    #include  unordered_set    #include  unordered_map    #include  iterator    #include <ctime>   #include <cassert>   #include  numeric    #include <cstdio>   #include  random    #include  chrono    #include  cstring          using namespace std;      #define maxi(a,b) a = max(a, b);   #define mini(a,b) a = min(a, b);      /////////////////   #define endl   n    /////////////////      #define all(a) a.begin(), a.end()   #define rall(a) a.rbegin(), a.rend()   #define sqr(x) ((x) * (x))   #define SZ(a) ((int)(a.size()))   #define watch(x) cout << (#x) <<   =   << x << endl;   //typedef long long ll;   typedef long double ld;      //#define int unsigned int   #define int long long   #define double ld   typedef pair<int, int> pii;   typedef pair<double, double> pdd;   typedef vector<int> vi;   typedef vector<double> vd;   typedef vector<pii> vpii;   typedef vector<vi> vvi;      template<class T>   void show(const vector<T> &a)   {    for (T x : a)     cout << x <<    ;    cout << endl;   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count());      int xx[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };   int yy[8] = { 0, 0, 1, -1, -1, -1, 1, 1 };   string dir =  RDLU ;   string travel =  ENWS ;      const int N = 2e5 + 50, oo = 1e9 + 500;   //const int mod = 998244353;   const int mod = 1e9 + 7;   //const int mod = 1e9 + 9;   const int M2 = 1000000093, x2 = 27162;   const int M1 = 1000000087, x1 = 241;   const double eps = 1e-18, PI = 2 * acos(0.0);      vi check(vi &a, vi &b)   {    int n = SZ(a);    int m = SZ(b);       vi c;    int i = 0, j = 0;    while(i < n)    {     if (j >= m)      break;        if (a[i] < b[j])     {      i++;      continue;     }        if (a[i] == b[j])     {      c.push_back(a[i]);      i++;      j++;      continue;     }        if (a[i] > b[j])     {      j++;      continue;     }    }    return c;   }      signed main()   {    ios::sync_with_stdio(0);    cout.tie(0); cin.tie(0);    //freopen( input.txt ,  r , stdin);    //freopen( output.txt ,  w , stdout);    //freopen( paintbarn.in ,  r , stdin);    //freopen( paintbarn.out ,  w , stdout);       int T;    cin >> T;       while (T--)    {     int n;     cin >> n;        vvi g(1 << n);     for (int i = 0; i < n*(1 << (n - 1)); i++)     {      int u, v;      cin >> u >> v;      g[u].push_back(v);      g[v].push_back(u);     }        for (int i = 0; i < (1 << n); i++)     {      sort(all(g[i]));     }       vi p(1 << n, -1);     vi used(1 << n, 0);     used[0] = 1;     p[0] = 0;        for (int i = 1; i < (1 << n); i++)     {      vi cur;      int par = -1;      for (int j = 0; j < n; j++)      {       int ii = i ^ (1 << j);       if (p[ii] != -1)       {        par = p[ii];        cur.push_back(p[ii]);       }      }         sort(all(cur));         for (int j: g[par]) //candidates      {       if (check(cur, g[j]) == cur && !used[j])       {        used[j] = 1;        p[i] = j;        break;       }      }     }        //cout <<  !!!  ;     show(p);        if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16)     {      cout << -1 << endl;      continue;     }        vi c(1 << n);     for (int i = 0; i < (1 << n); i++)     {      int col = 0;      for (int j = 0; j < n; j++)      {       if (i & (1 << j))        col ^= j;      }         c[i] = col;     }        vi cc(1 << n);     for (int i = 0; i < (1 << n); i++)     {      cc[p[i]] = c[i];     }     c = cc;        show(c);    }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   long long t[N], dp[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %lld , &t[i]);     }     dp[0] = 0;     long long sum = 0;     for (int i = 1; i <= n; i++) {       dp[i] = dp[i - 1] + 20;       for (int j = i - 1; j >= 1; j--) {         if (t[i] - t[j] < 90)           dp[i] = min(dp[i], dp[j - 1] + 50);         else if (t[i] - t[j] < 1440)           dp[i] = min(dp[i], dp[j - 1] + 120);         else           break;       }       printf( %lld n , dp[i] - sum);       sum = dp[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long GCD(long long a, long long b) {     return (a % b) == 0 ? b : GCD(b, a % b);   }   long long mod(long long x) { return ((x % 998244353 + 998244353) % 998244353); }   bool cmp(const pair<int, int> &left, const pair<int, int> &right) {     return left.first > right.first ||            (left.first == right.first && left.second < right.second);   }   int main() {     int n, m, p1, p2, p3, t1, t2, i, j, k, mx = 0;     vector<pair<int, int> > v;     cin >> n >> p1 >> p2 >> p3 >> t1 >> t2;     for (i = 0; i < n; i++) {       cin >> j >> k;       v.push_back(make_pair(j, k));     }     sort(v.begin(), v.end());     for (i = 0; i < n - 1; i++) {       mx += (v[i].second - v[i].first) * p1;       if (v[i + 1].first > v[i].second + t1) {         mx += t1 * p1;         if (v[i + 1].first > v[i].second + t1 + t2) {           mx += t2 * p2;           mx += (v[i + 1].first - (v[i].second + t1 + t2)) * p3;         } else           mx += (v[i + 1].first - (v[i].second + t1)) * p2;       } else         mx += (v[i + 1].first - v[i].second) * p1;     }     mx += (v[n - 1].second - v[n - 1].first) * p1;     cout << mx << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   const long long M = (long long)1e9 + 7;   int Int() {     int x;     scanf( %d , &x);     return x;   }   long long Long() {     long long x;     scanf( %lld , &x);     return x;   }   void Char(char *ch) { scanf( %s , ch); }   template <typename t>   inline t abs(t a) {     if (a >= 0) return a;     return -a;   }   template <typename T>   inline T modpow(T b, T p, T Mod) {     T res = 1;     while (p) {       if (p & 1) {         res *= b;         res %= Mod;       }       b *= b;       b %= Mod;       p >>= 1;     }     return res;   }   template <typename T>   inline T ModInv(T b, T Mod) {     return modpow(b, Mod - 2, Mod);   }   template <typename T>   inline T pwr(T b, T p) {     T res = 1;     while (p) {       if (p & 1) {         res *= b;       }       b *= b;       p >>= 1;     }     return res;   }   template <typename T>   inline T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <typename T>   inline void Int(T &n) {     n = 0;     int f = 1;     register int ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) f = -1;     for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch -  0 ;     n = n * f;   }   template <typename T, typename U>   inline void Int(T &a, U &b) {     Int(a);     Int(b);   }   template <typename T, typename U, typename W>   inline void Int(T &a, U &b, W &c) {     Int(a, b);     Int(c);   }   template <typename T, typename U, typename W, typename X>   inline void Int(T &a, U &b, W &c, X &d) {     Int(a, b, c);     Int(d);   }   vector<int> Unique(vector<int> v) {     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());     return v;   }   vector<string> split(const string &s, char c) {     vector<string> v;     stringstream ss(s);     string x;     while (getline(ss, x, c)) v.emplace_back(x);     return move(v);   }   void err(vector<string>::iterator it) {}   template <typename T, typename... Args>   void err(vector<string>::iterator it, T a, Args... args) {     cout << it->substr((*it)[0] ==    , it->length()) <<   =   << a <<    ;     err(++it, args...);   }   struct node {     int cnt;     node *tree[2];     node() {       cnt = 0;       tree[0] = tree[1] = NULL;     }   } * root;   void add(int x) {     node *cur = root;     for (int i = 30; i >= 0; i--) {       int f = (bool)(x & (1 << i));       if (cur->tree[f] == NULL) {         cur->tree[f] = new node();       }       cur = cur->tree[f];       cur->cnt++;     }   }   int sum(int x, int k) {     int res = 0;     node *cur = root;     for (int i = 30; i >= 0; i--) {       int p = (bool)(x & (1 << i));       int q = (bool)(k & (1 << i));       if (q == 0) {         if (p == 1) {           if (cur->tree[1] != NULL) {             cur = cur->tree[1];           } else {             return res;           }         } else {           if (cur->tree[0] != NULL) {             cur = cur->tree[0];           } else {             return res;           }         }       } else {         if (p == 1) {           if (cur->tree[1] != NULL) res += cur->tree[1]->cnt;           if (cur->tree[0] != NULL) {             cur = cur->tree[0];           } else {             return res;           }         } else {           if (cur->tree[0] != NULL) res += cur->tree[0]->cnt;           if (cur->tree[1] != NULL) {             cur = cur->tree[1];           } else {             return res;           }         }       }     }     return res;   }   int a[N];   int main() {     int t = 1, tc = 0;     while (t--) {       root = new node();       int n = Int(), k = Int();       long long res = 0;       add(0);       for (int i = 1; i <= n; i++) {         a[i] = Int();         a[i] ^= a[i - 1];         res += sum(a[i], k);         add(a[i]);       }       long long s = (1LL * n * (n + 1LL)) / 2LL;       printf( %lld n , s - res);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void read(T &x) {     x = 0;     char c = getchar();     int flag = 0;     while (c <  0  || c >  9 ) flag |= (c ==  - ), c = getchar();     while (c >=  0  && c <=  9 )       x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();     if (flag) x = -x;   }   template <class T>   T _max(T a, T b) {     return a > b ? a : b;   }   template <class T>   T _min(T a, T b) {     return a < b ? a : b;   }   template <class T>   bool checkmax(T &a, T b) {     return b > a ? a = b, 1 : 0;   }   template <class T>   bool checkmin(T &a, T b) {     return b < a ? a = b, 1 : 0;   }   const int N = 100005;   int n;   long long a[N];   int sg(long long x) {     if (x <= 3)       return 0;     else if (x <= 15)       return 1;     else if (x <= 81)       return 2;     else if (x <= 6723)       return 0;     else if (x <= 50625)       return 3;     else if (x <= (long long)2562890625)       return 1;     else       return 2;   }   void init() {     read(n);     for (int i = 1; i <= n; ++i) {       read(a[i]);     }   }   void solve() {     int ans = 0;     for (int i = 1; i <= n; ++i) {       ans ^= sg(a[i]);     }     if (ans)       printf( Furlo n );     else       printf( Rublo n );   }   int main() {     int T = 1;     while (T--) {       init();       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> vans;   set<int> s;   int main() {     scanf( %d , &n);     int start = 1;     for (int i = 1; i <= n; ++i) {       int x;       scanf( %d , &x);       if (s.count(x)) {         vans.push_back(start);         vans.push_back(i);         start = i + 1;         s.clear();       } else {         s.insert(x);       }     }     if (vans.empty()) {       printf( -1 n );       return 0;     } else {       printf( %d n , vans.size() / 2);       for (int i = 0; i < vans.size() - 2; i += 2)         printf( %d %d n , vans[i], vans[i + 1]);       if (!s.empty())         printf( %d %d n , vans[vans.size() - 2], n);       else         printf( %d %d n , vans[vans.size() - 2], vans[vans.size() - 1]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long N = 1e7 + 5;   std::vector<long long> v(N, 0);   std::vector<long long> p;   void sieve() {     for (long long i = 2; i < N; i++) {       if (v[i] != 0) continue;       p.push_back(i);       for (long long j = 2 * i; j < N; j += i) v[j] = 1;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     long long cnt = 0;     for (long long i = 2; i <= (long long)sqrt(n); i++)       if (n % i == 0) cnt++;     v[0] = 1;     v[1] = 1;     if (cnt == 0) {       cout <<  1  <<   n ;       cout << n;       return 0;     }     sieve();     long long bal = 0;     if (n % 2 == 0)       bal = 2;     else       bal = 3;     long long temp = n - bal;     if (n == 4) {       cout << 2 <<   n  << 2 <<     << 2;       return 0;     }     for (long long i = 0; i < p.size(); i++) {       cnt = 0;       for (long long j = 2; temp - p[i] > 0 and j <= (long long)sqrt(temp - p[i]);            j++) {         if ((temp - p[i]) % j == 0) {           cnt = 1;           break;         }       }       if (!cnt) {         cout << 3 <<   n  << bal <<     << p[i] <<     << temp - p[i];         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   using PII = pair<ll, ll>;   using piii = pair<pii, pii>;   using Pi = pair<int, pii>;   using Graph = vector<vector<int>>;   const int dx[4] = {0, -1, 1, 0};   const int dy[4] = {-1, 0, 0, 1};   bool check(int x, int y) {     if (0 <= x && x < 55 && 0 <= y && y < 55)       return true;     else       return false;   }   const ll INF = 1e+7;   int gcd(int x, int y) {     if (x < y) swap(x, y);     if (y == 0) return x;     return gcd(y, x % y);   }   void mul(ll a, ll b) { a = a * b % INF; }   using Graph = vector<vector<int>>;   long long modinv(long long a, long long m) {     long long b = m, u = 1, v = 0;     while (b) {       long long t = a / b;       a -= t * b;       swap(a, b);       u -= t * v;       swap(u, v);     }     u %= m;     if (u < 0) u += m;     return u;   }   const double PI = 3.14159265358979323846;   const int MAX = 510000;   ll fac[MAX], finv[MAX], inv[MAX];   void COMinit() {     fac[0] = fac[1] = 1;     finv[0] = finv[1] = 1;     inv[1] = 1;     for (int i = 2; i < MAX; ++i) {       fac[i] = fac[i - 1] * i % INF;       inv[i] = INF - inv[INF % i] * (INF / i) % INF;       finv[i] = finv[i - 1] * inv[i] % INF;     }   }   ll COM(int n, int k) {     if (n < k) return 0;     if (n < 0 || k < 0) return 0;     return fac[n] * (finv[k] * finv[n - k] % INF) % INF;   }   double Euclidean_distance(double x1, double y1, double x2, double y2) {     return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));   }   int prime[1001000];   bool is_prime[1001010];   int sieve(int n) {     int p = 0;     for (int i = 0; i <= n; ++i) {       is_prime[i] = true;     }     is_prime[0] = false;     is_prime[1] = false;     for (int i = 2; i <= n; ++i) {       if (is_prime[i]) {         prime[p] = i;         p++;         for (int j = 2 * i; j <= n; j += i) {           is_prime[j] = false;         }       }     }     return p;   }   map<int, int> prime_factor(int n) {     map<int, int> res;     for (int i = 2; i * i <= n; ++i) {       while (n % i == 0) {         ++res[i];         n /= i;       }     }     if (n != 1) res[n] = 1;     return res;   }   ll powmod(ll a, ll k, ll mod) {     ll ap = a, ans = 1;     while (k) {       if (k & 1) {         ans *= ap;         ans %= mod;       }       ap = ap * ap;       ap %= mod;       k >>= 1;     }     return ans;   }   ll invi(ll a, ll mod) { return powmod(a, mod - 2, mod); }   int main() {     int t;     cin >> t;     while (t--) {       ll n, m;       cin >> n >> m;       if (n == 1 || m == 1) {         cout <<  YES  << endl;       } else if (n == 2 && m == 2) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 4;   set<int> ts[N];   map<int, int> tmp;   int n, k, a[N];   void add(int v) {     auto it1 = tmp.find(v);     if (it1 == tmp.end()) {       tmp.emplace(v, 1);       ts[1].insert(v);     } else {       ts[it1->second].erase(v);       it1->second++;       ts[it1->second].insert(v);     }   }   void del(int v) {     auto it1 = tmp.find(v);     if (it1->second == 1) {       ts[1].erase(v);       tmp.erase(it1);     } else {       ts[it1->second].erase(v);       it1->second--;       ts[it1->second].insert(v);     }   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , a + i);     for (int i = 1; i <= n; i++) {       add(a[i]);       if (i > k) del(a[i - k]);       if (i >= k) {         if (ts[1].empty())           puts( Nothing );         else           printf( %d n , *(ts[1].rbegin()));       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   bool issafe(int i, int j, int n, int m) {     return i >= 0 && j >= 0 && i < n && j < m;   }   char s[] = { D ,  L ,  R ,  U };   int dx[] = {+1, 0, 0, -1};   int dy[] = {0, -1, +1, 0};   void solve() {     int n, m, k;     cin >> n >> m >> k;     char a[n][m];     int dist[n][m];     pair<int, int> start;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         dist[i][j] = -1;         cin >> a[i][j];         if (a[i][j] ==  X ) {           start = {i, j};         }       }     }     if (k & 1) {       cout <<  IMPOSSIBLE             <<   n ;       return;     }     queue<pair<int, int> > q;     q.push(start);     dist[start.first][start.second] = 0;     while (!q.empty()) {       pair<int, int> curr = q.front();       q.pop();       for (int i = 0; i < 4; i++) {         int nx = curr.first + dx[i], ny = curr.second + dy[i];         if (issafe(nx, ny, n, m) && a[nx][ny] !=  *  && dist[nx][ny] == -1) {           q.push({nx, ny});           dist[nx][ny] = dist[curr.first][curr.second] + 1;         }       }     }     string ans =   ;     pair<int, int> curr = start;     bool flag = 0;     while (k) {       flag = 0;       for (int i = 0; i < 4; i++) {         int nx = curr.first + dx[i], ny = curr.second + dy[i];         if (issafe(nx, ny, n, m) && a[nx][ny] !=  *  && dist[nx][ny] <= k) {           flag = 1;           ans += s[i];           curr = {nx, ny};           k--;           break;         }       }       if (!flag) break;     }     if (!flag) {       cout <<  IMPOSSIBLE             <<   n ;       return;     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4005;   int n;   struct child {     long long v, d, p;     int idx;     child(long long v, long long d, long long p, int idx)         : v(v), d(d), p(p), idx(idx) {}   };   long long v, d, p;   vector<child *> ve;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = (0); i < (n); i++) {       cin >> v >> d >> p;       ve.push_back(new child(v, d, p, i));     }     vector<int> ans;     while (!ve.empty()) {       ans.push_back(ve[0]->idx);       ve[0]->p = -1;       for (int i = (1); i < (((int)(ve).size())); i++) {         ve[i]->p -= max(0LL, ve[0]->v - i + 1);         if (ve[i]->p < 0) {           for (int j = (i + 1); j < (((int)(ve).size())); j++) {             ve[j]->p -= ve[i]->d;           }         }       }       ve.erase(stable_partition((ve).begin(), (ve).end(),                                 [&](child *c) { return c->p >= 0; }),                ve.end());     }     cout << ((int)(ans).size()) <<   n ;     for (int x : ans) {       cout << x + 1 <<    ;     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N, M;     cin >> N >> M;     vector<string> st(N);     vector<char> max(M,  0 );     for (int i = 0; i < N; i++) {       cin >> st[i];       for (int j = 0; j < M; j++)         if (st[i][j] > max[j]) max[j] = st[i][j];     }     int sum = 0;     for (int i = 0; i < st.size(); i++)       for (int j = 0; j < st[i].length(); j++)         if (st[i][j] == max[j]) {           sum++;           break;         }     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   char s[1005];   int a[25], mark[6071], dp[1005][6071], ch[6071][12], link[6071], tl, ans = 1e9,                                                                        x, n;   void insert(int len) {     int rt = 0;     for (int i = 1; i <= len; ++i) {       if (!ch[rt][a[i]]) ch[rt][a[i]] = ++tl;       rt = ch[rt][a[i]];     }     mark[rt] = 1;   }   void build() {     std::queue<int> q;     std::memset(link, -1, sizeof(link));     link[0] = 0;     for (int i = 0; i < 10; ++i)       if (ch[0][i]) {         q.push(ch[0][i]);         link[ch[0][i]] = 0;       }     while (!q.empty()) {       int u = q.front();       q.pop();       mark[u] |= mark[link[u]];       for (int i = 0; i < 10; ++i) {         if (ch[u][i]) {           mark[ch[u][i]] |= mark[u];           link[ch[u][i]] = ch[link[u]][i];           q.push(ch[u][i]);         } else           ch[u][i] = ch[link[u]][i];       }     }   }   void dfs(int u, int sum) {     if (sum == x) {       for (int l = 1; l < u; ++l) {         int s = 0;         for (int r = l; r < u; ++r) {           s += a[r];           if (s != x && x % s == 0) return;         }       }       insert(u - 1);       return;     }     for (int j = 1; sum + j <= x && j < 10; ++j) {       a[u] = j;       dfs(u + 1, sum + j);     }   }   void generate() {     dfs(1, 0);     build();   }   int main() {     scanf( %s%d , s + 1, &x);     n = std::strlen(s + 1);     generate();     std::memset(dp, 63, sizeof(dp));     dp[1][0] = 0;     for (int i = 1; i <= n; ++i) {       for (int j = 0; j <= tl; ++j) {         if (!mark[ch[j][s[i] -  0 ]])           dp[i + 1][ch[j][s[i] -  0 ]] =               std::min(dp[i + 1][ch[j][s[i] -  0 ]], dp[i][j]);         dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j] + 1);       }     }     for (int j = 0; j <= tl; ++j) ans = std::min(ans, dp[n + 1][j]);     printf( %d , ans);     return 0;   }