#include <bits/stdc++.h>   using namespace std;   const long long maxn = 3e5 + 7, mod = 998244353, inf = 1e18, MAXN = 1e6 + 7;   const double eps = 1e-9;   mt19937 rnd(time(0));   long long n;   long long cnt[maxn];   struct state {     long long a, b, d;     state(long long a, long long b, long long d) : a(a), b(b), d(d) {}     state() : a(0), b(0), d(0) {}   };   vector<state> have;   bool cmp(state p1, state p2) { return p1.d > p2.d; }   vector<long long> gr[maxn];   vector<long long> step[maxn];   void solve() {     cin >> n;     for (long long i = 1; i <= n; ++i) {       long long d;       cin >> d;       long long a = 2 * i;       long long b = a - 1;       have.emplace_back(b, a, d);     }     sort(have.begin(), have.end(), cmp);     vector<long long> even;     for (auto v : have) {       even.push_back(v.a);     }     for (long long i = 1; i < n; ++i) {       gr[even[i - 1]].push_back(even[i]);     }     long long size = n;     for (long long i = 0; i < n; ++i) {       --size;       long long d = have[i].d;       long long b = have[i].b;       if (size < d) {         gr[even.back()].push_back(b);         even.push_back(b);         ++size;       } else {         gr[even[i + d - 1]].push_back(b);       }     }     for (long long i = 1; i <= 2 * n; ++i) {       for (auto v : gr[i]) {         cout << i <<     << v <<   n ;       }     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.precision(20);     cout << fixed;     long long t = 1;     for (long long i = 0; i < t; ++i) {       solve();     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   struct pt {     int x, y;   };   int h, t, r, n, m;   int w[205][205], unc[205][205], siofdjsodi[205][205];   basic_string<pt> e[205][205], f[205][205], qu;   pt hd[205], td[205];   void ae(int i, int j, int u, int v) {     if (u + v > r) return;     e[i][j] += {u, v};     f[u][v] += {i, j};     unc[i][j]++;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cerr.tie(nullptr);     cin >> h >> t >> r;     cin >> n;     for (int i = 1; i <= n; i++) cin >> hd[i].x >> hd[i].y;     cin >> m;     for (int i = 1; i <= m; i++) cin >> td[i].x >> td[i].y;     for (int i = 0; i <= r; i++) {       for (int j = 0; i + j <= r; j++) {         for (int x = 1; x <= min(i, n); x++) {           int u = i - x + hd[x].x;           int v = j + hd[x].y;           ae(i, j, u, v);         }         for (int y = 1; y <= min(j, m); y++) {           int u = i + td[y].x;           int v = j - y + td[y].y;           ae(i, j, u, v);         }       }     }     {       w[0][0] = 1;       qu += pt{0, 0};       size_t qs = 0;       while (qs != qu.size()) {         auto [i, j] = qu[qs++];         for (auto [x, y] : f[i][j]) {           if (w[x][y] == 0) {             w[x][y] = 1;             siofdjsodi[x][y] = siofdjsodi[i][j] + 1;             qu += {x, y};           }         }       }     }     qu = {};     for (int i = 0; i <= r; i++) {       for (int j = 0; i + j <= r; j++) {         if (w[i][j] == 0 && unc[i][j] == 0) {           qu += {i, j};         }       }     }     size_t qs = 0;     while (qs != qu.size()) {       auto [i, j] = qu[qs++];       int q = 0;       for (auto [x, y] : e[i][j]) {         q = max(q, siofdjsodi[x][y]);       }       w[i][j] = -1;       siofdjsodi[i][j] = q + 1;       for (auto [x, y] : f[i][j]) {         if (!--unc[x][y]) {           qu += {x, y};         }       }     }     if (w[h][t] == 1) {       cout <<  Ivan n ;       cout << siofdjsodi[h][t] <<   n ;     } else if (w[h][t] == -1) {       cout <<  Zmey n ;       cout << siofdjsodi[h][t] <<   n ;     } else {       cout <<  Draw n ;     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   const int inf = 1e9 + 7;   const long long linf = 1e18;   const double pi = acos(-1.0);   int main() {     ios::sync_with_stdio(false);     int t;     cin >> t;     for (int total = 0; total < t; total++) {       int n, p;       cin >> n >> p;       for (int temp = 0; temp < 2 * n + p;) {         for (int i = 1; i <= n; i++) {           if (temp == 2 * n + p) break;           for (int j = i + 1; j <= n; j++) {             temp++;             cout << i <<     << j << endl;             if (temp == 2 * n + p) break;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   static struct FastInput {     static constexpr int BUF_SIZE = 1 << 20;     char buf[BUF_SIZE];     size_t chars_read = 0;     size_t buf_pos = 0;     FILE* in = stdin;     char cur = 0;     inline char get_char() {       if (buf_pos >= chars_read) {         chars_read = fread(buf, 1, BUF_SIZE, in);         buf_pos = 0;         buf[0] = (chars_read == 0 ? -1 : buf[0]);       }       return cur = buf[buf_pos++];     }     inline void tie(int) {}     inline explicit operator bool() { return cur != -1; }     inline static bool is_blank(char c) { return c <=    ; }     inline bool skip_blanks() {       while (is_blank(cur) && cur != -1) {         get_char();       }       return cur != -1;     }     inline FastInput& operator>>(char& c) {       skip_blanks();       c = cur;       return *this;     }     inline FastInput& operator>>(string& s) {       if (skip_blanks()) {         s.clear();         do {           s += cur;         } while (!is_blank(get_char()));       }       return *this;     }     template <typename T>     inline FastInput& read_integer(T& n) {       n = 0;       if (skip_blanks()) {         int sign = +1;         if (cur ==  - ) {           sign = -1;           get_char();         }         do {           n += n + (n << 3) + cur -  0 ;         } while (!is_blank(get_char()));         n *= sign;       }       return *this;     }     template <typename T>     inline typename enable_if<is_integral<T>::value, FastInput&>::type operator>>(         T& n) {       return read_integer(n);     }     inline FastInput& operator>>(__int128& n) { return read_integer(n); }     template <typename T>     inline typename enable_if<is_floating_point<T>::value, FastInput&>::type     operator>>(T& n) {       n = 0;       if (skip_blanks()) {         string s;         (*this) >> s;         sscanf(s.c_str(),  %lf , &n);       }       return *this;     }   } fast_input;   static struct FastOutput {     static constexpr int BUF_SIZE = 1 << 20;     char buf[BUF_SIZE];     size_t buf_pos = 0;     static constexpr int TMP_SIZE = 1 << 20;     char tmp[TMP_SIZE];     FILE* out = stdout;     inline void put_char(char c) {       buf[buf_pos++] = c;       if (buf_pos == BUF_SIZE) {         fwrite(buf, 1, buf_pos, out);         buf_pos = 0;       }     }     ~FastOutput() { fwrite(buf, 1, buf_pos, out); }     inline FastOutput& operator<<(char c) {       put_char(c);       return *this;     }     inline FastOutput& operator<<(const char* s) {       while (*s) {         put_char(*s++);       }       return *this;     }     inline FastOutput& operator<<(const string& s) {       for (int i = 0; i < (int)s.size(); i++) {         put_char(s[i]);       }       return *this;     }     template <typename T>     inline char* integer_to_string(T n) {       char* p = tmp + TMP_SIZE - 1;       if (n == 0) {         *--p =  0 ;       } else {         bool is_negative = false;         if (n < 0) {           is_negative = true;           n = -n;         }         while (n > 0) {           *--p = (char)( 0  + n % 10);           n /= 10;         }         if (is_negative) {           *--p =  - ;         }       }       return p;     }     template <typename T>     inline typename enable_if<is_integral<T>::value, char*>::type stringify(T n) {       return integer_to_string(n);     }     inline char* stringify(__int128 n) { return integer_to_string(n); }     template <typename T>     inline typename enable_if<is_floating_point<T>::value, char*>::type stringify(         T n) {       sprintf(tmp,  %.17f , n);       return tmp;     }     template <typename T>     inline FastOutput& operator<<(const T& n) {       auto p = stringify(n);       for (; *p != 0; p++) {         put_char(*p);       }       return *this;     }   } fast_output;   const int N = 1e6 + 3;   vector<int> inq[N];   int main() {     ios::sync_with_stdio(false);     fast_input.tie(0);     int n, m, q;     fast_input >> n >> m >> q;     vector<vector<int>> edge(n + 1);     vector<tuple<int, int, int>> edges;     for (int i = 0; i < m; i++) {       int u, v, w;       fast_input >> u >> v >> w;       edges.emplace_back(u, v, w);       edge[u].emplace_back(i);     }     priority_queue<pair<long long, int>> pq;     const long long inf = 1e18;     vector<long long> d(n + 1, inf);     d[1] = 0;     pq.emplace(d[1], 1);     while ((int)pq.size()) {       auto x = pq.top();       pq.pop();       if (-x.first != d[x.second]) continue;       int v = x.second;       for (auto& x : edge[v]) {         int u = get<1>(edges[x]);         if (d[u] > d[v] + get<2>(edges[x])) {           d[u] = d[v] + get<2>(edges[x]);           pq.emplace(-d[u], u);         }       }     }     vector<int> add(m);     for (int i = 0; i < m; i++) {       auto& x = edges[i];       add[i] = get<2>(x) - (d[get<1>(x)] - d[get<0>(x)]);     }     int up = 0;     while (q--) {       int type;       fast_input >> type;       if (type == 1) {         int v;         fast_input >> v;         if (up) {           vector<int> dist(n + 1, (int)1e9);           dist[1] = 0;           inq[0].emplace_back(1);           for (int i = 0; i <= up; i++) {             for (int j = 0; j < (int)inq[i].size(); j++) {               int v = inq[i][j];               if (dist[v] != i) continue;               for (auto& x : edge[v]) {                 int u = get<1>(edges[x]);                 if (dist[u] > dist[v] + add[x]) {                   dist[u] = dist[v] + add[x];                   if (dist[u] <= up) inq[dist[u]].emplace_back(u);                 }               }             }             inq[i].clear();           }           for (int i = 1; i <= n; i++) d[i] = min(d[i] + dist[i], inf);           for (int i = 0; i < m; i++) {             auto& x = edges[i];             add[i] -= (dist[get<1>(x)] - dist[get<0>(x)]);           }           up = 0;         }         if (d[v] != inf)           fast_output << d[v] <<   n ;         else           fast_output <<  -1 n ;       } else {         int foo;         fast_input >> foo;         for (int i = 0; i < foo; i++) {           int x;           fast_input >> x;           --x;           add[x]++;         }         up += foo;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   long long ksm(long long a, long long b) {     long long res = 1;     a %= mod;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   const int N = 1e5;   int n, t, p[N], pos[N], tmp[N];   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       for (int i = 1; i <= n; i++) {         scanf( %d , &p[i]);         tmp[i] = 0;         pos[p[i]] = i;       }       bool f = false;       for (int i = 1; i <= n; i++) {         if (tmp[pos[i]]) {           continue;         } else {           tmp[pos[i]] = i;           int x = i + 1;           for (int j = pos[i] + 1, x = i + 1; j <= n; j++) {             if (tmp[j]) break;             tmp[j] = x++;           }           i = x - 1;         }       }       for (int i = 1; i <= n; i++) {         if (p[i] == tmp[i]) continue;         f = true;         break;       }       if (f)         puts( NO );       else         puts( YES );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T1>   void debug(T1 e) {     cout << e << endl;   }   template <class T1, class T2>   void debug(T1 e1, T2 e2) {     cout << e1 <<   t  << e2 << endl;   }   template <class T1, class T2, class T3>   void debug(T1 e1, T2 e2, T3 e3) {     cout << e1 <<   t  << e2 <<   t  << e3 << endl;   }   template <class T1, class T2, class T3, class T4>   void debug(T1 e1, T2 e2, T3 e3, T4 e4) {     cout << e1 <<   t  << e2 <<   t  << e3 <<   t  << e4 << endl;   }   template <class T1, class T2, class T3, class T4, class T5>   void debug(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {     cout << e1 <<   t  << e2 <<   t  << e3 <<   t  << e4 <<   t  << e5 << endl;   }   template <class T1, class T2, class T3, class T4, class T5, class T6>   void debug(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {     cout << e1 <<   t  << e2 <<   t  << e3 <<   t  << e4 <<   t  << e5 <<   t           << e6 << endl;   }   template <class T>   void debug(vector<vector<T> > e, int row, int col) {     int i, j;     for (i = 0; i < row; i++) {       for (j = 0; j < col; j++) cout << e[i][j] <<    ;       cout << endl;     }     cout << endl;   }   template <class T>   void debug(vector<basic_string<T> > e, int row, int col) {     int i, j;     for (i = 0; i < row; i++) {       for (j = 0; j < col; j++) cout << e[i][j];       cout << endl;     }     cout << endl;   }   template <class T>   void debug(T e[110][110], int row, int col) {     int i, j;     for (i = 0; i < row; i++) {       for (j = 0; j < col; j++) cout << e[i][j] <<    ;       cout << endl;     }   }   template <class T>   string toString(T n) {     ostringstream oss;     oss << n;     oss.flush();     return oss.str();   }   int toInt(string s) {     int r = 0;     istringstream sin(s);     sin >> r;     return r;   }   bool isVowel(char ch) {     ch = tolower(ch);     if (ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u )       return true;     return false;   }   bool isUpper(char c) { return c >=  A  && c <=  Z ; }   bool isLower(char c) { return c >=  a  && c <=  z ; }   int n;   int arr[2000];   int main() {     int n;     while (cin >> n) {       int i;       for (i = 1; i <= n; i++) cin >> arr[i];       if (n % 2 == 0 || n == 1) {         cout <<  -1  << endl;         continue;       }       int ans = 0;       for (i = n - 1; i > 1; i -= 2) {         int ma = max(arr[i], arr[i + 1]);         arr[i] = 0;         arr[i + 1] = 0;         arr[i / 2] -= ma;         arr[i / 2] = max(0, arr[i / 2]);         ans += ma;       }       ans += arr[1];       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int read() {     char c = getchar();     int d = 0, f = 1;     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) f = -1;     for (; c >=  0  && c <=  9 ; d = d * 10 + c - 48, c = getchar())       ;     return d * f;   }   int r, d, n;   const int N = 100001;   struct xint {     int x, y, r;   } a[N];   int main() {     r = read(), d = read(), n = read();     int ans = 0;     for (int i = 1; i <= n; ++i) {       a[i].x = read(), a[i].y = read(), a[i].r = read();       int dis = a[i].x * a[i].x + a[i].y * a[i].y;       if (dis >= (r - d + a[i].r) * (r - d + a[i].r) && r >= a[i].r &&           dis <= (r - a[i].r) * (r - a[i].r))         ans++;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const double PI = acos(-1.0);   const double E = exp(1);   const double EPS = 1e-5;   const int maxn = 2000 + 5;   int n, m;   char mp[maxn][maxn] = {0};   int dx[] = {1, 0, 0, -1};   int dy[] = {0, 1, -1, 0};   int cnt = 0;   bool check(int x, int y) {     if (x < 0 || x >= m || y < 0 || y >= n || mp[x][y] !=  . ) return false;     return true;   }   void dfs(int x, int y) {     if (!check(x, y)) return;     int num = 0;     int dir;     for (int i = 0; i < (4); i++) {       int xx = x + dx[i];       int yy = y + dy[i];       if (check(xx, yy)) {         dir = i;         num++;       }     }     if (num == 1) {       if (dir == 0) {         mp[x][y] =  ^ ;         mp[x + dx[dir]][y + dy[dir]] =  v ;       } else if (dir == 1) {         mp[x][y] =  < ;         mp[x + dx[dir]][y + dy[dir]] =  > ;       } else if (dir == 2) {         mp[x][y] =  > ;         mp[x + dx[dir]][y + dy[dir]] =  < ;       } else if (dir == 3) {         mp[x][y] =  v ;         mp[x + dx[dir]][y + dy[dir]] =  ^ ;       }       cnt += 2;       for (int i = 0; i < (4); i++) dfs(x + dx[i] + dx[dir], y + dy[i] + dy[dir]);     }   }   int main(void) {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> m >> n;     for (int i = 0; i < (m); i++) cin >> mp[i];     for (int i = 0; i < (m); i++)       for (int j = 0; j < (n); j++) {         if (mp[i][j] ==  * ) cnt++;         dfs(i, j);       }     if (cnt != m * n)       puts( Not unique );     else       for (int i = 0; i < (m); i++) cout << mp[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using LL = long long;   using pss = pair<string, string>;   using pis = pair<int, string>;   const int maxn = 300300;   int n;   LL val[maxn], mask[maxn], sum;   signed main() {     while (cin >> n) {       sum = 0;       for (int i = 1; i <= n; i++) {         cin >> val[i] >> mask[i];         sum += val[i];       }       for (int i = 1; i <= n; i++) {         if (sum < 0) {           val[i] = -val[i];         }       }       LL ret = 0;       for (int k = 0; k < 62; k++) {         LL tot = 0;         for (int i = 1; i <= n; i++) {           if (mask[i] == (1LL << k)) {             tot += val[i];           }         }         if (tot > 0) ret |= (1LL << k);         for (int i = 1; i <= n; i++) {           if ((1LL << k) & mask[i]) {             mask[i] ^= (1LL << k);             if (tot > 0) {               val[i] = -val[i];             }           }         }       }       cout << ret << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   bool isprime(int n) {     for (int i = 2; i * i <= n; i++)       if (n % i == 0) return false;     return true;   }   int main() {     int n;     scanf( %d , &n);     int ans = 0;     if (n <= 3)       ans = 1;     else if (n % 2 == 0)       ans = 2;     else if (isprime(n))       ans = 1;     else if (isprime(n - 2))       ans = 2;     else       ans = 3;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool prime[100001];   bool ispoweroftwo(long long int x) { return x && !(x & (x - 1)); }   long long int xorcal(long long int n) {     if (n % 4 == 0) return n;     if (n % 4 == 1) return 1;     if (n % 4 == 2) return n + 1;     if (n % 4 == 3) return 0;   }   void sieve(long long int n) {     memset(prime, true, sizeof(prime));     prime[0] = false;     prime[1] = false;     for (long long int i = 2; i * i <= n; i++) {       if (prime[i] == true) {         for (long long int j = i * i; j <= n; j += i) {           prime[j] = false;         }       }     }   }   bool isprime(long long int n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 or n % 3 == 0) return false;     for (long long int i = 5; i * i <= n; i += 6) {       if (n % i == 0 or n % (i + 2) == 0) return false;     }     return true;   }   long long int modinverse(long long int a, long long int m) {     long long int m0 = m;     long long int y = 0, x = 1;     if (m == 1) return 0;     while (a > 1) {       long long int q = a / m;       long long int t = m;       m = a % m;       a = t;       t = y;       y = x - q * y;       x = t;     }     if (x < 0) x += m0;     return x;   }   long long int gcd(long long int a, long long int b) {     if (a == 0) {       return b;     }     return (b % a, a);   }   long long int power(long long int A, long long int B) {     if (A == 0) return 0;     if (B == 0) return 1;     long long int y;     if (B % 2 == 0) {       y = power(A, B / 2);       y = (y * y) % 1000000007;     } else {       y = A % 1000000007;       y = (y * power(A, B - 1) % 1000000007) % 1000000007;     }     return (long long int)((y + 1000000007) % 1000000007);   }   int main() {     ios_base::sync_with_stdio(false);     long long int n;     cin >> n;     string s;     cin >> s;     char f[10];     for (long long int i = 1; i < 10; i++) {       cin >> f[i];     }     long long int fl = 0;     for (long long int i = 0; i < n; i++) {       if (s[i] < f[s[i] -  0 ]) {         s[i] = f[s[i] -  0 ];         fl = 1;       } else if (fl == 1 and s[i] > f[s[i] -  0 ]) {         break;       }     }     cout << s <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   int n, m, pnt[N], vis[N];   long long vsum[N], esum[N], vcnt[N];   pair<pair<int, int>, pair<int, int> > edge[N];   vector<int> edge2[N];   int find(int x) {     if (x != pnt[x]) pnt[x] = find(pnt[x]);     return pnt[x];   }   void dfs(int u, int root) {     vis[u] = root;     vsum[u] = vcnt[u];     for (int i = 0; i < (int)(edge2[u]).size(); ++i) {       int v = edge2[u][i];       if (vis[v]) continue;       dfs(v, root);       vsum[u] += vsum[v];     }   }   void solve() {     int u, v, w;     m = n - 1;     memset(esum, 0, sizeof(esum));     for (int i = 0; i < n; ++i) {       vcnt[i] = 1;       pnt[i] = i;     }     for (int i = 0; i < m; ++i) {       scanf( %d%d%d , &u, &v, &w);       edge[i] = make_pair(make_pair(w, i), make_pair(u - 1, v - 1));     }     sort(edge, edge + m);     for (int i = 0; i < m; ++i) {       int j = i + 1;       for (; j < m; ++j)         if (edge[i].first.first != edge[j].first.first) break;       set<int> s;       for (int k = i; k < j; ++k) {         s.insert(find(edge[k].second.first));         s.insert(find(edge[k].second.second));       }       vector<int> vt((s).begin(), (s).end());       for (int k = 0; k < (int)(vt).size(); ++k) {         vsum[vt[k]] = vis[vt[k]] = 0;         edge2[vt[k]] = vector<int>();       }       for (int k = i; k < j; ++k) {         int p = find(edge[k].second.first);         int q = find(edge[k].second.second);         edge2[p].push_back(q);         edge2[q].push_back(p);       }       for (int k = 0; k < (int)(vt).size(); ++k) {         if (vis[vt[k]]) continue;         dfs(vt[k], vt[k] + 1);       }       for (int k = i; k < j; ++k) {         int p = find(edge[k].second.first);         int q = find(edge[k].second.second);         long long c = min(vsum[p], vsum[q]);         esum[edge[k].first.second] = 2 * c * (vsum[vis[p] - 1] - c);       }       for (int k = i; k < j; ++k) {         int p = find(edge[k].second.first);         int q = find(edge[k].second.second);         pnt[p] = q;         vcnt[q] += vcnt[p];       }       i = j - 1;     }     long long ans = 0;     int ct = 0;     for (int i = 0; i < m; ++i) {       if (ans < esum[i]) {         ans = esum[i];         ct = 1;       } else if (ans == esum[i]) {         ct++;       }     }     printf(          %I64d            %d n ,         ans, ct);     bool first = true;     for (int i = 0; i < m; ++i) {       if (esum[i] != ans) continue;       if (!first)         printf(   );       else         first = false;       printf( %d , i + 1);     }     puts(  );   }   int main() {     while (scanf( %d , &n) != EOF) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     bool v[60], h[60];     int a, b, n;     while (cin >> n) {       memset(v, false, sizeof(v));       memset(h, false, sizeof(h));       for (int i = 1; i <= n * n; i++) {         cin >> a >> b;         if (i == 1) {           cout << 1;           h[a] = true;           v[b] = true;         } else if (h[a] == false && v[b] == false) {           h[a] = true;           v[b] = true;           cout <<     << i;         }       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename S, typename T>   ostream &operator<<(ostream &os, pair<S, T> &m) {     os <<  [  << m.first <<     << m.second <<  ] ;     return os;   }   int n;   vector<int> v;   bool func(int k) {     int x = k - 1, y = n - 1;     for (int i = x; i >= 0; --i) {       if (2 * v[x] > v[y]) return false;       x--, y--;     }     return true;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     cin >> n;     v.resize(n);     for (int i = 0; i < n; ++i) cin >> v[i];     sort(begin(v), end(v));     int lo = 0, hi = n / 2, ans = 0;     while (lo <= hi) {       int mid = (lo + hi) / 2;       if (func(mid)) {         ans = mid;         lo = mid + 1;       } else         hi = mid - 1;     }     cout << n - ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   vector<int> v[1000010];   vector<int> vv;   bool visit[1000010] = {0};   int t, tt;   int res = 0;   void dfs(int x) {     visit[x] = 1;     t++;     for (int i = 0; i < v[x].size(); i++) {       int xi = v[x][i];       if (!visit[xi]) dfs(xi);     }   }   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 0; i < m; i++) {       int x, y;       scanf( %d%d , &x, &y);       x--, y--;       v[x].push_back(y);       v[y].push_back(x);     }     for (int i = 0; i < n; i++) {       if (!visit[i]) {         t = 0;         dfs(i);         vv.push_back(min(t, k));       }     }     if (k == 1) {       cout << max(0, (int)(vv.size() - 2));       return 0;     }     sort(vv.begin(), vv.end());     reverse(vv.begin(), vv.end());     tt = vv[0];     for (int i = 1; i < vv.size(); i++) {       if (tt)         tt += vv[i] - 2;       else {         tt++;         res++;       }     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4000 + 1;   const int sq = 100;   const long long int mod = 1e9 + 7;   const long long int inf = 4000 + 1;   unordered_map<long long int, int> dp;   int ps[N], n;   inline int get(int l, int r) { return ps[r] - ps[l]; }   int solve(int l, int r, int k, bool bol) {     if (r - l < k || l >= r || r > n || l < 0) return 0;     if (r - l == k && bol) return get(l, r);     if (r - l == k) return -get(l, r);     long long int key = l * N * sq * 2 + r * sq * 2 + k * 2 + bol;     if (dp.count(key)) return dp[key];     int ans, x, y;     if (bol) {       x = solve(l + k, r, k, 0) + get(l, l + k);       y = solve(l + k + 1, r, k + 1, 0) + get(l, l + k + 1);       ans = max(x, y);     } else {       x = solve(l, r - k, k, 1) - get(r - k, r);       y = solve(l, r - k - 1, k + 1, 1) - get(r - k - 1, r);       ans = min(x, y);     }     return dp[key] = ans;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     dp.rehash(16e6);     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int x;       scanf( %d , &x);       ps[i + 1] = ps[i] + x;     }     printf( %d  n , solve(0, n, 1, 1));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void enable_comma() {}   string tostring(char c);   string tostring(long long x);   template <class A, class B>   string tostring(pair<A, B> p);   template <class A>   string tostring(vector<A> v);   string tostring(char c) {     string s =   ;     s += c;     return s;   }   string tostring(string s) { return     + s +    ; }   string tostring(char *c) { return tostring((string)c); }   string tostring(long long x) {     if (x < 0) return  -  + tostring(-x);     if (x > 9)       return tostring(x / 10) + tostring(char( 0  + x % 10));     else       return tostring(char( 0  + x));   }   string tostring(int x) { return tostring((long long)x); }   string tostring(unsigned long long x) {     if (x > 9)       return tostring((long long)(x / 10)) + tostring(char( 0  + x % 10));     else       return tostring(char( 0  + x));   }   string tostring(unsigned x) { return tostring((long long)x); }   string tostring(double x) {     static char res[114];     sprintf(res,  %lf , x);     return tostring(res);   }   string tostring(long double x) { return tostring((double)x); }   template <class A, class B>   string tostring(pair<A, B> p) {     return  (  + tostring(p.first) +  ,  + tostring(p.second) +  ) ;   }   template <class A>   string tostring(vector<A> v) {     string res =  { ;     for (int i = (0); i <= ((int)v.size() - 1); i++) {       res += tostring(v[i]);       res += i == (int)v.size() - 1 ?  }  :  , ;     }     if (res ==  { ) res +=  } ;     return res;   }   template <class A>   string tostring(A a, int L, int R) {     string res =  { ;     for (int i = (L); i <= (R); i++) {       res += tostring(a[i]);       res += i == R ?  }  :  , ;     }     if (res ==  { ) res +=  } ;     return res;   }   string tostrings() { return   ; }   template <typename Head, typename... Tail>   string tostrings(Head H, Tail... T) {     return tostring(H) +     + tostrings(T...);   }   long long read() {     long long x = 0, f = 0;     char ch = getchar();     while (!isdigit(ch)) f = ch ==  - , ch = getchar();     while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();     return f ? -x : x;   }   template <class T>   void ckmax(T &x, const T y) {     if (x < y) x = y;   }   template <class T>   void ckmin(T &x, const T y) {     if (x > y) x = y;   }   const int N = 500005 * 2;   int n, m, q;   int p[N];   pair<int, int> e[N], t[N];   int ti[N];   namespace S {   int fa[N];   void init() { iota(fa, fa + N, 0); }   int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }   }  // namespace S   int cnt;   int fa[N], val[N];   vector<int> son[N];   int vis[N];   int I[N], O[N], ii[N];   int anc[N][20], dep[N];   int Time = 0;   void dfs(int x, int d = 1) {     vis[x] = 1;     anc[x][0] = fa[x];     for (int i = (1); i <= (19); i++) anc[x][i] = anc[anc[x][i - 1]][i - 1];     dep[x] = d;     I[x] = ++Time;     ii[Time] = x;     for (auto y : son[x]) dfs(y, d + 1);     O[x] = Time;   }   int pmax(int x, int y) { return p[x] > p[y] ? x : y; }   namespace seg {   const int S = N * 4;   int mx[S];   void pushup(int rt) {     int ls = rt << 1, rs = ls | 1;     mx[rt] = pmax(mx[ls], mx[rs]);   }   void build(int rt, int L, int R) {     if (L == R) {       mx[rt] = ii[L];       return;     }     int mid = (L + R) >> 1, ls = rt << 1, rs = ls | 1;     build(ls, L, mid);     build(rs, mid + 1, R);     pushup(rt);   }   int query(int rt, int L, int R, int xL, int xR) {     if (R < xL || L > xR) return 0;     if (xL <= L && R <= xR) return mx[rt];     int mid = (L + R) >> 1, ls = rt << 1, rs = ls | 1;     return pmax(query(ls, L, mid, xL, xR), query(rs, mid + 1, R, xL, xR));   }   void update(int rt, int L, int R, int x) {     if (L == R) {       mx[rt] = 0;       return;     }     int mid = (L + R) >> 1, ls = rt << 1, rs = ls | 1;     if (x <= mid)       update(ls, L, mid, x);     else       update(rs, mid + 1, R, x);     pushup(rt);   }   }  // namespace seg   void solve() {     n = read(), m = read(), q = read();     for (int i = (1); i <= (n); i++) p[i] = read();     for (int i = (1); i <= (m); i++) {       int x = read(), y = read();       e[i] = make_pair(x, y);       ti[i] = q + 1;     }     for (int i = (1); i <= (q); i++) {       int tp = read(), x = read();       t[i] = make_pair(tp, x);       if (tp == 2) ti[x] = i;     }     vector<int> id(m);     iota(id.begin(), id.end(), 1);     sort(id.begin(), id.end(), [&](int x, int y) { return ti[x] > ti[y]; });     cnt = n;     S::init();     for (auto i : id) {       int x = e[i].first, y = e[i].second;       int fx = S::getf(x), fy = S::getf(y);       if (fx != fy) {         int f = ++cnt;         fa[fx] = fa[fy] = S::fa[fx] = S::fa[fy] = f;         val[f] = ti[i];         son[f] = {fx, fy};         enable_comma();       }     }     for (int i = (cnt); i >= (1); i--)       if (!vis[i]) dfs(i);     seg::build(1, 1, cnt);     for (int i = (1); i <= (q); i++) {       int tp = t[i].first, x = t[i].second;       if (tp == 1) {         for (int j = (19); j >= (0); j--)           if (val[anc[x][j]] > i) x = anc[x][j];         int res = seg::query(1, 1, cnt, I[x], O[x]);         printf( %d n , p[res]);         p[res] = 0;         seg::update(1, 1, cnt, I[res]);       } else {       }     }   }   int main() {     int T = 1;     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void init() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   void skipInputLine() { cin.ignore(numeric_limits<streamsize>::max(),   n ); }   template <typename TYPE>   TYPE readNum() {     TYPE item;     cin >> item;     return item;   }   template <typename COLLECTION>   COLLECTION readNums(size_t count) {     COLLECTION collection;     typename COLLECTION::value_type item;     for (size_t i = 0; i < count; i++) {       cin >> item;       collection.insert(item);     }     return collection;   }   template <typename COLLECTION>   void writeNums(const COLLECTION &collection) {     for (auto item : collection) {       cout << item <<    ;     }     cout << endl;   }   struct Datum {     Datum() {}     Datum(uint32_t _x, uint32_t _count) : x(_x), count(_count) {}     uint32_t x;     uint32_t count;   };   void pass(const vector<Datum> &data, uint32_t k, vector<Datum> &ranges) {     uint32_t idx_left = 0, idx_right = 0;     uint32_t left_value = 0;     while (idx_left < data.size() && idx_right < data.size()) {       while (idx_right < data.size()) {         if (idx_right + 1 < data.size() &&             data[idx_right + 1].x <= data[idx_left].x + k) {           idx_right++;         } else {           break;         }       }       ranges.push_back(           Datum(data[idx_left].x, data[idx_right].count - left_value));       left_value = data[idx_left].count;       idx_left++;     }   }   uint32_t calc(uint32_t n, uint32_t k) {     map<uint32_t, uint32_t> xs;     for (size_t i = 0; i < n; i++) {       uint32_t x;       cin >> x;       xs[x]++;     }     skipInputLine();     skipInputLine();     uint32_t cumulative = 0;     vector<Datum> data(xs.size());     uint32_t index = 0;     for (map<uint32_t, uint32_t>::const_iterator it = xs.begin(); it != xs.end();          it++) {       cumulative += it->second;       data[index++] = Datum(it->first, cumulative);     }     xs.clear();     uint32_t width = data[data.size() - 1].x - data[0].x + 1;     if (width <= 2 * (k + 1)) {       return n;     }     vector<Datum> ranges;     ranges.reserve(2 * data.size());     pass(data, k, ranges);     data.clear();     map<uint32_t, uint32_t> best_sums;     uint32_t bs = 0;     for (size_t i = 0; i < ranges.size(); i++) {       const Datum &range = ranges[i];       bs = max(bs, range.count);       best_sums[range.x] = bs;     }     uint32_t best_sum = 0;     for (size_t i = 1; i < ranges.size(); i++) {       const Datum &range = ranges[i];       uint32_t sum = range.count;       int32_t x = int32_t(range.x) - int32_t(k + 1);       if (x < 0) {         x = 0;       }       map<uint32_t, uint32_t>::const_iterator it = best_sums.upper_bound(x);       if (it != best_sums.cbegin()) {         it--;         best_sum = max(best_sum, sum + it->second);       }     }     return best_sum;   }   int main() {     init();     uint32_t t = readNum<uint32_t>();     for (uint32_t i = 0; i < t; i++) {       uint32_t n, k;       cin >> n >> k;       uint32_t res = calc(n, k);       cout << res <<   n ;     }     cout << flush;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     register long long x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     return x * f;   }   const long long N = 1e5 + 10;   const long long M = N;   const long long INF = 1e16;   struct edge {     long long u, v, w, next;     bool operator<(const edge &x) const { return w < x.w; }   } e[M << 1];   long long head[N], cnt;   void add(long long u, long long v, long long w = 0) {     e[++cnt] = (edge){u, v, w, head[u]};     head[u] = cnt;   }   long long n, m, k, ans = INF;   long long dis[N], s[N], from[N];   bool vis[N];   struct node {     long long u, d;     bool operator<(const node &x) const { return d > x.d; }   };   priority_queue<node> q;   void Dij() {     for (long long i = 1; i <= n; i++) from[i] = vis[i] = 0, dis[i] = INF;     for (long long i = 1; i <= k; i++)       from[s[i]] = s[i], dis[s[i]] = 0, q.push((node){s[i], 0});     while (!q.empty()) {       long long u = q.top().u;       q.pop();       if (vis[u]) continue;       vis[u] = 1;       for (long long i = head[u]; i; i = e[i].next) {         long long v = e[i].v;         if (vis[v]) continue;         if (dis[v] > dis[u] + e[i].w) {           dis[v] = dis[u] + e[i].w;           from[v] = from[u];           q.push((node){v, dis[v]});         }       }     }   }   long long fa[N];   long long find(long long x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }   void krus() {     for (long long i = 1; i <= cnt; i++) {       e[i].w += dis[e[i].u] + dis[e[i].v];       e[i].u = from[e[i].u], e[i].v = from[e[i].v];     }     sort(e + 1, e + cnt + 1);     for (long long i = 1; i <= cnt; i++) {       long long fx = find(e[i].u), fy = find(e[i].v);       if (fx == fy) continue;       ans += e[i].w;       fa[fx] = fy;     }   }   signed main() {     n = read(), m = read();     for (long long i = 1; i <= n; i++) fa[i] = i;     for (long long i = 1; i <= m; i++) {       long long u = read(), v = read(), w = read();       add(u, v, w), add(v, u, w);     }     s[k = 1] = 1;     Dij();     k = read();     for (long long i = 1; i <= k; i++) {       s[i] = read();       ans = min(ans, dis[s[i]]);     }     Dij();     krus();     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string s;     int n;     while (cin >> n) {       cin >> s;       int gold = 0, cnt = 0, prev = 0, ans = 0;       for (auto ch : s) {         if (ch ==  G )           gold++, cnt++;         else           prev = cnt, cnt = 0;         ans = max(ans, prev + cnt + 1);       }       ans = min(ans, gold);       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> t;     while (t--) {       int a, b, c;       cin >> a >> b >> c;       if (c == 0) {         if (a <= b)           cout << 0 << endl;         else           cout << 1 << endl;         continue;       } else if (a + c <= b) {         cout << 0 << endl;         continue;       }       int res;       if (a == b) {         res = c / 2;         if (c % 2 == 1) res++;       } else if (a > b) {         if (b + c < a)           res = c + 1;         else if (b + c == a)           res = c;         else {           if ((b + c - a) % 2 == 1)             res = (b + c - a) / 2 + 1 + a - b;           else             res = (b + c - a) / 2 + a - b;         }       } else {         if ((a + c - b) % 2 == 1)           res = (a + c - b) / 2 + 1;         else           res = (a + c - b) / 2;       }       cout << res << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     std::cin >> n;     bool letters[26];     bool d = false;     int extra = 0;     for (int i = 0; i < 26; i++) {       letters[i] = true;     }     char action;     string input =   ;     for (int i = 0; i < n; i++) {       cin >> action;       cin >> input;       if (action ==  ! ) {         for (int j = 0; j < 26; j++) {           if (strchr(input.c_str(), j +  a ) == NULL) {             letters[j] = false;           }         }         if (d == true) extra++;       }       if (action ==  . ) {         for (int j = 0; j < 26; j++) {           if (strchr(input.c_str(), j +  a ) != NULL) {             letters[j] = false;           }         }       }       if (action ==  ? ) {         letters[input[0] -  a ] = false;         if (d == true && i < n - 1) extra++;       }       if (d == false) {         int truec = 0;         for (int j = 0; j < 26; j++) {           if (letters[j] == true) {             truec++;           }         }         if (truec == 1) {           d = true;         }       }     }     cout << extra << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 100010;   int n, m;   struct road {     int to, th;     bool f;   } in;   struct dot {     int come_road;     int come_point;     bool func;   } r[M];   vector<road> work[M];   vector<road> not_work[M];   vector<road> rw[M];   vector<road> rnw[M];   bool vis[M];   int d[M];   int broken_road[M];   bool inq[M];   int tot, u;   int input() {     int ans = 0;     char ch;     while (1) {       ch = getchar();       if (ch ==     || ch ==   n ) return ans;       ans *= 10;       ans += ch -  0 ;     }     return ans;   }   void output(int out) {     if (out == 0) {       putchar( 0 );       return;     }     char ans[10];     int cnt = 0;     while (out) {       ans[cnt++] = out % 10 +  0 ;       out /= 10;     }     for (int i = cnt - 1; i >= 0; i--) putchar(ans[i]);   }   int main() {     while (~scanf( %d%d , &n, &m)) {       tot = 0;       memset(broken_road, 0x3f, sizeof(broken_road));       memset(vis, 0, sizeof(vis));       memset(d, 0x3f, sizeof(d));       for (int i = 1; i <= n; i++) {         work[i].clear();         not_work[i].clear();         rw[i].clear();         rnw[i].clear();       }       int x, y, fun;       getchar();       for (int i = 0; i < m; i++) {         x = input();         y = input();         fun = input();         in.to = y;         in.th = i;         if (!fun) {           in.f = 0;           not_work[x].push_back(in);           in.to = x;           rnw[y].push_back(in);         } else {           tot++;           in.f = 1;           work[x].push_back(in);           in.to = x;           rw[y].push_back(in);         }       }       memset(inq, 0, sizeof(inq));       inq[1] = 1;       d[1] = 0;       queue<int> q;       q.push(1);       broken_road[1] = 0;       while (!q.empty()) {         u = q.front();         q.pop();         inq[u] = 0;         int v;         for (int i = 0; i < work[u].size(); i++) {           v = work[u][i].to;           if (d[u] + 1 < d[v] ||               (d[u] + 1 == d[v] && broken_road[u] < broken_road[v])) {             d[v] = d[u] + 1;             r[v].come_point = u;             r[v].come_road = work[u][i].th;             r[v].func = 1;             broken_road[v] = broken_road[u];             if (!inq[v]) {               q.push(v);               inq[v] = 1;             }           }         }         for (int i = 0; i < rw[u].size(); i++) {           v = rw[u][i].to;           if (d[u] + 1 < d[v] ||               (d[u] + 1 == d[v] && broken_road[u] < broken_road[v])) {             d[v] = d[u] + 1;             r[v].come_point = u;             r[v].come_road = rw[u][i].th;             r[v].func = 1;             broken_road[v] = broken_road[u];             if (!inq[v]) {               q.push(v);               inq[v] = 1;             }           }         }         for (int i = 0; i < not_work[u].size(); i++) {           v = not_work[u][i].to;           if (d[u] + 1 < d[v] ||               (d[u] + 1 == d[v] && broken_road[u] + 1 < broken_road[v])) {             d[v] = d[u] + 1;             r[v].come_point = u;             r[v].come_road = not_work[u][i].th;             r[v].func = 0;             broken_road[v] = broken_road[u] + 1;             if (!inq[v]) {               q.push(v);               inq[v] = 1;             }           }         }         for (int i = 0; i < rnw[u].size(); i++) {           v = rnw[u][i].to;           if (d[u] + 1 < d[v] ||               (d[u] + 1 == d[v] && broken_road[u] + 1 < broken_road[v])) {             d[v] = d[u] + 1;             r[v].come_point = u;             r[v].come_road = rnw[u][i].th;             r[v].func = 0;             broken_road[v] = broken_road[u] + 1;             if (!inq[v]) {               q.push(v);               inq[v] = 1;             }           }         }       }       int p = n;       while (p != 1) {         vis[r[p].come_road] = 1;         if (r[p].func)           tot--;         else           tot++;         p = r[p].come_point;       }       output(tot);       puts(  );       for (int i = 1; i <= n; i++) {         for (int j = 0; j < work[i].size(); j++) {           if (!vis[work[i][j].th]) {             output(i);             putchar(   );             output(work[i][j].to);             putchar(   );             puts( 0 );           }         }         for (int j = 0; j < not_work[i].size(); j++) {           if (vis[not_work[i][j].th]) {             output(i);             putchar(   );             output(not_work[i][j].to);             putchar(   );             puts( 1 );           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   void solve(int tc) {     int n, k;     cin >> n >> k;     string s;     cin >> s;     for (int i = k; i < s.size(); i++) {       if (s[i] ==  ? ) {         if (s[i - k] !=  ? ) {           s[i] = s[i - k];         }       } else {         if (s[i - k] ==  ? ) {           s[i - k] = s[i];         } else {           if (s[i] != s[i - k]) {             cout <<  NO  << endl;             return;           }         }       }     }     int count = 0;     int free = 0;     for (int i = 0; i < s.size(); i++) {       if (i >= k) {         if (s[i - k] ==  ? ) {           free--;         } else {           count -= ((s[i - k] ==  1 ) ? 1 : 0);         }       }       if (s[i] ==  ? ) {         free++;       } else {         count += ((s[i] ==  1 ) ? 1 : 0);       }       if (i >= k - 1) {         if (count > k / 2 || count + free < k / 2) {           cout <<  NO  << endl;           return;         }       }     }     cout <<  YES  << endl;   }   int main() {     int t;     cin >> t;     for (int i = 1; i <= t; i++) {       solve(i);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double pi = 3.14159265359;   inline long long add(long long a, long long b, long long m) {     if ((a + b) >= m) return (a + b) % m;     return a + b;   }   inline long long mul(long long a, long long b, long long m) {     if ((a * b) < m) return a * b;     return (a * b) % m;   }   long long power(long long x, long long y, long long m) {     long long res = 1;     x = x % m;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % m;       y = y >> 1;       x = (x * x) % m;     }     return res;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int main() {     ios_base::sync_with_stdio;     cin.tie(0);     cout.tie(0);     int t = 1, n;     cin >> t;     while (t--) {       string s;       cin >> n;       cin >> s;       string ans;       int i = 0;       for (; i < s.length(); i++) {         if (s[i] ==  0 ) {           ans.push_back(s[i]);         } else {           break;         }       }       string ans2;       for (int i = s.length() - 1; i > -1; i--) {         if (s[i] ==  1 ) {           ans2.push_back(s[i]);         } else {           break;         }       }       if (ans.length() + ans2.length() == s.length()) {         cout << s << endl;       } else         cout << ans <<  0  << ans2 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int N = 1e2 + 5;   int ans[N][N][N];   bool was[N][N][N];   vector<pair<int, int> > adj[N];   bool calc(int x, int y, int pr) {     if (was[x][y][pr]) return ans[x][y][pr];     was[x][y][pr] = 1;     for (auto to : adj[x]) {       if (to.second >= pr && !calc(y, to.first, to.second)) {         ans[x][y][pr] = 1;         break;       }     }     return ans[x][y][pr];   }   int main() {     ios ::sync_with_stdio(0), cin.tie(0);     int n, m;     cin >> n >> m;     for (int i = 1; i <= m; ++i) {       int u, v;       char c;       cin >> u >> v >> c;       adj[u].push_back({v, c -  a });     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         if (!calc(i, j, 0))           cout <<  B ;         else           cout <<  A ;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int v[2000];   int a, b, n, res;   int main() {     scanf( %d %d %d , &n, &a, &b);     for (int i = 0; i < n; i++) scanf( %d , &v[i]);     sort(v, v + n);     int res = v[b] - v[b - 1];     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxsize = 100000 + 9;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int T;     T = 1;     while (T--) {       long long i, j, k, m, n, flag = 0;       vector<int> v;       string s;       cin >> n >> s;       for (i = 0; i < n; i++) {         if (s[i] ==  2 ) {           v.push_back(2);         }         if (s[i] ==  3 ) {           v.push_back(3);         }         if (s[i] ==  4 ) {           v.push_back(3);           v.push_back(2);           v.push_back(2);         }         if (s[i] ==  5 ) {           v.push_back(5);         }         if (s[i] ==  6 ) {           v.push_back(5);           v.push_back(3);         }         if (s[i] ==  7 ) {           v.push_back(7);         }         if (s[i] ==  8 ) {           v.push_back(7);           v.push_back(2);           v.push_back(2);           v.push_back(2);         }         if (s[i] ==  9 ) {           v.push_back(7);           v.push_back(3);           v.push_back(3);           v.push_back(2);         }       }       sort(v.begin(), v.end());       for (i = v.size() - 1; i >= 0; i--) {         cout << v[i];       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, s = 0;     cin >> n;     if (n >= 1 && n <= 100) {       if (n == 1)         cout << 1;       else {         while (1) {           if (n == 2) {             s = s + 5;             break;           }           s = s + (n * 2) + (n - 2) * 2;           n = n - 1;         }         cout << s;       }     }   }


