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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, a[102400], i, j, o = 0, l = 0, ma, k = 0; n   cin >> n; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 1; i < n; i++) { n     if (a[i] + k < a[i - 1]) { n       o += a[i - 1] - (a[i] + k); n       k += a[i - 1] - (a[i] + k); n       a[i] = a[i - 1]; n     } else { n       if (a[i] < a[i - 1]) { n         k = a[i - 1] - a[i]; n         a[i] = a[i - 1]; n       } else n         k = 0; n     } n   } n   cout << o << endl; n } n


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


#include <bits/stdc++.h> n using namespace std; n long long qitaceruts(long long num) { n   long long n = 0, d, p = 1; n   while (num > 0) { n     d = num % 10; n     num = num / 10; n     if (d != 0) { n       n = (d * p) + n; n       p *= 10; n     } n   } n   return n; n } n int main() { n   long long a, b, c1, c2; n   cin >> a >> b; n   c1 = a + b; n   a = qitaceruts(a); n   b = qitaceruts(b); n   c1 = qitaceruts(c1); n   if (a + b == c1) { n     cout <<  YES n ; n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s1, s2; n int main() { n   int n, i, j; n   scanf( %d , &n); n   cin >> s1 >> s2; n   int ans = 0; n   for (i = 0; i < (n / 2); i++) { n     j = n - i - 1; n     map<char, int> mp; n     mp[s1[i]]++; n     mp[s1[j]]++; n     mp[s2[i]]++; n     mp[s2[j]]++; n     int len = mp.size(); n     if (len == 4) n       ans += 2; n     else if (len == 3) n       ans += 1 + (s1[i] == s1[j]); n     else if (len == 2) n       ans += (mp[s1[i]] != 2); n   } n   if ((n % 2) && (s1[n / 2] != s2[n / 2])) ans++; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cout.tie(0); n   int n; n   cin >> n; n   vector<bool> in(n + 1); n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     in[a] = true; n   } n   vector<int> b(n); n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n   } n   int ind = 0; n   int next_ = 1; n   vector<int> c = b; n   for (; next_ <= n; ind++) { n     int t = ind - n - 1; n     if (t >= 0 && t < n) { n       in[b[ind - n - 1]] = true; n     } n     if (c[ind % n] == next_) { n       next_++; n       c[ind % n] = -1; n       continue; n     } n     if (c[ind % n] == -1 && in[next_]) { n       next_++; n       continue; n     } n     c[ind % n] = -1; n     if (ind - next_ + 1 < n) { n       next_ = 1; n     } n   } n   cout << ind - n << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long L[500005]; n char ch[500005]; n class segtree { n  public: n   long long seg[(1 << 19) * 2]; n   long long lazy[(1 << 19) * 2]; n   void lazy_evaluate(int k) { n     if (k * 2 + 2 >= (1 << 19) * 2) return; n     lazy[k * 2 + 2] += lazy[k]; n     lazy[k * 2 + 1] += lazy[k]; n     seg[k * 2 + 2] += lazy[k]; n     seg[k * 2 + 1] += lazy[k]; n     lazy[k] = 0; n   } n   long long update(int beg, int end, int idx, int lb, int ub, long long num) { n     if (ub < beg || end < lb) { n       return seg[idx]; n     } n     if (beg <= lb && ub <= end) { n       lazy[idx] += num; n       seg[idx] += num; n       return seg[idx]; n     } n     if (lazy[idx]) { n       lazy_evaluate(idx); n     } n     return seg[idx] = n                min(update(beg, end, idx * 2 + 1, lb, (lb + ub) / 2, num), n                    update(beg, end, idx * 2 + 2, (lb + ub) / 2 + 1, ub, num)); n   } n   long long query(int beg, int end, int idx, int lb, int ub) { n     if (ub < beg || end < lb) { n       return 1500000000000000000LL; n     } n     if (beg <= lb && ub <= end) { n       return seg[idx]; n     } n     if (lazy[idx]) { n       lazy_evaluate(idx); n     } n     return min(query(beg, end, idx * 2 + 1, lb, (lb + ub) / 2), n                query(beg, end, idx * 2 + 2, (lb + ub) / 2 + 1, ub)); n   } n } kaede; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lld , &L[i]); n   scanf( %s , &ch); n   long long cur = 0; n   long long ans = 0; n   bool ok = 0; n   for (int i = 0; i < n; i++) { n     if (ch[i] ==  L ) n       ans += L[i]; n     else if (ch[i] ==  W ) n       ans += L[i] * 3LL; n     else n       ans += L[i] * 5LL; n   } n   vector<long long> vec; n   int lasw, lasg; n   for (int i = 0; i < n; i++) { n     if (ch[i] ==  G ) { n       cur += L[i]; n       kaede.update(i, i, 0, 0, (1 << 19) - 1, cur); n       vec.push_back(L[i]); n       lasg = i; n       continue; n     } n     if (ch[i] ==  W ) { n       cur += L[i]; n       lasw = i; n       ok = 1; n       kaede.update(i, i, 0, 0, (1 << 19) - 1, cur); n       vec.push_back(L[i]); n       continue; n     } n     if (ch[i] ==  L ) { n       if (cur >= L[i]) { n         cur -= L[i]; n         kaede.update(i, i, 0, 0, (1 << 19) - 1, cur); n         vec.push_back(L[i]); n         continue; n       } else { n         long long need = L[i] - cur; n         if (ok) { n           ans += 3LL * need; n           vec[lasw] += need; n         } else { n           ans += 5LL * need; n           vec[lasg] += need; n         } n         cur = 0; n         vec.push_back(L[i]); n         kaede.update(i, i, 0, 0, (1 << 19) - 1, cur); n         continue; n       } n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     if (ch[i] ==  L ) continue; n     if (ch[i] ==  W ) continue; n     long long mn = kaede.query(i, n - 1, 0, 0, (1 << 19) - 1); n     if (2LL * vec[i] <= mn) { n       kaede.update(i, n - 1, 0, 0, (1 << 19) - 1, -2LL * vec[i]); n       ans -= 4LL * vec[i]; n     } else { n       kaede.update(i, n - 1, 0, 0, (1 << 19) - 1, -mn); n       ans -= 2LL * mn; n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     if (ch[i] ==  L ) continue; n     if (ch[i] ==  G ) continue; n     long long mn = kaede.query(i, n - 1, 0, 0, (1 << 19) - 1); n     if (2LL * vec[i] <= mn) { n       kaede.update(i, n - 1, 0, 0, (1 << 19) - 1, -2LL * vec[i]); n       ans -= 2LL * vec[i]; n     } else { n       kaede.update(i, n - 1, 0, 0, (1 << 19) - 1, -mn); n       ans -= 1LL * mn; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int INF = 0x3f3f3f3f; n const int MAXN = 3e6 + 3; n bool appear[MAXN]; n int ans[MAXN]; n signed main() { n   int cnt = 1; n   int n, x; n   scanf( %d%d , &n, &x); n   int now = 0; n   appear[0] = true; n   appear[x] = true; n   for (int i = 1; i < (1 << n); ++i) { n     if (appear[i]) continue; n     appear[i ^ x] = true; n     ans[cnt++] = i; n   } n   printf( %d n , cnt - 1); n   for (int i = 1; i < cnt; ++i) printf( %d  , ans[i] ^ ans[i - 1]); n } n


