#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-12; n const int INF = 1 << 30; n const long long LINF = 1ll << 61; n const int BUFFER_SZ = 1 << 20; n const int MOD = 1000 * 1000 * 1000 + 7; n char BUFFER[BUFFER_SZ]; n int gi() { n   int x; n   scanf( %d , &x); n   return x; n } n double gd() { n   double x; n   scanf( %lf , &x); n   return x; n } n long long gll() { n   long long x; n   scanf( %lld , &x); n   return x; n } n vector<int> gvi(int n) { n   vector<int> a; n   while (n-- > 0) a.push_back(gi()); n   return a; n } n string gs() { n   scanf( %s , BUFFER); n   return string(BUFFER); n } n template <class T> n void print(vector<T> &x, string format) { n   format +=  %c ; n   for (int i = 0; i < x.size(); ++i) n     printf(format.c_str(), x[i], i + 1 == x.size() ?   n  :    ); n } n void solve() { n   int n = gi(); n   vector<int> a = gvi(n); n   for (int i = 0; i < a.size(); ++i) { n     for (int j = i + 1; j < a.size(); ++j) { n       for (int k = j + 1; k < a.size(); ++k) { n         if (a[i] == a[j] + a[k]) { n           printf( %d %d %d n , i + 1, j + 1, k + 1); n           return; n         } n         if (a[j] == a[i] + a[k]) { n           printf( %d %d %d n , j + 1, i + 1, k + 1); n           return; n         } n         if (a[k] == a[i] + a[j]) { n           printf( %d %d %d n , k + 1, i + 1, j + 1); n           return; n         } n       } n     } n   } n   printf( -1 n ); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   long long n, m, i, ans = 0, z, j; n   pair<long long, long long> a[105]; n   vector<pair<long long, long long> > q; n   cin >> n >> m; n   for (i = 1; i <= n; i++) a[i].second = 0; n   for (i = 1; i <= n; i++) cin >> a[i].first; n   for (j = 1; j <= m; j++) { n     cin >> z; n     a[z].second = 1; n   } n   for (i = 1; i <= n; i++) q.push_back(a[i]); n   sort(q.begin(), q.end()); n   for (i = 0; i < n; i++) n     if (q[i].second == 0) ans += q[i].first; n   for (i = n - 1; i >= 0; i--) n     if (q[i].second == 1) ans = ans + max(ans, q[i].first); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 998244353; n long long p[400001]; n long long n; n vector<long long> a(100001); n bool check(long long x) { n   long long cnt[n + 1]; n   for (int i = 1; i < n + 1; i++) cnt[i] = 0; n   for (int i = 0; i < n; i++) cnt[a[i]]++; n   set<pair<long long, long long>, greater<pair<long long, long long> > > ss; n   for (int i = 1; i < n + 1; i++) { n     if (cnt[i] > 0) ss.insert({cnt[i], i}); n   } n   vector<long long> b; n   for (int i = 0; i < n; i++) { n     if (i >= x && cnt[b[i - x]] >= 1) { n       ss.insert({cnt[b[i - x]], b[i - x]}); n     } n     if (ss.empty()) return false; n     b.push_back(ss.begin()->second); n     cnt[ss.begin()->second]--; n     ss.erase(ss.begin()); n   } n   return true; n } n int main() { n   long long t; n   cin >> t; n   while (t-- > 0) { n     cin >> n; n     for (int i = 0; i < n; i++) cin >> a[i]; n     long long l = 0, h = n; n     long long ans; n     while (l + 1 < h) { n       long long mid = (l + h) / 2; n       if (check(mid)) { n         ans = mid; n         l = mid; n       } else n         h = mid; n     } n     cout << ans - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char x[5][105]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < n; j++) { n       x[i][j] =  . ; n     } n   } n   if (m % 2 == 0) { n     printf( YES n ); n     for (int i = 1; i <= m / 2; i++) { n       x[1][i] =  # ; n       x[2][i] =  # ; n     } n     for (int i = 0; i < 4; i++) { n       for (int j = 0; j < n; j++) { n         printf( %c , x[i][j]); n       } n       printf(  n ); n     } n   } else { n     if (m >= n - 2) { n       printf( YES n ); n       for (int i = 1; i <= n - 2; i++) { n         x[1][i] =  # ; n       } n       if (m - n + 2 >= 2) { n         x[2][1] =  # ; n         x[2][n - 2] =  # ; n         for (int i = 0; i < m - n; i++) { n           x[2][i + 2] =  # ; n         } n       } n       for (int i = 0; i < 4; i++) { n         for (int j = 0; j < n; j++) { n           printf( %c , x[i][j]); n         } n         printf(  n ); n       } n     } else { n       printf( YES n ); n       x[1][n / 2] =  # ; n       for (int i = 1; i <= m / 2; i++) { n         x[1][(n / 2) + i] =  # ; n         x[1][(n / 2) - i] =  # ; n       } n       for (int i = 0; i < 4; i++) { n         for (int j = 0; j < n; j++) { n           printf( %c , x[i][j]); n         } n         printf(  n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> m; n long long n, p; n int main() { n   cin >> n; n   while (cin >> n) m[n]++; n   for (n = 1; n < 11; n++) p += m[n] * m[n * -1]; n   cout << p + m[0] * (m[0] - 1) / 2; n } n


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


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int ar[1000]; n   int i; n   for (i = 0; i < n; scanf( %d , &ar[i++])) n     ; n   int b = 0, e = n - 1; n   int s = 0, d = 0; n   i = 1; n   while (b <= e) { n     if (ar[b] > ar[e]) { n       if (i == 1) n         s = s + ar[b]; n       else n         d = d + ar[b]; n       b++; n     } else { n       if (i == 1) n         s = s + ar[e]; n       else n         d = d + ar[e]; n       e--; n     } n     i = i * -1; n   } n   printf( %d %d , s, d); n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 5; n const long long infll = 1e18 + 5ll; n const long double eps = 1e-9; n const int maxn = 1e5 + 100; n const long long maxlog = 21; n const int P = 97; n const int mod = 1e9 + 9; n int n, m, lvl[maxn], up[maxlog][maxn], sub[maxn], par[maxn], ans[maxn]; n std::vector<int> g[maxn]; n bool ban[maxn]; n void dfs1(int v = 1, int pr = 0) { n   lvl[v] = lvl[pr] + 1; n   up[0][v] = pr; n   for (int i = 1; i < 20; ++i) up[i][v] = up[i - 1][up[i - 1][v]]; n   for (auto to : g[v]) n     if (pr != to) { n       dfs1(to, v); n     } n } n int nn; n void dfs2(int v, int pr) { n   sub[v] = 1; n   ++nn; n   for (auto to : g[v]) { n     if (to != pr && !ban[to]) { n       dfs2(to, v); n       sub[v] += sub[to]; n     } n   } n } n int getCentroid(int v, int pr) { n   for (auto to : g[v]) { n     if (to != pr && !ban[to] && sub[to] * 2 > nn) { n       return getCentroid(to, v); n     } n   } n   return v; n } n void decompose(int v, int pr) { n   nn = 0; n   dfs2(v, pr); n   int centroid = getCentroid(v, pr); n   par[centroid] = pr; n   ban[centroid] = 1; n   for (auto to : g[centroid]) { n     if (to == pr || ban[to]) continue; n     decompose(to, centroid); n   } n } n int lca(int a, int b) { n   if (lvl[a] > lvl[b]) swap(a, b); n   int d = lvl[b] - lvl[a]; n   for (int i = 0; i < maxlog; ++i) n     if (d & (1 << i)) b = up[i][b]; n   if (a == b) return b; n   for (int i = maxlog - 1; i >= 0; --i) n     if (up[i][a] != up[i][b]) a = up[i][a], b = up[i][b]; n   return up[0][a]; n } n int dist(int u, int v) { return lvl[u] + lvl[v] - 2 * lvl[lca(u, v)]; } n void upd(int v) { n   int x = v; n   while (x != 0) { n     ans[x] = min(ans[x], dist(x, v)); n     x = par[x]; n   } n } n int query(int v) { n   int x = v, res = inf; n   while (x != 0) { n     res = min(res, ans[x] + dist(x, v)); n     x = par[x]; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL); n   if (fopen( sorting  n              .in , n              r )) n     freopen( n          sorting  n          .in , n          r , stdin), n         freopen( n              sorting  n              .out , n              w , stdout); n   ; n   cin >> n >> m; n   for (int i = 1, u, v; i < n; ++i) { n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs1(); n   decompose(1, 0); n   memset(ans, inf, sizeof ans); n   upd(1); n   for (int i = 1, t, v; i <= m; ++i) { n     cin >> t >> v; n     if (t == 1) { n       upd(v); n     } else { n       cout << query(v) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ed, x, y; n int l[200005], qd[200005], qn[200005], p[200005]; n void bd(int x, int y) { qd[++ed] = y, qn[ed] = l[x], l[x] = ed; } n int notinE(int x, int y) { n   for (int i = (l[x]); i; i = qn[i]) n     if (qd[i] == y) return 0; n   return 1; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = (0); i < (m); i++) n     scanf( %d%d , &x, &y), bd(x - 1, y - 1), bd(y - 1, x - 1); n   for (int i = (0); i < (n); i++) p[i] = i; n   for (int i = (0); i < (75); i++) { n     random_shuffle(p, p + n); n     p[n] = p[0]; n     int ok = 1; n     for (int j = (1); j <= (m); j++) n       if (!notinE(p[j - 1], p[j])) { n         ok = 0; n         break; n       } n     if (ok) { n       for (int j = (1); j <= (m); j++) n         printf( %d %d n , p[j - 1] + 1, p[j] + 1); n       return 0; n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cmpfunc(const void *a, const void *b) { return (*(int *)a - *(int *)b); } n long long int gcd(long long int m, long long int n) { n   if (n == 0) return m; n   return gcd(n, m % n); n } n long long int fastpow(long long int a, long long int b, long long int m) { n   long long int r = 1; n   while (b > 0) { n     if (b % 2 == 1) r = (r * a) % m; n     b = b >> 1; n     a = (a * a) % m; n   } n   return r; n } n int prime(long long int x) { n   if (x == 1) return 0; n   if (x <= 3) return 1; n   if (x % 6 == 1 || x % 6 == 5) { n     long long int y = sqrt(x), i; n     for (i = 2; i <= y; i++) n       if (x % i == 0) return 0; n     return 1; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long int n, t; n   cin >> n >> t; n   if (n == 1 && t == 10) n     cout << -1 << endl; n   else if (t <= 9) { n     for (long long int i = 0; i < n; i++) cout << t; n     cout << endl; n   } else { n     cout << 1; n     for (long long int i = 1; i < n; i++) cout << 0; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long blockSize; n long long cnt[100005]; n long long ans[100005]; n struct Query { n   long long L, R, idx; n }; n bool compare(Query x, Query y) { n   if (x.L / blockSize != y.L / blockSize) n     return x.L / blockSize < y.L / blockSize; n   return x.R < y.R; n } n void queryResults(long long a[], long long n, Query q[], long long m) { n   blockSize = (long long)sqrt(n); n   sort(q, q + m, compare); n   long long currL = 0, currR = 0; n   long long currCnt = 0; n   for (long long i = 0; i < m; i++) { n     long long L = q[i].L, R = q[i].R; n     while (currL < L) { n       if (a[currL] > 100000) { n         currL++; n         continue; n       } n       if (cnt[a[currL]] == a[currL]) currCnt--; n       cnt[a[currL]]--; n       if (cnt[a[currL]] == a[currL]) currCnt++; n       currL++; n     } n     while (currL > L) { n       if (a[currL - 1] > 100000) { n         currL--; n         continue; n       } n       if (cnt[a[currL - 1]] == a[currL - 1]) currCnt--; n       cnt[a[currL - 1]]++; n       if (cnt[a[currL - 1]] == a[currL - 1]) currCnt++; n       currL--; n     } n     while (currR <= R) { n       if (a[currR] > 100000) { n         currR++; n         continue; n       } n       if (cnt[a[currR]] == a[currR]) currCnt--; n       cnt[a[currR]]++; n       if (cnt[a[currR]] == a[currR]) currCnt++; n       currR++; n     } n     while (currR > R + 1) { n       if (a[currR - 1] > 100000) { n         currR--; n         continue; n       } n       if (cnt[a[currR - 1]] == a[currR - 1]) currCnt--; n       cnt[a[currR - 1]]--; n       if (cnt[a[currR - 1]] == a[currR - 1]) currCnt++; n       currR--; n     } n     ans[q[i].idx] = currCnt; n   } n   for (long long i = 0; i < m; i++) cout << ans[i] << endl; n } n int main() { n   long long n, m; n   cin >> n >> m; n   long long a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   Query q[m]; n   for (long long i = 0; i < m; i++) { n     cin >> q[i].L >> q[i].R; n     q[i].L--; n     q[i].R--; n     q[i].idx = i; n   } n   queryResults(a, n, q, m); n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4101) n using namespace std; n const double pi = acos(-1.0); n const int INF = 1000000000; n const int MAX = 100007; n const int MAX2 = 2000000; n const int inf = (int)1e9 + 5; n const long long mod = 1e9 + 7; n const int BASE = 10; n const int ST = 1000003; n const int CNT = 100; n const double eps = 1e-6; n const long long base = 2204234849; n const long long l_inf = (long long)4e18; n const int logn = 30; n int main() { n   long long n, a, b, c, d, maxim1, maxim2; n   cin >> n >> a >> b >> c >> d; n   long long sum1 = a + b, sum2 = b + d, sum3 = c + a, sum4 = d + c; n   int minim = min(sum1, min(sum2, min(sum3, sum4))); n   int kol = 0; n   maxim1 = minim + n; n   int maxim = max(sum1, max(sum2, max(sum3, sum4))); n   if (maxim > maxim1) { n     cout << 0; n     return 0; n   } n   cout << n * (maxim1 - maxim); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n long long fun(long long n) { n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) return i; n   } n   return n; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, c = 0; n   cin >> n; n   if (n % 2 != 0) { n     n = n - fun(n); n     c++; n   } n   cout << c + n / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 5009; n string s[MAX]; n int n, m, b[MAX], ans, a[MAX]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int i = 0; i < m; i++) { n     memset(a, 0, sizeof a); n     for (int j = 0; j < n; j++) n       if (s[j][i] ==  1 ) n         b[j]++, a[b[j]]++; n       else n         b[j] = 0; n     for (int i = m; i >= 0; i--) a[i] += a[i + 1], ans = max(ans, a[i] * i); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 200 * 1000 + 10; n pair<int, int> a[maxN]; n int nx[maxN]; n stack<int> curr; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int d, n, m; n   cin >> d >> n >> m; n   for (int i = 0; i < m; i++) cin >> a[i].first >> a[i].second; n   sort(a, a + m); n   a[m].first = d; n   curr.push(m); n   for (int i = m - 1; i >= 0; i--) { n     while (curr.size() > 1 && a[curr.top()].second >= a[i].second) curr.pop(); n     nx[i] = curr.top(); n     curr.push(i); n   } n   int fuel = n; n   long long cost = 0; n   for (int i = 0; i <= m; i++) { n     fuel -= a[i].first - (i ? a[i - 1].first : 0); n     if (fuel < 0) { n       cout << -1 << endl; n       return 0; n     } n     if (i == m) break; n     int x = min(n - fuel, max(0, a[nx[i]].first - a[i].first - fuel)); n     fuel += x; n     cost += a[i].second * 1ll * x; n   } n   cout << cost << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int ar[300005]; n char ch[300005]; n int n, m, ii, k; n int pre[300005]; n vector<pair<int, int>> v; n void solve() { n   v.clear(); n   scanf( %d%d%s , &n, &m, ch); n   for (int i = 1; i <= n; i++) { n     pre[i] = pre[i - 1]; n     if (i & 1) { n       if (ch[i - 1] ==  + ) n         pre[i]++; n       else n         pre[i]--; n     } else { n       if (ch[i - 1] ==  - ) n         pre[i]++; n       else n         pre[i]--; n     } n     v.push_back({pre[i] + n, i}); n   } n   sort(v.begin(), v.end()); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     int len = y - x + 1; n     if (pre[y] - pre[x - 1] == 0) { n       printf( 0 n ); n       continue; n     } n     if (len & 1) n       printf( 1 n ); n     else n       printf( 2 n%d  , x), x++; n     int val = pre[y] - pre[x - 1]; n     if (x % 2 == 0) val *= -1; n     int extra = val - (val / 2); n     if (x % 2 == 0) extra *= -1; n     val = pre[x - 1] + extra + n; n     auto it = lower_bound(v.begin(), v.end(), make_pair(val, x)); n     printf( %d n , it->second); n   } n } n int main() { n   int t = 1; n   scanf( %d , &t); n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n const int mod = 1e9 + 7; n namespace math n { n  n     int inc(int x, int y) { return x + y >= mod ? x + y - mod : x + y; } n     int qpow(int a, int x, int mo) n     { n         int res = 1; n         while (x) n         { n             if (x & 1) n                 res = 1ll * res * a % mod; n             a = 1ll * a * a % mod; n             x >>= 1; n         } n         return res; n     } n     vector<int> Fac, invFac; n     void Finit(int n) n     { n         Fac.resize(n + 1); n         invFac.resize(n + 1); n         Fac[0] = 1; n         invFac[0] = 1; n         for (int i = 1; i <= n; i++) n             Fac[i] = 1ll * Fac[i - 1] * i % mod; n         invFac[n] = qpow(Fac[n], mod - 2, mod); n         for (int i = n - 1; i >= 1; i--) n             invFac[i] = 1ll * invFac[i + 1] * (i + 1) % mod; n     } n     int C(int n, int m) n     { n         if (m == 0) n             return 1; n         if (n < m || m < 0) n             return 0; n         return (int)(1ll * Fac[n] * invFac[m] % mod * invFac[n - m] % mod); n     } n     int A(int n, int m) n     { n         if (n < m || m < 0) n             return 0; n         return (int)(1ll * Fac[n] * invFac[n - m] % mod); n     } n } n using namespace math; n void solve() n { n     int n, l, r; n     cin >> n >> l >> r; n     Finit(n); n     int ans = 0; n     int up = r - n, dn = 1 - l; n     if (dn < up) n     { n         if (n & 1) n         { n             ans = inc(ans, 2ll * dn * C(n, n / 2) % mod); n         } n         else n         { n             ans = inc(ans, 1ll * dn * C(n, n / 2) % mod); n         } n         for (int i = 1; i <= n; i++) n         { n             int need_add = i; n             int need_del = max(0, dn - up + i); n             if (n - need_del - need_add < 0) n                 break; n             ans = inc(ans, 1ll * C(n - need_del - need_add, n / 2 - need_add) % mod); n             if (n & 1) n                 ans = inc(ans, 1ll * C(n - need_del - need_add, n / 2 + 1 - need_add) % mod); n         } n     } n     else n     { n         if (n & 1) n         { n             ans = inc(ans, 2ll * up * C(n, n / 2) % mod); n         } n         else n         { n             ans = inc(ans, 1ll * up * C(n, n / 2) % mod); n         } n  n         for (int i = 1; i <= n; i++) n         { n             int need_del = i; n             int need_add = max(0, up - dn + i); n  n             if (n - need_del - need_add < 0) n                 break; n             ans = inc(ans, 1ll * C(n - need_del - need_add, n / 2 - need_add) % mod); n             if (n & 1) n                 ans = inc(ans, 1ll * C(n - need_del - need_add, n / 2 + 1 - need_add) % mod); n             //cout << ans << endl; n         } n     } n     cout << ans << endl; n } n int main() n { n     ios::sync_with_stdio(false); n     cin.tie(nullptr); n     int T = 1; n     cin >> T; n     while (T--) n     { n         solve(); n     } n }


#include <bits/stdc++.h> n using namespace std; n const long long p = 1e9 + 7; n long long read() { n   long long s = 0; n   char c = getchar(), lc =  + ; n   while (c <  0  ||  9  < c) lc = c, c = getchar(); n   while ( 0  <= c && c <=  9 ) s = s * 10 + c -  0 , c = getchar(); n   return lc ==  -  ? -s : s; n } n void write(long long x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x < 10) n     putchar(x +  0 ); n   else { n     write(x / 10); n     putchar(x % 10 +  0 ); n   } n } n void print(long long x, char c =   n ) { n   write(x); n   putchar(c); n } n inline void add(long long &x, long long y) { n   x += y; n   if (x >= p) x -= p; n } n inline long long lowbit(long long x) { return x & -x; } n inline long long calc(long long x1, long long x2, long long y1, long long y2, n                       long long k) { n   if (x2 - x1 < y2 - y1) swap(x1, y1), swap(x2, y2); n   long long tmp = x2 - x1; n   x2 = (x1 = (x1 ^ y1) & ~(tmp - 1)) + tmp; n   x1++; n   x2 = min(x2, k); n   if (x1 > x2 || y1 > y2) return 0; n   return (x1 + x2) * (x2 - x1 + 1) / 2 % p * (y2 - y1) % p; n } n inline long long solve(long long x, long long y, long long k) { n   long long ret = 0; n   for (long long i = x; i; i &= i - 1) n     for (long long j = y; j; j &= j - 1) n       add(ret, calc(i - lowbit(i), i, j - lowbit(j), j, k)); n   return ret; n } n signed main() { n   long long q = read(); n   while (q--) { n     long long x1 = read(), y1 = read(), x2 = read(), y2 = read(), k = read(); n     print((solve(x2, y2, k) - solve(x1 - 1, y2, k) - solve(x2, y1 - 1, k) + n            solve(x1 - 1, y1 - 1, k) + p + p) % n           p); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Nmax = 100000 + 10; n int n; n int nr[Nmax]; n char s[Nmax], ans[Nmax]; n bool ok(int L, int R) { n   int i, sum; n   if (L > R) return 0; n   for (i = 1; i <= n; ++i) nr[i] = s[i] -  0 ; n   if (L == 1 && nr[R] == 0) return 0; n   if (L == 2 && nr[1] != 1) return 0; n   for (nr[L] += nr[L - 1] * 10; true; L++, R--) { n     if (L == R) { n       ans[L] = nr[L] / 2 +  0 ; n       return nr[L] % 2 == 0; n     } n     if (L + 1 == R) { n       sum = nr[L]; n       if (nr[L] == nr[R] + 11) sum--; n       ans[L] = ans[R] = sum / 2 +  0 ; n       if (sum & 1) ans[L]++; n       return (nr[L] == nr[R] || nr[L] == nr[R] + 11); n     } n     if (nr[L] - nr[R] != 0 && nr[L] - nr[R] != 1 && nr[L] - nr[R] != 10 && n         nr[L] - nr[R] != 11) n       return 0; n     sum = nr[L]; n     if (nr[L] == nr[R]) n       ; n     if (nr[L] == nr[R] + 1 || nr[L] == nr[R] + 11) sum--, nr[L + 1] += 10; n     if (nr[L] == nr[R] + 10 || nr[L] == nr[R] + 11) { n       if (nr[R] == 9) return 0; n       for (i = R - 1; i >= L && nr[i] == 0; --i) nr[i] = 9; n       if (i == L) return 0; n       nr[i]--; n     } n     ans[L] = ans[R] = sum / 2 +  0 ; n     if (sum & 1) ans[L]++; n     int mata = 5; n   } n   return 1; n } n int main() { n   gets(s + 1); n   n = strlen(s + 1); n   if (ok(1, n)) n     puts(ans + 1); n   else if (ok(2, n)) n     puts(ans + 2); n   else n     puts( 0 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200005; n set<int> ab[MAXN][2]; n int N, M, Q; n struct segTree { n   int f, t, mini, maxi; n   bool ans; n   segTree *L, *R; n   segTree(int _f, int _t) : f(_f), t(_t) { n     ans = false; n     mini = INT_MAX; n     maxi = INT_MIN; n     if (f != t) { n       L = new segTree(f, (f + t) / 2); n       R = new segTree((f + t) / 2 + 1, t); n     } n   } n   void update(int x, int v, bool flag) { n     if (f == t && t == x) { n       if (flag) n         maxi = v; n       else n         mini = v; n       ans = maxi >= mini; n       return; n     } n     if (x < R->f) n       L->update(x, v, flag); n     else n       R->update(x, v, flag); n     mini = min(R->mini, L->mini); n     maxi = max(R->maxi, L->maxi); n     ans = R->ans | L->ans | (R->maxi >= L->mini); n   } n }; n int main() { n   scanf( %d %d %d , &N, &M, &Q); n   segTree *tree = new segTree(0, N + 1); n   for (int i = 0; i < Q; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     x--, y--; n     bool flag = (x & 1); n     int mult = (flag ? -1 : 1); n     if (ab[x / 2][flag].count((y / 2) * mult)) n       ab[x / 2][flag].erase((y / 2) * mult); n     else n       ab[x / 2][flag].insert((y / 2) * mult); n     int val = INT_MAX; n     if (flag) val = INT_MIN; n     if (ab[x / 2][flag].size() > 0) val = abs(*ab[x / 2][flag].begin()); n     tree->update(x / 2, val, flag); n     puts((!tree->ans ?  YES  :  NO )); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, sum, m[17], r[17]; n double ans; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> m[i]; n   for (int i = 1; i <= n; i++) cin >> r[i]; n   for (int i = 1; i <= 999999; i++) n     for (int j = 1; j <= n; j++) n       if (i % m[j] == r[j]) { n         sum++; n         break; n       } n   ans = 1.0 * sum / 999999; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int o; n   long long w, s, t; n   cin >> o; n   for (int i = 0; i < o; i++) { n     cin >> w >> s >> t; n     long long minnn = min(s, t); n     cout << (w - minnn) + 1 <<   n ; n   } n } n


#include<bits/stdc++.h> n using namespace std; n  n #define ll long long n #define mtv(kit) cout<<#kit<<  -  <<kit<<  n ; n #define ff first n #define ss second n #define pb push_back n #define rep(i,a,b) for(ll i=a;i<b;i++) n #define pii pair<ll , ll> n #define all(x) x.begin(),x.end() n #define nl   n  n #define ump unordered_map n  n  n void doit(){ n  ll b, g, k; cin >> b >> g >> k; n  vector<pii>p(k); n  vector<ll>B(b + 2), G(g + 2); n  for(ll i = 0; i < k; i++){ n   cin >> p[i].ff; n   B[p[i].ff]++; n  } n  for(ll i = 0; i < k; i++){ n   cin >> p[i].ss; n   G[p[i].ss]++; n  } n  ll ans = 0; n  for(ll i = 0; i < k ; i++){ n   ans += (k - i - 1) - (B[p[i].ff] + G[p[i].ss] - 2); n   B[p[i].ff]--; n   G[p[i].ss]--; n  } n  cout << ans << nl; n } n  n int main(){ n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     ll t = 1; n  cin >> t; n     for(ll i = 1; i <= t; i++){ n         doit(); n     } n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[5]; n int main() { n   int n, t; n   cin >> n; n   memset(cnt, 0, sizeof(0)); n   for (int i = 0; i < n; i++) { n     cin >> t; n     cnt[t]++; n   } n   bool flag = true; n   int ret = 0; n   int minN = min(cnt[1], cnt[2]); n   cnt[3] += minN; n   cnt[1] -= minN; n   cnt[2] -= minN; n   ret += minN; n   if (cnt[1] != 0) { n     while (cnt[1] >= 3) { n       cnt[3]++; n       ret += 2; n       cnt[1] -= 3; n     } n     if (cnt[1] == 1) { n       if (cnt[3] > 0) { n         cnt[4] + 1; n         cnt[3]--; n         cnt[1] = 0; n         ret += 1; n       } else if (cnt[4] >= 2) { n         cnt[4] -= 2; n         cnt[3] += 3; n         cnt[1] = 0; n         ret += 2; n       } else n         flag = false; n     } else if (cnt[1] == 2) { n       if (cnt[4] > 0) { n         cnt[4] -= 1; n         cnt[3] += 2; n         ret += 2; n         cnt[1] = 0; n       } else if (cnt[3] >= 2) { n         cnt[3] -= 2; n         cnt[4] += 2; n         cnt[1] = 0; n         ret += 2; n       } else n         flag = false; n     } n   } else { n     while (cnt[2] >= 3) { n       cnt[2] -= 3; n       ret += 2; n       cnt[3] += 2; n     } n     if (cnt[2] == 1) { n       if (cnt[4] > 0) { n         cnt[4]--; n         cnt[3] += 1; n         cnt[2] = 0; n         ret++; n       } else if (cnt[3] > 1) { n         cnt[4] += 2; n         cnt[3] -= 2; n         cnt[2] = 0; n         ret += 2; n       } else n         flag = false; n     } else if (cnt[2] == 2) { n       cnt[2] = 0; n       cnt[4]++; n       ret += 2; n     } n   } n   if (flag) n     cout << ret << endl; n   else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n, t, k; n int main() { n   while (scanf( %d%d , &n, &k) == 2) { n     if (k > n || (n >= 2 && k == 1)) n       printf( -1 n ); n     else if (k == 1) n       printf( %c n ,  a ); n     else { n       int m = n - (k - 2); n       for (int i = 0; i < m; i++) printf( %c , i % 2 == 0 ?  a  :  b ); n       for (int i = 0; i < k - 2; i++) printf( %c ,  a  + 2 + i); n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n long long dx[4] = {0, 0, -1, 1}; n long long dy[4] = {1, -1, 0, 0}; n int n, m, q; n char s[11]; n long long bz[100010 << 1][52]; n int pos[100010 << 1][52]; n int head[100010 << 1], nex[100010 << 1], val[100010 << 1], op[100010 << 1], tot; n int T[100010 << 2]; n void pushdown(int rt) { n   if (T[rt]) { n     T[rt << 1] = T[rt << 1 | 1] = T[rt]; n     T[rt] = 0; n   } n } n void insert(int l, int r, int rt, int x, int y, int v) { n   if (x <= l && r <= y) { n     T[rt] = v; n     return; n   } n   int mid = l + r >> 1; n   pushdown(rt); n   if (mid >= x) insert(l, mid, rt << 1, x, y, v); n   if (mid < y) insert(mid + 1, r, rt << 1 | 1, x, y, v); n } n int query(int l, int r, int rt, int v) { n   if (l == r) return T[rt]; n   int mid = l + r >> 1; n   pushdown(rt); n   if (mid >= v) n     return query(l, mid, rt << 1, v); n   else n     return query(mid + 1, r, rt << 1 | 1, v); n } n void add(long long x, long long y, int tp) { n   tot++; n   nex[tot] = head[x]; n   head[x] = tot; n   val[tot] = y; n   op[tot] = tp; n } n struct vec { n   long long x1, x2, y1, y2; n   int op; n   void read() { n     scanf( %I64d%I64d%I64d%I64d , &x1, &y1, &x2, &y2); n     if (y1 < y2) op = 0; n     if (y1 > y2) op = 1; n     if (x1 > x2) op = 2; n     if (x1 < x2) op = 3; n   } n } a[100010]; n struct node { n   int op; n   long long len, x, y; n   void read() { n     scanf( %I64d%I64d%s%I64d , &x, &y, s, &len); n     if (s[0] ==  U ) op = 0; n     if (s[0] ==  D ) op = 1; n     if (s[0] ==  L ) op = 2; n     if (s[0] ==  R ) op = 3; n   } n } b[100010]; n pair<long long, long long> cal(long long x, long long y, int p) { n   if (a[p].x1 == a[p].x2) { n     if (a[p].x1 == x) { n       if (min(a[p].y1, a[p].y2) <= y && y <= max(a[p].y1, a[p].y2)) n         return make_pair(x, y); n       if (y < min(a[p].y1, a[p].y2)) n         return make_pair(x, min(a[p].y1, a[p].y2)); n       else n         return make_pair(x, max(a[p].y1, a[p].y2)); n     } else n       return make_pair(a[p].x1, y); n   } else { n     if (a[p].y1 == y) { n       if (min(a[p].x1, a[p].x2) <= x && x <= max(a[p].x1, a[p].x2)) n         return make_pair(x, y); n       if (x < min(a[p].x1, a[p].x2)) n         return make_pair(min(a[p].x1, a[p].x2), y); n       else n         return make_pair(max(a[p].x1, a[p].x2), y); n     } else n       return make_pair(x, a[p].y1); n   } n } n int dis(long long x1, long long y1, long long x2, long long y2) { n   return abs(x1 - x2) + abs(y1 - y2); n } n pair<long long, long long> find(long long x, long long y, int tp, n                                 long long step) { n   return make_pair(x + step * dx[tp], y + step * dy[tp]); n } n void print(pair<long long, long long> p) { n   long long x = p.first, y = p.second; n   x = max(x, 0ll); n   x = min(x, (long long)m); n   y = max(y, 0ll); n   y = min(y, (long long)m); n   printf( %I64d %I64d n , x, y); n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) a[i].read(); n   q = read(); n   for (int i = 1; i <= q; i++) b[i].read(); n   for (int i = 0; i <= 3; i++) { n     tot = 0; n     memset(head, 0, sizeof(head)); n     memset(T, 0, sizeof(T)); n     for (int j = 1; j <= q; j++) n       if (b[j].op == i) { n         if (i < 2) n           add(b[j].y, j + n, 1); n         else n           add(b[j].x, j + n, 1); n       } n     for (int j = 1; j <= n; j++) { n       if (i == 0) add(max(a[j].y1, a[j].y2), j, 0); n       if (i == 1) add(min(a[j].y1, a[j].y2), j, 0); n       if (i == 2) add(min(a[j].x1, a[j].x2), j, 0); n       if (i == 3) add(max(a[j].x1, a[j].x2), j, 0); n     } n     for (int j = 1; j <= n; j++) n       if (a[j].op == i) { n         if (i < 2) n           add(a[j].y2, j, 1); n         else n           add(a[j].x2, j, 1); n       } n     int now = (i == 0 || i == 3) ? m : 0; n     while (now != -1 && now != m + 1) { n       for (int j = head[now]; j; j = nex[j]) { n         if (!op[j]) { n           if (i < 2) n             insert(0, m, 1, min(a[val[j]].x1, a[val[j]].x2), n                    max(a[val[j]].x1, a[val[j]].x2), val[j]); n           else n             insert(0, m, 1, min(a[val[j]].y1, a[val[j]].y2), n                    max(a[val[j]].y1, a[val[j]].y2), val[j]); n         } else { n           int tmp; n           if (val[j] <= n) { n             if (i < 2) n               tmp = query(0, m, 1, a[val[j]].x1); n             else n               tmp = query(0, m, 1, a[val[j]].y1); n           } else { n             if (i < 2) n               tmp = query(0, m, 1, b[val[j] - n].x); n             else n               tmp = query(0, m, 1, b[val[j] - n].y); n           } n           pos[val[j]][0] = tmp; n           if (tmp) { n             if (val[j] <= n) n               bz[val[j]][0] = n                   abs(a[tmp].x2 - a[val[j]].x2) + abs(a[tmp].y2 - a[val[j]].y2); n             else n               bz[val[j]][0] = abs(a[tmp].x2 - b[val[j] - n].x) + n                               abs(a[tmp].y2 - b[val[j] - n].y); n           } else n             bz[val[j]][0] = 1ll << 50; n         } n       } n       if (i == 0 || i == 3) n         now--; n       else n         now++; n     } n   } n   for (int i = 1; i <= 50; i++) bz[0][i] = 1ll << 50; n   for (int i = 1; i <= 50; i++) n     for (int j = 1; j <= n + q; j++) { n       pos[j][i] = pos[pos[j][i - 1]][i - 1]; n       bz[j][i] = bz[j][i - 1] + bz[pos[j][i - 1]][i - 1]; n       bz[j][i] = min(bz[j][i], 1ll << 50); n     } n   for (int i = 1; i <= q; i++) { n     int tmp = i + n; n     long long t = b[i].len; n     for (int j = 50; j >= 0; j--) n       if (bz[tmp][j] <= t) { n         t -= bz[tmp][j]; n         tmp = pos[tmp][j]; n       } n     if (tmp == i + n) { n       if (pos[tmp][0]) { n         pair<long long, long long> tq = cal(b[i].x, b[i].y, pos[tmp][0]); n         int tw = dis(tq.first, tq.second, b[i].x, b[i].y); n         if (tw >= t) n           print(find(b[i].x, b[i].y, b[i].op, t)); n         else n           print(find(tq.first, tq.second, a[pos[tmp][0]].op, t - tw)); n       } else n         print(find(b[i].x, b[i].y, b[i].op, t)); n     } else { n       if (!pos[tmp][0]) n         print(find(a[tmp].x2, a[tmp].y2, a[tmp].op, t)); n       else { n         pair<long long, long long> tq = cal(a[tmp].x2, a[tmp].y2, pos[tmp][0]); n         int tw = dis(tq.first, tq.second, a[tmp].x2, a[tmp].y2); n         if (tw >= t) n           print(find(a[tmp].x2, a[tmp].y2, a[tmp].op, t)); n         else n           print(find(tq.first, tq.second, a[pos[tmp][0]].op, t - tw)); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const long long INF = (long long)1e18 + 100; n long long safe_sum(long long a, long long b) { n   long long s = (a + b); n   s = min(s, INF); n   return s; n } n long long safe_mult(long long a, long long b) { n   if (a == 0 || b == 0) return 0; n   if (a >= ((INF + b - 1) / b)) return INF; n   return (a * b); n } n const int maxN = 1005; n long long c[maxN][maxN]; n long long gcd(long long x, long long y) { n   while (x > 0 && y > 0) { n     if (x < y) swap(x, y); n     x %= y; n   } n   return (x + y); n } n long long cnk(long long x, int a) { n   vector<long long> cur; n   for (int i = 0; i < a; i++) { n     cur.push_back(x - i); n   } n   for (int j = 1; j <= a; j++) { n     int now = j; n     for (auto& it : cur) { n       if (now == 1) break; n       int d = (int)(gcd(it, now)); n       it /= d; n       now /= d; n     } n   } n   long long ans = 1; n   for (auto& it : cur) ans = safe_mult(ans, it); n   return ans; n } n int n; n long long k; n const int maxT = 2 * (int)1e5 + 100; n const int BUBEN = 10; n long long a[maxT]; n long long get_cnk(long long x, long long y) { n   if (x - y < y) return get_cnk(x, x - y); n   if (x < y) return 0; n   if (x < maxN) { n     return c[x][y]; n   } n   if (y >= BUBEN) n     return INF; n   else n     return cnk(x, (int)y); n } n bool ok(long long who) { n   long long f = 0; n   for (int i = 0; i < n; i++) { n     f = safe_sum(f, safe_mult(a[i], get_cnk(n - i - 1 + who - 1, who - 1))); n     if (f >= k) return true; n   } n   return (f >= k); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   c[0][0] = 1; n   for (int i = 1; i < maxN; i++) { n     c[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       c[i][j] = safe_sum(c[i - 1][j], c[i - 1][j - 1]); n     } n   } n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] >= k) { n       cout << 0; n       return 0; n     } n   } n   long long l = 0; n   long long r = (long long)(1e18) + 100; n   while (r - l > 1) { n     long long mid = (l + r) / 2; n     if (ok(mid)) n       r = mid; n     else n       l = mid; n   } n   cout << r; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct pvec { n   long long int x, y; n   pvec(long long int x = 0, long long int y = 0) : x(x), y(y) {} n   pvec operator-(pvec p) { return pvec(x - p.x, y - p.y); } n   bool operator==(pvec p) { return x == p.x and y == p.y; } n   long long int dot(pvec p) { return x * p.x + y * p.y; } n   long long int abs2() { return x * x + y * y; } n }; n pvec v[12], Q[12], R[12]; n long long int Qi[12], Ri[12]; n long long int perm[][3] = {{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, n                            {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}; n inline bool quadrado() { n   for (long long int i = 0; i < 6; i++) { n     pvec p0 = Q[0]; n     pvec p1 = Q[perm[i][0]]; n     pvec p2 = Q[perm[i][1]]; n     pvec p3 = Q[perm[i][2]]; n     pvec v1 = p1 - p0, v2 = p2 - p1, v3 = (p2 - p3), v4 = (p3 - p0); n     if (!(v1 == v3 and v2 == v4)) continue; n     if (v1.abs2() != v2.abs2()) continue; n     if (v1.dot(v2) != 0) continue; n     return true; n   } n   return false; n } n inline bool retangulo() { n   for (long long int i = 0; i < 6; i++) { n     pvec p0 = R[0]; n     pvec p1 = R[perm[i][0]]; n     pvec p2 = R[perm[i][1]]; n     pvec p3 = R[perm[i][2]]; n     pvec v1 = p1 - p0, v2 = p2 - p1, v3 = (p2 - p3), v4 = (p3 - p0); n     if (!(v1 == v3 and v2 == v4)) continue; n     if (v1.dot(v2) != 0) continue; n     return true; n   } n   return false; n } n bool da(long long int u, long long int quad, long long int ret) { n   if (quad > 4 or ret > 4) return false; n   if (u == 8) { n     if (quadrado() and retangulo()) return true; n     return false; n   } n   Q[quad] = v[u]; n   Qi[quad] = u; n   if (da(u + 1, quad + 1, ret)) return true; n   R[ret] = v[u]; n   Ri[ret] = u; n   if (da(u + 1, quad, ret + 1)) return true; n   return false; n } n int main() { n   for (long long int i = 0; i < 8; i++) { n     long long int x, y; n     cin >> x >> y; n     v[i] = pvec(x, y); n   } n   if (!da(0, 0, 0)) { n     cout <<  NO  << endl; n   } else { n     cout <<  YES  << endl; n     for (long long int i = 0; i < 4; i++) { n       if (i) printf(   ); n       cout << Qi[i] + 1; n     } n     cout << endl; n     for (long long int i = 0; i < 4; i++) { n       if (i) printf(   ); n       cout << Ri[i] + 1; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a = 1, n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     a *= i + 1; n   } n   cout << a * 2 / n / n; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long l, r, b, c, a; n     long long m; n     cin >> l >> r >> m; n     for (long long i = l; i <= r; i++) { n       a = i; n       long long n = m / a, temp = a * n, diff = m - temp; n       if (n > 0 && abs(m - temp) <= r - l) { n         if (diff >= 0) { n           b = r; n           c = r - m + temp; n         } else { n           b = l; n           c = l + temp - m; n         } n         cout << a <<     << b <<     << c <<   n ; n         break; n       } n       n = m / a + 1, temp = a * n, diff = m - temp; n       if (abs(m - temp) <= r - l) { n         if (diff >= 0) { n           b = r; n           c = r - m + temp; n         } else { n           b = l; n           c = l + temp - m; n         } n         cout << a <<     << b <<     << c <<   n ; n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n double a, v, d, w, l, ans, mid, t2, t; n bool check(double maxv) { n   return d - ((maxv + w) / 2) * (((maxv - w) * 2) / a) > 1e-8; n } n int main() { n   scanf( %lf %lf %lf %lf %lf , &a, &v, &l, &d, &w); n   w = min(min(w, v), sqrt(2 * a * d)); n   if (2 * a * d + w * w > 2 * v * v) { n     t = (d - v * v / a + w * w / a / 2) / v + (2 * v - w) / a; n   } else { n     t = (2 * sqrt(a * d + w * w / 2) - w) / a; n   } n   l = l - d; n   t2 = (v - w) / a; n   if (l - w * t2 - 0.5 * a * t2 * t2 > 0) { n     t2 = t2 + (l - w * t2 - 0.5 * a * t2 * t2) / v; n   } else { n     t2 = (-w + sqrt(w * w + 2.0 * a * l)) / a; n   } n   printf( %.10f n , t + t2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 200020; n int n, a[Maxn]; n long long sum[Maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 0; i < n - 1; i++) { n     int cur; n     for (cur = 1; cur <= i;) { n       int val = i / cur; n       int ncur = i / val + 1; n       if (a[i + 2] < a[val + 1]) { n         sum[cur]++; n         sum[ncur]--; n       } n       cur = ncur; n     } n     if (a[i + 2] < a[1]) sum[cur]++, sum[n]--; n   } n   for (int i = 1; i < n; i++) sum[i] += sum[i - 1], printf( %I64d n , sum[i]); n   ; n } n


#include <bits/stdc++.h> n int main() { n   int n, i; n   int maximum, maxIndex, target; n   int A[1000]; n   scanf( %d , &n); n   maximum = -1; n   for (i = 0; i < n; ++i) { n     scanf( %d , &A[i]); n     if (A[i] > maximum) { n       maximum = A[i]; n       maxIndex = i; n     } n   } n   A[maxIndex] = -2; n   target = -1; n   for (i = 0; i < n; ++i) { n     if (A[i] > target) { n       target = A[i]; n     } n   } n   printf( %d %d , maxIndex + 1, target); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i]--; n   } n   vector<int> v(n), d(n); n   queue<int> q; n   q.push(0); n   v[0] = 1; n   int l = 0; n   while (!q.empty()) { n     int nn = q.size(); n     while (nn-- > 0) { n       int t = q.front(); n       q.pop(); n       d[t] = l; n       if (t > 0 && v[t - 1] == 0) { n         q.push(t - 1); n         v[t - 1] = 1; n       } n       if (t < n - 1 && v[t + 1] == 0) { n         q.push(t + 1); n         v[t + 1] = 1; n       } n       if (v[a[t]] == 0) { n         q.push(a[t]); n         v[a[t]] = 1; n       } n     } n     l++; n   } n   for (int i : d) cout << i <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n int n, k; n int a[2011]; n long long pre[1 << 11 + 1]; n long long cur[1 << 11 + 1]; n void solve() { n   memset(pre, 0, sizeof(pre)); n   pre[0] = 1; n   for (int i = 0; i < n; i++) { n     memset(cur, 0, sizeof(cur)); n     for (int j = 0; j < (1 << k) + 1; j++) { n       if (a[i] == 2 || a[i] == 0) { n         int nj = min(j + 2, 1 << k); n         cur[nj] = (cur[nj] + pre[j]) % mod; n       } n       if (a[i] == 4 || a[i] == 0) { n         int nj = min(j + 4, 1 << k); n         if ((j & 2) != 0) { n           nj = 4; n         } n         cur[nj] = (cur[nj] + pre[j]) % mod; n       } n     } n     memcpy(pre, cur, sizeof(cur)); n   } n   cout << pre[1 << k] << endl; n } n void coding() { n   while (cin >> n >> k) { n     for (int i = 0; i < n; i++) cin >> a[i]; n     solve(); n   } n } n int main() { coding(); } n


#include <bits/stdc++.h> n using namespace std; n const long long dp = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int n, a; n   cin >> n >> a; n   vector<int> vec(n); n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     cin >> vec[i]; n     if (vec[i]) cnt++; n   } n   for (int i = 0; i < a; i++) { n     if (vec[a - 1 - i] + vec[a - 1 + i] == 1) cnt--; n     if (i >= n - a) break; n   } n   cout << cnt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 2005; n const int inf = 1e9; n using namespace std; n vector<pair<int, int>> G[maxn]; n vector<int> C[maxn], v; n bool critical[maxn * maxn]; n vector<int> seen, sum, col, depth, up; n void dfs(int node, int ind) { n   seen[node] = 1; n   int minFromSons = inf; n   up[node] = depth[node]; n   for (auto tmp : G[node]) { n     if (seen[tmp.first] && tmp.second != ind) n       up[node] = min(up[node], depth[tmp.first]); n     else if (!seen[tmp.first]) { n       depth[tmp.first] = depth[node] + 1; n       dfs(tmp.first, tmp.second); n       minFromSons = min(minFromSons, up[tmp.first]); n     } n   } n   up[node] = min(up[node], minFromSons); n   if (minFromSons >= depth[node] && ind != -1) { n     critical[ind] = 1; n   } n } n void dfsColor(int node, int color) { n   col[node] = color; n   v[color]++; n   for (auto tmp : G[node]) { n     if (critical[tmp.second] == 1 && col[tmp.first] == 0) continue; n     if (critical[tmp.second] == 1 && col[tmp.first]) { n       C[col[tmp.first]].push_back(color); n       C[color].push_back(col[tmp.first]); n       continue; n     } else if (col[tmp.first] == 0) n       dfsColor(tmp.first, color); n   } n } n void dfsSub(int node) { n   seen[node] = 1; n   sum[node] = v[node]; n   for (auto tmp : C[node]) n     if (!seen[tmp]) { n       dfsSub(tmp); n       sum[node] += sum[tmp]; n     } n } n int main() { n   ios_base ::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int a, b; n     cin >> a >> b; n     G[a].push_back(make_pair(b, i)); n     G[b].push_back(make_pair(a, i)); n   } n   seen = vector<int>(n + 1, 0); n   depth = vector<int>(n + 1, 0); n   up = vector<int>(n + 1, 0); n   dfs(1, -1); n   int c = 0; n   col = vector<int>(n + 1, 0); n   v = vector<int>(n + 1, 0); n   int all = 0; n   for (int i = 1; i <= n; ++i) n     if (col[i] == 0) { n       ++c; n       dfsColor(i, c); n       all += v[c] * v[c]; n     } n   int ans = 0; n   for (int i = 1; i <= c; ++i) { n     seen = vector<int>(c + 1, 0); n     sum = vector<int>(c + 1, 0); n     dfsSub(i); n     int tmp = 0; n     for (int j = 1; j <= c; ++j) tmp += sum[j] * v[j]; n     vector<int> can(n + 1, 0); n     can[0] = 1; n     for (auto tmp : C[i]) { n       for (int j = n; j >= 0; --j) n         if (can[j] && j + sum[tmp] <= n) can[j + sum[tmp]] = 1; n     } n     int res = -1; n     for (int j = (n - v[i]) / 2; j >= 0; --j) n       if (can[j]) { n         res = j; n         break; n       } n     tmp += res * (n - v[i] - res); n     ans = max(ans, tmp); n   } n   cerr << all <<   n ; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n int main() { n   int x, count = 0, i; n   char ar[50]; n   for (i = 0; i < 9; i++) scanf(  %c , &ar[i]); n   for (i = 0; i < 9; i++) { n     if (ar[i] == ar[8 - i]) count++; n   } n   if (count == 9) n     printf( YES ); n   else n     printf( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int shape[4][4][2] = {{{1, 0}, {2, 0}, {1, 1}, {1, 2}}, n                             {{1, 0}, {2, 0}, {2, -1}, {2, 1}}, n                             {{0, 1}, {0, 2}, {-1, 2}, {1, 2}}, n                             {{0, 1}, {0, 2}, {1, 1}, {2, 1}}}; n int x, y; n struct node { n   char g[9][9]; n   int num; n   int sx, sy; n   int layer; n   node() { n     layer = 0; n     sx = sy = 0; n     memset(g, 0, sizeof(g)); n     num = 0; n   } n   void printNode() { n     for (int i = 0; i < y; i++) { n       for (int j = 0; j < x; j++) printf( %c , g[i][j] ? g[i][j] :  . ); n       cout <<    << endl; n     } n   } n }; n stack<node> q; n int ans; n node ansNode; n int maxG[9][9][4]; n void bfs() { n   while (!q.empty()) { n     node now = q.top(); n     q.pop(); n     if (now.num > ans) { n       ans = now.num; n       ansNode = now; n     } n     now.num++; n     char ch =  A  + now.num - 1; n     for (int i = now.sy; i < y; i++) { n       for (int j = 0; j < x; j++) { n         if (!now.g[i][j]) { n           for (int k = 0; k < 4; k++) { n             if (maxG[i][j][k] > now.num + 1) continue; n             bool flag = true; n             for (int l = 0; l < 4 && flag; l++) { n               int tx = j + shape[k][l][1]; n               int ty = i + shape[k][l][0]; n               if (tx < 0 || tx >= x || ty < 0 || ty >= y || now.g[ty][tx]) { n                 flag = false; n               } n             } n             if (flag) { n               node tNode = now; n               tNode.g[i][j] = ch; n               for (int l = 0; l < 4; l++) { n                 int tx = j + shape[k][l][1]; n                 int ty = i + shape[k][l][0]; n                 tNode.g[ty][tx] = ch; n               } n               tNode.sx = j + 1; n               tNode.sy = i; n               tNode.layer++; n               q.push(tNode); n               maxG[i][j][k] = max(maxG[i][j][k], now.num); n             } n           } n         } n       } n     } n   } n } n int main() { n   ans = -1; n   cin >> y >> x; n   while (!q.empty()) q.pop(); n   q.push(node()); n   bfs(); n   cout << ans << endl; n   ansNode.printNode(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100009; n int sol, qs[N], qe[N], sorted[N], ans[N], a[N], s, e; n unordered_map<int, int> freq; n void add(int el) { n   sol -= !(freq[el] ^ el); n   freq[el]++; n   sol += !(freq[el] ^ el); n } n void remove(int el) { n   sol -= !(freq[el] ^ el); n   freq[el]--; n   sol += !(freq[el] ^ el); n } n void mo(int idx) { n   while (s > qs[idx]) add(a[--s]); n   while (e <= qe[idx]) add(a[e++]); n   while (s < qs[idx]) remove(a[s++]); n   while (e > qe[idx] + 1) remove(a[--e]); n   ans[idx] = sol; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) { n     cin >> qs[i] >> qe[i]; n     qs[i]--, qe[i]--; n     sorted[i] = i; n   } n   int sq = sqrt(n); n   sort(sorted, sorted + m, [sq](int i, int j) { n     return make_pair(qs[i] / sq, qe[i]) < make_pair(qs[j] / sq, qe[j]); n   }); n   for (int i = 0; i < m; i++) mo(sorted[i]); n   for (int i = 0; i < m; i++) cout << ans[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using min_pq = priority_queue<T, vector<T>, greater<T>>; n const int MAXN = 1e5 + 10, MAXQ = 3e5 + 10, MAXL = 20, ALP = 26, MOD = 1e9 + 7, n           MAXK = 17, INF = 1e9 + 10, MAXA = 30, MAXB = 24, MAXBB = (1 << MAXB); n const string no =  NO n , yes =  YES n ; n const int hA[4] = {1, 0, -1, 0}, kA[4] = {0, 1, 0, -1}; n void solve() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int a; n       cin >> a; n       if ((a & 1) != ((i + j) & 1)) a++; n       cout << a <<    ; n     } n     cout <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, sum1 = 0, res1 = 0, last = 0; n   bool flag = true; n   cin >> n; n   unordered_map<long long int, long long int> m; n   vector<pair<long long int, long long int>> v; n   for (long long int i = 0; i < n; i++) { n     long long int element; n     cin >> element; n     m[element]++; n     v.emplace_back(make_pair(element, i)); n   } n   if (n == 1) { n     cout << 1 <<   n ; n     cout << v[0].first <<     n          <<  0 n ; n     return 0; n   } n   sort(v.begin(), v.end()); n   vector<pair<long long int, long long int>> v1; n   for (long long int i = 0; i < v.size() - 1; i++) { n     flag = true; n     if (i < v.size() - 1 && v[i].first == v[i + 1].first) n       sum1 = v[i + 1].second - v[i].second; n     else if (i < v.size() - 1) n       sum1 = 0; n     while (i < v.size() - 1 && v[i].first == v[i + 1].first) { n       res1 = v[i + 1].second - v[i].second; n       if (res1 != sum1) { n         flag = false; n         break; n       } n       i++; n     } n     if (flag == true) { n       v1.push_back(make_pair(v[i].first, sum1)); n     } n     while (i < v.size() - 1 && v[i].first == v[i + 1].first) { n       i++; n     } n   } n   if (v[v.size() - 1].first != v[v.size() - 2].first) { n     v1.emplace_back(make_pair(v[v.size() - 1].first, 0)); n   } n   cout << v1.size() <<   n ; n   for (long long int i = 0; i < v1.size(); i++) { n     cout << v1[i].first <<     << v1[i].second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   long long int t, temp, i, ans = 0, n, m, index, j; n   cin >> n >> m; n   vector<pair<long long int, long long int> > pa; n   vector<long long int> v(n), finger(m); n   for (i = 0; i < n; i++) { n     cin >> v[i]; n   } n   for (i = 0; i < m; i++) { n     cin >> finger[i]; n     for (j = 0; j < n; j++) { n       if (v[j] == finger[i]) { n         pa.push_back(make_pair(j, finger[i])); n       } n     } n   } n   sort(pa.begin(), pa.end()); n   if (pa.size() == 0) n     cout <<   n ; n   else { n     for (auto it = pa.begin(); it != pa.end(); it++) { n       cout << it->second <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long inf2 = 0x3f3f3f3f3f3f3f3f; n const double eps = 1e-6; n const int mod = 1000000007; n namespace fastio { n char in[100000]; n int itr = 0, llen = 0; n char get() { n   if (itr == llen) llen = fread(in, 1, 100000, stdin), itr = 0; n   if (llen == 0) return EOF; n   return in[itr++]; n } n char out[100000]; n int itr2 = 0; n void put(char c) { n   out[itr2++] = c; n   if (itr2 == 100000) { n     fwrite(out, 1, 100000, stdout); n     itr2 = 0; n   } n } n int clear() { n   fwrite(out, 1, itr2, stdout); n   itr2 = 0; n   return 0; n } n int getint() { n   int r = 0; n   bool ng = 0; n   char c; n   c = get(); n   while (c !=  -  && (c <  0  || c >  9 )) c = get(); n   if (c ==  - ) ng = 1, c = get(); n   while (c >=  0  && c <=  9 ) r = r * 10 + c -  0 , c = get(); n   return ng ? -r : r; n } n string getstr() { n   string ret =   ; n   char ch = get(); n   while (ch ==     || ch ==   n ) ch = get(); n   while (ch !=     && ch !=   n ) ret.push_back(ch), ch = get(); n   return ret; n } n void putstr(string s) { n   for (int i = 0; i < s.size(); i++) put(s[i]); n } n void putint(int x) { n   if (x < 0) { n     put( - ); n     x = -x; n   } n   if (x == 0) { n     put( 0 ); n     return; n   } n   char c[20]; n   int pos = 0; n   while (x) { n     c[pos++] =  0  + x % 10; n     x /= 10; n   } n   for (int i = pos - 1; i >= 0; i--) put(c[i]); n } n void getarr(int arrname[], int size) { n   for (int i = 0; i < size; i++) arrname[i] = getint(); n } n }  // namespace fastio n using namespace fastio; n int number[1000005], BITN = 1e6; n void modify(int x, int y) { n   while (x <= BITN) { n     number[x] += y; n     x += x & -x; n   } n } n int sum(int x) { n   int ret = 0; n   while (x) { n     ret += number[x]; n     x &= x - 1; n   } n   return ret; n } n void add(int l, int r, int v) { n   modify(l, v); n   modify(r, -v); n } n int qry(int x) { return sum(x); } n int n, k; n vector<int> g[1000005]; n vector<int> ord; n int pos[1000005]; n bool used[1000005]; n int par[1000005]; n int sz[1000005], depth[1000005]; n void dfs(int x, int pr) { n   depth[x] = x == n ? 0 : depth[pr] + 1; n   pos[x] = ord.size() + 1; n   ord.push_back(x); n   sz[x] = 1; n   for (auto to : g[x]) n     if (to != pr) par[to] = x, dfs(to, x), sz[x] += sz[to]; n } n int main() { n   n = getint(); n   k = getint(); n   for (int i = 1; i < n; i++) { n     int a = getint(), b = getint(); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs(n, 0); n   for (int i = 0; i < ord.size(); i++) { n     modify(i + 1, depth[ord[i]] - (i == 0 ? 0 : depth[ord[i - 1]])); n   } n   used[n] = true; n   int rest = n - k - 1; n   for (int v = n - 1; v > 0 && rest; v--) { n     if (used[v]) continue; n     int len = qry(pos[v]); n     if (len <= rest) { n       int cur = v; n       while (!used[cur]) { n         used[cur] = true; n         add(pos[cur], pos[cur] + sz[cur], -1); n         cur = par[cur]; n         rest--; n       } n     } n   } n   vector<int> ans; n   for (int i = 1; i <= n; i++) n     if (!used[i]) ans.push_back(i); n   for (int i = 0; i < ans.size(); i++) putint(ans[i]), put(   ); n   clear(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5555; n int hackable[N]; n int sols[N]; n int pts[N][13]; n int score[N]; n int z[N]; n int bound[N]; n int cnt_hack[N]; n int rgt[N]; n int task[N][13]; n int guys[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int j = 0; j < 3; j++) { n     hackable[j] = 0; n     sols[j] = 0; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 3; j++) { n       scanf( %d , task[i] + j); n       if (task[i][j] < 0) { n         hackable[j]++; n       } n       if (task[i][j] > 0) { n         sols[j]++; n       } n     } n   } n   int cost[3]; n   int ans = n; n   for (cost[0] = 500; cost[0] <= 3000; cost[0] += 500) { n     for (cost[1] = 500; cost[1] <= 3000; cost[1] += 500) { n       for (cost[2] = 500; cost[2] <= 3000; cost[2] += 500) { n         bool err = false; n         for (int j = 0; j < 3; j++) { n           cnt_hack[j] = -1; n           for (int u = hackable[j]; u >= 0; u--) { n             int k = sols[j] + (hackable[j] - u); n             int real_cost = 500; n             int coeff = 2; n             while (coeff * k <= n && real_cost < 3000) { n               coeff *= 2; n               real_cost += 500; n             } n             if (real_cost == cost[j]) { n               cnt_hack[j] = u; n               break; n             } n           } n           if (cnt_hack[j] == -1) { n             err = true; n             break; n           } n         } n         if (err) { n           continue; n         } n         for (int i = 0; i < n; i++) { n           score[i] = 0; n           for (int j = 0; j < 3; j++) { n             int a = abs(task[i][j]); n             pts[i][j] = (a > 0 ? (cost[j] * (250 - a) / 250) : 0); n             score[i] += pts[i][j]; n           } n         } n         score[0] += 100 * (cnt_hack[0] + cnt_hack[1] + cnt_hack[2]); n         int cnt = 0; n         for (int type = 0; type < 128; type++) { n           guys[type] = 0; n         } n         for (int i = 1; i < n; i++) { n           if (score[i] > score[0]) { n             int type = 0; n             for (int t = 1; t < 8; t++) { n               int dec = 0; n               for (int j = 0; j < 3; j++) { n                 if (t & (1 << j)) { n                   if (task[i][j] < 0) { n                     dec += pts[i][j]; n                   } n                 } n               } n               if (score[i] - dec <= score[0]) { n                 type |= (1 << (t - 1)); n               } n             } n             guys[type]++; n             cnt++; n           } n         } n         for (int mask = 1; mask < (1 << 7); mask++) { n           rgt[mask] = 0; n           for (int type = 0; type < 128; type++) { n             if (type & mask) { n               rgt[mask] += guys[type]; n             } n           } n         } n         for (int j = 0; j < 3; j++) { n           cnt_hack[j] = min(cnt_hack[j], cnt); n         } n         for (int AB = 0; AB <= cnt_hack[0]; AB++) { n           for (int AC = 0; AB + AC <= cnt_hack[0]; AC++) { n             for (int BC = 0; AB + BC <= cnt_hack[1] && AC + BC <= cnt_hack[2]; n                  BC++) { n               for (int ABC = 0; n                    AB + AC + ABC <= cnt_hack[0] && n                    AB + BC + ABC <= cnt_hack[1] && AC + BC + ABC <= cnt_hack[2]; n                    ABC++) { n                 int A = cnt_hack[0] - AB - AC - ABC; n                 int B = cnt_hack[1] - AB - BC - ABC; n                 int C = cnt_hack[2] - AC - BC - ABC; n                 z[1] = 0; n                 z[2] = 0; n                 z[3] = AB; n                 z[4] = 0; n                 z[5] = AC; n                 z[6] = BC; n                 z[7] = ABC; n                 bound[0] = 0; n                 bound[1] = A; n                 bound[2] = B; n                 bound[3] = A + B; n                 bound[4] = C; n                 bound[5] = A + C; n                 bound[6] = B + C; n                 bound[7] = A + B + C; n                 for (int mask = 1; mask < (1 << 7); mask++) { n                   int lft = 0; n                   for (int t = 1; t < 8; t++) { n                     if (mask & (1 << (t - 1))) { n                       lft += z[t]; n                     } n                   } n                   bound[mask & 7] = min(bound[mask & 7], rgt[mask] - lft); n                 } n                 if (bound[0] < 0) { n                   continue; n                 } n                 for (int realA = 0; realA <= bound[1]; realA++) { n                   int boundB = min(bound[2], bound[3] - realA); n                   int boundC = min(bound[4], bound[5] - realA); n                   int boundBC = min(bound[6], bound[7] - realA); n                   if (boundB >= 0 && boundC >= 0 && boundBC >= 0) { n                     int realBplusC = min(boundB + boundC, boundBC); n                     ans = min(ans, 1 + (cnt - realA - realBplusC - AB - AC - n                                         BC - ABC)); n                   } n                 } n               } n             } n           } n         } n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long m, l, r, k; n struct Matrix { n   long long W[2][2]; n }; n Matrix operator*(const Matrix& A, const Matrix& B) { n   Matrix S; n   memset(S.W, 0, sizeof(S.W)); n   for (int i = 0; i < 2; ++i) n     for (int j = 0; j < 2; ++j) n       for (int l = 0; l < 2; ++l) n         S.W[i][j] = (S.W[i][j] + A.W[i][l] * B.W[l][j]) % m; n   return S; n } n Matrix operator^(Matrix A, long long e) { n   Matrix S; n   S.W[0][0] = S.W[1][1] = 1; n   S.W[0][1] = S.W[1][0] = 0; n   while (e) { n     if (e & 1) S = S * A; n     A = A * A; n     e >>= 1; n   } n   return S; n } n long long gcd() { n   long long s = 1; n   for (long long i = 1; i * i <= r; ++i) { n     if (r / i - (l - 1) / i >= k && i > s) s = i; n     long long e = r / i; n     if (i - (l - 1) / e >= k && e > s) s = e; n   } n   return s; n } n int main() { n   cin >> m >> l >> r >> k; n   long long n = gcd(); n   if (n <= 2) n     cout << 1 % m << endl; n   else { n     Matrix D, S; n     D.W[0][0] = 0; n     D.W[0][1] = D.W[1][0] = D.W[1][1] = 1; n     S = D ^ (n - 2); n     cout << (S.W[0][1] + S.W[1][1]) % m << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n vector<int> a, b, c; n multiset<int> m; n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(NULL), cout.tie(NULL); n   cin >> N; n   a.resize(N); n   b.resize(N); n   c.resize(N); n   for (int i = 0; i < N; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < N; i++) { n     cin >> b[i]; n     m.insert(b[i]); n   } n   for (int i = 0; i < N; i++) { n     int cur = N - a[i]; n     multiset<int>::iterator iter = m.lower_bound(cur); n     if (iter == m.end()) { n       iter = m.begin(); n     } n     c[i] = (a[i] + (*iter)) % N; n     m.erase(iter); n   } n   for (int i = 0; i < N; i++) { n     cout << c[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int h, w, r; n } edge[110]; n bool vis[11]; n int x[5000000]; n int n, k, t; n void init() { n   cin >> n >> k >> t; n   for (int i = 1; i <= k; i++) cin >> edge[i].h >> edge[i].w >> edge[i].r; n } n void solve() { n   int l = 0; n   for (int j = 1; j <= (1 << k) - 1; j++) { n     int t = 0; n     for (int i = 1; i <= k; i++) n       if ((j & (1 << (i - 1))) != 0) t++; n     if (t > n) continue; n     memset(vis, 0, sizeof(vis)); n     int s = 0; n     bool flag = true; n     for (int i = 1; i <= k; i++) n       if ((j & (1 << (i - 1))) != 0) { n         if (vis[edge[i].h] || vis[edge[i].w + n]) { n           flag = false; n           break; n         } n         vis[edge[i].h] = vis[edge[i].w + n] = true; n         s += edge[i].r; n       } n     if (flag) x[++l] = s; n   } n   sort(x, x + l + 1); n   cout << x[t - 1] << endl; n } n int main() { n   ios::sync_with_stdio(false); n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int TAM = 100000 + 15; n const long long MOD = 1000000007LL; n const double PI = 3.14159265359; n long long add(long long x, long long y) { return (x + y + 2 * MOD) % MOD; } n long long mult(long long x, long long y) { n   return (add(x, 0) * add(y, 0)) % MOD; n } n long long fast_expo(long long x, long long y) { n   if (x == 0) return 0; n   if (y == 0) return 1; n   long long res = 1; n   long long mid = fast_expo(x, y / 2); n   if (y % 2 != 0) res = x; n   return mult(mult(mid, mid), res); n } n long long inver(long long x) { return fast_expo(x, MOD - 2); } n long long gcd(long long e1, long long e2) { n   if (e2 == 0) return e1; n   return gcd(e2, e1 % e2); n } n int n; n map<int, int> mapa[TAM]; n int color[TAM]; n vector<int> G[TAM]; n long long dom[TAM]; n int tam[TAM]; n int cant[TAM]; n void dfs_tam(int u, int pd = -1) { n   tam[u] = 1; n   for (int v : G[u]) { n     if (v != pd) { n       dfs_tam(v, u); n       tam[u] += tam[v]; n     } n   } n } n void dfs(int u, int pd = -1) { n   int m = -1, val = -1; n   for (int v : G[u]) { n     if (v != pd) { n       if (tam[v] > val) { n         m = v; n         val = tam[v]; n       } n     } n   } n   if (m == -1) { n     mapa[u][color[u]]++; n     dom[u] = color[u]; n     cant[u] = 1; n     return; n   } n   for (int v : G[u]) { n     if (v != pd) { n       dfs(v, u); n     } n   } n   swap(mapa[u], mapa[m]); n   dom[u] = dom[m]; n   cant[u] = cant[m]; n   mapa[u][color[u]]++; n   if (mapa[u][color[u]] > cant[u]) { n     cant[u] = mapa[u][color[u]]; n     dom[u] = 1LL * color[u]; n   } else if (mapa[u][color[u]] == cant[u]) { n     dom[u] += 1LL * color[u]; n   } n   for (int v : G[u]) { n     if (v != pd && v != m) { n       int k, t; n       for (map<int, int>::iterator it = mapa[v].begin(); it != mapa[v].end(); n            it++) { n         k = it->first; n         t = it->second; n         mapa[u][k] += t; n         if (mapa[u][k] > cant[u]) { n           cant[u] = mapa[u][k]; n           dom[u] = 1LL * k; n         } else if (mapa[u][k] == cant[u]) { n           dom[u] += 1LL * k; n         } n       } n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &color[i]); n   int u, v; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d%d , &u, &v); n     G[u - 1].push_back(v - 1); n     G[v - 1].push_back(u - 1); n   } n   dfs_tam(0); n   dfs(0); n   for (int i = 0; i < n; i++) cout << 1LL * dom[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool compare(pair<int, int> p1, pair<int, int> p2) { n   return p1.first > p2.first; n } n long long int power(int a, int b) { n   if (b == 0) return 1; n   return a * power(a, b - 1); n } n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   int n, m; n   cin >> n >> m; n   int a[n + 1], b[m + 1]; n   for (int i = 0; i < n + 1; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < m + 1; i++) { n     cin >> b[i]; n   } n   if (n > m) { n     if (a[0] * b[0] > 0) n       cout <<  Infinity ; n     else n       cout <<  -Infinity ; n   } else if (n < m) { n     cout <<  0/1 ; n   } else { n     if (a[0] * b[0] < 0) { n       cout <<  - ; n     } n     cout << abs(a[0]) / gcd(abs(a[0]), abs(b[0])) <<  /  n          << abs(b[0]) / gcd(abs(a[0]), abs(b[0])); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans, i, N, H, alive, last1, last2, last3, a, b, c; n long long A[2][32][32][32], B[2][32][32][32]; n int main() { n   cin >> N >> H; n   A[1][0][0][0] = 4; n   for (i = 1; i < N; i++) { n     for (alive = 0; alive < 2; alive++) n       for (last1 = 0; i - last1 > 0 && last1 < H; last1++) n         for (last2 = 0; i - last2 > 0 && last2 < H; last2++) n           for (last3 = 0; i - last3 > 0 && last3 < H; last3++) { n             if (last1 == 0 || last1 == H - 1) n               a = 0; n             else n               a = last1 + 1; n             if (last2 == 0 || last2 == H - 1) n               b = 0; n             else n               b = last2 + 1; n             if (last3 == 0 || last3 == H - 1) n               c = 0; n             else n               c = last3 + 1; n             B[alive][a][b][c] += A[alive][last1][last2][last3]; n             B[alive][a][b][c] %= 1000000009; n             B[last1 > 0 || i < H][alive][b][c] += A[alive][last1][last2][last3]; n             B[last1 > 0 || i < H][alive][b][c] %= 1000000009; n             B[last2 > 0 || i < H][a][alive][c] += A[alive][last1][last2][last3]; n             B[last2 > 0 || i < H][a][alive][c] %= 1000000009; n             B[last3 > 0 || i < H][a][b][alive] += A[alive][last1][last2][last3]; n             B[last3 > 0 || i < H][a][b][alive] %= 1000000009; n           } n     for (alive = 0; alive < 2; alive++) n       for (last1 = 0; last1 < H; last1++) n         for (last2 = 0; last2 < H; last2++) n           for (last3 = 0; last3 < H; last3++) { n             A[alive][last1][last2][last3] = B[alive][last1][last2][last3]; n             B[alive][last1][last2][last3] = 0; n           } n   } n   for (a = 0; a < H; a++) n     for (b = 0; b < H; b++) n       for (c = 0; c < H; c++) ans = (ans + A[1][a][b][c]) % 1000000009; n   for (a = 0; a < H; a++) n     for (b = 0; b < H; b++) n       for (c = 0; c < H; c++) n         if (a > 0 || b > 0 || c > 0) ans = (ans + A[0][a][b][c]) % 1000000009; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2 * 1e5 + 10; n long long t[2][4 * N], a, b; n void update(int l, int r, int j, int pos, int val) { n   if (r < pos || pos < l) return; n   if (pos <= l && r <= pos) { n     t[0][j] = min(a, t[0][j] + val); n     t[1][j] = min(b, t[1][j] + val); n     return; n   } n   int mid = (l + r) >> 1; n   update(l, mid, 2 * j, pos, val); n   update(mid + 1, r, 2 * j + 1, pos, val); n   if (l != r) { n     t[0][j] = t[0][2 * j] + t[0][2 * j + 1]; n     t[1][j] = t[1][2 * j] + t[1][2 * j + 1]; n   } n } n long long getSum(int l, int r, int j, int x, int y, int wh) { n   if (r < x || y < l) return 0LL; n   if (x <= l && r <= y) { n     return t[wh][j]; n   } n   int mid = (l + r) >> 1; n   long long X = getSum(l, mid, (j << 1), x, y, wh); n   long long Y = getSum(mid + 1, r, ((j << 1) | 1), x, y, wh); n   return X + Y; n } n int n, k, q; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k >> a >> b >> q; n   for (int i = 0; i < q; i++) { n     int type; n     cin >> type; n     if (type == 1) { n       int di, ai; n       cin >> di >> ai; n       update(1, n, 1, di, ai); n     } else { n       int p; n       cin >> p; n       long long res = getSum(1, n, 1, 1, p - 1, 1); n       res += getSum(1, n, 1, p + k, n, 0); n       cout << res <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int a[N], b[N]; n bool p[N]; n vector<int> P; n pair<bool, int> c[N]; n int num(string s) { n   int x = 0; n   for (int i = 1; i < s.size(); i++) x = x * 10 + (s[i] -  0 ); n   return x; n } n int main() { n   int n, m, t = 0, T = 0; n   string s; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> s; n     if (s[0] ==  + ) { n       a[num(s)]++; n       c[i + 1] = {0, num(s)}; n     } else { n       b[num(s)]++; n       t++; n       c[i + 1] = {1, num(s)}; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] + t - b[i] == m) { n       p[i] = 1; n       P.push_back(i); n       T++; n     } n   } n   if (T == 1) { n     for (int i = 1; i <= n; i++) { n       if (c[i].first == 0) { n         if (c[i].second == P[0]) { n           cout <<  Truth n ; n         } else { n           cout <<  Lie n ; n         } n       } else { n         if (c[i].second == P[0]) { n           cout <<  Lie n ; n         } else { n           cout <<  Truth n ; n         } n       } n     } n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     if (c[i].first == 0) { n       if (p[c[i].second]) { n         cout <<  Not defined n ; n       } else { n         cout <<  Lie n ; n       } n     } else { n       if (p[c[i].second]) { n         cout <<  Not defined n ; n       } else { n         cout <<  Truth n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, w, m; n double a[100]; n int kol[100]; n vector<pair<int, double> > v[100]; n int main() { n   cin >> n >> w >> m; n   double q = n * w * 1.0 / m; n   memset(kol, 0, sizeof(kol)); n   for (int i = 0; i < 100; ++i) a[i] = w; n   for (int i = 0; i < m; ++i) { n     double p = q; n     int z = 0; n     for (int j = 0; j < n; ++j) { n       if (a[j] > 1e-9) { n         double temp = min(p, a[j]); n         v[i].push_back(make_pair(j + 1, temp)); n         p -= temp; n         a[j] -= temp; n         ++z; n         ++kol[j]; n         if (kol[j] > 2) { n           cout <<  NO n ; n           return 0; n         } n         if (p < 1e-9) break; n       } n     } n     if (z == 0 || p > 1e-9) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   for (int i = 0; i < m; ++i) { n     for (int j = 0; j < v[i].size(); ++j) { n       if (j) cout <<    ; n       cout << v[i][j].first <<     << fixed << setprecision(10) n            << v[i][j].second; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Antenna { n   int iniLeft, iniRight; n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int nbAntennas, totLen; n   cin >> nbAntennas >> totLen; n   vector<Antenna> ants(nbAntennas); n   for (int iAnt = 0; iAnt < nbAntennas; ++iAnt) { n     int center, iniScope; n     cin >> center >> iniScope; n     ants[iAnt].iniLeft = max(0, center - iniScope); n     ants[iAnt].iniRight = min(totLen, center + iniScope); n   } n   vector<int> minCost(totLen + 1); n   minCost[totLen] = 0; n   for (int pos = totLen - 1; pos >= 0; --pos) { n     minCost[pos] = (totLen - pos); n     for (int iAnt = 0; iAnt < nbAntennas; ++iAnt) { n       int left = ants[iAnt].iniLeft, right = ants[iAnt].iniRight; n       if (left <= pos + 1 && pos + 1 <= right) { n         minCost[pos] = minCost[pos + 1]; n         break; n       } n       if (pos < left) { n         int accessCost = (left - pos - 1); n         int nextPos = min(totLen, right + accessCost); n         minCost[pos] = min(minCost[pos], accessCost + minCost[nextPos]); n       } n     } n   } n   cout << minCost[0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   ll n, m, k; n   cin >> n >> m >> k; n   vector<ll> vec(m); n   for (ll &v : vec) cin >> v; n   ll ans = 0, i = 0; n   while (i < m) { n     ll t = (vec[i] - i - 1) / k, j = i + 1; n     while (j < m && (vec[j] - i - 1) / k == t) j++; n     ans++, i = j; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[2222], n; n int D[2222]; n int Find(int x) { n   if (D[x] != 0) return D[x]; n   if (p[x] == -1) n     return D[x] = 1; n   else n     return D[x] = Find(p[x]) + 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &p[i]); n   int ans = 0, tmp; n   for (int i = 1; i <= n; i++) { n     tmp = Find(i); n     if (tmp > ans) ans = tmp; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t INF = 4e18, MOD = 998244353; n template <int64_t maxn> n struct NTT { n   constexpr static int lg2(int n) { return 32 - __builtin_clz(n - 1); } n   const static int MAXN = 1 << lg2(maxn), MOD = 998244353, root = 3; n   int rev[MAXN], rt[MAXN]; n   int mul(int a, int b) { return (long long)a * b % MOD; } n   int sub(int a, int b) { return b > a ? a - b + MOD : a - b; } n   int add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; } n   int binExp(int base, long long exp) { n     if (exp == 0) return 1; n     return mul(binExp(mul(base, base), exp / 2), exp & 1 ? base : 1); n   } n   NTT() { n     rt[1] = 1; n     for (int k = 1; k < lg2(MAXN); k++) { n       int z[] = {1, binExp(root, (MOD - 1) >> (k + 1))}; n       for (int i = (1 << k); i < 2 << k; i++) rt[i] = mul(rt[i / 2], z[i & 1]); n     } n   } n   void ntt(int *a, int n) { n     for (int i = 0; i < n; i++) rev[i] = (rev[i / 2] | (i & 1) << lg2(n)) / 2; n     for (int i = 0; i < n; i++) n       if (i < rev[i]) swap(a[i], a[rev[i]]); n     for (int k = 1; k < n; k *= 2) n       for (int i = 0; i < n; i += 2 * k) n         for (int j = 0; j < k; j++) { n           int z = mul(rt[j + k], a[i + j + k]); n           a[i + j + k] = sub(a[i + j], z); n           a[i + j] = add(a[i + j], z); n         } n   } n   int in[2][MAXN]; n   vector<int> multiply(const vector<int> &a, const vector<int> &b) { n     if (a.empty() || b.empty()) return {}; n     int sz = a.size() + b.size() - 1, n = 1 << lg2(sz); n     fill(in[0], in[0] + n, 0), fill(in[1], in[1] + n, 0); n     copy(a.begin(), a.end(), in[0]), copy(b.begin(), b.end(), in[1]); n     ntt(in[0], n), ntt(in[1], n); n     int invN = binExp(n, MOD - 2); n     for (int i = 0; i < n; i++) in[0][i] = mul(mul(in[0][i], in[1][i]), invN); n     reverse(in[0] + 1, in[0] + n); n     ntt(in[0], n); n     return vector<int>(in[0], in[0] + sz); n   } n }; n struct DSU { n   int64_t n; n   vector<int64_t> p, sz, cnt; n   vector<bool> good; n   vector<vector<int64_t>> g; n   DSU(int64_t n) : n(n), p(n, -1), sz(n, 1), cnt(n, 0), good(n, 1), g(n) {} n   int64_t find(int64_t c) { n     if (p[c] == -1) return c; n     return p[c] = find(p[c]); n   } n   void check(int64_t a) { n     good.resize(p.size()); n     good[a] = (cnt[a] == (sz[a] * (sz[a] - 1)) / 2); n   } n   void uni(int64_t a, int64_t b) { n     a = find(a), b = find(b); n     if (a == b) { n       cnt[a]++; n       check(a); n       return; n     } n     p[a] = p[b] = p.size(); n     p.push_back(-1); n     sz.push_back(sz[a] + sz[b]); n     cnt.push_back(cnt[a] + cnt[b] + 1); n     g.resize(p.size()); n     g.back() = {a, b}; n     check(p.size() - 1); n   } n }; n void add(int32_t &a, int64_t b) { a = (a + b) % MOD; } n int64_t n, k; n vector<vector<int64_t>> g; n vector<vector<int32_t>> ans; n vector<int64_t> sz; n vector<bool> good; n NTT<2 * 1505> ntt; n void dfs(int64_t cur) { n   for (auto nei : g[cur]) { n     dfs(nei); n   } n   if (g[cur].size()) { n     int64_t a = g[cur][0], b = g[cur][1]; n     ans[cur] = ntt.multiply(ans[a], ans[b]); n   } n   if (good[cur]) add(ans[cur][1], 1); n   ans[cur].resize(sz[cur] + 1); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   vector<vector<int64_t>> mat(n, vector<int64_t>(n)); n   vector<pair<int64_t, pair<int64_t, int64_t>>> edges; n   for (int64_t i = 0; i < n; i++) { n     for (int64_t j = 0; j < n; j++) { n       cin >> mat[i][j]; n       if (j < i) edges.push_back({mat[i][j], {j, i}}); n     } n   } n   sort(edges.begin(), edges.end()); n   DSU dsu(n); n   for (auto e : edges) { n     dsu.uni(e.second.first, e.second.second); n   } n   g = dsu.g, good = dsu.good, sz = dsu.sz; n   good.resize(g.size()); n   int64_t root = dsu.find(0); n   k = n, n = g.size(); n   ans.resize(n, vector<int32_t>(k + 1)); n   dfs(root); n   for (int64_t i = 1; i < k + 1; i++) cout << ans[root][i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n #define f(i,q,n) for(int i=(int)q;i<n;i++) n #define pb push_back n #define debug(x) cout<<#x<<   <<x<<endl; n int main() n { n     ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); n     int t=1; n     cin>>t; n     while(t--){ n         int n,m; n         cin>>n>>m; n         string s; n         cin>>s; n         while(m--){ n         string ans=s; n         f(i,0,n){ n             if(i==0 and ans[i]== 0  and ans[i+1]== 1 ){s[i]= 1 ;} n             else if(i==n-1 and ans[i]== 0  and ans[i-1]== 1 ){s[i]= 1 ;} n             else{ n                 if(i<n-1 and i>0 and ans[i]== 0  and ans[i+1]!=ans[i-1]){ n                     s[i]= 1 ; n                 } n             } n         } n         if(s==ans){ n             break; n         } n  n  n         } n          n     cout<<s<<endl; n  n     } n     return 0; n } n //check constraints, testcases, ll vs int  n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n long long a[200005]; n struct poi { n   long long val; n   int pos; n } tp; n bool operator<(const poi &a, const poi &b) { n   return (a.val > b.val) || (a.val == b.val and a.pos > b.pos); n } n priority_queue<poi> q; n int main() { n   int n; n   int cnt = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     tp.val = a[i]; n     tp.pos = i; n     q.push(tp); n   } n   while (!q.empty()) { n     poi a1 = q.top(); n     q.pop(); n     poi b1 = q.top(); n     q.pop(); n     while (!q.empty() && a1.val != b1.val) { n       a1 = b1; n       b1 = q.top(); n       q.pop(); n     } n     if (a1.val == b1.val and a1.pos != b1.pos) { n       a[b1.pos] = b1.val * 2; n       b1.val = b1.val * 2; n       a[a1.pos] = -1; n       cnt++; n       if (q.empty()) break; n       q.push(b1); n     } n   } n   printf( %d n , n - cnt); n   for (int i = 0; i < n; i++) { n     if (a[i] != -1) printf( %lld  , a[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n string str; n const long long MOD = 1e9 + 7; n template <class T> n inline void amin(T &x, const T &y) { n   if (y < x) x = y; n } n template <class T> n inline void amax(T &x, const T &y) { n   if (x < y) x = y; n } n int read() { n   long long s = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     s = s * 10 + c -  0 ; n     c = getchar(); n   } n   return s * f; n } n long long Pow(long long a, long long b) { n   if (a == 1 || b == 1) return a; n   if (b % 2 == 1) return (a * Pow(a, b - 1)) % MOD; n   return (Pow(a, b / 2) * Pow(a, b / 2)) % MOD; n } n long long factorial(long long n) { n   long long ans = 1; n   while (--n) ans = (ans * (n + 1)) % MOD; n   return ans; n } n vector<bool> b(12, 0); n bool binod(int k) { n   bool arr[13][13]; n   memset(arr, 0, sizeof(arr)); n   int a = 0; n   for (int i = 0; i < k; i++) n     for (int j = 0; j < 12 / k; j++) { n       arr[i][j] = b[a]; n       a++; n     } n   a = 0; n   bool d; n   for (int j = 0; j < 12 / k; j++) { n     d = 1; n     for (int i = 0; i < k; i++) { n       if (!arr[i][j]) d = 0; n     } n     if (d) return 1; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     for (int i = 0; i < 12; i++) { n       char sign; n       cin >> sign; n       if (sign ==  X ) n         b[i] = 1; n       else n         b[i] = 0; n     } n     vector<int> v; n     if (binod(1)) v.push_back(1); n     if (binod(2)) v.push_back(2); n     if (binod(3)) v.push_back(3); n     if (binod(4)) v.push_back(4); n     if (binod(6)) v.push_back(6); n     if (binod(12)) v.push_back(12); n     cout << v.size() <<    ; n     for (int i = 0; i < v.size(); i++) { n       cout << v[i] <<  x  << 12 / v[i] <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string str; n int make(int star, int en) { n   if (en - star >= 7) return -1e7; n   if (en - star >= 1 && str[star] ==  0 ) return -1e7; n   if (str[star] ==  0  && str[en] !=  0 ) return -1e7; n   int sum = 0; n   for (int i = star; i <= en; i++) { n     sum = sum * 10 + (str[i] -  0 ); n   } n   if (sum > 1e6) return -1e7; n   return sum; n } n int main() { n   int m = -1, i, j, len, a, b, c; n   cin >> str; n   len = str.length(); n   for (i = 0; i < len - 2; i++) { n     for (j = i + 1; j < len - 1; j++) { n       a = make(0, i); n       b = make(i + 1, j); n       c = make(j + 1, len - 1); n       m = max(m, a + b + c); n     } n   } n   if (m < 0) n     printf( -1 ); n   else n     printf( %d , m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const double EPS = 1e-9; n static const double PI = acos(-1.0); n char ans[4][110]; n int main() { n   int n; n   while (scanf( %d , &n) > 0) { n     if (n == 1) { n       printf( a na nb nb n ); n       continue; n     } n     ans[0][0] =  a ; n     ans[1][0] =  a ; n     ans[2][0] =  b ; n     ans[3][0] =  c ; n     int ud = 2; n     char p =  b ; n     for (int i = 1; i < n; i++) { n       ans[ud][i] = p; n       ans[ud + 1][i] = p + 1; n       p += 2; n       if (p >=  x ) { n         p =  a ; n       } n       ud ^= 2; n       if (i == n - 1) { n         ans[ud][i] = p; n         ans[ud + 1][i] = p; n       } else { n         ans[ud][i] = p; n         ans[ud + 1][i] = p + 1; n       } n     } n     for (int y = 0; y < (int)(4); y++) { n       for (int x = 0; x < (int)(n); x++) { n         putchar(ans[y][x]); n       } n       puts(  ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, a; n   vector<pair<int, int> > v; n   cin >> n; n   for (i = 0; i < n; ++i) { n     cin >> a; n     v.push_back(make_pair(a, i + 1)); n   } n   for (i = 0; i < n; ++i) { n     cin >> a; n     v.push_back(make_pair(a, i + 1)); n   } n   long long m; n   cin >> m; n   m *= 2; n   sort(v.begin(), v.end()); n   long long r = 1; n   int c = 0, c1 = 0; n   for (i = 1; i < n + n; ++i) { n     if (v[i].first == v[i - 1].first) { n       ++c; n       if (v[i].second == v[i - 1].second) { n         ++c1; n       } else n         c1 = 0; n     } else { n       c1 = c = 0; n     } n     r = (r * (c + 1) / (c1 + 1)) % m; n     c1 = 0; n   } n   cout << r % (m / 2); n   return 0; n } n int cpp4cf_main() { n   freopen( B.cpp ,  r , stdin); n   char s[99]; n   bool f; n   while (true) { n     cin >> s; n     if (cin.eof()) break; n     if (strstr(s,  /* )) { n       cin >> s; n       if (strstr(s,  Test )) { n         cin >> s; n         if (strstr(s,  on )) { n           cout <<  Output:  ; n           cpp4cf_main(); n           cout <<   nAnswer:  ; n           f = false; n           while (true) { n             cin >> s; n             if (strstr(s,  */ )) break; n             if (strstr(s,  // )) { n               if (f) n                 cout << endl; n               else n                 f = true; n             } else n               cout << s <<    ; n           } n           cout <<   n n ; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   map<long long, long long> mp; n   long long ans = 0; n   long long prev = 0; n   long long sum = 0; n   for (long long i = 0; i < n; i++) { n     sum += a[i]; n     if (sum == 0) n       prev = max(prev, mp[0] + 1); n     else if (mp.find(sum) != mp.end()) n       prev = max(prev, mp[sum] + 1); n     ans += i + 1 - prev; n     mp[sum] = i + 1; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const bool debug = 0; n struct mout { n   int i, j; n   long long first; n   mout(){}; n   mout(int i, int j, long long first) : i(i), j(j), first(first) {} n }; n int main() { n   if (!debug) { n     cin.tie(0); n     cout.tie(0); n     ios::sync_with_stdio(0); n   } n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<long long> a(n); n     vector<mout> out; n     for (int i = 0; i < n; i++) cin >> a[i]; n     long long s = 0; n     for (int i = 0; i < n; i++) s += a[i]; n     if (s != (s / n) * n) { n       cout <<  -1 n ; n       continue; n     } n     s /= n; n     for (int i = 1; i < n; i++) { n       if (a[i] % (i + 1) != 0) { n         out.push_back({1, i + 1, i + 1 - a[i] % (i + 1)}); n         a[1] -= i + 1 - a[i] % (i + 1); n         a[i] += i + 1 - a[i] % (i + 1); n       } n       out.push_back({i + 1, 1, a[i] / (i + 1)}); n       a[1] += a[i]; n       a[i] = 0; n     } n     for (int i = 1; i < n; i++) { n       out.push_back({1, i + 1, s}); n     } n     cout << out.size() <<   n ; n     for (int i = 0; i < out.size(); i++) n       cout << out[i].i <<     << out[i].j <<     << out[i].first <<   n ; 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 = 1e14; n const long long TE3 = 1005; n const long long TE5 = 300005; n const string YN[2] = { NO ,  YES }; n using namespace std; n long long f(long long n, vector<long long>& q1, vector<long long>& q2) { n   unordered_map<long long, long long> e; n   long long c = 0; n   e[0] = -1; n   long long i = 0, j = 0; n   long long ans = 0; n   while (i < q1.size() || j < q2.size()) { n     long long g; n     long long q1t = (i >= q1.size()) ? INF : q1[i]; n     long long q2t = (j >= q2.size()) ? INF : q2[j]; n     if (q1t < q2t) { n       g = q1t; n       ++c; n       ++i; n     } else { n       g = q2t; n       --c; n       ++j; n     } n     if (e.find(c) == e.end()) { n       e[c] = g; n     } else { n       q2t = (j >= q2.size()) ? INF : q2[j]; n       q1t = (i >= q1.size()) ? INF : q1[i]; n       long long tmp = min(q1t, q2t); n       long long cur = (tmp == INF ? n : tmp) - e[c] - 1; n       ans = max(ans, cur); n     } n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   long long n; n   cin >> n; n   long long x; n   unordered_map<long long, vector<long long> > mpq; n   for (long long i = (0); i < (n); ++i) { n     cin >> x; n     mpq[x].push_back(i); n   } n   if (mpq.size() == 1) { n     cout << 0 << endl; n     return 0; n   } n   unordered_map<long long, long long> rev; n   long long ma = -INF, mo = 0; n   for (auto it : mpq) { n     rev[it.second.size()]++; n     if (it.second.size() > mo) { n       ma = it.first; n       mo = it.second.size(); n     } n   } n   if (rev[mo] >= 2) { n     cout << n << endl; n     return 0; n   } n   long long ans = 0; n   for (auto it : mpq) { n     if (it.first == ma) continue; n     ans = max(ans, f(n, mpq[ma], it.second)); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 1e18; n struct dta { n   int u, id; n   dta(int _u = 0, int _id = 0) { n     u = _u; n     id = _id; n   } n }; n struct TDij { n   long long val; n   int x; n   TDij(long long _val = 0, int _x = 0) { n     x = _x; n     val = _val; n   } n }; n inline bool operator<(const TDij &a, const TDij &b) { return (a.val > b.val); } n int n, m, k, cost[300010], trace[300010], delneed; n bool flag[300010]; n long long F[300010]; n vector<dta> A[300010]; n priority_queue<TDij> Q; n void dijks() { n   memset(flag, true, sizeof(flag)); n   memset(trace, 0, sizeof(trace)); n   for (int i = 2; i <= n; ++i) F[i] = oo; n   F[1] = 0; n   Q.push(TDij(0, 1)); n   while (!Q.empty()) { n     TDij topx = Q.top(); n     Q.pop(); n     if (topx.val != F[topx.x]) continue; n     int u = topx.x; n     for (int i = 0; i <= A[u].size() - 1; ++i) { n       if (F[A[u][i].u] > F[u] + cost[A[u][i].id]) { n         F[A[u][i].u] = F[u] + cost[A[u][i].id]; n         flag[trace[A[u][i].u]] = true; n         trace[A[u][i].u] = A[u][i].id; n         flag[A[u][i].id] = false; n         Q.push(TDij(F[A[u][i].u], A[u][i].u)); n       } n     } n   } n } n void dfs(int x, int par, int pared) { n   for (int i = 0; i <= A[x].size() - 1; ++i) n     if (A[x][i].u != par && !flag[A[x][i].id]) { n       dfs(A[x][i].u, x, A[x][i].id); n       if (delneed == 0) return; n     } n   if (delneed > 0) { n     (--delneed); n     flag[pared] = true; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= m; ++i) { n     int u, v; n     cin >> u >> v >> cost[i]; n     A[u].push_back(dta(v, i)); n     A[v].push_back(dta(u, i)); n   } n   dijks(); n   delneed = m - k; n   if (!delneed) { n     for (int i = 1; i <= m; ++i) delneed += (!flag[i]); n     cout << delneed <<   n ; n     for (int i = 1; i <= m; ++i) n       if (!flag[i]) cout << i <<    ; n     return 0; n   } n   for (int i = 1; i <= m; ++i) n     if (flag[i]) { n       if ((--delneed) == 0) { n         cout << k <<   n ; n         for (int j = 1; j <= i; ++j) n           if (!flag[j]) cout << j <<    ; n         for (int j = i + 1; j <= m; ++j) cout << j <<    ; n         return 0; n       } n     } n   dfs(1, 0, 0); n   cout << k <<   n ; n   for (int i = 1; i <= m; ++i) n     if (!flag[i]) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> solve(int k) { n   vector<string> s; n   if (k == 0) { n     s.push_back( + ); n     return s; n   } n   vector<string> prev = solve(k - 1), rev; n   for (int i = 0; i < prev.size(); i++) { n     string temp; n     for (int j = 0; j < prev.size(); j++) { n       if (prev[i][j] ==  + ) n         temp +=  * ; n       else n         temp +=  + ; n     } n     rev.push_back(temp); n   } n   for (int i = 0; i < prev.size(); i++) { n     s.push_back(prev[i] + prev[i]); n     s.push_back(prev[i] + rev[i]); n   } n   return s; n } n int main() { n   int k; n   cin >> k; n   vector<string> s; n   s = solve(k); n   for (int i = 0; i < s.size(); i++) { n     cout << s[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int s = 0; n   char c; n   while ((c = getchar()) <  0  || c >  9 ) n     ; n   do { n     s = s * 10 + c -  0 ; n   } while ((c = getchar()) >=  0  && c <=  9 ); n   return s; n } n const int N = 3010, M = 310; n int n, m, rp[M]; n double f[M][2][N], tp[N][M]; n priority_queue<pair<double, int> > h; n double nxdp(int i) { n   int p = (++rp[i]) & 1; n   if (rp[i] > n) return -1e9; n   f[i][p][0] = 0; n   for (int k = 1; k <= n; k++) n     f[i][p][k] = n         tp[k][i] * f[i][p ^ 1][k - 1] + (1 - tp[k][i]) * f[i][p][k - 1]; n   return f[i][p][n]; n } n double work() { n   int i, k; n   double ans = 0; n   for (i = 1; i <= m; i++) n     for (k = 0; k <= n; k++) f[i][0][k] = 1; n   for (i = 1; i <= m; i++) h.push(make_pair(nxdp(i), i)); n   for (i = 1; i <= n; i++) { n     pair<double, int> t = h.top(); n     h.pop(); n     ans += t.first; n     h.push(make_pair(nxdp(t.second), t.second)); n   } n   return ans; n } n int main() { n   int i, j; n   n = read(), m = read(); n   for (i = 1; i <= n; i++) n     for (j = 1; j <= m; j++) tp[i][j] = read() * 0.001; n   printf( %.11lf n , work()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int siz = 1e5 + 10; n const long long modu = 1e9 + 7; n int pre[13] = {1, 4, 10, 20, 35, 56, 83, 116, 155, 198, 244, 292, 341}; n int main() { n   int n; n   scanf( %d , &n); n   if (n < 13) { n     cout << pre[n] << endl; n     return 0; n   } n   long long n1 = n; n   long long ans = pre[12]; n   ans += (n1 - 12ll) * 49ll; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n int main() { n   int n, num, flag = 1, f; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &num); n     ++mp[num]; n   } n   map<int, int>::iterator it = mp.begin(); n   int ln = it->first; n   int lm = it->second; n   int l = ln, r = ln; n   ++it; n   for (; it != mp.end(); ++it) { n     if (it->first > r + 1) { n       flag = 0; n       break; n     } else { n       if (lm == it->second) { n         lm = 1; n         r = it->first; n       } else if (lm > it->second) { n         lm -= it->second; n         if (lm > 1) { n           flag = 0; n           break; n         } n       } else if (lm < it->second) { n         if (l + 1 != it->first) { n           lm = it->second - lm + 1; n           r = it->first; n           continue; n         } n         l = it->first; n         r = it->first; n         lm = it->second - lm; n       } n     } n   } n   if (l + 1 != r) flag = 0; n   if (flag && lm < 2) { n     printf( YES ); n   } else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int ct[2][6] = {0}; n   for (int i = 0; i < n; i++) { n     int tmp; n     scanf( %d , &tmp); n     ct[0][tmp]++; n   } n   for (int i = 0; i < n; i++) { n     int tmp; n     scanf( %d , &tmp); n     ct[1][tmp]++; n   } n   int diff = 0; n   int flag = 0; n   for (int i = 1; i <= 5; i++) { n     if (ct[0][i] != ct[1][i]) { n       int tmp = abs(ct[0][i] - ct[1][i]); n       if (tmp % 2 != 0) flag = 1; n       diff += tmp / 2; n     } n   } n   if (flag == 0) n     cout << diff / 2; n   else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long j, k, s, l, m, n, t, d; n long long e[111][33333], a, b, i, p, c; n map<string, long long> y; n string q, r, w; n int main() { n   cin >> n >> a >> b >> c; n   n *= 2; n   for (i = 0; i <= a; i++) { n     for (j = 0; j <= b; j++) { n       k = n - i - 2 * j; n       if (k % 4 == 0 && k / 4 <= c && k >= 0) s++; n     } n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t, k, d, t1, t2, kol; n   bool f; n   f = false; n   cin >> n; n   cin >> t; n   cin >> k; n   cin >> d; n   t1 = 0; n   t2 = 0; n   kol = 0; n   while (kol < n) { n     t1++; n     if (t1 % t == 0) { n       kol = kol + k; n     } n   } n   kol = 0; n   while (kol < n) { n     t2++; n     if (f == true) { n       if ((t2 - d) % t == 0) { n         kol = kol + k; n       } n     } n     if (t2 % t == 0) { n       kol = kol + k; n     } n     if (t2 % d == 0) { n       f = true; n     } n   } n   if (t1 > t2) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n } n