#include <bits/stdc++.h>   using namespace std;   using namespace std::chrono;   long long gcd(long long a, long long b) {     if (b == 0) {       return a;     } else {       return gcd(b, a % b);     }   }   void change(map<long long, long long> &m, long long n) {     long long i;     for (long long i = 1; i < n + 1; i++) {       if (m[i] == 1)         m.erase(i);       else         m[i]--;     }     return;   }   void solve() {     long long n, i, l, t;     cin >> n >> t;     string ans =   ;     map<long long, long long> m;     for (long long i = 0; i < t; i++) {       cin >> l;       m[l]++;       if ((long long)m.size() == n) {         ans +=  1 ;         change(m, n);       } else {         ans +=  0 ;       }     }     cout << ans;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     long long t = 1;     while (t--) {       solve();       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, a[101][101] = {0}, d = 0;     cin >> n;     for (long long int i = 1; i <= n; i++) {       long long int x1, x2, y1, y2;       cin >> x1 >> y1 >> x2 >> y2;       for (long long int j = x1; j <= x2; j++) {         for (long long int k = y1; k <= y2; k++) {           d++;         }       }     }     cout << d << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long getLL() {     long long ret = 0;     char c;     while (1) {       c = getchar();       if (c >=  0  && c <=  9 ) break;     }     while (1) {       ret = ret * 10 + c -  0 ;       c = getchar();       if (c <  0  || c >  9 ) break;     }     return ret;   }   int getInt() {     int ret = 0;     char c;     while (1) {       c = getchar();       if (c >=  0  && c <=  9 ) break;     }     while (1) {       ret = ret * 10 + c -  0 ;       c = getchar();       if (c <  0  || c >  9 ) break;     }     return ret;   }   const double eps = 1e-10;   int dcmp(double x) { return (x < -eps) ? -1 : (x > eps); }   struct Point {     double x, y;     int who;     Point() {}     Point(double _x, double _y, int _who) : x(_x), y(_y), who(_who){};     bool operator<(const Point &b) const {       if (dcmp(x - b.x) == 0) return y < b.y;       return x < b.x;     }     Point operator-(const Point &b) const { return Point(x - b.x, y - b.y, 0); }     double operator*(const Point &b) const { return x * b.y - y * b.x; }   } A[1020304];   int n, Stack[1020304], can[1020304], sameas[1020304];   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       double r, s;       scanf( %lf%lf , &r, &s);       A[i] = Point(10000.0 / r, 10000.0 / s, i);     }     sort(A + 1, A + n + 1);     int Top = 0;     vector<int> ans;     for (int i = 1; i <= n; ++i) {       if (i > 1 && dcmp(A[i].x - A[i - 1].x) == 0) continue;       while (Top > 1 &&              (dcmp((A[i] - A[Stack[Top]]) * (A[i] - A[Stack[Top - 1]])) > 0))         --Top;       Stack[++Top] = i;     }     for (int i = 1; i <= Top; ++i)       if (i == 1 || dcmp(A[Stack[i]].y - A[Stack[i - 1]].y) < 0)         can[Stack[i]] = true;     for (int i = 1; i <= n; ++i) {       if (dcmp(A[i].x - A[i - 1].x) == 0 && dcmp(A[i].y - A[i - 1].y) == 0)         can[i] |= can[i - 1];       if (can[i]) ans.push_back(A[i].who);     }     sort(ans.begin(), ans.end());     for (auto x : ans) printf( %d  , x);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 12;   int p[1 << N], q[1 << N], a[1 << N], pos[1 << N], ans[1 << N];   void do_swap(int x, int y) {     int val = (p[x] ^ q[x] ^ a[x]);     if (!val) {       return;     }     int idx = pos[a[x] ^ q[x]];     swap(pos[p[x]], pos[p[idx]]);     swap(p[x], p[idx]);     swap(q[y], q[idx]);     do_swap(idx, y);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, i, j, k, l = 0;     cin >> n;     n = (1 << n);     for (i = 0; i < n; i++) {       cin >> a[i];       l ^= a[i];       p[i] = q[i] = pos[i] = i;     }     if (l) {       cout <<  Fou ;       return 0;     }     for (i = 0; i + 1 < n; i++) {       do_swap(i, i + 1);     }     cout <<  Shi n ;     for (i = 0; i < n; i++) {       cout << p[i] <<    ;     }     cout <<   n ;     for (i = 0; i < n; i++) {       cout << q[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long r, d, n;   vector<vector<long long>> data;   const long double pi = 3.1415926535897932;   long long per(vector<long long> x, vector<long long> y) {     long long su = 0;     for (long long(i) = 0; (i) < (5); (i)++) {       su += x[i] * y[i];     }     return su;   }   long double pere(vector<long long> x, vector<long long> y) {     long double su = per(x, y);     su /= sqrt(per(x, x)) * sqrt(per(y, y));     su = acos(su);     return su;   }   int main() {     cin >> n;     data.resize(n);     for (long long(i) = 0; (i) < (n); (i)++) {       for (long long(j) = 0; (j) < (5); (j)++) {         data[i].resize(5);         cin >> data[i][j];       }     }     if (n > 32) {       cout << 0;       return 0;     }     vector<long long> result;     for (long long(i) = 0; (i) < (n); (i)++) {       bool mark = false;       for (long long(j) = 0; (j) < (n); (j)++) {         if (mark) {           break;         }         for (long long(l) = 0; (l) < (n); (l)++) {           if (i == j || j == l || i == l) {             continue;           }           vector<long long> x, y;           x.resize(5);           y.resize(5);           for (long long(h) = 0; (h) < (5); (h)++) {             x[h] = data[j][h] - data[i][h];             y[h] = data[l][h] - data[i][h];           }           if (pere(x, y) < pi / 2) {             mark = true;             break;           }         }       }       if (!mark) {         result.push_back(i);       }     }     cout << result.size() << endl;     for (long long(i) = 0; (i) < (result.size()); (i)++) {       cout << result[i] + 1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long ans = 0, turn = 1;       while (n > 0) {         if (n % 2) {           n--;           if (turn % 2) {             ans++;           }         } else {           if ((n / 2) % 2 == 0 && (n / 2) != 2) {             n--;             if (turn % 2) {               ans++;             }           } else {             if (turn % 2) {               ans += (n / 2);             }             n /= 2;           }         }         turn++;       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   using namespace chrono;   const int infinity = (int)1e9 + 42;   const int64_t llInfinity = (int64_t)1e18 + 256;   const int module = (int)1e9 + 7;   const long double eps = 1e-8;   mt19937_64 randGen(system_clock().now().time_since_epoch().count());   inline void raiseError(string errorCode) {     cerr <<  Error :   << errorCode << endl;     exit(42);   }   inline basic_string<int64_t> arith(int64_t n, int64_t base, int64_t step) {     basic_string<int64_t> res;     for (int64_t i = 0; i < n; i++) {       res += base;       base += step;     }     return res;   }   inline int64_t ask(basic_string<int64_t> b) {     cout << b.size();     for (int64_t i : b) {       cout <<     << i;     }     cout << endl;     int64_t x;     cin >> x;     if (x == -2) {       exit(42);     }     if (x == -1) {       exit(0);     }     return x;   }   inline void simpleGuess(int64_t l, int64_t r, int64_t knt, int64_t att) {     for (int64_t i = 0; i < att; i++) {       if (r - l + 1 <= knt) {         ask(arith(r - l + 1, l, 1));         exit(42);       }       int64_t delta = (r - l + 1 - knt) / (knt + 1);       int64_t mod = (r - l + 1 - knt) % (knt + 1);       int64_t val = l;       basic_string<int64_t> bounds;       bounds += l - 1;       for (int64_t i = 1; i <= knt; i++) {         int64_t add = delta + (i <= mod);         val += add;         bounds += val;         val++;       }       bounds += r + 1;       int64_t q = ask(bounds.substr(1, knt));       l = bounds[q] + 1;       r = bounds[q + 1] - 1;     }     exit(42);   }   signed main() {     ios_base::sync_with_stdio(false);     if (ask({204761410474}) == 0) {       if (ask({2046}) == 0) {         if (ask({6}) == 0) {           if (ask({2}) == 0) {             ask({1});           } else {             ask(arith(3, 3, 1));           }         } else {           int64_t q = ask({14, 30, 62, 126, 254, 510, 1022});           int64_t w = (8 << q) - 1;           ask(arith(w, w, 1));         }       } else {         int64_t base1 = 20468427, step1 = 100020001;         int64_t w1 = ask(arith(2047, base1, step1));         if (w1 == 0) {           int64_t base2 = 16382, step2 = 10001;           int64_t w2 =               ask(basic_string<int64_t>{4094, 8190} + arith(2045, base2, step2));           if (w2 <= 2) {             int64_t abase = (2048 << w2) - 1;             ask(arith(abase, abase, 1));           } else {             ask(arith(10000, base2 + (w2 - 3) * step2 + 1, 1));           }         } else {           simpleGuess(base1 + (w1 - 1) * step1 + 1, base1 + w1 * step1 - 1, 10000,                       2);         }       }     } else {       simpleGuess(204761410475, 10004205361450474, 10000, 4);     }     return 42;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, i, f = 0, se = 0, so = 0, sume = 0, sumo = 0, c = 0, x, y;     cin >> n;     long long int a[n];     for (i = 0; i < n; i++) cin >> a[i];     for (i = 0; i < n; i++) {       if (i % 2)         so += a[i];       else         se += a[i];     }     for (i = 0; i < n; i++) {       if (i % 2) {         x = so - sumo - a[i] + sume;         y = se - sume + sumo;         sumo += a[i];       } else {         x = so - sumo + sume;         y = se - sume + sumo - a[i];         sume += a[i];       }       if (x == y) c++;     }     cout << c <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int dividers[60];   int main() {     long long int n, k;     cin >> n >> k;     if (k == 1) {       cout <<  Yes ;       return 0;     }     if (k == 2 && n == 1) {       cout <<  Yes ;       return 0;     }     if (n <= k) {       cout <<  No ;       return 0;     }     if (k > 50) {       cout <<  No ;       return 0;     }     long long int c = n + 1;     for (int i = 2; i <= k; i++) {       if (c % i != 0) {         cout <<  No ;         return 0;       }     }     cout <<  Yes ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e3 + 5;   char a[MAX][MAX];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     int n, m, k;     cin >> n >> m >> k;     for (int i = (1); i <= (n); ++i)       for (int j = (1); j <= (m); ++j) cin >> a[i][j];     int ans = 0;     for (int i = (1); i <= (n); ++i) {       int cnt = 0;       for (int j = (1); j <= (m); ++j) {         if (a[i][j] ==  . )           ++cnt;         else {           if (cnt >= k) ans += cnt - k + 1;           cnt = 0;         }       }       if (cnt >= k) ans += cnt - k + 1;     }     if (k > 1) {       for (int i = (1); i <= (m); ++i) {         int cnt = 0;         for (int j = (1); j <= (n); ++j) {           if (a[j][i] ==  . )             ++cnt;           else {             if (cnt >= k) ans += cnt - k + 1;             cnt = 0;           }         }         if (cnt >= k) ans += cnt - k + 1;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = 10000000;   int n, m, x, y, a, b;   int vabs(int t) { return (t > 0) ? t : (-t); }   int calc(int cx, int cy) {     int dx = vabs(cx - x);     int dy = vabs(cy - y);     if (dx == 0 && dy == 0) return 0;     if (dx % a != 0 || dy % b != 0) return oo;     int ca = dx / a, cb = dy / b;     if (vabs(ca - cb) % 2 != 0) return oo;     if (ca < cb && cx - a < 1 && cx + a > n) return oo;     if (ca > cb && cy - b < 1 && cy + b > m) return oo;     return max(ca, cb);   }   int main() {     while (cin >> n) {       cin >> m >> x >> y >> a >> b;       int ans = oo;       ans = min(ans, calc(1, 1));       ans = min(ans, calc(1, m));       ans = min(ans, calc(n, 1));       ans = min(ans, calc(n, m));       if (ans == oo)         cout <<  Poor Inna and pony!  << endl;       else         cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e10;   void solve() {     long long n, c;     cin >> n >> c;     long long a[n], b[n];     for (long long i = 0; i < n; i++) {       cin >> a[i] >> b[i];     }     long long low = -1, high = -1;     for (long long i = 1; i <= 100000; i++) {       long long cnt = 0;       for (long long j = 0; j < n; j++) {         cnt++;         cnt += (a[j] * i) / b[j];       }       if (cnt > c) break;       if (cnt < c) continue;       if (cnt == c) {         if (low == -1) low = i;         high = i;       }     }     if (high == -1)       cout << 0;     else       cout << high - low + 1;   }   int32_t main() {     long long t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, x;     std::cin >> n >> x;     std::vector<int> data;     int counter = 0;     for (int i = 0; i != n; i++) {       int temp;       std::cin >> temp;       data.push_back(temp);     }     std::sort(data.begin(), data.end());     if (std::find(data.begin(), data.end(), x) != data.end()) {       auto it = std::find(data.begin(), data.end(), x);       counter++;       data.erase(it);     }     for (int i = 0; i < x; i++) {       if (std::find(data.begin(), data.end(), i) == data.end()) {         data.push_back(i);         counter++;       }     }     std::cout << counter << std::endl;     return 0;   }