#include <bits/stdc++.h> n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n void JIZZ(string output =   ) { n   cout << output; n   exit(0); n } n const long double PI = 3.14159265358979323846264338327950288; n const long double eps = 1e-13; n const long long mod = 1e9 + 7; n long long a[200005], pre[200005]; n vector<int> good; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   long long k; n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= n; ++i) pre[i] = pre[i - 1] + a[i]; n   for (int i = 1; i <= n; ++i) n     if (a[i] > 1) good.push_back(i); n   good.push_back(n + 1); n   if (0) { n     long long ans = 0; n     for (int i = 1; i <= n; ++i) { n       long long s = 0, p = 1; n       for (int j = i; j <= n; ++j) { n         if (((long double)p) * a[j] > 2e18) break; n         p *= a[j], s += a[j]; n         if (s * k == p) cout << i <<     << j << endl, ++ans; n       } n     } n     cout << ans << endl; n   } n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     long long p = a[i], s = a[i]; n     int now = i; n     auto ptr = upper_bound(good.begin(), good.end(), i); n     if (s * k <= p && p <= (s + *ptr - now) * k && p % k == 0) ++ans; n     while (p <= 2e18) { n       if (*ptr == n + 1) break; n       if (((long double)p) * a[*ptr] > 2e18) break; n       p *= a[*ptr]; n       s += pre[*ptr] - pre[now]; n       now = *ptr; n       ++ptr; n       if (s * k <= p && p <= (s + *ptr - now - 1) * k && p % k == 0) ++ans; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long N, Q; n long long A[1100100], a, b; n vector<pair<long long, long long> > qu[1100100]; n long long sol[1100100], dp[1100100]; n long long solve(long long I, long long b) { n   if (I > N) return 0; n   long long ret = A[I]; n   return ret += solve(I + b, b); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> N; n   for (long long I = 1; I <= N; I++) cin >> A[I]; n   cin >> Q; n   for (long long q = 1; q <= Q; q++) { n     cin >> a >> b; n     qu[b].push_back(pair<long long, long long>(a, q)); n   } n   for (long long b = 1; b <= N; b++) { n     if (b < 300) { n       for (long long I = N; I >= 1; I--) { n         dp[I] = A[I]; n         if (I + b <= N) dp[I] += dp[I + b]; n       } n       for (typeof((qu[b]).begin()) it = (qu[b]).begin(); it != (qu[b]).end(); n            it++) n         sol[it->second] = dp[it->first]; n     } else { n       for (typeof((qu[b]).begin()) it = (qu[b]).begin(); it != (qu[b]).end(); n            it++) n         sol[it->second] = solve(it->first, b); n     } n   } n   for (long long q = 1; q <= Q; q++) cout << sol[q] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100000 + 10; n int n, m; n pair<int, int> s[MAX]; n pair<long long, long long> ans; n void update(long long a, long long b) { n   if (b <= 0) return; n   if (a > ans.first) { n     ans.first = a; n     ans.second = b; n   } else if (a == ans.first) n     ans.second += b; n } n void work1() { n   int i, mi = n, mm = 0; n   long long num = 0; n   for ((i) = (1); (i) <= (m); ++(i)) n     if (s[i].first == 0) { n       ++num; n       mi = min(mi, s[i].second); n       mm = max(mm, s[i].second); n     } n   long long num_mi = 0, num_mm = 0; n   for ((i) = (1); (i) <= (m); ++(i)) { n     if (s[i].first == 0 && s[i].second == mi) ++num_mi; n     if (s[i].first == 0 && s[i].second == mm) ++num_mm; n   } n   if (mm == mi) { n     update(0, num * (num - 1) * (num - 2) / 6); n     return; n   } n   num -= num_mi; n   num -= num_mm; n   update((mm - mi) * 2, (num_mi - 1) * num_mi / 2 * num_mm + n                             (num_mm - 1) * num_mm / 2 * num_mi + n                             num * num_mi * num_mm); n } n int cmp(pair<int, int> a, pair<int, int> b) { n   if (a.second != b.second) n     return a.second < b.second; n   else n     return a.first < b.first; n } n void work2() { n   sort(s + 1, s + m + 1, cmp); n   int i; n   long long num0 = 0; n   for ((i) = (1); (i) <= (m); ++(i)) n     if (s[i].first == 0) ++num0; n   int largb = 0, numb = 0, tmp = num0; n   for ((i) = (1); (i) <= (m); ++(i)) n     if (s[i].first == 0) { n       --tmp; n       if (largb != s[i].second) n         update(2LL * n + 2 * largb - 2 * s[i].second, (long long)tmp * numb); n     } else if (s[i].second == largb) n       ++numb; n     else if (s[i].second > largb) n       largb = s[i].second, numb = 1; n   int smallb = n + 1; n   numb = 0; n   tmp = num0; n   for (i = m; i >= 1; --i) n     if (s[i].first == 0) { n       --tmp; n       if (smallb != s[i].second) n         update(2LL * n - 2 * smallb + 2 * s[i].second, (long long)tmp * numb); n     } else if (s[i].second == smallb) n       ++numb; n     else if (s[i].second < smallb) n       smallb = s[i].second, numb = 1; n   map<int, int> hash; n   for ((i) = (1); (i) <= (m); ++(i)) n     if (s[i].first == 0) hash[s[i].second]++; n   tmp = 0; n   for ((i) = (1); (i) <= (m); ++(i)) { n     if (s[i].first == 0) n       ++tmp; n     else { n       long long u = hash[s[i].second]; n       long long now = (long long)tmp * (num0 - tmp) + n                       (long long)u * (u - 1) / 2 + (tmp - u) * u; n       update(2LL * n, now); n     } n   } n } n void work() { n   work1(); n   work2(); n } n int main() { n   int i; n   scanf( %d%d , &n, &m); n   for ((i) = (1); (i) <= (m); ++(i)) scanf( %d%d , &s[i].first, &s[i].second); n   work(); n   for ((i) = (1); (i) <= (m); ++(i)) s[i].first ^= 1; n   work(); n   cout << ans.second << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n const int inf = 1e9 + 7; n int main() { n   int n; n   scanf( %d , &n); n   while (n--) { n     int x; n     scanf( %d , &x); n     if (x <= 3 || x == 5 || x == 7 || x == 11) n       puts( -1 ); n     else { n       int ans = 0, tg = x & 3; n       if (!tg) ans = max(ans, x / 4); n       if (tg == 2) ans = max(ans, 1 + (x - 6) / 4); n       if (tg == 1) ans = max(ans, 1 + (x - 9) / 4); n       if (tg == 3) ans = max(ans, 2 + (x - 15) / 4); n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = ((int)1e5) + 100; n int arr[Maxn], n; n map<int, int> num, numL, numR; n long long Solve(int a[]) { n   int l = 0, r = n - 1; n   while (a[l] == a[r] && l < r) ++l, --r; n   if (l >= r) return n * 1ll * (n + 1) / 2; n   for (int(i) = (l); (i) <= (r); ++(i)) num[a[i]] += 1; n   int odds = 0, key = -1; n   for (auto it = num.begin(); it != num.end(); ++it) n     if ((it->second) & 1) odds += 1, key = it->first; n   if (odds > 1) return 0; n   int oks = 0, l2 = l - 1; n   while (oks < num.size()) { n     numL[a[++l2]] += 1; n     int _a = numL[a[l2]] << 1; n     int _b = _a - 2, _c = num[a[l2]]; n     if (_a >= _c && _b < _c) ++oks; n   } n   oks = 0; n   int r2 = r + 1; n   while (oks < num.size()) { n     numR[a[--r2]] += 1; n     int _a = numR[a[r2]] << 1; n     int _b = _a - 2, _c = num[a[r2]]; n     if (_a >= _c && _b < _c) ++oks; n   } n   int mid = (r + l) >> 1; n   if (l2 <= mid && (!(n & 1) || a[mid] == key)) { n     int g, h; n     if (n & 1) n       g = mid - 1, h = mid + 1; n     else n       g = mid, h = mid + 1; n     while (a[g] == a[h]) --g, ++h; n     l2 = g, r2 = h; n   } n   long long sum = 0; n   sum += (l + 1) * 1ll * (n - l2); n   sum += (n - r) * 1ll * (r2 + 1); n   sum -= (l + 1) * 1ll * (n - r); n   return sum; n } n int main() { n   iostream::sync_with_stdio(false); n   cin >> n; n   for (int(i) = (0); (i) <= (n - 1); ++(i)) cin >> arr[i]; n   cout << Solve(arr) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const long long INF = 1e18; n const int M = 1e9 + 7; n const int maxn = 1e6; n int b[maxn], c[maxn]; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout.setf(ios::fixed); n   cout.precision(10); n   int m, k, n, s; n   cin >> m >> k >> n >> s; n   vector<int> a(m); n   for (int i = 0; i < m; ++i) { n     cin >> a[i]; n   } n   for (int i = 0; i < s; ++i) { n     int x; n     cin >> x; n     ++b[x]; n   } n   int res = inf; n   int l = 0, r = 0, cnt = 0; n   int L = 0, R = 0; n   while (r < m) { n     while (r < m && cnt < s) { n       if (c[a[r]] < b[a[r]]) { n         ++cnt; n       } n       ++c[a[r]]; n       ++r; n     } n     while (l < m && cnt == s) { n       int tl = l / k, tr = (r - 1) / k; n       int tot; n       if (tl == tr) { n         tot = 0; n       } else { n         tot = r - tl * k - k; n       } n       if (tot < res) { n         L = l; n         R = r - 1; n         res = tot; n       } n       c[a[l]]--; n       if (c[a[l]] < b[a[l]]) { n         --cnt; n       } n       ++l; n     } n   } n   if ((m - res) / k < n) { n     cout << -1 << endl; n   } else { n     cout << res << endl; n     for (int i = R; i >= L - L % k; --i) { n       if (res == 0) break; n       if (!b[a[i]]) { n         cout << i + 1 <<    ; n         --res; n       } else { n         --b[a[i]]; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100007; n const int maxv = 100000; n long long fac[maxn], fac2[maxn]; n map<pair<int, int>, int> save; n int cnt; n long long mypow(int base, int n) { n   if (n == 0) return 1; n   long long t = mypow(base, n / 2); n   t = (t * t) % 1000000007; n   if (n & 1) n     return (t * base) % 1000000007; n   else n     return t; n } n long long C(int n, int k) { n   long long res = fac[n] * fac2[k] % 1000000007; n   res = res * fac2[n - k] % 1000000007; n   return res; n } n long long solve(int f, int n) { n   if (f == 1) return (n == 1); n   if (f == n) return 1; n   if (f > n) return 0; n   if (save.count(pair<int, int>(f, n))) return save[pair<int, int>(f, n)]; n   long long res = 0; n   for (int i = 2; i * i <= n; i++) n     if (n % i == 0) { n       int x = i; n       res = (res + solve(f, n / x)) % 1000000007; n       if (i * i == n) continue; n       x = n / i; n       res = (res + solve(f, n / x)) % 1000000007; n     } n   save[pair<int, int>(f, n)] = n       (C(n - 1, f - 1) - res + 1000000007) % 1000000007; n   return save[pair<int, int>(f, n)]; n } n int main() { n   fac[0] = 1; n   for (int i = 1, _c = maxv; i <= _c; i++) fac[i] = fac[i - 1] * i % 1000000007; n   for (int v = 0, _c = maxv; v <= _c; v++) n     fac2[v] = mypow(fac[v], 1000000007 - 2); n   int q, f, n; n   scanf( %d , &q); n   for (int i = 0, _a = (q); i < _a; ++i) { n     scanf( %d%d , &n, &f); n     printf( %I64d n , solve(f, n)); n   } n } n