#include <bits/stdc++.h>      #define ll long long   #define sz(x) ((int) (x).size())   #define all(x) (x).begin(), (x).end()   #define vi vector<int>   #define pii pair<int, int>   #define rep(i, a, b) for(int i = (a); i < (b); i++)   using namespace std;   template<typename T>   using minpq = priority_queue<T, vector<T>, greater<T>>;      struct pt {       ll x, y;       pt(ll x = 0, ll y = 0) : x(x), y(y) {}       pt operator-(const pt &o) const {           return pt(x - o.x, y - o.y);       }       ll cross(const pt &o) const {           return x * o.y - y * o.x;       }       bool operator<(const pt &o) const {           return cross(o) > 0;       }   };      struct dsu {       vi a;       dsu(int n) {           a.assign(n, -1);       }       int trace(int x) {           return a[x] < 0 ? x : a[x] = trace(a[x]);       }       void join(int x, int y) {           if((x = trace(x)) == (y = trace(y))) return;           if(a[x] > a[y]) swap(x, y);           a[x] += a[y];           a[y] = x;       }   };      int main() {       ios::sync_with_stdio(false);       cin.tie(0);       int n;       cin >> n;       vi a(n + 1);       vector<pt> p(n + 1);       rep(i, 1, n + 1) {           cin >> p[i].x >> p[i].y >> a[i];       }       int idx = -1;       rep(i, 1, n + 1) {           if(a[i] != i && (idx == -1 || p[i].y < p[idx].y)) {               idx = i;           }       }       if(idx == -1) {           cout << 0 <<   n ;           return 0;       }       vi balls;       rep(i, 1, n + 1) {           if(i != idx && a[i] != i) {               balls.push_back(i);           }       }       sort(all(balls), [&](int i, int j) {           return p[i] - p[idx] < p[j] - p[idx];       });       dsu D(n + 1);       rep(i, 1, n + 1) D.join(i, a[i]);       int k = sz(balls);       vector<pii> ops;       auto pass = [&](int i, int j) {           swap(a[i], a[j]);           ops.push_back({i, j});       };       rep(i, 0, k - 1) {           if(D.trace(balls[i]) != D.trace(balls[i + 1])) {               pass(balls[i], balls[i + 1]);               D.join(balls[i], balls[i + 1]);           }       }       while(a[idx] != idx) {           pass(idx, a[idx]);       }          cout << sz(ops) <<   n ;       for(auto &pa : ops) {           cout << pa.first <<     << pa.second <<   n ;       }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100110;   const int maxm = 100010;   char s[maxn][5];   long long sum[maxm * 4][5][11];   int n;   void init() {     for (int i = 1; i < maxn; i++)       for (int z = 2; z <= 6; z++) {         int tp = i % (2 * (z - 1));         if (tp == 0)           s[i][z - 2] = 2;         else if (tp <= z)           s[i][z - 2] = tp;         else           s[i][z - 2] = 2 * z - tp;       }   }   void build(int pos, int l, int r) {     for (int i = 2; i <= 6; i++)       for (int j = 0; j <= 10; j++) sum[pos][i - 2][j] = 0;     if (l == r) return;     int mid = (l + r) >> 1;     build((pos << 1), l, mid);     build(((pos << 1) | 1), mid + 1, r);   }   void insert(int pos, int l, int r, int i, int a) {     if (i < l || i > r) return;     if (l == r) {       for (int z = 2; z <= 6; z++)         for (int j = 0; j <= 10; j++)           sum[pos][z - 2][j] = (long long)s[i + j][z - 2] * a;       return;     }     int mid = (l + r) >> 1;     insert((pos << 1), l, mid, i, a);     insert(((pos << 1) | 1), mid + 1, r, i, a);     for (int z = 2; z <= 6; z++)       for (int j = 0; j <= 10; j++)         sum[pos][z - 2][j] =             sum[(pos << 1)][z - 2][j] + sum[((pos << 1) | 1)][z - 2][j];   }   long long search(int pos, int l, int r, int x, int y, int z, int offset) {     if (y < l || r < x) return 0;     if (x <= l && r <= y) return sum[pos][z - 2][offset];     int mid = (l + r) >> 1;     return search((pos << 1), l, mid, x, y, z, offset) +            search(((pos << 1) | 1), mid + 1, r, x, y, z, offset);   }   int main() {     init();     while (~scanf( %d , &n)) {       build(1, 1, n);       for (int i = 1; i <= n; i++) {         int a;         scanf( %d , &a);         insert(1, 1, n, i, a);       }       int m;       scanf( %d , &m);       while (m-- > 0) {         int t, p, v, l, r, z;         scanf( %d , &t);         if (t == 1) {           scanf( %d%d , &p, &v);           insert(1, 1, n, p, v);         } else {           scanf( %d%d%d , &l, &r, &z);           int offset = 0;           if (s[l][z - 2] <= s[l + 1][z - 2]) {             if (s[l][z - 2] == 1)               offset = 0;             else               offset = z - s[l][z - 2] + z - 1;           } else {             offset = s[l][z - 2] - 1;           }           cout << search(1, 1, n, l, r, z, offset) << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     string s;     cin >> n;     getline(cin, s);     for (int i = 0; i < n; ++i) {       getline(cin, s);       int len = s.length();       if (len < 5)         cout <<  OMG>.< I don t know!  << endl;       else {         string prefix(s.substr(0, 5));         string suffix(s.substr(len - 5, 5));         if (prefix ==  miao.  && suffix ==  lala. )           cout <<  OMG>.< I don t know!  << endl;         else if (prefix ==  miao. )           cout <<  Rainbow s  << endl;         else if (suffix ==  lala. )           cout <<  Freda s  << endl;         else           cout <<  OMG>.< I don t know!  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int inf = 2e9 + 5;   const int N = 1e5;   int cost[26];   map<long long, long long> m[26];   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     for (int i = 0; i < 26; i++) cin >> cost[i];     string s;     cin >> s;     long long n = s.length(), sum = 0;     long long ans = 0;     for (int i = 0; i < n; i++) {       ans += m[s[i] -  a ][sum];       sum += cost[s[i] -  a ];       m[s[i] -  a ][sum] += 1;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void prv(vector<int> v) {     int n = v.size();     for (int i = 0; i < n; i++) cout << v[i] <<    ;   }   void swap(long long int &a, long long int &b) {     int tm = a;     a = b;     b = tm;   }   bool comp(const pair<int, int> &p1, const pair<int, int> &p2) {     if (p1.first > p2.first)       return 1;     else       return 0;   }   const int N = 3e5 + 5;   void findPrefix(string pattern, int m, int prefArray[]) {     int length = 0;     prefArray[0] = 0;     for (int i = 1; i < m; i++) {       if (pattern[i] == pattern[length]) {         length++;         prefArray[i] = length;       } else {         if (length != 0) {           length = prefArray[length - 1];           i--;         } else           prefArray[i] = 0;       }     }   }   int kmpPattSearch(string mainString, string pattern) {     int n, m, i = 0, j = 0, fr = 0;     n = mainString.size();     m = pattern.size();     int prefixArray[m];     findPrefix(pattern, m, prefixArray);     while (i < n) {       if (mainString[i] == pattern[j]) {         i++;         j++;       }       if (j == m) {         fr++;         j = prefixArray[j - 1];       } else if (i < n && pattern[j] != mainString[i]) {         if (j != 0)           j = prefixArray[j - 1];         else           i++;       }     }     return fr;   }   string crts(int d, int m, int y) {     string t;     t.clear();     while (y != 0) {       t.push_back(y % 10 +  0 );       y = y / 10;     }     t.push_back( - );     while (m != 0) {       t.push_back(m % 10 +  0 );       m = m / 10;     }     if (t.size() == 6) t.push_back( 0 );     t.push_back( - );     while (d != 0) {       t.push_back(d % 10 +  0 );       d = d / 10;     }     if (t.size() == 9) t.push_back( 0 );     reverse((t).begin(), (t).end());     return t;   }   void solve() {     string s, ans;     cin >> s;     int mx = 0;     for (int i = 2013; i < 2016; ++i) {       for (int j = 1; j <= 12; j++) {         int k;         if (j == 2)           k = 28;         else if (j == 1 || j == 3 || j == 5 || j == 7 || j == 8 || j == 10 ||                  j == 12)           k = 31;         else           k = 30;         for (int l = 1; l <= k; l++) {           string t;           int frq;           t = crts(l, j, i);           frq = kmpPattSearch(s, t);           if (frq > mx) {             mx = frq;             ans = t;           }         }       }     }     cout << ans;   }   signed int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       solve();       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   template <class T>   T SQR(const T &a) {     return a * a;   }   void run() {     char str[1000100];     scanf( %s , str);     int n = strlen(str);     int m;     cin >> m;     int A[10001];     int B[10001];     map<int, int> res;     char pat[100];     for (int i = (0), ei = (m); i < ei; i++) {       scanf(  %s , pat);       int mask = 0;       for (int j = (0), ej = (strlen(pat)); j < ej; j++)         mask |= 1 << (pat[j] -  a );       B[i] = A[i] = mask;     }     sort(B, B + m);     int Pos[26] = {0};     for (int i = (0), ei = (26); i < ei; i++) Pos[i] = -1;     int mask = 0, curr;     pair<int, int> P[26];     str[n] =  z  + 1;     for (int i = (0), ei = (n); i < ei; i++) {       mask |= 1 << (str[i] -  a );       Pos[str[i] -  a ] = i;       for (int j = (0), ej = (26); j < ej; j++) {         P[j].first = Pos[j];         P[j].second = j;       }       sort(P, P + 26);       curr = mask;       int next = 1 << (str[i + 1] -  a );       if (binary_search(B, B + m, mask) && (mask & next) == 0) res[mask]++;       for (int j = (0), ej = (25); j < ej; j++)         if (P[j].first != -1) {           curr &= ~(1 << P[j].second);           if (binary_search(B, B + m, curr) && (curr & next) == 0) res[curr]++;         }     }     for (int i = (0), ei = (m); i < ei; i++) printf( %d n , res[A[i]]);   }   int main() {     run();     return 0;   }