#include <bits/stdc++.h> n char T[1 << 21]; n int n, m, ar[21][21]; n int U[21], V[21], P[21], way[21], minv[21]; n int hungarian(int n, int m, int ar[21][21]) { n   memset(way, 0, sizeof(way)), memset(U, 0, sizeof(U)), memset(V, 0, sizeof(V)), n       memset(P, 0, sizeof(P)); n   int i, j, i0, i1, j0, j1, x, y, cur, delta, used, lim = (1 << (m + 1)) - 1; n   for (i = 1; i <= n; i++) { n     used = 0; n     P[0] = i, j0 = 0; n     for (j = 0; j <= m; j++) minv[j] = (1 << 27); n     do { n       used |= (1 << j0); n       i0 = P[j0], j1 = 0, delta = (1 << 27), x = (used | 1) ^ lim; n       while (x) { n         y = (-x & x); n         j = T[y]; n         x ^= y; n         cur = ar[i0][j] - U[i0] - V[j]; n         if (cur < minv[j]) { n           minv[j] = cur; n           way[j] = j0; n         } n         if (minv[j] < delta) { n           delta = minv[j]; n           j1 = j; n         } n       } n       for (j = 0; j <= m; j++) { n         if (used & (1 << j)) { n           U[P[j]] += delta; n           V[j] -= delta; n         } else n           minv[j] -= delta; n       } n       j0 = j1; n     } while (P[j0] != 0); n     do { n       j1 = way[j0]; n       P[j0] = P[j1]; n       j0 = j1; n     } while (j0 != 0); n   } n   return -V[0]; n } n int fri[21][21], sat[21][21]; n int main() { n   int i, j, k, x, res, lim, mask; n   for (i = 0; i < 21; i++) T[1 << i] = i; n   while (scanf( %d , &n) != EOF) { n     for (i = 0; i < n; i++) { n       for (j = 0; j < n; j++) { n         scanf( %d , &fri[i][j]); n       } n     } n     for (i = 0; i < n; i++) { n       for (j = 0; j < n; j++) { n         scanf( %d , &sat[i][j]); n       } n     } n     res = 0, lim = 1 << n; n     for (mask = 0; mask < lim; mask++) { n       if (__builtin_popcount(mask) == (n >> 1)) { n         for (i = 0; i < n; i++) { n           for (j = 0; j < n; j++) { n             if (mask & (1 << i)) n               ar[i + 1][j + 1] = -fri[i][j]; n             else n               ar[i + 1][j + 1] = -sat[i][j]; n           } n         } n         x = -hungarian(n, n, ar); n         if (x > res) res = x; n       } n     } n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long in() { n   int32_t x; n   scanf( %d , &x); n   return x; n } n inline string getStr() { n   char ch[1000000]; n   scanf( %s , ch); n   return ch; n } n const long long MOD = 1e9 + 7; n const long long base = 29; n const long long MAX_N = 3e5 + 1; n inline long long divide(long long a, long long b) { return (a + b - 1) / b; } n vector<long long> g[MAX_N]; n long long deg[MAX_N]; n bool vis[MAX_N]; n long long res[MAX_N]; n inline void dfs(long long node, long long part) { n   if (vis[node] == true) return; n   res[node] = part; n   long long cnt = 0; n   for (long long i = 0; i < g[node].size(); i++) { n     long long u = g[node][i]; n     if (res[node] == res[u]) cnt++; n   } n   if (cnt > 1) { n     res[node] ^= 1; n     dfs(node, res[node]); n     vis[node] = false; n     return; n   } n   vis[node] = true; n   for (long long pt = 0; pt < g[node].size(); pt++) { n     long long u = g[node][pt]; n     if (res[u] == res[node]) { n       dfs(u, res[u]); n       vis[node] = 0; n       return; n     } n   } n   vis[node] = false; n } n int32_t main() { n   long long n = in(), m = in(); n   for (long long i = 0; i < m; i++) { n     long long v = in(), u = in(); n     deg[v]++, deg[u]++; n     g[v].push_back(u); n     g[u].push_back(v); n   } n   for (long long i = 1; i <= n; i++) res[i] = 1; n   for (long long i = 1; i <= n; i++) { n     dfs(i, res[i]); n   } n   for (long long i = 1; i <= n; i++) cout << res[i]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int res = 0; n   for (int i = 0; i < n; i++) { n     res++; n     if (i == n - 1) break; n     if (s[i] != s[i + 1]) { n       i++; n     } n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y, z, x_, y_, z_; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x_ >> y_ >> z_; n     x += x_; n     z += z_; n     y += y_; n   } n   if (x != 0 || y != 0 || z != 0) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct IInt { n   int z; n }; n bool cmp(int a, int b) { return a < b; } n bool operator<(IInt a, IInt b) { return a.z > b.z; } n int main() { n   int n, m, x, i, ans, ans1; n   IInt t; n   scanf( %d%d , &n, &m); n   vector<int> q; n   priority_queue<IInt> w; n   for (i = 0; i < m; i++) { n     scanf( %d , &x); n     q.push_back(x); n     t.z = x; n     w.push(t); n   } n   make_heap(q.begin(), q.end(), cmp); n   ans = ans1 = 0; n   for (i = 0; i < n; i++) { n     ans += q[0]; n     q[0]--; n     make_heap(q.begin(), q.end(), cmp); n   } n   for (i = 0; i < n; i++) { n     t = w.top(); n     w.pop(); n     ans1 += t.z; n     if (t.z == 1) { n       continue; n     } else { n       t.z--; n       w.push(t); n     } n   } n   printf( %d %d n , ans, ans1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005, M = 2000010; n const long long MOD = 1E9 + 7; n long long Pow(long long a, long long b) { n   long long c = 1; n   for (; b; a = a * a % MOD, b >>= 1) n     if (b & 1) c = c * a % MOD; n   return c; n } n struct Node { n   int x, y; n   bool operator<(const Node &a) const { n     return (x != a.x) ? (x < a.x) : (y < a.y); n   } n } p[N]; n int h, w, n, m; n long long fac[M], fac_inv[M]; n long long f[N]; n void Init() { n   fac[0] = 1; n   for (int i = 1; i <= m; ++i) fac[i] = fac[i - 1] * i % MOD; n   fac_inv[m] = Pow(fac[m], MOD - 2); n   for (int i = m; i; --i) fac_inv[i - 1] = fac_inv[i] * i % MOD; n   return; n } n inline long long F(int a, int b) { n   return fac[a + b] * fac_inv[a] % MOD * fac_inv[b] % MOD; n } n int main() { n   scanf( %d%d%d , &h, &w, &n); n   m = h + w; n   Init(); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &p[i].x, &p[i].y); n   sort(p + 1, p + n + 1); n   for (int i = 1; i <= n; ++i) { n     f[i] = F(p[i].x - 1, p[i].y - 1); n     for (int j = 1; j < i; ++j) n       if (p[j].y <= p[i].y) n         f[i] = (f[i] + MOD - f[j] * F(p[i].x - p[j].x, p[i].y - p[j].y) % MOD) % n                MOD; n   } n   long long res = F(h - 1, w - 1); n   for (int i = 1; i <= n; ++i) n     res = (res + MOD - f[i] * F(h - p[i].x, w - p[i].y) % MOD) % MOD; n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n const int MOD = 1000000007; n signed main() { n   cin.tie(0), cout.tie(0); n   ios::sync_with_stdio(0); n   int n, k; n   cin >> n >> k; n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   if (k < n + 1 || n <= 4) n     cout << -1 <<   n , exit(0); n   else { n     cout << a <<     << c <<    ; n     for (int i = 1; i <= n; i++) n       if (i != a && i != b && i != c && i != d) cout << i <<    ; n     cout << d <<     << b <<   n ; n     cout << c <<     << a <<    ; n     for (int i = 1; i <= n; i++) n       if (i != a && i != b && i != c && i != d) cout << i <<    ; n     cout << b <<     << d <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int h1, a1, c1, h2, a2; n   int move_counter = 0; n   vector<char> move; n   cin >> h1 >> a1 >> c1 >> h2 >> a2; n   while (h2 > 0) { n     if (h2 - a1 <= 0) { n       move.push_back( S ); n       ++move_counter; n       break; n     } else { n       if (h1 - a2 <= 0) { n         h1 += c1; n         move.push_back( H ); n         ++move_counter; n       } else { n         h2 -= a1; n         move.push_back( S ); n         ++move_counter; n       } n       h1 -= a2; n     } n   } n   cout << move_counter <<   n ; n   for (const auto it : move) { n     if (it ==  H ) n       cout <<  HEAL  <<   n ; n     else n       cout <<  STRIKE  <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, x[1010], dp[1010][1010], sm[1010]; n long long solve(long long gap) { n   dp[0][0] = 1, x[0] = -200000; n   for (long long i = 1; i <= k; ++i) { n     for (long long j = 0; j <= n; ++j) n       sm[j] = j == 0 ? dp[i - 1][j] : (dp[i - 1][j] + sm[j - 1]) % 998244353; n     long long pos = 0; n     for (long long j = 1; j <= n; ++j) { n       while (x[j] - x[pos + 1] >= gap) pos++; n       dp[i][j] = sm[pos]; n     } n   } n   long long sum = 0; n   for (long long i = 1; i <= n; ++i) sum = (sum + dp[k][i]) % 998244353; n   return sum; n } n int32_t main() { n   cin.tie(0), cout.sync_with_stdio(0); n   cin >> n >> k; n   for (long long i = 1; i <= n; ++i) cin >> x[i]; n   sort(x + 1, x + 1 + n); n   long long dis = 100000 / (k - 1), pre = solve(1), ans = 0; n   for (long long i = 2; i <= dis + 5; ++i) { n     long long now = solve(i); n     ans = (ans + (pre - now + 998244353) * (i - 1)) % 998244353; n     pre = now; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[1000]; n   int num; n   cin >> num; n   for (int i = 0; i < num; i++) { n     cin >> arr[i]; n     arr[num + i] = arr[i]; n   } n   int ans = 360; n   for (int i = 0; i < num; i++) { n     int temp = 0; n     for (int j = i; j < num * 2; j++) { n       temp += arr[j]; n       ans = min(ans, abs(360 - 2 * temp)); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b; n char s[1000005]; n int arr[205]; n int main() { n   scanf( %d%d%d%s , &n, &a, &b, s); n   int i, j, len = strlen(s), tag; n   if (len > n * b || len < n * a) { n     puts( No solution ); n     return 0; n   } n   int eve = len / n; n   for (i = 0; i < n; i++) { n     arr[i] = eve; n   } n   eve = len % n; n   for (i = 0; i < eve; i++) arr[i]++; n   for (i = tag = 0; i < n; i++) { n     for (j = 0; j < arr[i]; j++) printf( %c , s[tag++]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char instr[51], outstr[51] = {}, ch; n   int n, i; n   cin >> instr; n   cin >> n; n   for (i = 0; i < strlen(instr); i++) { n     if (instr[i] < 97) instr[i] += 32; n   } n   for (i = 0; i < strlen(instr); i++) { n     ch = instr[i]; n     if (int(ch) < (n + 97)) { n       outstr[i] = ch - 32; n     } else { n       outstr[i] = ch; n     } n   } n   cout << outstr; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int N, M; n   cin >> N >> M; n   vector<int> arr(N + 2); n   arr[0] = 1; n   for (int i = 0; i < N; ++i) cin >> arr[i + 1]; n   arr[N + 1] = 1; n   int q = M, w = M; n   int r = 0; n   while (q > 0 || w < N + 1) { n     int s = arr[q] + arr[w]; n     if (s == 2) r += s - (q == 0) - (w == N + 1) - (w == q); n     if (q > 0) q--; n     if (w < N + 1) w++; n   } n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int d[300002]; n int safe[300002]; n bool solve() { n   long long int n, k, l, i; n   cin >> n >> k >> l; n   for (i = 1; i <= n; i++) cin >> d[i]; n   int tide, dir, mtide; n   tide = k; n   dir = -1; n   for (i = 1; i <= n; i++) { n     if (d[i] + k <= l) n       tide = k, dir = -1; n     else if (d[i] > l) n       return false; n     else { n       mtide = l - d[i]; n       if (tide + dir <= mtide) { n         tide += dir; n         if (tide == 0) dir = 1; n       } else if (dir == -1 && mtide < tide - 1) { n         tide = mtide; n         if (tide == 0) dir = 1; n       } else n         return false; n     } n   } n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t, i; n   cin >> t; n   while (t--) { n     if (solve()) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     long long int a[n]; n     unordered_map<long long int, long long int> mp; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       mp[a[i]]++; n     } n     if (mp.size() == 1) n       cout <<  NO  << endl; n     else { n       cout <<  YES  << endl; n       int f = -1, s = -1; n       for (int i = 1; i < n; i++) { n         if (a[i] != a[0]) { n           f = i; n           break; n         } n       } n       for (int i = 1; i < n; i++) { n         if (a[i] != a[f]) n           cout << i + 1 <<     << f + 1 << endl; n         else n           cout << 1 <<     << i + 1 << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 2e5 + 10; n int n; n string s, xx =  RGB ; n int getID(char c) { n   if (c ==  R ) n     return 1; n   else if (c ==  G ) n     return 2; n   return 3; n } n int last[4]; n int dp[MAX][4][4][4]; n int mn = 1e9; n vector<char> v, ans; n int tokyoGhoul(int cur, int cnt) { n   if (cur == n) { n     if (cnt < mn) { n       mn = cnt; n       ans = v; n     } n     return cnt; n   } n   int &ret = dp[cur][last[0]][last[1]][last[2]]; n   if (~ret) return ret; n   ret = 1e18; n   for (int i = 0; i < xx.size(); i++) { n     int id = getID(xx[i]); n     int prev = last[id]; n     char ch = xx[i]; n     if (prev == -1 || (cur - prev) != 1) { n       v.push_back(ch); n       last[id] = cur; n       if (xx[i] != s[cur]) n         ret = min(ret, tokyoGhoul(cur + 1, cnt + 1)); n       else n         ret = min(ret, tokyoGhoul(cur + 1, cnt)); n       last[id] = prev; n       v.pop_back(); n     } n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> s; n   vector<string> vv; n   int cnt = 0, cnt2 = 0; n   string s1 =   , s2 =   ; n   for (int j = 0; j < 3; j++) { n     cnt = 0, s1 =   ; n     s1 += xx[j]; n     if (s[0] != s1[0]) cnt++; n     for (int i = 1; i < n; i++) { n       if (s[i] == s1[i - 1]) { n         cnt++; n         bool f = 1; n         for (int j = 0; j < xx.size(); j++) { n           if (s1[i - 1] != xx[j] && (i + 1 == n || s[i + 1] != xx[j])) { n             s1 += xx[j]; n             f = 0; n             break; n           } n         } n         if (f) n           for (int j = 0; j < xx.size(); j++) { n             if (s[i - 1] != xx[j]) { n               s1 += xx[j]; n               break; n             } n           } n       } else n         s1 += s[i]; n     } n     if (cnt < mn) { n       mn = cnt; n       s2 = s1; n     } n   } n   cout << mn << endl; n   for (auto i : s2) cout << i; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T min(T a, T b, T c) { n   return min(a, min(b, c)); n } n template <class T> n T max(T a, T b, T c) { n   return max(a, max(b, c)); n } n int r, c, k, n, a[15][15]; n int main() { n   scanf( %d%d%d%d , &r, &c, &n, &k); n   for (int i = (1); i <= (n); ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     a[x][y] = 1; n   } n   int ans = 0; n   for (int x1 = (1); x1 <= (r); ++x1) { n     for (int y1 = (1); y1 <= (c); ++y1) { n       for (int x2 = (x1); x2 <= (r); ++x2) { n         for (int y2 = (y1); y2 <= (c); ++y2) { n           int sum = 0; n           for (int x = (x1); x <= (x2); ++x) { n             for (int y = (y1); y <= (y2); ++y) sum += a[x][y]; n           } n           if (sum >= k) ++ans; n         } n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5010, inf = 0x7fffffff; n bool has[maxn][maxn], flag = 0; n int sum[maxn][maxn], n, m, l[10], r[10], req; n long long ans; n void init() { n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       sum[i][j] = n           sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (has[i][j] == 1); n } n int calculate(int x1, int y1, int x2, int y2) { n   return sum[x2][y2] - sum[x1][y2] - sum[x2][y1] + sum[x1][y1]; n } n void work(int up, int down, int left, int right, bool dir) { n   if (left == right || up == down) return; n   if (left + 1 == right && up + 1 == down) { n     ans += calculate(up, left, down, right) == req; n     return; n   } n   if (dir == 0) { n     int mid = (left + right) >> 1; n     work(up, down, left, mid, dir ^ 1); n     work(up, down, mid, right, dir ^ 1); n     for (int i = up; i < down; i++) { n       l[0] = r[0] = mid; n       for (int k = 1; k <= req + 1; k++) l[k] = left, r[k] = right; n       for (int j = i + 1; j <= down; j++) { n         for (int k = 1; k <= req + 1; k++) n           while (calculate(i, l[k], j, mid) >= k) l[k]++; n         for (int k = 1; k <= req + 1; k++) n           while (calculate(i, mid, j, r[k]) >= k) r[k]--; n         for (int k = 0; k <= req; k++) n           ans += (l[k] - l[k + 1]) * (r[req - k + 1] - r[req - k]); n       } n     } n   } else { n     int mid = (up + down) >> 1; n     work(up, mid, left, right, dir ^ 1); n     work(mid, down, left, right, dir ^ 1); n     for (int i = left; i < right; i++) { n       l[0] = r[0] = mid; n       for (int k = 1; k <= req + 1; k++) l[k] = up, r[k] = down; n       for (int j = i + 1; j <= right; j++) { n         for (int k = 1; k <= req + 1; k++) n           while (calculate(l[k], i, mid, j) >= k) l[k]++; n         for (int k = 1; k <= req + 1; k++) n           while (calculate(mid, i, r[k], j) >= k) r[k]--; n         for (int k = 0; k <= req; k++) n           ans += (l[k] - l[k + 1]) * (r[req - k + 1] - r[req - k]); n       } n     } n   } n } n int main() { n   while (scanf( %d%d%d , &n, &m, &req) == 3) { n     memset(has, 0, sizeof(has)); n     ans = 0; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) scanf( %1d , &has[i][j]); n     init(); n     work(0, n, 0, m, n < m); n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, ta, tb; n   cin >> a >> ta >> b >> tb; n   int t, m; n   char c; n   cin >> t >> c >> m; n   m += 60 * t; n   int cur = 5 * 60; n   int res = 0; n   while (cur < 24 * 60) { n     if (min(cur + tb, m + ta) > max(cur, m)) ++res; n     cur += b; n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 100000007; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<long long> nums(n); n     for (int i = 0; i < n; i++) cin >> nums[i]; n     double all = 0; n     sort(nums.rbegin(), nums.rend()); n     for (int i = 1; i < n; i++) all += nums[i]; n     all = 1.0 * all / (n - 1) + nums[0]; n     printf( %.8lf n , all); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string res =   ; n   cin >> n; n   int arr[2004] = {0}; n   for (int i = 0; i < n; ++i) { n     cin >> arr[i]; n   } n   int now_rejt = 0; n   int mesto = 1; n   for (int i = 0; i < n; ++i) { n     now_rejt = arr[i]; n     mesto = 1; n     for (int j = 0; j < n; ++j) { n       if (now_rejt < arr[j]) { n         ++mesto; n       } n     } n     stringstream ss; n     ss << mesto; n     string strmesto; n     ss >> strmesto; n     res = res +     + strmesto; n   } n   res.erase(0, 1); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, map<int, vector<int>>> answers; n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int bus, tram, money; n     cin >> bus >> tram >> money; n     string s; n     cin >> s; n     int cur = s.size() - 2; n     while (true) { n       char cur_char = s[cur]; n       if (cur_char ==  A ) { n         if (bus > money) { n           cout << cur + 2 <<   n ; n           break; n         } n         money -= bus; n       } n       if (cur_char ==  B ) { n         if (tram > money) { n           cout << cur + 2 <<   n ; n           break; n         } n         money -= tram; n       } n       while (cur >= 0 && s[cur] == cur_char) { n         cur -= 1; n       } n       if (cur == -1) { n         cout << 1 <<   n ; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int GCD(int x, int y) { return !y ? x : GCD(y, x % y); } n int LCM(int x, int y) { return x * y / GCD(x, y); } n long long ceilDivision(long long dividend, long long divisor) { n   return (dividend + divisor - 1) / divisor; n } n bool check(pair<long long, int> right, pair<long long, int> left) { n   return ((left.second > right.second) || n           (left.second == right.second && left.first > right.first)); n } n int main() { n   long long r, avg, sum = 0, target, counter = 0; n   int n, pointer = 0; n   cin >> n >> r >> avg; n   vector<pair<long long, int> > arr(n); n   target = avg * n; n   for (int i = 0; i < n; i++) n     cin >> arr[i].first >> arr[i].second, sum += arr[i].first; n   if (sum < target) { n     sort(arr.begin(), arr.end(), check); n     target -= sum; n     while (target) { n       if (arr[pointer].first < r) n         counter += (min(target, r - arr[pointer].first) * arr[pointer].second), n             target -= min(target, r - arr[pointer].first); n       pointer++; n     } n   } n   cout << counter << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> dir_graph; n vector<int> vis, order; n void dfs(int i) { n   vis[i] = true; n   for (int j = 0; j < dir_graph[i].size(); j++) { n     if (!vis[dir_graph[i][j]]) { n       dfs(dir_graph[i][j]); n     } n   } n   order.push_back(i); n } n int main() { n   int cases; n   cin >> cases; n   while (cases--) { n     int n, m; n     cin >> n >> m; n     dir_graph = vector<vector<int>>(n); n     order.clear(); n     vis = vector<int>(n); n     vector<pair<int, int>> edges; n     int type, s, d; n     for (int i = 0; i < m; i++) { n       cin >> type >> s >> d; n       s--; n       d--; n       if (type == 1) dir_graph[s].push_back(d); n       edges.push_back({s, d}); n     } n     for (int i = 0; i < n; i++) { n       if (!vis[i]) { n         dfs(i); n       } n     } n     vector<int> pos(n); n     reverse(order.begin(), order.end()); n     for (int i = 0; i < n; ++i) { n       pos[order[i]] = i; n     } n     bool hasCycle = false; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < dir_graph[i].size(); j++) { n         if (pos[i] > pos[dir_graph[i][j]]) { n           hasCycle = true; n           break; n         } n       } n     } n     if (hasCycle) n       cout <<  NO  << endl; n     else { n       cout <<  YES n ; n       for (int i = 0; i < edges.size(); i++) { n         if (pos[edges[i].first] < pos[edges[i].second]) { n           cout << edges[i].first + 1 <<     << edges[i].second + 1 << endl; n         } else { n           cout << edges[i].second + 1 <<     << edges[i].first + 1 << endl; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char ch[] = { B ,  u ,  l ,  b ,  a ,  s ,  a ,  u ,  r }; n   map<char, int> mp; n   string ss; n   int n, k, i, cnt = 100000; n   cin >> ss; n   for (i = 0; i < ss.size(); i++) { n     mp[ss[i]]++; n   } n   for (i = 0; i < 9; i++) { n     k = mp[ch[i]]; n     if (ch[i] ==  u  || ch[i] ==  a ) k = k / 2; n     cnt = min(cnt, k); n   } n   cout << cnt << endl; n   return 0; n } n