#include <bits/stdc++.h>       #define st first   #define nd second   #define pb push_back   #define BOOST ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);   #define PI 3.14159265359       using namespace std;       typedef long long ll;   constexpr ll MOD = 1000000007, sup = 29;   constexpr ll MOD_1 = 1234567891, sup_1 = 1009;   constexpr ll MOD_2 = 1000000009, sup_2 = 107;   constexpr int MXN = 200000+10, CZAPA = (1<<18), INF = 1000000000;      int n, m;      struct tree{    pair<int, int> tab[2*CZAPA+10];       void add(int leaf, int x){     int v = leaf + CZAPA;     tab[v] = {x, x};     v /= 2;     while(v >= 1){      tab[v].st = min(tab[2*v].st, tab[2*v+1].st);      tab[v].nd = max(tab[2*v].nd, tab[2*v+1].nd);      v /= 2;     }    }       pair<int, int> query(int a, int b, int v, int p, int q){     if(q < a || b < p)      return {INF, -INF};     if(p >= a && q <= b)      return tab[v];     int mid = (p+q)/2;     pair<int, int> x = query(a, b, 2*v, p, mid), y = query(a, b, 2*v+1, mid+1, q);     return {min(x.st, y.st), max(x.nd, y.nd)};    }       pair<int, int> query(int a, int b){     return query(a+CZAPA, b+CZAPA, 1, CZAPA, 2*CZAPA-1);    }   };      tree T;      int tab[MXN];         int main(){    BOOST;       int t;    cin>> t;    T.add(0, 0);    while(t--){     string s;      cin>> n >> m >> s;     for(int i=1; i<=n; i++){      tab[i] = tab[i-1];      if(s[i-1] ==  - )       tab[i]--;      else       tab[i]++;      T.add(i, tab[i]);      //cout<< tab[i] <<    ;     }     //cout<<   n ;     while(m--){      int a, b;      cin>> a >> b;      pair<int, int> x = T.query(0, a-1);      int sum = tab[b] - tab[a-1];      pair<int, int> y = T.query(b+1, n);      if(y.st == INF){       cout<< abs(x.st-x.nd)+1 <<   n ;       continue;      }      y.st -= sum;      y.nd -= sum;      pair<int, int> ans = {min(x.st, y.st), max(x.nd, y.nd)};      cout<< abs(ans.st-ans.nd)+1 <<   n ;      //cout<< x.st <<   ~   << x.nd <<  ,   << y.st <<   ~   << y.nd <<   n ;     }    }               return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int s = 0, w = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar();     return s * w;   }   const int inf = 2e9 + 7;   double eps = 1e-6;   const int mod = 1e9 + 9;   const int N = 1e3 + 10;   const double pi = acos(-1.0);   long long power(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = res * a % mod;       a = a * a % mod;       b >>= 1;     }     return res;   }   long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }   long long exgcd(long long a, long long b, long long& x, long long& y) {     long long c;     return !b ? (x = 1, y = 0, a)               : (c = exgcd(b, a % b, y, x), y -= (a / b) * x, c);   }   long long INV(long long a) {     long long x, y;     exgcd(a, mod, x, y);     x %= mod;     if (x < 0) x += mod;     return x;   }   int n;   long long dp[N][N][11];   struct AC {     int tr[N][26], tot;     int fail[N];     int tr2[N][26];     int Len[N];     bool key[N];     int insert(char s[]) {       int u = 0;       for (int i = 1; s[i]; i++) {         int c = s[i] -  a ;         if (!tr[u][c]) tr[u][c] = ++tot;         Len[tr[u][c]] = Len[u] + 1;         u = tr[u][c];       }       key[u] = 1;       return u;     }     void build() {       for (int i = 0; i <= tot; ++i)         for (int j = 0; j <= 3; ++j) tr2[i][j] = tr[i][j];       queue<int> q;       for (int i = 0; i <= 25; ++i)         if (tr[0][i]) q.push(tr[0][i]);       while (!q.empty()) {         int u = q.front();         q.pop();         for (int i = 0; i <= 25; ++i) {           if (tr[u][i]) {             fail[tr[u][i]] = tr[fail[u]][i], q.push(tr[u][i]);           } else             tr[u][i] = tr[fail[u]][i];         }       }     }     int dfs(int u, int len, int k) {       bool ok = key[u];       int now = u;       while (1) {         now = fail[now];         if (Len[now] >= k && key[now]) {           ok |= 1;           break;         }         if (now == 0) break;       }       if (ok) k = 0;       if (!u) return 0;       if (dp[u][len][k] != -1) return dp[u][len][k];       if (len == n) {         bool ok = key[u];         int now = u;         while (1) {           now = fail[now];           if (Len[now] >= k && key[now]) {             ok |= 1;             break;           }           if (now == 0) break;         }         return dp[u][len][k] = ok;       }       long long& res = dp[u][len][k] = 0;       for (int i = 0; i <= 3; i++) {         if (tr2[u][i])           res += dfs(tr2[u][i], len + 1, k + 1);         else {           int now = u;           while (1) {             now = fail[now];             if (Len[now] >= k && tr2[now][i]) {               res += dfs(tr2[now][i], len + 1, k + 1);               break;             }             if (now == 0) break;           }         }       }       res %= mod;       return res;     }     int work() {       memset(dp, -1, sizeof(dp));       long long res = 0;       for (int i = 0; i <= 3; i++) res += dfs(tr[0][i], 1, 1);       res %= mod;       return res;     }   } ac;   char s[N];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int m;     cin >> n >> m;     map<char, char> mp;     mp[ A ] =  a ;     mp[ C ] =  b ;     mp[ T ] =  c ;     mp[ G ] =  d ;     for (int i = 1; i <= m; ++i) {       cin >> s + 1;       for (int i = 1; i <= strlen(s + 1); ++i) s[i] = mp[s[i]];       ac.insert(s);     }     ac.build();     cout << ac.work() <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     long long to, cost;     edge() {}     edge(long long a, long long b) { to = a, cost = b; }   };   const long long dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};   const long long mod = 1000000007;   struct mint {     long long x = 0;     mint(long long y = 0) {       x = y;       if (x < 0 || x >= mod) x = (x % mod + mod) % mod;     }     mint(const mint& ope) { x = ope.x; }     mint operator-() { return mint(-x); }     mint operator+(const mint& ope) { return mint(x) += ope; }     mint operator-(const mint& ope) { return mint(x) -= ope; }     mint operator*(const mint& ope) { return mint(x) *= ope; }     mint operator/(const mint& ope) { return mint(x) /= ope; }     mint& operator+=(const mint& ope) {       x += ope.x;       if (x >= mod) x -= mod;       return *this;     }     mint& operator-=(const mint& ope) {       x += mod - ope.x;       if (x >= mod) x -= mod;       return *this;     }     mint& operator*=(const mint& ope) {       x *= ope.x, x %= mod;       return *this;     }     mint& operator/=(const mint& ope) {       long long n = mod - 2;       mint mul = ope;       while (n) {         if (n & 1) *this *= mul;         mul *= mul;         n >>= 1;       }       return *this;     }     mint inverse() { return mint(1) / *this; }     bool operator==(const mint& ope) { return x == ope.x; }     bool operator!=(const mint& ope) { return x != ope.x; }   };   mint modpow(mint a, long long n) {     if (n == 0) return mint(1);     if (n % 2)       return a * modpow(a, n - 1);     else       return modpow(a * a, n / 2);   }   istream& operator>>(istream& is, mint& ope) {     long long t;     is >> t, ope.x = t;     return is;   }   ostream& operator<<(ostream& os, mint& ope) { return os << ope.x; }   ostream& operator<<(ostream& os, const mint& ope) { return os << ope.x; }   vector<mint> fact, fact_inv;   void make_fact(int n) {     fact.resize(n + 1), fact_inv.resize(n + 1);     fact[0] = mint(1);     for (long long i = (1); (i) <= (n); (i)++) fact[i] = fact[i - 1] * mint(i);     fact_inv[n] = fact[n].inverse();     for (long long i = (n - 1); (i) >= (0); (i)--)       fact_inv[i] = fact_inv[i + 1] * mint(i + 1);   }   mint comb(long long n, long long k) {     if (n < 0 || k < 0 || n < k) return mint(0);     return fact[n] * fact_inv[k] * fact_inv[n - k];   }   mint perm(long long n, long long k) { return comb(n, k) * fact[k]; }   vector<int> prime;   void make_prime(int n) {     prime.resize(n + 1);     for (long long i = (2); (i) <= (n); (i)++) {       if (prime[i]) continue;       for (int j = i; j <= n; j += i) prime[j] = i;     }   }   bool exceed(long long x, long long y, long long m) { return x >= m / y + 1; }   void mark() { cout <<  *  << endl; }   void yes() { cout <<  YES  << endl; }   void no() { cout <<  NO  << endl; }   long long sgn(long long x) {     if (x > 0) return 1;     if (x < 0) return -1;     return 0;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   long long digitnum(long long x, long long b = 10) {     long long ret = 0;     for (; x; x /= b) ret++;     return ret;   }   long long digitsum(long long x, long long b = 10) {     long long ret = 0;     for (; x; x /= b) ret += x % b;     return ret;   }   string lltos(long long x) {     string ret;     for (; x; x /= 10) ret += x % 10 +  0 ;     reverse(ret.begin(), ret.end());     return ret;   }   long long stoll(string& s) {     long long ret = 0;     for (auto c : s) ret *= 10, ret += c -  0 ;     return ret;   }   template <typename T>   void uniq(T& vec) {     sort(vec.begin(), vec.end());     vec.erase(unique(vec.begin(), vec.end()), vec.end());   }   template <class S, class T>   pair<S, T>& operator+=(pair<S, T>& s, const pair<S, T>& t) {     s.first += t.first, s.second += t.second;     return s;   }   template <class S, class T>   pair<S, T>& operator-=(pair<S, T>& s, const pair<S, T>& t) {     s.first -= t.first, s.second -= t.second;     return s;   }   template <class S, class T>   pair<S, T> operator+(const pair<S, T>& s, const pair<S, T>& t) {     return pair<S, T>(s.first + t.first, s.second + t.second);   }   template <class S, class T>   pair<S, T> operator-(const pair<S, T>& s, const pair<S, T>& t) {     return pair<S, T>(s.first - t.first, s.second - t.second);   }   template <typename T>   ostream& operator<<(ostream& os, vector<T>& vec) {     for (int i = 0; i < vec.size(); i++)       os << vec[i] << (i + 1 == vec.size() ?    :    );     return os;   }   template <typename T>   ostream& operator<<(ostream& os, deque<T>& deq) {     for (int i = 0; i < deq.size(); i++)       os << deq[i] << (i + 1 == deq.size() ?    :    );     return os;   }   template <typename T, typename U>   ostream& operator<<(ostream& os, pair<T, U>& pair_var) {     os <<  (  << pair_var.first <<  ,   << pair_var.second <<  ) ;     return os;   }   template <typename T, typename U>   ostream& operator<<(ostream& os, const pair<T, U>& pair_var) {     os <<  (  << pair_var.first <<  ,   << pair_var.second <<  ) ;     return os;   }   template <typename T, typename U>   ostream& operator<<(ostream& os, map<T, U>& map_var) {     for (typename map<T, U>::iterator itr = map_var.begin(); itr != map_var.end();          itr++) {       os <<  (  << itr->first <<  ,   << itr->second <<  ) ;       itr++;       if (itr != map_var.end()) os <<  , ;       itr--;     }     return os;   }   template <typename T>   ostream& operator<<(ostream& os, set<T>& set_var) {     for (typename set<T>::iterator itr = set_var.begin(); itr != set_var.end();          itr++) {       os << *itr;       ++itr;       if (itr != set_var.end()) os <<    ;       itr--;     }     return os;   }   template <typename T>   ostream& operator<<(ostream& os, multiset<T>& set_var) {     for (typename multiset<T>::iterator itr = set_var.begin();          itr != set_var.end(); itr++) {       os << *itr;       ++itr;       if (itr != set_var.end()) os <<    ;       itr--;     }     return os;   }   template <typename T>   void outa(T a[], long long s, long long t) {     for (long long i = s; i <= t; i++) {       cout << a[i];       if (i < t) cout <<    ;     }     cout << endl;   }   void dump_func() { cout << endl; }   template <class Head, class... Tail>   void dump_func(Head&& head, Tail&&... tail) {     cout << head;     if (sizeof...(Tail) > 0) cout <<    ;     dump_func(std::move(tail)...);   }   long long n;   long long a[150005];   bool check(long long i) {     if (i <= 0 || i >= n) return true;     if (i % 2)       return a[i] < a[i + 1];     else       return a[i] > a[i + 1];   }   int main(void) {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (long long i = (1); (i) <= (n); (i)++) cin >> a[i];     set<long long> S;     set<pair<long long, long long> > ans;     for (long long i = (1); (i) <= (n - 1); (i)++)       if (!check(i)) S.insert(i);     if (((long long)(S).size()) > 4) {       dump_func(0);       return 0;     }     set<long long> T;     for (auto x : S) {       T.insert(x);       T.insert(x + 1);     }     for (auto x : T) {       for (long long i = (1); (i) <= (n); (i)++) {         if (i == x) continue;         swap(a[i], a[x]);         bool flag = true;         for (auto y : S) flag &= check(y);         for (long long j = (-1); (j) <= (1); (j)++)           flag &= check(i + j), flag &= check(x + j);         if (flag) {           long long u = i, v = x;           if (u > v) swap(u, v);           ans.insert(pair<long long, long long>(u, v));         }         swap(a[i], a[x]);       }     }     dump_func(((long long)(ans).size()));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3fLL;   const double PI = acos(-1.0);   const double EPS = 1e-8;   const int MOD = 998244353;   template <typename T>   void cmin(T &x, T y) {     if (y < x) x = y;   }   template <typename T>   void cmax(T &x, T y) {     if (y > x) x = y;   }   long long qpow(long long x, long long n, long long mod = MOD) {     long long res = 1;     while (n) {       if (n & 1) res = res * x % mod;       x = x * x % mod;       n >>= 1;     }     return res;   }   namespace Solver {   void InitOnce() {}   int n, m, k;   int u[200005];   int v[200005];   int deg[200005];   vector<pair<int, int> > G[200005];   void Read() {     int res = scanf( %d%d%d , &n, &m, &k);     if (res == -1) exit(0);     for (int i = 1; i <= n; ++i) G[i].clear();     for (int i = 1; i <= m; ++i) {       scanf( %d%d , &u[i], &v[i]);       G[u[i]].emplace_back(v[i], i);       G[v[i]].emplace_back(u[i], i);     }     for (int i = 1; i <= n; ++i) deg[i] = G[i].size();   }   bool vis[200005];   int ans[200005];   bool inQ[200005];   queue<int> Q;   void delEdge(int u, int v, int id) {     if (vis[id]) return;     vis[id] = 1;     --deg[u];     --deg[v];     if (inQ[v] == 0 && deg[v] < k) {       Q.push(v);       inQ[v] = 1;     }   }   void delEdge2(int u, int v, int id) {     if (vis[id]) return;     vis[id] = 1;     --deg[u];     --deg[v];     if (inQ[u] == 0 && deg[u] < k) {       Q.push(u);       inQ[u] = 1;     }     if (inQ[v] == 0 && deg[v] < k) {       Q.push(v);       inQ[v] = 1;     }   }   void Solve() {     memset(vis, 0, sizeof(vis));     memset(ans, 0, sizeof(ans));     memset(inQ, 0, sizeof(inQ));     for (int i = 1; i <= n; ++i) {       if (deg[i] < k) {         Q.push(i);         inQ[i] = 1;       }     }     int cur = n;     while (!Q.empty()) {       int u = Q.front();       Q.pop();       --cur;       for (pair<int, int> &p : G[u]) {         int v = p.first, id = p.second;         delEdge(u, v, id);       }     }     for (int i = 1; i <= n; ++i) {       if (deg[i]) assert(deg[i] >= k);     }     ans[m] = cur;     for (int i = m - 1; i >= 0; --i) {       int de = i + 1;       delEdge2(u[de], v[de], de);       while (!Q.empty()) {         int u = Q.front();         Q.pop();         --cur;         for (pair<int, int> &p : G[u]) {           int v = p.first, id = p.second;           delEdge(u, v, id);         }       }       ans[i] = cur;     }     for (int i = 1; i <= n; ++i) {       assert(deg[i] == 0);       assert(inQ[i] == 1);     }     for (int i = 1; i <= m; ++i) assert(vis[i] == 1);     for (int i = 1; i <= m; ++i) printf( %d n , ans[i]);     return;   }   }  // namespace Solver   int main() {     Solver::InitOnce();     while (true) {       Solver::Read();       Solver::Solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long INF = 1e18;   const string nl =   n ;   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(nullptr);     long long n, k;     cin >> n >> k;     vector<long long> a(n);     for (auto& i : a) {       cin >> i;     }     vector<long long> pref(n);     long long sum = 0, ans = 0;     bool ok = 0;     map<long long, long long> cnt;     cnt[0] = 1;     for (long long i = 0; i < n; ++i) {       if (a[i] > k) {         ++sum;       } else if (a[i] < k) {         --sum;       }       if (a[i] == k) {         ok = 1;       }       if (ok) {         ans += cnt[sum] + cnt[sum - 1];       } else {         ++cnt[sum];       }     }     cout << ans << nl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, cc = 0;     cin >> a >> b >> c;     if (a < 0 || b < 2 || c < 4)       cout << 0 << endl;     else {       while (a >= 1 && b >= 2 && c >= 4) {         cc++;         a--;         b -= 2;         c -= 4;       }       cout << cc * 7 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> v[100001];   int main() {     int n;     cin >> n;     for (long long i = 1; i < n; i++) {       long long a, b;       cin >> a >> b;       v[a].push_back(b);       v[b].push_back(a);     }     for (long long i = 1; i <= n; i++) {       if (v[i].size() == 2) {         cout <<  NO ;         return 0;       }     }     cout <<  YES ;   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( -Ofast )   #pragma GCC optimize( trapv )   #pragma GCC target(         sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native )   #pragma GCC optimize( -ffast-math )   #pragma GCC optimize( -funroll-loops )   #pragma GCC optimize( -funroll-all-loops,-fpeel-loops,-funswitch-loops )   const int N = 1e3 + 40;   const int MAXK = 12;   const long long int INF = 1000000000000000000;   const int RANDOM =       chrono::high_resolution_clock::now().time_since_epoch().count();   long long int expo_pow(long long int x, long long int y) {     if (y == 0) return 1;     y = y % ((1000000007) - 1);     x %= (1000000007);     if (y == 0) y = (1000000007) - 1;     long long int res = 1;     while (y) {       if (y & 1) res = (res * x) % (1000000007);       x = (x * x) % (1000000007);       y >>= 1;     }     return res;   }   long long int modInverse(long long int a, long long int m = (1000000007)) {     return expo_pow(a, m - 2);   }   void solve() {     int n;     cin >> n;     vector<vector<long long int>> cnt(n + 2);     map<int, int> ones;     map<int, int> t;     for (long long int i = (1); i < (n + 1); i++) {       int tp, w;       cin >> tp >> w;       if (w) ones[tp]++;       t[tp]++;     }     for (auto x : t) {       cnt[x.second].push_back(ones[x.first]);     }     priority_queue<pair<long long int, long long int>> pq;     long long int ans = 0;     long long int fo = 0;     set<long long int> second;     for (long long int i = (1); i < (n + 1); i++) {       if (1) second.insert(i);       for (long long int j = (0); j < (cnt[i].size()); j++) {         pq.push(make_pair(cnt[i][j], i));       }     }     while (second.size()) {       if (pq.empty()) break;       int x = pq.top().second;       auto it = second.upper_bound(x);       if (it == second.begin()) {         pq.pop();         continue;       }       it--;       ans += *it;       fo += min(*it, pq.top().first);       pq.pop();       second.erase(*it);     }     cout << ans <<     << fo <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int T = 1;     cin >> T;     while (T--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   long n, k, iters, p[10000], q[1000], rp[1000], ar[200000], ar1[20000];   long ans;   bool not_same() {     for (int i = 1; i <= n; i++)       if (ar[i] != q[i]) return true;     return false;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> p[i];     for (int i = 1; i <= n; i++) cin >> q[i];     for (int i = 1; i <= n; i++) rp[p[i]] = i;     long tf = 0;     for (int i = 1; i <= n; i++)       if (rp[i] != p[i]) tf = 1;     ans = 0;     for (int i = 1; i <= n; i++) ar[i] = i;     iters = 0;     while (not_same()) {       ++iters;       for (int i = 1; i <= n; i++) ar1[i] = ar[p[i]];       for (int i = 1; i <= n; i++) ar[i] = ar1[i];       if (iters > k) break;     }     if (iters <= k && iters % 2 == k % 2) ans = 1;     for (int i = 1; i <= n; i++) ar[i] = i;     iters = 0;     while (not_same()) {       ++iters;       for (int i = 1; i <= n; i++) ar1[i] = ar[rp[i]];       for (int i = 1; i <= n; i++) ar[i] = ar1[i];       if (iters > k) break;     }     if (iters <= k && iters % 2 == k % 2) ans = 1;     long fl = 0;     for (int i = 1; i <= n; i++)       if (q[i] != i) fl = 1;     if (fl == 0) ans = 0;     if (tf == 0 && k > 1) ans = 0;     if (ans)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     cin.get();     cin.get();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b, ans = 6;   int len[15], last[15];   void dfs(int x, int va) {     if (x == 7) {       ans = min(ans, va);       return;     }     for (int i = 1; i <= va; i++) {       if (last[i] >= len[x]) {         last[i] -= len[x];         dfs(x + 1, va);         last[i] += len[x];       }     }     last[va + 1] = n - len[x];     dfs(x + 1, va + 1);   }   int main() {     scanf( %d%d%d , &n, &a, &b);     for (int i = 1; i <= 4; i++) len[i] = a;     for (int i = 5; i <= 6; i++) len[i] = b;     dfs(1, 0);     printf( %d , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<string> s;     set<string> ss;     string a, b, res;     int n, cnt_a = 0, cnt_b = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a;       ss.insert(a);       s.push_back(a);     }     if (ss.size() == 1)       cout << s[0] << endl;     else {       sort(s.begin(), s.end());       a = s[0];       b = s[n - 1];       for (int i = 0; i < n; i++) {         if (s[i].compare(a) == 0)           cnt_a++;         else           cnt_b++;       }       res = (cnt_a > cnt_b) ? a : b;       cout << res << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     int x, y;     while (t--) {       cin >> x >> y;       if (y <= sqrt(x)) {         if (x % 2 == 0 && y % 2 == 0)           cout <<  YES  << endl;         else if (x % 2 == 1 && y % 2 == 1)           cout <<  YES  << endl;         else           cout <<  NO  << endl;       } else         cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000003;   long double pi = 3.1415926536;   const int mx = 1000003;   long long gcd(long long x, long long y) {     if (y == 0) return x;     return gcd(y, x % y);   }   long long bp(long long a, long long b) {     long long ans = 1;     while (b > 0) {       if (b & 1) ans = (ans * a) % mod;       a = (a * a) % mod;       b >>= 1;     }     return ans;   }   long long p(long long n) {     for (long long i = 2; i * i <= n; i++) {       if (n % i == 0) {         return n / i;       }     }     return 1;   }   void solve() {     long long a, d, x, i = 1;     cin >> a >> d;     x = a++;     while ((a % i) <= d) {       x = (a - (a % i));       i *= 10;     }     cout << x - 1;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int a, b;     cin >> a >> b;     if (a == 0 && b == 0) {       cout << 1;     }     if (a == 0 && b != 0) {       for (int i = b + 1; i > 0; i--) {         cout << i <<    ;       }     }     if (b == 0 && a != 0) {       for (int i = 1; i <= a + 1; i++) {         cout << i <<    ;       }     }     int z = a + b + 1;     if (a != 0 && b != 0) {       a++;       b++;       int m = 0;       int n = 0;       while (a--) {         m = z - a;         cout << m <<    ;       }       while (b > 1) {         b--;         cout << b <<    ;       }     }     return 0;   }