#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, a, b, diff; n   cin >> n >> a >> b; n   vector<long long int> v; n   long long int arr[n]; n   for (long long int i = 0; i < n; i++) { n     cin >> arr[i]; n     v.push_back(arr[i]); n   } n   sort(v.begin(), v.end()); n   diff = v[n - a] - v[b - 1]; n   if (diff < 0) { n     cout <<  0  << endl; n   } else { n     cout << diff << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, k; n vector<int> v[1000010]; n int lastans = 0, res[1000010]; n void dfs(int np, int fath, int val) { n   res[np] = val; n   for (auto &x : v[np]) { n     if (x == fath) continue; n     dfs(x, np, min(val, x)); n   } n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1, ti, tj; i < n; i++) { n     scanf( %d%d , &ti, &tj); n     v[ti].push_back(tj); n     v[tj].push_back(ti); n   } n   int a, b; n   scanf( %d%d , &a, &b); n   b = b % n + 1; n   dfs(b, 0, b); n   lastans = 0; n   k = b; n   for (int i = 2, ti, tj; i <= q; i++) { n     scanf( %d%d , &ti, &tj); n     tj = (tj + lastans) % n + 1; n     if (ti == 1) n       k = min(k, res[tj]); n     else n       printf( %d n , lastans = min(k, res[tj])); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100]; n long long b[100]; n vector<long long> pos[101]; n int main() { n   long long i, j, k, l, m, n, t; n   scanf( %lld , &n); n   scanf( %lld , &k); n   for (i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     pos[a[i]].push_back(i); n   } n   long long sum = 0, count = 0; n   long long flag = 0; n   for (i = 1; i <= 100; i++) { n     for (j = 0; j < pos[i].size(); j++) { n       sum = sum + a[pos[i][j]]; n       if (sum > k) { n         flag = 1; n         break; n       } n       b[count++] = pos[i][j]; n     } n     if (flag == 1) break; n   } n   cout << count << endl; n   for (j = 0; j < count; j++) cout << b[j] + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n template <class T> n inline T biton(T n, T pos) { n   return n | ((T)1 << pos); n } n template <class T> n inline T bitoff(T n, T pos) { n   return n & ~((T)1 << pos); n } n template <class T> n inline T ison(T n, T pos) { n   return (bool)(n & ((T)1 << pos)); n } n template <class T> n inline T gcd(T a, T b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n template <typename T> n string NumberToString(T Number) { n   ostringstream second; n   second << Number; n   return second.str(); n } n inline int nxt() { n   int aaa; n   scanf( %d , &aaa); n   return aaa; n } n inline long long int lxt() { n   long long int aaa; n   scanf( %lld , &aaa); n   return aaa; n } n inline double dxt() { n   double aaa; n   scanf( %lf , &aaa); n   return aaa; n } n template <class T> n inline T bigmod(T p, T e, T m) { n   T ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % m; n     p = (p * p) % m; n   } n   return (T)ret; n } n int ar[1000010]; n long long F(long long int limit, long long int a, long long int b) { n   if (limit < a) return 1LL; n   long long int sum = 1LL; n   long long int x = 0; n   while (true) { n     if (x + a > limit) break; n     long long int temp = (limit - x) / a; n     sum += temp; n     long long int r = (a * temp) + x; n     if (r - b < 0) break; n     temp = (r + b - 1) / b; n     temp--; n     sum += temp; n     r -= temp * b; n     x = r; n     if (x % a == 0) break; n   } n   return sum; n } n long long int get(long long int st, long long int len) { n   long long int sum = (len * len + len) / 2LL; n   sum *= st; n   return sum; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long n, a, b; n   cin >> n >> a >> b; n   long long limit = min(a + b - 2, n); n   long long sum = 0; n   for (long long i = 0; i <= limit; i++) { n     sum += F(i, a, b); n   } n   limit++; n   long long int gc = gcd(a, b); n   while (limit <= n) { n     if (limit % gc == 0) break; n     sum += (limit / gc) + 1; n     limit++; n   } n   long long int upperLimit = n; n   while (upperLimit >= limit) { n     sum += (upperLimit / gc) + 1; n     if (upperLimit % gc == 0) break; n     upperLimit--; n   } n   if (upperLimit > limit) { n     long long int diff = (upperLimit - limit); n     sum += diff; n     diff /= gc; n     long long int chunk = limit * diff + get(gc, diff - 1); n     sum += chunk; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, A[2005], DP[2005]; n inline int ok(int X) { n   memset(DP, 0, sizeof(DP)); n   int i, j; n   long long l1, r1, l2, r2; n   for (i = 1; i <= n; i++) DP[i] = i - 1; n   for (i = 2; i <= n; i++) n     if (abs(A[i] - A[i - 1]) <= X) n       DP[i] = 0; n     else n       break; n   for (i = 1; i <= n; i++) n     for (j = i + 1; j <= n; j++) { n       l1 = (long long)A[i] - (long long)X * (j - i - 1); n       r1 = (long long)A[i] + (long long)X * (j - i - 1); n       l2 = A[j] - X; n       r2 = A[j] + X; n       if (!(l2 > r1 || l1 > r2)) DP[j] = min(DP[j], DP[i] + j - i - 1); n     } n   for (i = 1; i <= n; i++) n     if (DP[i] + n - i <= k) return 1; n   return 0; n } n int cbin() { n   int i, step = (1 << 30); n   for (i = -1; step; step >>= 1) n     if (!ok(i + step)) i += step; n   return ++i; n } n int main() { n   scanf( %d%d , &n, &k); n   int i; n   for (i = 1; i <= n; i++) scanf( %d , &A[i]); n   printf( %d n , cbin()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n int A[100005]; n int G[100005]; n int m; n int main() { n   scanf( %d , &N); n   for (int i = 0; i < N; ++i) scanf( %d , &A[i]); n   for (int i = 0; i < N; ++i) { n     if (i < N / 2) { n       if (A[i] - i >= 1) ++G[A[i] - i]; n     } else { n       if (A[i] - (N - i - 1) >= 1) ++G[A[i] - (N - i - 1)]; n     } n   } n   m = 0; n   for (int i = 0; i <= 1e5; ++i) n     if (G[i] >= m) m = G[i]; n   printf( %d , N - m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int size = 1007; n const long long modulo = 1000000007; n const long long oo = 1e18; n const double EPS = 1e-13; n const double PI = acos(-1.0); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<vector<int> > g(n); n   vector<long long> cost(n), v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   while (m--) { n     int a, b; n     cin >> a >> b; n     --a; n     --b; n     cost[a] += v[b]; n     cost[b] += v[a]; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   vector<bool> was(n, false); n   long long ans = 0; n   for (int h = 0; h < n; h++) { n     int ver = -1; n     for (int i = 0; i < n; i++) n       if (!was[i] && (ver < 0 || v[ver] < v[i])) ver = i; n     was[ver] = true; n     ans += cost[ver]; n     for (int i = 0; i < g[ver].size(); i++) { n       int to = g[ver][i]; n       if (!was[to]) { n         cost[ver] -= v[to]; n         cost[to] -= v[ver]; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int test, a, sum = 0, wait = 0, counter = 0; n   priority_queue<int, vector<int>, greater<int> > q; n   queue<int> qqq; n   cin >> test; n   int num = test; n   while (test--) { n     cin >> a; n     sum += a; n     q.push(a); n     qqq.push(a); n   } n   while (!q.empty()) { n     int serve = q.top(); n     if (serve >= wait) n       counter++; n     else { n       q.pop(); n       continue; n     } n     wait += serve; n     q.pop(); n   } n   cout << counter << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3010; n const int M = 11; n const int INF = 1 << 30; n const double EPS = 1e-8; n const int MOD = 1000000007LL; n int n, tot[N]; n vector<int> edge[N], edge2[N]; n int dfs(int u, int fa) { n   int num = 0, sz; n   sz = edge[u].size(); n   for (int i = 0; i < sz; i++) { n     int v = edge[u][i]; n     if (v == fa) { n       continue; n     } n     num += dfs(v, u); n   } n   sz = edge2[u].size(); n   for (int i = 0; i < sz; i++) { n     int v = edge2[u][i]; n     if (v == fa) { n       continue; n     } n     num += dfs(v, u) + 1; n   } n   return num; n } n void init() { n   memset(tot, 0, sizeof(tot)); n   for (int i = 0; i < n; i++) { n     edge[i].clear(); n     edge2[i].clear(); n   } n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     u--, v--; n     edge[u].push_back(v); n     edge2[v].push_back(u); n   } n   for (int u = 0; u < n; u++) { n     int sz; n     sz = edge[u].size(); n     for (int i = 0; i < sz; i++) { n       int v = edge[u][i]; n       int num = dfs(v, u); n       tot[u] += num; n     } n     sz = edge2[u].size(); n     for (int i = 0; i < sz; i++) { n       int v = edge2[u][i]; n       int num = dfs(v, u) + 1; n       tot[u] += num; n     } n   } n } n int dfs2(int u, int fa) { n   int sz, ret = tot[u]; n   sz = edge[u].size(); n   for (int i = 0; i < sz; i++) { n     int v = edge[u][i]; n     if (v == fa) { n       continue; n     } n     ret = min(ret, dfs2(v, u)); n   } n   sz = edge2[u].size(); n   for (int i = 0; i < sz; i++) { n     int v = edge2[u][i]; n     if (v == fa) { n       continue; n     } n     ret = min(ret, dfs2(v, u)); n   } n   return ret; n } n void solve() { n   int ans = INF; n   for (int i = 0; i < n; i++) { n     ans = min(ans, tot[i]); n   } n   for (int i = 0; i < n; i++) { n     vector<int> p; n     int sz; n     sz = edge[i].size(); n     for (int j = 0; j < sz; j++) { n       int v = edge[i][j]; n       p.push_back(dfs2(v, i)); n     } n     sz = edge2[i].size(); n     for (int j = 0; j < sz; j++) { n       int v = edge2[i][j]; n       p.push_back(dfs2(v, i)); n     } n     if (p.size() <= 1) { n       continue; n     } n     sort(p.begin(), p.end()); n     int tmp = p[0] + p[1] - tot[i]; n     ans = min(ans, tmp); n   } n   cout << ans << endl; n } n int main() { n   while (cin >> n) { n     init(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define rep(i,a,b) for(int i = (a); i < b; ++i) n #define rrep(i,a,b) for(int i = (b); i --> (a);) n #define trav(x, v) for(auto &x : v) n #define all(v) (v).begin(),(v).end() n #define sz(v) (int)(v).size() n  n bool que_big(int s, string x){ n  cout << s+1 <<     << x << endl; n  string res; n  cin >> res; n  return res ==  yes ; n } n  n int main(){ n  int n = 200, b = 200; n  cin >> n >> b; n  n  vector<string> ub(n, string(b,  1 )); n  string lb(b,  0 ); n  deque<int> ko; n  rep(i,0,n) ko.push_back(i); n  string nxt = string(b,  1 ); n  nxt[0] =  0 ; n  while(true){ n   int s = ko.front(); n   if(ub[s] < lb){ n    ko.pop_front(); n    continue; n   } n   if(ub[s] == lb){ n    cout << 0 <<     << lb << endl; n    return 0; n   } n   if(que_big(s, nxt)){ n    lb = nxt; n    rrep(i,0,b){ n     if(lb[i]== 0 ){ n      lb[i] =  1 ; n      nxt[i] =  1 ; n      break; n     } else { n      lb[i] =  0 ; n     } n    } n   } else { n    ub[s] = nxt; n    ko.pop_front(); n    ko.push_back(s); n    rep(i,0,b) if(nxt[i] > lb[i]){ n     nxt[i] =  0 ; n     break; n    } n   } n  } n } n


#include <bits/stdc++.h> n using namespace std; n long long int con = 0; n vector<pair<long long int, long long int> > v; n long long int t[5000][5001]; n vector<vector<pair<long long int, long long int> > > ans(5001); n bool istrue[5001]; n vector<long long int> a[5001]; n long long int dis[5001]; n int n; n int ppp = 0; n queue<int> q; n void bfs(int src) { n   q.push(src); n   istrue[src] = true; n   dis[src] = 0; n   while (!q.empty()) { n     int p = q.front(); n     q.pop(); n     ans[con].push_back({(dis[p] + 1) % 2, p}); n     for (auto u : a[p]) { n       if (!istrue[u]) { n         dis[u] = dis[p] + 1; n         if (dis[u] % 2) n           v[con].second++; n         else n           v[con].first++; n         istrue[u] = true; n         q.push(u); n       } else { n         if (dis[u] % 2 == dis[p] % 2) { n           cout <<  NO  n                <<   n ; n           ppp = 1; n           return; n         } n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tt = 1; n   while (tt--) { n     long long int m; n     cin >> n >> m; n     long long int n1, n2, n3; n     cin >> n1 >> n2 >> n3; n     for (int i = 0; i < m; i++) { n       int x, y; n       cin >> x >> y; n       a[x].push_back(y); n       a[y].push_back(x); n     } n     for (int i = 1; i <= n; i++) { n       if (!istrue[i]) { n         v.push_back({1, 0}); n         bfs(i); n         con++; n       } n       if (ppp == 1) return 0; n     } n     memset(t, -1, sizeof(t)); n     if (v[0].first <= n2) t[0][v[0].first] = 0; n     if (v[0].second <= n2) t[0][v[0].second] = 0; n     for (int i = 1; i <= (long long int)v.size() - 1; i++) { n       for (int j = 0; j < n2 + 1; j++) { n         long long int vv = j - v[i].first; n         if (vv >= 0 && t[i - 1][vv] != -1) t[i][j] = vv; n         vv = j - v[i].second; n         if (vv >= 0 && t[i - 1][vv] != -1) t[i][j] = vv; n       } n     } n     char ss[n + 1]; n     if (t[(long long int)v.size() - 1][n2] == -1) n       cout <<  NO  n            <<   n ; n     else { n       cout <<  YES  n            <<   n ; n       long long int i = (long long int)v.size() - 1, ind = n2; n       while (i != -1) { n         long long int pp = ind - t[i][ind], kk = 0; n         if (v[i].first == pp) kk = 1; n         for (auto u : ans[i]) { n           if (u.first == kk) n             ss[u.second] =  2 ; n           else n             ss[u.second] =  1 ; n         } n         ind = t[i][ind]; n         i--; n       } n       for (int j = 1; j <= n; j++) { n         if (ss[j] ==  1 ) { n           if (n1 == 0) n             ss[j] =  3 ; n           else n             n1--; n         } n         cout << ss[j]; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; 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 k; n       cin >> k; n       if (k % 2 != (i + j) % 2) k++; n       cout << k <<    ; n     } n     cout << endl; n   } n } n int main() { n   int n; n   cin >> n; n   while (n--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MXN = 5e4 + 5; n const int INF = 1e9; n const long long P = 29; n const long long MOD = 1e9 + 7; n int t; n int main() { n   srand(time(0)); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(10); n   cin >> t; n   while (t--) { n     int n, x1, x2, x3; n     cin >> n; n     cin >> x1 >> x2; n     for (int i = 2; i < n; i++) { n       cin >> x3; n     } n     if (x1 + x2 <= x3) n       cout << 1 <<     << 2 <<     << n <<   n ; n     else n       cout << -1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0, flag = 1; n   char c = getchar(); n   while ((c >  9  || c <  0 ) && c !=  - ) c = getchar(); n   if (c ==  - ) flag = 0, c = getchar(); n   while (c <=  9  && c >=  0 ) { n     x = (x << 3) + (x << 1) + c -  0 ; n     c = getchar(); n   } n   return flag ? x : -x; n } n const int maxn = 2005; n int f[maxn][maxn]; n char s[maxn][maxn]; n int main() { n   int n = read(), m = read(); n   for (int i = (1); i <= (int)(n); i++) scanf( %s , s[i] + 1); n   for (int i = (1); i <= (int)(2); i++) n     for (int j = (1); j <= (int)(m); j++) f[i][j] = 1; n   for (int i = (3); i <= (int)(n); i++) n     for (int j = (1); j <= (int)(m); j++) { n       if (j == 1 || j == m) { n         f[i][j] = 1; n         continue; n       } n       if (s[i][j] != s[i - 1][j - 1] || s[i][j] != s[i - 1][j] || n           s[i][j] != s[i - 1][j + 1] || s[i][j] != s[i - 2][j]) { n         f[i][j] = 1; n         continue; n       } n       int x = 100000; n       x = min(x, f[i - 1][j - 1]); n       x = min(x, f[i - 1][j]); n       x = min(x, f[i - 1][j + 1]); n       x = min(x, f[i - 2][j]); n       f[i][j] = x + 1; n     } n   long long ans = 0; n   for (int i = (1); i <= (int)(n); i++) n     for (int j = (1); j <= (int)(m); j++) ans += f[i][j]; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename S, typename T> n ostream& operator<<(ostream& out, pair<S, T> const& p) { n   out <<  (  << p.first <<  ,   << p.second <<  ) ; n   return out; n } n template <typename T> n ostream& operator<<(ostream& out, vector<T> const& v) { n   long long l = v.size(); n   for (long long i = 0; i < l - 1; i++) out << v[i] <<    ; n   if (l > 0) out << v[l - 1]; n   return out; n } n template <typename T> n void trace(const char* name, T&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename T, typename... Args> n void trace(const char* names, T&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   trace(comma + 1, args...); n } n const long long N = 3e5 + 100; n long long MOD; n long long power(long long x, long long t) { n   long long ans = 1; n   while (t > 0) { n     if (t & 1) ans = 1LL * ans * x % MOD; n     x = 1LL * x * x % MOD; n     t >>= 1; n   } n   return ans; n } n inline long long sub(long long a, long long b) { n   a %= MOD; n   b %= MOD; n   a = ((a - b) % MOD + MOD) % MOD; n   return a; n } n inline long long add(long long a, long long b) { n   a %= MOD; n   b %= MOD; n   a = (a + b) % MOD; n   return a; n } n inline long long mul(long long a, long long b) { n   a %= MOD; n   b %= MOD; n   a = (a * b) % MOD; n   return a; n } n inline long long inv(long long a) { n   a = a % MOD; n   a = power(a, MOD - 2); n   return a; n } n long long a[N], n, k; n map<long long, long long> cnt; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> MOD >> k; n   for (long long i = 1; i <= n; i++) { n     long long temp; n     cin >> temp; n     long long kk = power(temp, 4); n     long long k2 = mul(k, temp); n     kk = sub(kk, k2); n     cnt[kk]++; n   } n   long long ans = 0; n   auto c2 = [](long long a) -> long long { return ((a * (a - 1)) / 2); }; n   for (auto i : cnt) { n     ans += c2(i.second); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 200000 + 10; n const long long INF = 2e9 + 10; n long long dic[N], idx[N]; n int main() { n   long long n, k, ii; n   scanf( %lld%lld , &n, &k); n   for (long long i = 0; i <= n; ++i) scanf( %lld , idx + i), dic[i] = idx[i]; n   for (long long i = 1; i <= n + 1; ++i) n     dic[i] += dic[i - 1] / 2, dic[i - 1] %= 2; n   for (long long i = 0; i <= n + 1; ++i) { n     if (dic[i]) { n       ii = i; n       break; n     } n   } n   long long x = 0, ans = 0; n   for (long long i = n + 1; i >= 0; --i) { n     x = x * 2 + dic[i]; n     if (abs(x) > INF) break; n     if (i > ii || i == n + 1 || (i == n && x == idx[i])) continue; n     if (abs(x - idx[i]) <= k) ++ans; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortmahstyle(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.first < b.first) return true; n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, m; n   cin >> n >> m; n   long long int hash[n + 1], col[4]; n   memset(hash, 0, sizeof(hash)); n   while (m--) { n     memset(col, 0, sizeof(col)); n     long long int inp[3]; n     for (long long int i = 0; i < 3; i++) { n       cin >> inp[i]; n       col[hash[inp[i]]] = 1; n     } n     for (long long int i = 0; i < 3; i++) { n       long long int x = inp[i]; n       if (hash[x] == 0) { n         for (long long int j = 1; j < 4; j++) { n           if (col[j] == 0) { n             hash[x] = j; n             col[hash[x]] = 1; n             break; n           } n         } n       } else { n         col[hash[x]] = 1; n       } n     } n   } n   for (long long int i = 1; i < n + 1; i++) cout << hash[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<long long int> a(n), b(n); n   set<long long int> s; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     s.insert(a[i]); n   } n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n     s.insert(b[i]); n   } n   long long int count = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       long long int temp = a[i] ^ b[j]; n       if (s.find(temp) != s.end()) count++; n     } n   } n   if (count % 2 != 0) n     cout <<  Koyomi  << endl; n   else n     cout <<  Karen  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100010]; n int main() { n   long long n, pos; n   while (cin >> n >> pos) { n     for (long long i = 0; i < n; i++) scanf( %I64d , &a[i]); n     sort(a, a + n); n     long long res1 = 0, res2, res; n     if (pos < a[0]) n       res1 = a[n - 2] - pos; n     else if (a[0] <= pos && pos <= a[n - 2]) n       res1 = min((pos - a[0]) * 2 + a[n - 2] - pos, n                  (a[n - 2] - pos) * 2 + pos - a[0]); n     else if (pos > a[n - 2]) n       res1 = pos - a[0]; n     if (pos < a[1]) n       res2 = a[n - 1] - pos; n     else if (a[1] <= pos && pos <= a[n - 1]) n       res2 = min((pos - a[1]) * 2 + a[n - 1] - pos, n                  (a[n - 1] - pos) * 2 + pos - a[1]); n     else if (pos > a[n - 1]) n       res2 = pos - a[1]; n     res = min(res1, res2); n     if (n == 1) res = 0; n     printf( %I64d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const int M = 310; n int n, m, s, e0, E; n pair<int, int> dp[N][M]; n int a[N]; n vector<int> elAtB[N]; n inline int findj(int j0, int val) { n   if (j0 >= m) return -1; n   vector<int> &v = elAtB[val]; n   if (v.size() == 0) return -1; n   vector<int>::iterator it = lower_bound(v.begin(), v.end(), j0); n   if (it == v.end()) n     return -1; n   else n     return *it; n } n int main() { n   cin >> n >> m >> s >> e0; n   E = s / e0; n   int ost = s - E * e0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int j = 0; j < m; j++) { n     int x; n     cin >> x; n     elAtB[x].push_back(j); n   } n   int ans = 0; n   for (int i = 0; i <= n; i++) { n     for (int e = 0; e <= E; e++) { n       dp[i][e] = make_pair(0, 0); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int e = 1; e <= E; e++) { n       int dol = dp[i][e].first; n       int j0 = dp[i][e].second; n       int dolnxt = dp[i + 1][e].first; n       if (dol > dolnxt) { n         dp[i + 1][e] = make_pair(dol, j0); n       } n       int j = findj(j0, a[i]); n       if (j == -1) n         continue; n       else { n       } n       dolnxt = dp[i + 1][e - 1].first; n       int jnxt = dp[i + 1][e - 1].second; n       if (((dol + 1 > dolnxt) || (dol + 1 == dolnxt && j < jnxt)) && n           ((i + 1 + j + 1) <= (e - 1) * e0 + ost)) { n         dp[i + 1][e - 1] = make_pair(dol + 1, j + 1); n         ans = max(ans, dol + 1); n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i, j, k, n; n   scanf( %d , &n); n   k = sqrt(n); n   for (i = k; i >= 1; i--) { n     if (n % i == 0) { n       j = i; n       break; n     } n   } n   printf( %d %d , j, (n / j)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 100005; n long long n, d, b; n long long a[maxn]; n bool f(long long x) { n   long long l = 1 + x; n   long long r = n - x; n   deque<pair<long long, long long>> dq; n   for (long long i = 1; i <= n; i++) n     if (a[i] != 0) dq.push_front({i, a[i]}); n   long long t = 1 + x; n   while (l <= r) { n     long long left = b; n     while (left > 0) { n       if (dq.empty()) return false; n       pair<long long, long long> p = dq.back(); n       dq.pop_back(); n       long long amt = min(p.second, left); n       if (p.first - l > t * d) return false; n       p.second -= amt; n       left -= amt; n       if (p.second > 0) dq.push_back(p); n     } n     l++; n     if (l <= r) { n       long long right = b; n       while (right > 0) { n         if (dq.empty()) return false; n         pair<long long, long long> p = dq.front(); n         dq.pop_front(); n         long long amt = min(p.second, right); n         if (r - p.first > t * d) return false; n         p.second -= amt; n         right -= amt; n         if (p.second > 0) dq.push_front(p); n       } n       r--; n     } n     t++; n   } n   return true; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> d >> b; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   long long l = 0; n   long long r = n; n   while (r - l > 3) { n     long long mid = (r - l) / 2 + l; n     if (f(mid)) n       r = mid; n     else n       l = mid; n   } n   for (long long i = l; i <= r; i++) { n     if (f(i)) { n       cout << i <<   n ; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O3 ) n #pragma GCC target( avx ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n const int MOD = 1000000007; n const int INF = 1000000009; n const int MAXN = 1000005; n vector<string> split(const string& s, char c) { n   vector<string> v; n   stringstream ss(s); n   string x; n   while (getline(ss, x, c)) v.emplace_back(x); n   return move(v); n } n template <typename T, typename... Args> n inline string arrStr(T arr, int n) { n   stringstream s; n   s <<  [ ; n   for (int i = 0; i < n - 1; i++) s << arr[i] <<  , ; n   s << arr[n - 1] <<  ] ; n   return s.str(); n } n bool is_prime(long long n) { n   if (n < 2) return false; n   for (long long x = 2; x * x <= n; x++) { n     if (n % x == 0) return false; n   } n   return true; n } n vector<long long> get_prime_factors(long long n) { n   vector<long long> f; n   for (long long x = 2; x * x <= n; x++) { n     while (n % x == 0) { n       f.push_back(x); n       n /= x; n     } n   } n   if (n > 1) f.push_back(n); n   return f; n } n long long fpow(long long x, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) { n       res = (1LL * res * x) % MOD; n     } n     x = (1LL * x * x) % MOD; n     n >>= 1; n   } n   return (res % MOD); n } n vector<long long> fac, inv; n void create_fac(long long n = MAXN) { n   fac.resize(n + 5); n   inv.resize(n + 5); n   fac[0] = 1; n   inv[0] = fpow(fac[0], MOD - 2); n   for (long long i = 1; i <= n; i++) { n     fac[i] = (fac[i - 1] % MOD * i % MOD) % MOD; n     inv[i] = fpow(fac[i], MOD - 2); n   } n } n long long binomial_coefficient(long long n, long long r) { n   long long res = 1; n   res = (res * (fac[n] % MOD)) % MOD; n   res = (res * (inv[r] % MOD)) % MOD; n   res = (res * (inv[n - r] % MOD)) % MOD; n   return res; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n long long sceil(long long a, long long b) { return (a + b - 1) / b; } n vector<long long> lp; n vector<long long> pr; n void sieve(long long n = MAXN) { n   lp.resize(n + 1); n   pr.clear(); n   for (long long i = 2; i <= n; ++i) { n     if (lp[i] == 0) { n       lp[i] = i; n       pr.push_back(i); n     } n     for (long long j = 0; n          j < (long long)pr.size() && pr[j] <= lp[i] && i * pr[j] <= n; ++j) n       lp[i * pr[j]] = pr[j]; n   } n } n long long extended_euclidean(long long a, long long b, long long& x, n                              long long& y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long x1, y1; n   long long d = extended_euclidean(b, a % b, x1, y1); n   x = y1; n   y = x1 - y1 * (a / b); n   return d; n } n int mod_inv(long long a, long long m) { n   long long x, y; n   long long g = extended_euclidean(a, m, x, y); n   if (g != 1) { n     return -1; n   } else { n     x = (x % m + m) % m; n     return x; n   } n } n long long solution() { n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   long long ans = 0; n   if (k <= n) ans = k; n   vector<long long> f(26, 0); n   for (auto c : s) { n     f[c -  a ]++; n   } n   long long MX = *max_element(f.begin(), f.end()); n   vector<long long> v(MX, 0); n   for (int times = MX; times > 0; times--) { n     for (int i = 0; i < 26; i++) { n       v[times - 1] += f[i] / times; n     } n   } n   reverse(v.begin(), v.end()); n   42; n   for (long long i = 1; i <= k && i <= n; i++) { n     if (k % i == 0) { n       long long times = MX - (lower_bound(v.begin(), v.end(), i) - v.begin()); n       42; n       42; n       ans = max(ans, times * i); n     } n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int tt = 1; n   cin >> tt; n   while (tt--) { n     cout << solution() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int iinf = 1e9 + 10; n const long long inf = 1ll << 60; n const long long mod = 1e9 + 7; n void GG() { n   cout <<  0 n ; n   exit(0); n } n long long mpow(long long a, long long n, long long mo = mod) { n   long long re = 1; n   while (n > 0) { n     if (n & 1) re = re * a % mo; n     a = a * a % mo; n     n >>= 1; n   } n   return re; n } n long long inv(long long b, long long mo = mod) { n   if (b == 1) return b; n   return (mo - mo / b) * inv(mo % b, mo) % mo; n } n const int maxn = 1e6 + 5; n long long dp[2003][22]; n long long fac[200006], ifac[200006]; n inline long long CC(int a, int b) { n   a = a + b; n   return fac[a] * ifac[b] % mod * ifac[a - b] % mod; n } n int R[2003], C[2003]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   fac[0] = ifac[0] = 1; n   for (int i = 1; i <= 200006; ++i) { n     fac[i] = fac[i - 1] * i % mod; n     ifac[i] = inv(fac[i]); n   } n   int n, m, k, second; n   cin >> n >> m >> k >> second; n   vector<pair<int, int> > v; n   v.push_back({1, 1}); n   v.push_back({n, m}); n   for (int i = 0; i < k; ++i) { n     int r, c; n     cin >> r >> c; n     v.push_back({r, c}); n   } n   sort(v.begin(), v.end()); n   for (int i = 1; i <= (int)v.size() - 1; ++i) { n     R[i] = v[i].first, C[i] = v[i].second; n     dp[i][0] = CC(R[i] - 1, C[i] - 1); n     for (int fk = 0; fk < 21; ++fk) { n       for (int j = 1; j <= i - 1; ++j) { n         if (R[j] <= R[i] && C[j] <= C[i]) { n           dp[i][fk] -= dp[j][fk] * CC(R[i] - R[j], C[i] - C[j]) % mod; n           if (dp[i][fk] < 0) dp[i][fk] += mod; n           dp[i][fk + 1] += dp[j][fk] * CC(R[i] - R[j], C[i] - C[j]) % mod; n           if (dp[i][fk + 1] >= mod) dp[i][fk + 1] -= mod; n         } n       } n     } n   } n   long long re = 0; n   for (int fk = 0; fk < 22; ++fk) { n     re += dp[(int)v.size() - 1][fk] * second % mod; n     second = (second + 1) / 2; n   } n   cout << re * inv(CC(n - 1, m - 1)) % mod <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct lem { n   long long m; n   long long v; n   int id; n   bool operator<(const lem& other) const { n     if (m > other.m) return true; n     if (m < other.m) return false; n     if (v > other.v) return true; n     if (v < other.v) return false; n     return id > other.id; n   } n }; n int N, K, H; n lem L[100010]; n int asst[100010]; n int tempa[100010]; n bool solve(long double t) { n   memset(tempa, 0, sizeof(tempa)); n   int start = 0; n   for (int k = K; k >= 1; k--) { n     bool found = false; n     for (int i = (start); i <= (N - 1); i++) { n       long double time = n           ((long double)k) / ((long double)L[i].v) * ((long double)H); n       if (time <= t) { n         found = true; n         tempa[k] = i; n         start = i + 1; n         break; n       } n     } n     if (!found) return false; n   } n   return true; n } n int main() { n   cin >> N >> K >> H; n   for (int i = 0; i < N; i++) cin >> L[i].m; n   for (int i = 0; i < N; i++) cin >> L[i].v; n   for (int i = 0; i < N; i++) L[i].id = i + 1; n   sort(L, L + N); n   long double low = 0.l; n   long double high = 1000000000.l; n   int times = 100; n   while (times--) { n     long double t = (low + high) / 2.l; n     if (solve(t)) { n       for (int i = (1); i <= (K); i++) asst[i] = tempa[i]; n       high = t; n     } else { n       low = t; n     } n   } n   for (int k = (1); k <= (K); k++) { n     if (k > 1) cout <<    ; n     cout << L[asst[k]].id; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 20; n const int inf = 1e9; n int dp[maxn][maxn][maxn]; n int vis[maxn][maxn][maxn]; n int path[maxn][maxn][maxn]; n int n, a, b; n int h[maxn]; n int dfs(int i, int cur, int pre) { n   cur = cur < 0 ? 0 : cur; n   pre = pre < 0 ? 0 : pre; n   if (i == n) { n     if (cur == 0) n       return 0; n     else n       return inf; n   } n   if (vis[i][cur][pre]) return dp[i][cur][pre]; n   vis[i][cur][pre] = 1; n   int &ans = dp[i][cur][pre]; n   ans = inf; n   int lb = (pre + b - 1) / b, n       hb = max(lb, max((cur + a - 1) / a, (h[i + 1] + b) / b)); n   int p; n   for (int j = lb; j <= hb; j++) { n     int tmp = j + dfs(i + 1, h[i + 1] + 1 - j * b, cur - j * a); n     if (ans > tmp) { n       ans = tmp; n       path[i][cur][pre] = j; n     } n   } n   return ans; n } n void print(int i, int cur, int pre) { n   cur = cur < 0 ? 0 : cur; n   pre = pre < 0 ? 0 : pre; n   if (i == n) return; n   int tmp = path[i][cur][pre], cnt = tmp; n   while (cnt--) printf( %d  , i); n   print(i + 1, h[i + 1] + 1 - tmp * b, cur - tmp * a); n } n int main() { n   cin >> n >> a >> b; n   for (int i = 1; i <= n; i++) cin >> h[i]; n   memset(vis, 0, sizeof(vis)); n   int ans = dfs(2, h[2] + 1, h[1] + 1); n   cout << ans <<   n ; n   print(2, h[2] + 1, h[1] + 1); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, s1, s2, s3; n int sa[300010][20], lcp[300010], rnk[300010], root[300010], cnt[300010][4], n     col[300010], n; n pair<pair<int, int>, int> q[300010]; n pair<int, pair<int, int> > lcp_pair[300010]; n vector<long long> ansvec; n long long ans, mod = 1e9 + 7; n int get(int x) { return root[x] = (x == root[x] ? x : get(root[x])); } n void merge(int x, int y) { n   root[y] = x; n   ans -= (1LL * cnt[y][1] * cnt[y][2] * cnt[y][3]) % mod + n          (1LL * cnt[x][1] * cnt[x][2] * cnt[x][3]) % mod; n   while (ans < 0) ans += mod; n   for (int i = 1; i <= 3; ++i) cnt[x][i] += cnt[y][i]; n   ans = (ans + 1LL * cnt[x][1] * cnt[x][2] * cnt[x][3]) % mod; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> s1 >> s2 >> s3; n   n = min(s1.size(), min(s2.size(), s3.size())); n   s = s1 +  1  + s2 +  2  + s3; n   for (int i = 0; i < s1.size(); ++i) col[i] = 1; n   for (int i = s1.size() + 1; i < s1.size() + 1 + s2.size(); ++i) col[i] = 2; n   for (int i = s.size() - s3.size(); i < s.size(); ++i) col[i] = 3; n   for (int i = 0; i < s.size(); ++i) sa[i][0] = s[i]; n   for (int i = 1; i < 20; ++i) { n     for (int j = 0; j < s.size(); ++j) n       if (j + (1 << i - 1) < s.size()) n         q[j] = n             make_pair(make_pair(sa[j][i - 1], sa[j + (1 << i - 1)][i - 1]), j); n       else n         q[j] = make_pair(make_pair(sa[j][i - 1], -1), j); n     sort(q, q + s.size()); n     int cnt = 0; n     for (int j = 0; j < s.size(); ++j) { n       if (j && q[j].first != q[j - 1].first) ++cnt; n       sa[q[j].second][i] = cnt; n     } n   } n   for (int i = 0; i < s.size(); ++i) { n     rnk[sa[i][19]] = i; n     root[i] = i; n     cnt[i][col[i]] = 1; n   } n   int cnt = 0, index, ii = 0; n   for (int i = 0; i < s.size(); ++i) { n     index = sa[i][19]; n     if (index == s.size() - 1) { n       cnt = 0; n       continue; n     } n     while (i + cnt < s.size() && rnk[index + 1] + cnt < s.size() && n            s[i + cnt] == s[rnk[index + 1] + cnt]) n       ++cnt; n     lcp[i] = cnt; n     lcp_pair[ii] = make_pair(lcp[i], make_pair(i, rnk[index + 1])); n     if (cnt) --cnt; n     ++ii; n   } n   sort(lcp_pair, lcp_pair + s.size() - 1); n   reverse(lcp_pair, lcp_pair + s.size() - 1); n   for (int i = n, j = 0; i >= 1; --i) { n     int x, y; n     while (j < s.size() - 1 && lcp_pair[j].first >= i) { n       x = get(lcp_pair[j].second.first); n       y = get(lcp_pair[j].second.second); n       merge(x, y); n       ++j; n     } n     ansvec.push_back(ans); n   } n   for (int i = ansvec.size() - 1; i >= 0; --i) cout << ansvec[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> v; n string s; n const int N = 1000001; n int n; n long long d, arr[N]; n unsigned long long gcd(long long x, long long y) { n   if (y == 0) { n     return x; n   } n   return gcd(y, x % y); n } n void gen(long long ind, string t) { n   if (ind == s.size()) { n     if (t.size() == 3) v.push_back(t); n     return; n   } n   gen(ind + 1, t + s[ind]); n   gen(ind + 1, t); n } n long long check() { n   long long res = 0; n   for (long long i = 0; i < v.size(); ++i) { n     if (((v[i][v[i].size() - 1] -  0 ) - (v[i][0] -  0 )) <= d) { n       res++; n     } n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, l, r; n   bool lie = 0; n   cin >> n >> l >> r; n   int v[n], s[n]; n   for (int i = 1; i <= n; ++i) { n     cin >> v[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> s[i]; n   } n   for (int i = 1; i < l; ++i) { n     if (v[i] != s[i]) { n       lie = 1; n       break; n     } n   } n   if (!lie) { n     for (int i = r + 1; i <= n; ++i) { n       if (v[i] != s[i]) { n         lie = 1; n         break; n       } n     } n   } n   if (lie) { n     cout <<  LIE n ; n   } else { n     cout <<  TRUTH n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1 + 9e6; n const int inf = 0x3f3f3f; n vector<bool> dp[24]; n int n; n int a[23]; n vector<pair<int, int> > pre[23]; n int ans; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   for (int i = 0; i < 24; i++) dp[i].resize(1 << n); n   for (int i = 1; i < n; ++i) { n     for (int j = 0; j < i; ++j) { n       for (int k = j; k < i; ++k) { n         if (a[j] + a[k] == a[i]) { n           pre[i].push_back({j, k}); n         } n       } n     } n   } n   dp[0][0] = 0; n   dp[0][1] = 1; n   for (int i = 0; i < n - 1; ++i) { n     for (int mask = 0; mask < (1 << n); ++mask) { n       if (dp[i][mask]) { n         for (int j = 0; j < pre[i + 1].size(); ++j) { n           int u = pre[i + 1][j].first; n           int v = pre[i + 1][j].second; n           if (((mask >> u) & 1) && ((mask >> v) & 1)) { n             int bit = mask + (1 << (i + 1)); n             dp[i + 1][bit] = 1; n             for (int k = 0; k < i + 1; ++k) { n               if (((bit >> k) & 1)) { n                 dp[i + 1][bit ^ (1 << k)] = 1; n               } n             } n           } n         } n       } n     } n   } n   ans = inf; n   for (int mask = 0; mask < (1 << n); ++mask) { n     if (dp[n - 1][mask]) { n       ans = min(ans, (int)(__builtin_popcount(mask))); n     } n   } n   if (ans == inf) n     cout << -1; n   else n     cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string onezero(int n) { n   string s =  1 ; n   for (int i = 1; i < n; ++i) { n     s +=  0 ; n   } n   return s; n } n string findSum(string str1, string str2) { n   if (str1.length() > str2.length()) swap(str1, str2); n   string str =   ; n   int n1 = str1.length(), n2 = str2.length(); n   reverse(str1.begin(), str1.end()); n   reverse(str2.begin(), str2.end()); n   int carry = 0; n   for (int i = 0; i < n1; i++) { n     int sum = ((str1[i] -  0 ) + (str2[i] -  0 ) + carry); n     str.push_back(sum % 10 +  0 ); n     carry = sum / 10; n   } n   for (int i = n1; i < n2; i++) { n     int sum = ((str2[i] -  0 ) + carry); n     str.push_back(sum % 10 +  0 ); n     carry = sum / 10; n   } n   if (carry) str.push_back(carry +  0 ); n   reverse(str.begin(), str.end()); n   return str; n } n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(NULL); n   cout.tie(NULL); n   int l; n   string s, ans; n   cin >> l >> s; n   if (s.size() % l != 0) { n     string zero = onezero(l); n     for (int i = 0; i < s.size() / l + 1; ++i) { n       ans += zero; n     } n   } else { n     string temp = s.substr(0, l); n     for (int i = 0; i < s.size() / l; ++i) { n       ans += temp; n     } n     if (ans <= s) { n       ans =   ; n       temp = findSum(temp,  1 ); n       if (temp.size() != l) { n         temp = onezero(l); n         ans = onezero(l); n       } n       for (int i = 0; i < s.size() / l; ++i) { n         ans += temp; n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 1e5 + 1; n long long int n, m, i, j, l, k, x, y, a, b, cnt[3], sum[3]; n void solve() { n   string s; n   cin >> s; n   long long int n = s.length(); n   long long int lst = 0, maxi = -1; n   long long int val = 0; n   for (long long int i = 0; i < n; i++) { n     if (s[i] ==  L ) { n       val++; n     } else { n       maxi = max(maxi, val); n       val = 0; n     } n   } n   maxi = max(maxi, val); n   cout << maxi + 1 <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[1010], d[1010]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     d[a[i]]++; n   } n   int mx = 0, mxi = 0; n   for (int i = 1; i <= n; i++) { n     if (d[a[i]] > mx) mx = d[a[i]]; n     if (d[a[i]] != 0) { n       mxi++; n       d[a[i]] = 0; n     } n   } n   cout << mx <<     << mxi; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   long long answer = 0; n   for (int i = s.length() - 1; i > 0; i--) { n     string temp = s.substr(i - 1, 2); n     if (stoi(temp) % 4 == 0) { n       answer += i; n     } n   } n   for (int i = 0; i < s.length(); i++) { n     if (stoi(s.substr(i, 1)) % 4 == 0) { n       answer++; n     } n   } n   cout << answer; n } n


#include <bits/stdc++.h> n using namespace std; n int c[1005]; n int a[12] = {0, 4, 10, 20, 35, 56, 83, 116, 155, 198, 244, 292}; n int main() { n   long long n; n   while (cin >> n) { n     if (n <= 11) { n       cout << a[n] << endl; n     } else { n       long long m; n       m = a[11] + (n - 11) * 49; n       cout << m << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int ret = 0, flag = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) flag = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) ret = ret * 10 + (c ^  0 ), c = getchar(); n   return flag * ret; n } n struct use { n   int x, dis; n }; n const int N = 5e3 + 5; n int T, n, d; n vector<int> edge[N]; n int dad[N]; n int build(int x, int dis) { n   int sum = 0; n   if ((x << 1) <= n) { n     edge[x].push_back(x << 1); n     sum += dis + 1; n     sum += build(x << 1, dis + 1); n   } n   if ((x << 1 | 1) <= n) { n     edge[x].push_back(x << 1 | 1); n     sum += dis + 1; n     sum += build(x << 1 | 1, dis + 1); n   } n   return sum; n } n void bfs() { n   queue<use> q; n   q.push({1, 0}); n   int now = 0; n   vector<int> p1, p2; n   while (q.empty() == 0) { n     int x = q.front().x, dis = q.front().dis; n     q.pop(); n     if (now != dis) { n       now = dis, p1.clear(), p2.clear(); n     } n     for (auto it = edge[x].begin(); it != edge[x].end(); it++) { n       int y = *it; n       if (edge[y].size() == 0) { n         p1.push_back(y); n         if (p2.size()) { n           edge[p2[0]].push_back(y); n           edge[x].erase(it); n           return; n         } n       } n       if (edge[y].size() != 2) p2.push_back(y); n       q.push({y, dis + 1}); n     } n   } n } n int main() { n   T = read(); n   while (T--) { n     n = read(), d = read(); n     for (int i = 1; i <= n; i++) edge[i].clear(); n     int mn = build(1, 0); n     int mx = (n - 1) * n / 2; n     if (!(mn <= d && d <= mx)) { n       printf( NO n ); n       continue; n     } n     int now = mn; n     while (now < d) { n       bfs(); n       now++; n     } n     for (int i = 1; i <= n; i++) { n       for (int j = 0; j < edge[i].size(); j++) dad[edge[i][j]] = i; n     } n     printf( YES n ); n     for (int i = 2; i <= n; i++) printf( %d  , dad[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double ans, m; n int n; n double binpow(double a, int b) { n   if (b == 0) n     return 1; n   else if (b == 1) n     return a; n   else if (b % 2 == 0) { n     double t = binpow(a, b / 2); n     return t * t; n   } else n     return a * binpow(a, b - 1); n } n int main() { n   cin >> m >> n; n   for (double i = 1; i <= m; i++) { n     ans += i * binpow(i / m, n) * (1 - binpow((i - 1) / i, n)); n   } n   cout << fixed << setprecision(15) << ans; n } n


#include <bits/stdc++.h> n int main() { n   int i; n   for (i = 0; i < 10; i++) { n     printf( ??%d>>%d?? n , i, i); n   } n   printf( ??>>? n ); n   for (i = 0; i < 9; i++) { n     printf( %d?<>%d n , i, i + 1); n   } n   printf( 9?>>?0 n?<>1 n>>?? n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   long long t = n; n   vector<pair<int, int> > laptop; n   for (int i = 0; i < n; i++) { n     long long temp1, temp2; n     cin >> temp1 >> temp2; n     laptop.push_back(make_pair(temp1, temp2)); n   } n   sort(laptop.begin(), laptop.end()); n   int cur = laptop[0].second; n   for (int i = 1; i < n; ++i) { n     if (cur > laptop[i].second) { n       cout <<  Happy Alex n ; n       return 0; n     } n     cur = laptop[i].second; n   } n   cout <<  Poor Alex n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 7, M = 1e9 + 7; n vector<int> primes; n bool isp[N]; n void sieve() { n   fill(isp, isp + N, 1); n   isp[0] = isp[1] = 0; n   for (int i = 2; i * i < N; i++) n     if (isp[i]) n       for (int j = i * i; j < N; j += i) isp[j] = 0; n   for (int i = 2; i < N; i++) n     if (isp[i]) primes.push_back(i); n } n vector<pair<int, int> > factorize(int x) { n   vector<pair<int, int> > ans; n   for (int i = 2; i * i <= x; i++) { n     int cnt = 0; n     while (x % i == 0) cnt++, x /= i; n     if (cnt) ans.push_back(make_pair(i, cnt)); n   } n   if (x > 1) ans.push_back(make_pair(x, 1)); n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   long long ans = 1; n   map<int, int> fac; n   map<int, int> cnt; n   vector<vector<pair<int, int> > > factors; n   int pp[n]; n   for (int i = 0; i < n; i++) cin >> pp[i]; n   sort(pp, pp + n); n   reverse(pp, pp + n); n   for (int i = 1; i <= n; i++) { n     int p = pp[i - 1]; n     if (fac.count(p) == 0) { n       ans = (ans * p) % M; n       fac[p] = 1; n       cnt[p] = 1; n       vector<pair<int, int> > v; n       v.push_back(make_pair(p, 1)); n       factors.push_back(v); n     } else { n       vector<pair<int, int> > pf = factorize(p - 1); n       factors.push_back(pf); n       for (auto pr : pf) { n         int p = pr.first; n         int e = pr.second; n         int dif = e - fac[p]; n         if (dif < 0) continue; n         if (dif == 0) { n           cnt[p]++; n           continue; n         } n         fac[p] += dif; n         cnt[p] = 1; n         for (int i = 0; i < dif; i++) ans = (ans * p) % M; n       } n     } n   } n   for (auto pf : factors) { n     bool found = true; n     for (auto pr : pf) { n       int p = pr.first; n       int e = pr.second; n       int mx = fac[p]; n       assert(e <= mx); n       if (e == mx && cnt[p] == 1) { n         found = false; n         break; n       } n     } n     if (found) { n       ans++; n       break; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long n, k, b1, b2, b[200007], ans, dl, dll[200007], an[200007]; n map<unsigned long long, unsigned long long> a[11]; n int main() { n   ios::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   cin >> n >> k; n   for (unsigned long long i = 1; i <= n; i++) { n     cin >> b[i]; n     b1 = b[i]; n     dl = 0; n     while (b1 > 0) { n       dl++; n       b1 = b1 / 10; n     } n     dll[i] = dl; n     b1 = b[i]; n     a[dl][b[i] % k]++; n   } n   for (unsigned long long i = 1; i <= n; i++) { n     b1 = 1; n     for (unsigned long long j = 1; j <= 10; j++) { n       b1 *= 10; n       b2 = b[i] * b1; n       b2 %= k; n       b2 = k - b2; n       if (b2 == k) b2 = 0; n       if (a[j].find(b2) != a[j].end()) { n         ans += a[j][b2]; n         if (dll[i] == j && b2 == b[i] % k) ans--; n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = int(1e6) + 100; n const long long inf = 1e6; n int n, m; n int a[maxn], cnt[maxn], b[maxn]; n void read() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= n; ++i) b[i] = a[i]; n } n long long POW(long long num, int times) { n   long long ans = 1; n   while (times) { n     if (times & 1) ans = ans * num; n     num = num * num; n     if (ans >= inf) return inf; n     if (num >= inf) num = inf; n     times >>= 1; n   } n   return ans; n } n void solve() { n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     int num = a[i]; n     a[i] = 1; n     long long inv = 1; n     for (int j = 2; j * j <= num && num != 1; ++j) n       if (num % j == 0) { n         int k = 0; n         int x = a[i]; n         while (num % j == 0) { n           k++; n           if (k == m) k = 0; n           if (k == 0) n             a[i] = x; n           else n             a[i] *= j; n           num /= j; n         } n         int tmp = POW(j, (m - k) % m); n         if (tmp == inf) n           inv = inf; n         else n           inv *= tmp; n         if (inv >= inf) inv = inf; n       } n     if (num != 1) a[i] *= num, inv *= POW(num, m - 1); n     if (inv < inf) ans += cnt[inv]; n     ++cnt[a[i]]; n   } n   printf( %lld n , ans); n } n int main() { n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 300000; n int n, arr[MX], bit[MX], ans = 0, lis; n void update(int x, int V) { n   while (x <= n) { n     bit[x] = max(bit[x], V); n     x += x & -x; n   } n } n int get(int x) { n   int ret = 0; n   while (x > 0) { n     ret = max(ret, bit[x]); n     x -= x & -x; n   } n   return ret; n } n int main() { n   scanf( %d , &n); n   memset(bit, 0, sizeof(bit)); n   for (int j = 1; j <= n; j++) { n     scanf( %d , &arr[j]); n     lis = get(arr[j]) + 1; n     update(arr[j] + 1, lis); n     ans = max(ans, lis); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<long long, std::vector<long long>, std::greater<long long> > pqs; n queue<long long> q; n stack<long long> sta; n set<long long> st; n set<long long>::iterator P; n long long i, j, n, m, t, k, a, b, c, x, mx, mn; n string s, s1; n char z; n vector<pair<long long, long long> > v; n map<pair<long long, long long>, long long> mp, pm; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int T = 1; n   cin >> T; n   while (T--) { n     i = j = n = m = t = k = a = b = c = x = mx = mn = 0; n     cin >> n >> m; n     m += 2 * n; n     for (i = 1; i <= n && m; i++) n       for (j = i + 1; j <= n && m; j++) cout << i <<     << j << endl, m--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   Node *arr[26]; n   int state; n   bool isEnd = false; n   Node() { n     for (int i = 0; i < 26; i++) arr[i] = nullptr; n     isEnd = true; n   } n }; n void insert(const string &s, Node *root) { n   Node *curr = root; n   for (char c : s) { n     if (curr->arr[c -  a ] == nullptr) curr->arr[c -  a ] = new Node(); n     curr = curr->arr[c -  a ]; n   } n   curr->isEnd = true; n } n int find_state(Node *curr, bool leaf = true) { n   bool winning = false; n   int cnt = 0; n   for (int i = 0; i < 26; i++) { n     if (curr->arr[i] != nullptr) { n       cnt++; n       if (find_state(curr->arr[i], leaf) == 0) winning = true; n     } n   } n   if (cnt == 0) winning = leaf; n   return curr->state = winning ? 1 : 0; n } n int main() { n   int n, k; n   cin >> n >> k; n   Node *root = new Node(); n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     insert(s, root); n   } n   int st1 = find_state(root, false); n   if (k == 1) { n     cout << (st1 == 1 ?  First  :  Second ) << endl; n   } else { n     int st2 = find_state(root, true); n     if (st2 == 1 && st1 == 1) { n       cout <<  First  << endl; n     } else if (st2 == 1 && st1 == 0) { n       cout <<  Second  << endl; n     } else if (st2 == 0 && st1 == 1) { n       cout << (k % 2 == 0 ?  Second  :  First ) << endl; n     } else { n       cout <<  Second  << endl <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n int main() { n   int n; n   while (cin >> n) { n     int ans = 0; n     for (int i = 1; i < n; i++) { n       int x, y; n       cin >> x >> y; n       ans += a[x] + a[y]; n       a[x]++, a[y]++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = pow(10, 9) + 7; n const long long MAX = LONG_LONG_MAX; n void solve() { n   int n; n   cin >> n; n   vector<string> a(n,   ); n   for (int i = 0; i < n; i += 1) cin >> a[i]; n   for (int i = 0; i < n; i += 1) { n     for (int j = 0; j < n; j += 1) { n       if (a[i][j] ==  1 ) { n         if (i == n - 1 || j == n - 1) { n           continue; n         } n         if (a[i + 1][j] ==  1  || a[i][j + 1] ==  1 ) { n           continue; n         } else { n           cout <<  NO ; n           return; n         } n       } n     } n   } n   cout <<  YES ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   cin >> t; n   while (t > 0) { n     solve(); n     t--; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int8_t sign(int a, int b, int &c, int x, int y) { n   return a * 1LL * x + b * 1LL * y + c < 0LL ? -1 : 1; n } n void solve() { n   int x, y, x1, y1; n   cin >> x >> y >> x1 >> y1; n   int16_t ans = 0; n   int16_t n; n   cin >> n; n   for (int i = 0, u, v, c; i < n; i++) { n     cin >> u >> v >> c; n     ans += sign(u, v, c, x, y) != sign(u, v, c, x1, y1); n   } n   cout << ans; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tkd = 1; n   while (tkd-- > 0) { n     solve(); n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64777216 ) n using namespace std; n template <class T> n inline T sqr(T x) { n   return x * x; n } n const double pi = acos(-1.0), eps = 1e-9; n const int INF = 1000 * 1000 * 1000, MAXN = 1000005, MOD = 1000000007; n const long long INFL = 1000000000000000000; n long long n, m, cnt[MAXN][26]; n string x, y; n void prepare(string s) { n   if ((int)s.size() != 0) { n     freopen((s +  .in ).c_str(),  r , stdin); n     freopen((s +  .out ).c_str(),  w , stdout); n   } n } n long long gcd(long long a, long long b) { return (b) ? gcd(b, a % b) : a; } n long long lca(long long a, long long b) { return a / gcd(a, b) * b; } n void get_data() { cin >> n >> m >> x >> y; } n void solve() { n   long long L = lca((int)x.size(), (int)y.size()); n   long long g = gcd((int)x.size(), (int)y.size()); n   long long ans = L; n   for (int i = 0; i < (int)y.size(); ++i) cnt[i % g][y[i] -  a ]++; n   for (int j = 0; j < (int)x.size(); ++j) ans -= cnt[j % g][x[j] -  a ]; n   cout << ans * (n * (int)x.size() / L); n } n int main() { n   prepare(  ); n   get_data(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[2004], temp[2004], e[2004], c, cnt; n int n; n int q[27]; n void f(int x) { n   cnt++; n   if (cnt != n * 3) n     printf( %d  , x); n   else n     printf( %d n , x); n   int i; n   for (i = n - x + 1; i <= n; i++) temp[n - i + 1] = s[i]; n   for (i = 1; i < n - x + 1; i++) temp[i + x] = s[i]; n   for (i = 1; i <= n; i++) s[i] = temp[i]; n } n int main() { n   int t, i, j, k, x, l1, l2, flag, o; n   scanf( %d , &t); n   scanf( %s , s + 1); n   scanf( %s , e + 1); n   l1 = strlen(s + 1); n   l2 = strlen(e + 1); n   if (l1 != l2) { n     printf( -1 n ); n     return 0; n   } n   memset(q, 0, sizeof(q)); n   for (i = 1; i <= l1; i++) { n     q[s[i] -  a ]++; n     q[e[i] -  a ]--; n   } n   flag = 1; n   for (i = 0; i < 26; i++) { n     if (q[i]) flag = 0; n   } n   if (flag == 0) { n     printf( -1 n ); n     return 0; n   } n   n = l1; n   printf( %d n , n * 3); n   cnt = 0; n   for (o = 0; o < n; o++) { n     f(n); n     c = e[n - o]; n     for (i = n - o; i >= 0; i--) { n       if (s[i] == c) break; n     } n     f(n - i); n     f(1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 10, MAX = 7e5 + 1e4, MOD = 1e9 + 7, MAXL = 25; n void OUT(long double o, int x) { n   cout << fixed << setprecision(x) << o; n   return; n } n long long sth[MAX], mah[MAX], reg[MAX], ti[MAX], en[MAX], he[MAX], hh[MAX], n     pa[MAX]; n long long la[MAX]; n pair<pair<long long, long long>, long long> so[MAX]; n set<long long> st; n map<long long, long long> mp; n vector<pair<pair<long long, long long>, long long> > v[MAX]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m, ans = 0, bo, da, in; n   bool inff = 0; n   cin >> n >> m >> bo >> in >> da; n   st.insert(0); n   for (int i = 1; i <= n; i++) { n     cin >> mah[i] >> sth[i] >> reg[i]; n     if (mah[i] <= da) inff = 1; n     hh[i] = sth[i]; n     if (hh[i] <= da && (reg[i] != 0 && mah[i] > da)) { n       st.insert((da - hh[i]) / reg[i]); n       st.insert(((da - hh[i]) / reg[i]) + 1); n     } n   } n   for (int i = 1; i <= m; i++) { n     cin >> ti[i] >> en[i] >> he[i]; n     so[i] = {{ti[i], en[i]}, he[i]}; n   } n   so[0].first.first = -1; n   sort(so + 1, so + m + 1); n   for (int i = 1; i <= m; i++) { n     hh[so[i].first.second] = so[i].second; n     st.insert(so[i].first.first); n     st.insert(so[i].first.first - 1); n     if (so[i].second <= da && n         (reg[so[i].first.second] != 0 && mah[so[i].first.second] > da)) { n       st.insert(so[i].first.first + n                 ((da - so[i].second) / reg[so[i].first.second])); n       st.insert(1 + so[i].first.first + n                 ((da - so[i].second) / reg[so[i].first.second])); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (reg[i] == 0 && hh[i] <= da) inff = 1; n   } n   if (inff == 1 && in != 0) return cout << -1, 0; n   long long cnt = 0, x = 0; n   for (int u : st) { n     mp[u] = cnt; n     cnt++; n   } n   for (int i = 1; i <= n; i++) { n     hh[i] = sth[i]; n     la[i] = -1; n     if (hh[i] <= da) n       x++; n     else n       la[i] = -2; n     if (hh[i] <= da && (reg[i] != 0 && mah[i] > da)) { n       pa[mp[1 + ((da - hh[i]) / reg[i])]]--; n       la[i] = ((da - hh[i]) / reg[i]); n     } n   } n   for (int i = 1; i <= m; i++) { n     v[mp[ti[i]]].push_back({{ti[i], en[i]}, he[i]}); n   } n   for (int u : st) { n     for (pair<pair<long long, long long>, long long> w : v[mp[u]]) { n       hh[w.first.second] = w.second; n       if (la[w.first.second] == -1 || la[w.first.second] >= u) { n         if (la[w.first.second] != -1) pa[mp[la[w.first.second] + 1]]++; n         if (hh[w.first.second] > da) n           x--; n         else { n           if ((reg[w.first.second] != 0 && mah[w.first.second] > da)) { n             pa[mp[1 + u + ((da - hh[w.first.second]) / reg[w.first.second])]]--; n             la[w.first.second] = n                 u + ((da - hh[w.first.second]) / reg[w.first.second]); n           } n         } n       } else { n         if (hh[w.first.second] <= da) { n           x++; n           if (reg[w.first.second] > 0) { n             pa[mp[u + 1 + ((da - hh[w.first.second]) / reg[w.first.second])]]--; n             la[w.first.second] = n                 u + ((da - hh[w.first.second]) / reg[w.first.second]); n           } else n             la[w.first.second] = -1; n         } n       } n     } n     x += pa[mp[u]]; n     ans = max(ans, (x * bo) + (x * in * u)); n   } n   cout << ans; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const int inf = 99999999; n int n; n char c[20]; n int r[20]; n int b[20]; n int dp[1 << 16][123]; n int main() { n   int n; n   cin >> n; n   int maxi = 0; n   int ans1 = 0; n   int ans2 = 0; n   for (int i = 0; i < n; i++) { n     cin >> c[i] >> r[i] >> b[i]; n     maxi += i; n     ans1 += r[i]; n     ans2 += b[i]; n   } n   memset(dp, -1, sizeof(dp)); n   dp[0][0] = 0; n   for (int i = 1; i < 1 << n; i++) { n     for (int j = 0; j < n; j++) { n       if (i >> j & 1) { n         int R = 0; n         int B = 0; n         for (int k = 0; k < n; k++) { n           if (k != j && i >> k & 1) { n             R += c[k] ==  R ; n             B += c[k] ==  B ; n           } n         } n         R = min(R, r[j]); n         B = min(B, b[j]); n         for (int k = 0; k <= maxi; k++) { n           if (dp[i ^ (1 << j)][k] != -1) { n             dp[i][k + R] = max(dp[i][k + R], dp[i ^ (1 << j)][k] + B); n           } n         } n       } n     } n   } n   int ans = max(ans1, ans2); n   for (int i = 0; i <= maxi; i++) { n     if (dp[(1 << n) - 1][i] != -1) { n       ans = min(ans, max(ans2 - dp[(1 << n) - 1][i], ans1 - i)); n     } n   } n   cout << ans + n; n   return 0; n } n


#include <bits/stdc++.h> n void output1(int n) { n   for (int i = 0; i < n; ++i) { n     printf( 10 ); n   } n   printf(  n ); n } n void output2(int m) { n   --m; n   for (int i = 0; i < m; ++i) { n     printf( 01 ); n   } n   printf( 0 n ); n } n void output3(int n, int m) { n   if (n == m + 1) { n     --n; n     for (int i = 0; i < n; ++i) { n       printf( 10 ); n     } n     printf( 1 n ); n     return; n   } n   int tmp = n - m - 1; n   int up = m + 1; n   up--; n   for (int i = 0; i < up; ++i) { n     printf( 1 ); n     if (tmp) { n       printf( 1 ); n       --tmp; n     } n     printf( 0 ); n   } n   printf( 1 ); n   if (tmp) { n     printf( 1 ); n   } n   printf(  n ); n } n int main() { n   int n, m; n   while (scanf( %d%d , &m, &n) != EOF) { n     if (m == n) { n       output1(n); n     } else if (m == n + 1) { n       output2(m); n     } else if (n > m && n <= 2 * m + 2) { n       output3(n, m); n     } else { n       printf( -1 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n const char nl =   n ; n using namespace std; n using ll = long long; n using vi = vector<int>; n using vl = vector<ll>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using str = string; n str to_string(char c) { return str(1, c); } n str to_string(bool b) { return b ?  true  :  false ; } n str to_string(const char* second) { return (str)second; } n str to_string(str second) { return second; } n template <class A> n str to_string(complex<A> c) { n   stringstream ss; n   ss << c; n   return ss.str(); n } n str to_string(vector<bool> v) { n   str res =  { ; n   for (int i = 0; i < (int)v.size(); i++) res += char( 0  + v[i]); n   res +=  } ; n   return res; n } n template <size_t SZ> n str to_string(bitset<SZ> b) { n   str res =   ; n   for (int i = 0; i < b.size(); i++) res += char( 0  + b[i]); n   return res; n } n template <class A, class B> n str to_string(pair<A, B> p); n template <class T> n str to_string(T v) { n   bool fst = 1; n   str res =  { ; n   for (const auto& x : v) { n     if (!fst) res +=  ,  ; n     fst = 0; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <class A, class B> n str to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n void DBG() { cerr <<  ]  << endl; } n template <class H, class... T> n void DBG(H h, T... t) { n   cerr << to_string(h); n   if (sizeof...(t)) cerr <<  ,  ; n   DBG(t...); n } n string c; n int n; n int dp[26][26]; n signed main() { n   cin.tie(nullptr); n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < 26; i++) { n     for (int j = 0; j < 26; j++) { n       dp[i][j] = -1e9; n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> c; n     int m = c.length(); n     int a = c[0] -  a ; n     int b = c[m - 1] -  a ; n     for (int j = 0; j < 26; j++) { n       dp[j][b] = max(dp[j][b], dp[j][a] + m); n     } n     dp[a][b] = max(dp[a][b], m); n   } n   for (int i = 0; i < 26; i++) { n     ans = max(ans, dp[i][i]); n   } n   cout << ans << nl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5100; n const long long M = 15; n const long long mod = 1e9 + 7; n const long long MOD = 998244353; n const long long P = 1336; n const long double eps = 0.000000001; n const long long inf = 1e9; n mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count()); n long long w[N], b[N], a[N], dp[N], u[N]; n vector<long long> nom[N]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   srand(time(0)); n   long long n, k, V; n   cin >> n >> k >> V; n   long long sum = 0; n   if (V == 0) { n     cout <<  YES  <<   n ; n     cout << inf <<   1 2 ; n     return 0; n   } n   for (long long i = 0; i < n; i++) { n     cin >> w[i]; n     sum += w[i]; n     b[i] = w[i] % k; n     a[i] = w[i] - b[i]; n     if (a[i] > V) a[i] = V / k * k; n   } n   if (sum < V) { n     cout <<  NO ; n     return 0; n   } n   dp[0] = 1; n   for (long long i = 0; i < n; i++) { n     for (long long j = k; j >= 0; j--) { n       if (dp[j] != 1) continue; n       long long per = (j + b[i]) % k; n       if (per == 0) per = k; n       if (dp[per]) continue; n       dp[per] = 2; n       for (auto to : nom[j]) { n         nom[per].push_back(to); n       } n       nom[per].push_back(i); n     } n     for (long long j = k; j >= 0; j--) { n       if (dp[j] == 2) dp[j] = 1; n     } n   } n   for (long long i = 0; i < n; i++) { n     long long need = (V - a[i]) % k; n     if (need == 0) need = k; n     if (!dp[need] && need != k) continue; n     long long no = -1; n     vector<pair<long long, pair<long long, long long> > > g; n     for (long long j = 0; j < n; j++) { n       u[j] = 0; n     } n     for (auto to : nom[need]) { n       u[to] = 1; n     } n     if (u[i] == 0 && need != k) continue; n     long long s = 0; n     for (auto to : nom[need]) { n       u[to] = 1; n       if (to == i) { n         s += w[i]; n         continue; n       } n       g.push_back({inf, {to, i}}); n       s += w[to]; n     } n     long long e = -1; n     for (long long j = 0; j < n; j++) { n       if (u[j]) continue; n       e = j; n     } n     if (s < V) { n       need = (V - s) / k; n       for (long long j = 0; j < n; j++) { n         if (u[j] || j == e) continue; n         long long c = w[j] / k; n         if (w[j] % k) c++; n         if (c) g.push_back({c, {j, e}}); n         w[e] += w[j]; n       } n       long long c = w[e] / k; n       if (c < need) continue; n       g.push_back({need, {e, i}}); n     } n     if (s > V) { n       need = (s - V) / k; n       if (i) n         g.push_back({need, {i, 0}}); n       else n         g.push_back({need, {i, 1}}); n     } n     cout <<  YES  <<   n ; n     for (auto to : g) { n       cout << to.first <<     << to.second.first + 1 <<     n            << to.second.second + 1 <<   n ; n     } n     return 0; n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 500000 + 24; n int a[MAX]; n int main() { n   int n, m, ans, tmp, u, v, fc; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   m = a[0]; n   ans = 0; n   for (int i = 0; i < n; i++) { n     while (i < n && a[i] == m) { n       i++; n     } n     tmp = 0; n     u = i; n     if (m == 0) { n       while (i + 1 < n && a[i] == 1 && a[i + 1] == 0) { n         tmp++; n         i += 2; n       } n     } else { n       while (i + 1 < n && a[i] == 0 && a[i + 1] == 1) { n         tmp++; n         i += 2; n       } n     } n     v = i; n     ans = max(ans, tmp); n     int j = u; n     for (; j < v - tmp; j++) { n       a[j] = m; n     } n     fc = (i < n) ? a[i] : a[n - 1]; n     for (; j < v; j++) { n       a[j] = fc; n     } n     m = a[i]; n     i--; n   } n   printf( %d n , ans); n   for (int i = 0; i < n - 1; i++) { n     printf( %d  , a[i]); n   } n   printf( %d n , a[n - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int n, a[maxn], cnt[maxn], mx = 0, last[maxn * 2], ans = 0; n void solve1(int x) { n   for (int i = 1; i <= n; i++) last[n - i] = last[n + i] = -1; n   last[n] = 0; n   int cur = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] == x) n       cur++; n     else if (a[i] == mx) n       cur--; n     if (last[n + cur] == -1) n       last[n + cur] = i; n     else n       ans = max(ans, i - last[n + cur]); n   } n } n void solve2(int x) { n   for (int i = 1; i <= n; i++) cnt[i] = 0; n   int l = 1, equal = 0; n   cnt[a[l]]++; n   if (cnt[a[l]] == x) equal++; n   for (int i = 2; i <= n; i++) { n     cnt[a[i]]++; n     if (cnt[a[i]] == x) n       equal++; n     else if (cnt[a[i]] > x) { n       while (cnt[a[i]] > x) { n         cnt[a[l]]--; n         if (cnt[a[l]] == x - 1) equal--; n         l++; n       } n     } n     if (equal >= 2) ans = max(ans, i - l + 1); n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     cnt[a[i]]++; n     mx = cnt[mx] < cnt[a[i]] ? a[i] : mx; n   } n   int limt = sqrt(n); n   for (int i = 1; i <= n; i++) n     if (cnt[i] >= limt && i != mx) solve1(i); n   for (int i = 1; i < limt; i++) solve2(i); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[6]; n   for (int i = 0; i < 6; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + 6); n   int tracker[10]; n   for (int i = 0; i < 10; i++) { n     tracker[i] = 0; n   } n   for (int i = 0; i < 6; i++) { n     tracker[arr[i]]++; n   } n   int legs = -1; n   for (int i = 0; i < 10; i++) { n     if (tracker[i] >= 4) { n       legs = i; n       break; n     } n   } n   if (legs != -1) { n     int head1 = -1, head2; n     int taken[6]; n     int takenCounter = 0; n     for (int i = 0; i < 6; i++) { n       taken[i] = 0; n     } n     for (int i = 0; i < 6; i++) { n       if (arr[i] == legs) { n         taken[i] = 1; n         takenCounter++; n       } n       if (takenCounter == 4) { n         break; n       } n     } n     for (int i = 0; i < 6; i++) { n       if (taken[i] == 0) { n         if (head1 == -1) { n           head1 = arr[i]; n         } else { n           head2 = arr[i]; n         } n       } n     } n     if (head1 != head2) { n       cout <<  Bear ; n     } else { n       cout <<  Elephant ; n     } n   } else { n     cout <<  Alien ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n, a, b, ans, nr1, nr0, minD; n int main() { n   cin >> n >> a >> b; n   cin >> s; n   a--; n   b--; n   minD = (int)1e8; n   nr1 = (int)1e8; n   nr0 = (int)1e8; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] == s[a]) { n       nr1 = i; n       if (abs(nr1 - nr0) < minD) minD = abs(nr1 - nr0); n     } n     if (s[i] == s[b]) { n       nr0 = i; n       if (abs(nr0 - nr1) < minD) minD = abs(nr0 - nr1); n     } n   } n   cout << minD; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void minn(T& a, T b) { n   if (a > b) a = b; n } n template <class T> n void maxx(T& a, T b) { n   if (a < b) a = b; n } n void debug() { return; } n template <class H, class... T> n void debug(H a, T... b) { n   cerr << a; n   debug(b...); n } n void is() { return; } n template <class H, class... T> n void is(H& a, T&... b) { n   cin >> a; n   is(b...); n } n void os() { return; } n template <class H, class... T> n void os(H a, T... b) { n   cout << a; n   os(b...); n } n const int mod = 1e9 + 7; n const int N = 2e5 + 5; n int n, q; n int nx[N], pr[N], nx100[N]; n void sw(int x, int y) { n   swap(nx[x], nx[y]); n   pr[nx[x]] = x; n   pr[nx[y]] = y; n   int k = x; n   for (int i = 1; i <= 100; i++) k = pr[k]; n   for (int i = 1; i <= 105; i++) { n     nx100[k] = x; n     x = nx[x]; n     k = nx[k]; n   } n   k = y; n   for (int i = 1; i <= 100; i++) k = pr[k]; n   for (int i = 1; i <= 105; i++) { n     nx100[k] = y; n     y = nx[y]; n     k = nx[k]; n   } n } n void solve() { n   is(n, q); n   for (int i = 1; i <= n; i++) { n     is(nx[i]); n     pr[nx[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     int k = i; n     for (int j = 1; j <= 100; j++) { n       k = nx[k]; n     } n     nx100[i] = k; n   } n   while (q--) { n     int t, x, y; n     is(t, x, y); n     if (t == 2) { n       int k = x; n       while (y >= 100) { n         y -= 100; n         k = nx100[k]; n       } n       while (y--) k = nx[k]; n       os(k,   n ); n     } else { n       sw(x, y); n     } n   } n } n signed main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int T = 1; n   while (T--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int mod = 1e9 + 7; n int n, m, a[N], t[N], r[N], second[N]; n pair<int, int> T[2][N * 4]; n pair<int, int> get_max(int l, int r, int v = 1, int tl = 1, int tr = n) { n   if (tl > r || tr < l) return make_pair(-mod, 0); n   if (l <= tl && tr <= r) return T[0][v]; n   int tm = (tl + tr) / 2; n   return max(get_max(l, r, v * 2, tl, tm), n              get_max(l, r, v * 2 + 1, tm + 1, tr)); n } n pair<int, int> get_min(int l, int r, int v = 1, int tl = 1, int tr = n) { n   if (tl > r || tr < l) return make_pair(mod, 0); n   if (l <= tl && tr <= r) return T[1][v]; n   int tm = (tl + tr) / 2; n   return min(get_min(l, r, v * 2, tl, tm), n              get_min(l, r, v * 2 + 1, tm + 1, tr)); n } n void upd(int p, int v = 1, int tl = 1, int tr = n) { n   if (tl == tr) { n     T[0][v] = T[1][v] = make_pair(a[p], p); n     return; n   } n   int tm = (tl + tr) / 2; n   if (p <= tm) n     upd(p, v * 2, tl, tm); n   else n     upd(p, v * 2 + 1, tm + 1, tr); n   T[0][v] = max(T[0][v * 2], T[0][v * 2 + 1]); n   T[1][v] = min(T[1][v * 2], T[1][v * 2 + 1]); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     upd(i); n   } n   for (int i = 1; i <= m; i++) cin >> t[i] >> r[i]; n   for (int i = m; i; i--) second[i] = max(second[i + 1], r[i]); n   for (int i = 1; i <= m; i++) { n     if (r[i] > second[i + 1]) { n       if (t[i] == 1) { n         for (int j = r[i]; j > second[i + 1]; j--) { n           pair<int, int> x = get_max(1, j); n           swap(a[x.second], a[j]); n           upd(x.second); n           upd(j); n         } n       } else { n         for (int j = r[i]; j > second[i + 1]; j--) { n           pair<int, int> x = get_min(1, j); n           swap(a[x.second], a[j]); n           upd(x.second); n           upd(j); n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e6 + 3; n const int INFI = 1e9 * 2; n const long long LINFI = 1e17; n const double pi = acos(-1.0); n const int N = 111; n const int M = 222222; n const int move[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1}; n int a[N][N]; n int main() { n   int n, m; n   while (scanf( %d , &n) != EOF) { n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) scanf( %d , &a[i][j]); n     if (n == 1) n       printf( %d n , 0); n     else { n       for (int i = 0; i < n; i++) { n         m = 0; n         for (int j = 0; j < n; j++) n           if (i != j) m |= a[i][j]; n         printf( %d  , m); n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const long long inff = 0x3f3f3f3f3f3f3f3f; n const int N = 5e2 + 8; n int n, kk, a[N], dp[2][N][N]; n int main() { n   cin.tie(0); n   cout.tie(0); n   cin >> n >> kk; n   int qw = 0; n   dp[qw ^ 1][0][0] = 1; n   for (int i(1); i <= (n); ++i) { n     scanf( %d , &a[i]); n     for (int j(0); j <= (kk); ++j) n       for (int k(0); k <= (kk); ++k) dp[qw][j][k] = dp[qw ^ 1][j][k]; n     for (int j(kk); j >= (a[i]); --j) n       for (int k(kk); k >= (0); --k) n         dp[qw][j][k] = max(dp[qw][j][k], dp[qw ^ 1][j - a[i]][k]); n     for (int j(kk); j >= (a[i]); --j) n       for (int k(kk); k >= (0); --k) n         dp[qw][k][j] = max(dp[qw][k][j], dp[qw ^ 1][k][j - a[i]]); n     qw ^= 1; n   } n   qw ^= 1; n   int num = 0; n   for (int i(0); i <= (kk); ++i) n     if (dp[qw][i][kk - i]) num++; n   cout << num << endl; n   for (int i(0); i <= (kk); ++i) n     if (dp[qw][i][kk - i]) printf( %d  , i); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s1, s2, s3; n int win(string s1, string s2) { n   if (s1 ==  rock ) { n     if (s2 ==  rock ) return 0; n     if (s2 ==  scissors ) return 1; n     if (s2 ==  paper ) return 2; n   } n   if (s1 ==  paper ) { n     if (s2 ==  rock ) return 1; n     if (s2 ==  scissors ) return 2; n     if (s2 ==  paper ) return 0; n   } n   if (s1 ==  scissors ) { n     if (s2 ==  rock ) return 2; n     if (s2 ==  scissors ) return 0; n     if (s2 ==  paper ) return 1; n   } n } n int main() { n   cin >> s1 >> s2 >> s3; n   if (win(s1, s2) == 1 && win(s1, s3) == 1) { n     cout <<  F ; n     return 0; n   } n   if (win(s2, s1) == 1 && win(s2, s3) == 1) { n     cout <<  M ; n     return 0; n   } n   if (win(s3, s1) == 1 && win(s3, s2) == 1) { n     cout <<  S ; n     return 0; n   } n   cout <<  ? ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[101]; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a, a + n); n   int i = 1; n   while (a[i] == a[i - 1] && i < n) i++; n   if (i == n) n     printf( NO n ); n   else n     printf( %d n , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace placeholders; n template <class T> n void mini(T &l, T r) { n   l = min(l, r); n } n template <class T> n void maxi(T &l, T r) { n   l = max(l, r); n } n template <class TH> n void _dbg(const char *sdbg, TH h) { n   cerr << sdbg <<  =  << h <<   n ; n } n template <class TH, class... TA> n void _dbg(const char *sdbg, TH h, TA... a) { n   while (*sdbg !=  , ) cerr << *sdbg++; n   cerr <<  =  << h <<  , ; n   _dbg(sdbg + 1, a...); n } n template <class T> n ostream &operator<<(ostream &os, vector<T> v) { n   os <<  [ ; n   for (auto vv : v) os << vv <<  , ; n   return os <<  ] ; n } n template <class L, class R> n ostream &operator<<(ostream &os, pair<L, R> p) { n   return os <<  (  << p.first <<  ,  << p.second <<  ) ; n } n using llint = long long; n const int B = 1e9 + 7; n int a[40][40]; n llint mem[40], cnt[40]; n void add(llint &a, llint b) { n   a = (a + b) % B; n   if (a < 0) a += B; n } n void init() { n   mem[0] = 1; n   cnt[0] = 1; n   for (int i = 1; i < 40; ++i) { n     int c = 1 << (i - 1); n     cnt[i] = cnt[i - 1] * 4 % B; n     add(mem[i], mem[i - 1] * 4 % B + 1ll * c * c % B * c % B * 2 % B); n   } n } n llint calc(llint a, llint b) { n   a %= B, b %= B; n   return ((a + b) * (b - a + 1) / 2) % B; n } n pair<llint, llint> get(int x, int y, int k, int d) { n   llint ret = 0; n   int sz = 1 << d; n   if (x < y) swap(x, y); n   if (x <= 0 || y <= 0 || k <= 0) return make_pair(0, 0); n   if (x >= sz) { n     llint a = min(k, sz); n     llint b = min(y, sz); n     ret = calc(1, a) * b % B; n     ; n     return make_pair(b * calc(1, a) % B, b * a % B); n   } n   if (d == 0) return make_pair(1 <= k, 1 <= k); n   int t = 1 << (d - 1); n   auto t1 = get(x, y, k, d - 1); n   auto t2 = get(x - t, y, k - t, d - 1); n   auto t3 = get(y, y - t, k - t, d - 1); n   auto t4 = get(x - t, y - t, k, d - 1); n   ; n   add(ret, t1.first); n   add(ret, t2.first + t2.second * t % B); n   add(ret, t3.first + t3.second * t % B); n   add(ret, t4.first); n   llint c = 0; n   add(c, t1.second); n   add(c, t2.second); n   add(c, t3.second); n   add(c, t4.second); n   ; n   return make_pair(ret, c); n } n llint go(int x1, int y1, int x2, int y2, int k) { n   llint ret = 0; n   add(ret, get(x2, y2, k, 30).first); n   add(ret, -get(x1 - 1, y2, k, 30).first); n   add(ret, -get(x2, y1 - 1, k, 30).first); n   add(ret, get(x1 - 1, y1 - 1, k, 30).first); n   return ret; n } n void run() { n   init(); n   ; n   int q; n   scanf( %d , &q); n   for (int i = 1; i <= q; ++i) { n     int x1, y1, x2, y2, k; n     scanf( %d%d%d%d%d , &x1, &y1, &x2, &y2, &k); n     printf( %lld n , go(x1, y1, x2, y2, k)); n   } n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long up = 2e5 + 5; n pair<int, int> pt[2005]; n inline bool cmp(const pair<int, int>& a, const pair<int, int>& b) { n   if (a.first == b.first) return a.second < b.second; n   return a.first < b.first; n } n long long fac[up], inv[up], facinv[up], facinv2[up]; n long long pow_mod(long long a, long long k) { n   long long res = 1; n   while (k) { n     if (k & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     k >>= 1; n   } n   return res; n } n void init() { n   fac[0] = fac[1] = 1; n   for (long long i = 2; i < up; ++i) fac[i] = fac[i - 1] * i % mod; n   inv[1] = 1, facinv[0] = facinv[1] = 1; n   facinv2[0] = facinv2[1] = 1; n   for (long long i = 2; i < up; ++i) { n     long long t = mod / i, k = mod % i; n     inv[i] = i * t * t % mod * inv[k] % mod * inv[k] % mod; n     facinv[i] = facinv[i - 1] * inv[i] % mod; n   } n } n long long C(long long n, long long m) { n   return fac[n] * facinv[n - m] % mod * facinv[m] % mod; n } n long long ans[2005]; n int main() { n   init(); n   int h, w, k; n   cin >> h >> w >> k; n   for (long long i = 0; i < k; ++i) cin >> pt[i].first >> pt[i].second; n   pt[k].first = h, pt[k].second = w; n   ++k; n   sort(pt, pt + k, cmp); n   for (long long i = 0; i < k; ++i) { n     ans[i] = C(pt[i].first + pt[i].second - 2, pt[i].first - 1); n     for (long long j = 0; j < i; ++j) { n       ans[i] -= ans[j] * n                 C(pt[i].first - pt[j].first + pt[i].second - pt[j].second, n                   pt[i].first - pt[j].first) % n                 mod; n       ans[i] = (ans[i] + mod) % mod; n     } n   } n   cout << ans[k - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int big = ((long long int)1 << 30); n const long long int mod = 1000000007; n int main(void) { n   int n, m, ans = 0, i, q, w, gen = 0; n   vector<int> kaz; n   vector<bool> uma; n   scanf( %d %d , &n, &m); n   m--; n   kaz.resize(n); n   uma.resize(n + 1); n   for (i = 0; i < n; i++) { n     scanf( %d , &q); n     if (i == m && q != 0) { n       ans++; n       q = 0; n     } n     if (i != m && q == 0) { n       q = n; n     } n     kaz[i] = q; n     uma[q] = 1; n   } n   sort(kaz.begin(), kaz.end()); n   while (-1) { n     for (; gen <= n; gen++) { n       if (uma[gen] == 0) { n         break; n       } n     } n     if (kaz.back() <= gen) { n       break; n     } n     ans++; n     kaz.pop_back(); n     uma[gen] = 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int INF_INT = 0x3f3f3f3f; n const long long INF_LL = 0x7f7f7f7f; n const int MOD = 1e9 + 7; n const double eps = 1e-10; n const double pi = acos(-1); n using namespace std; n const int N = 200200; n int n, m, q; n int u[N], v[N]; n struct Query { n   int l, r, s, t, id; n   Query(int l, int r, int s, int t, int id) : l(l), r(r), s(s), t(t), id(id) {} n   Query() {} n   bool operator<(const Query& rhs) const { return l > rhs.l; } n }; n vector<Query> qs; n int dp[1010][1010]; n int ans[N]; n int main(int argc, char const* argv[]) { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= m; i++) scanf( %d%d , &u[i], &v[i]); n   for (int i = 1; i <= q; i++) { n     int l, r, s, t; n     scanf( %d%d , &l, &r); n     scanf( %d%d , &s, &t); n     qs.emplace_back(l, r, s, t, i); n   } n   sort(qs.begin(), qs.end()); n   memset((dp), (INF_INT), sizeof(dp)); n   for (int i = 1; i <= n; i++) dp[i][i] = 0; n   int cnt = 0; n   for (int i = m; i >= 1; i--) { n     dp[u[i]][v[i]] = dp[v[i]][u[i]] = i; n     for (int j = 1; j <= n; j++) { n       if (j != u[i]) dp[v[i]][j] = min(dp[v[i]][j], dp[u[i]][j]); n       if (j != v[i]) dp[u[i]][j] = min(dp[u[i]][j], dp[v[i]][j]); n     } n     while (cnt < qs.size() && qs[cnt].l >= i) { n       ans[qs[cnt].id] = dp[qs[cnt].s][qs[cnt].t] <= qs[cnt].r; n       cnt++; n     } n   } n   for (int i = 1; i <= q; i++) puts(ans[i] ?  Yes  :  No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool deb = false; n int n; n vector<int> p, b; n void init() { n   if (!deb) return; n   p.resize(n); n   for (auto& x : p) cin >> x; n   b.resize(n); n   for (int i = 0; i < n; ++i) b[p[i]] = i; n } n int get(int i, int j) { n   if (deb) return p[i] ^ b[j]; n   cout <<  ?   << i <<     << j << endl; n   int x; n   cin >> x; n   if (x == -1) exit(0); n   return x; n } n void ans(int cnt, vector<int> p) { n   cout <<  ! n ; n   cout << cnt << endl; n   for (int x : p) cout << x <<    ; n   cout << endl; n } n int main() { n   cin >> n; n   init(); n   vector<int> a(n); n   for (int i = 0; i < n; ++i) a[i] = get(0, i); n   vector<int> x(n); n   for (int i = 0; i < n; ++i) x[i] = get(i, 0); n   int cnt = 0; n   vector<int> pans; n   for (int i = 0; i < n; ++i) { n     vector<int> b(n); n     for (int j = 0; j < n; ++j) b[j] = i ^ a[j]; n     vector<int> arr(n); n     for (int i : b) n       if (i < n) ++arr[i]; n     if (count(arr.begin(), arr.end(), 1) != n) continue; n     vector<int> p(n); n     for (int j = 0; j < n; ++j) p[b[j]] = j; n     bool f = true; n     for (int j = 0; j < n; ++j) n       if ((p[j] ^ b[0]) != x[j]) f = false; n     if (f) ++cnt, pans = p; n   } n   ans(cnt, pans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int n, k; n   while (cin >> n >> k) { n     int r = n, l = 1; n     cout << l; n     l++; n     for (int i = 1; i < k; i++) n       if (i % 2) { n         cout <<     << r; n         r--; n       } else { n         cout <<     << l; n         l++; n       } n     if (k % 2) n       for (int i = k; i < n; i++, l++) cout <<     << l; n     else n       for (int i = k; i < n; i++, r--) cout <<     << r; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long m[1000005], n[1000005], w[1000005]; n int main() { n   long long a, b, c; n   char ch[10]; n   while ((scanf( %lld%lld%lld , &a, &b, &c)) != EOF) { n     long long t; n     scanf( %lld , &t); n     long long p = 0, q = 0, r, sum1 = 0, sum2 = 0, f; n     for (int i = 0; i < t; i++) { n       scanf( %lld , &f); n       scanf( %s , ch); n       if (ch[0] ==  U ) n         m[p++] = f; n       else n         n[q++] = f; n     } n     sort(m, m + p); n     sort(n, n + q); n     long long i, j; n     long long l = 0; n     for (i = 0; i < a && i < p; i++) { n       sum2 += m[i]; n       sum1++; n     } n     while (m[i]) { n       w[l++] = m[i++]; n     } n     for (i = 0; i < b && i < q; i++) { n       sum2 += n[i]; n       sum1++; n     } n     while (n[i]) w[l++] = n[i++]; n     sort(w, w + l); n     for (int i = 0; i < c && i < l; i++) { n       sum2 += w[i]; n       sum1++; n     } n     printf( %lld %lld n , sum1, sum2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char s[25][25]; n int c[25][25]; n int chs[1 << 21]; n int dp[1 << 21]; n vector<int> hv; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) { n     scanf( %s , s[i]); n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       scanf( %d , c[i] + j); n     } n   } n   memset(chs, 0x3f, sizeof(chs)); n   for (int i = 0; i < n; ++i) { n     hv.push_back(1 << i); n     for (int j = 0; j < m; ++j) { n       chs[1 << i] = min(chs[1 << i], c[i][j]); n     } n   } n   for (int i = 0; i < m; ++i) { n     for (int j = 0; j < 26; ++j) { n       int num = 0, maxx = 0, sum = 0; n       for (int k = 0; k < n; ++k) { n         if (s[k][i] == j +  a ) { n           num |= 1 << k; n           maxx = max(maxx, c[k][i]); n           sum += c[k][i]; n         } n       } n       if (!num) { n         continue; n       } n       if (chs[num] == 0x3f3f3f3f) { n         hv.push_back(num); n       } n       chs[num] = min(chs[num], sum - maxx); n     } n   } n   memset(dp, 0x3f, sizeof(dp)); n   dp[0] = 0; n   for (int i = 0; i < (1 << n) - 1; ++i) { n     for (int j : hv) { n       dp[i | j] = min(dp[i | j], dp[i] + chs[j]); n     } n   } n   printf( %d n , dp[(1 << n) - 1]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const long long mod = 998244353; n const long long maxn = 3e5 + 10; n long long p[2] = {1000000007, 998244353}; n long long seed[2] = {500000004, 500000004}; n int main() { n   int x, a[6]; n   scanf( %d , &x); n   for (int i = 0; i < 6; i++) n     if (x & (1 << i)) n       a[i] = 1; n     else n       a[i] = 0; n   swap(a[0], a[4]); n   swap(a[2], a[3]); n   int y = 0; n   for (int i = 0; i < 6; i++) n     if (a[i]) y += 1 << i; n   printf( %d n , y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n; n long long int u; n string s; n int q; n int count(long long int number) { n   if (!number) return 0; n   int result = 0; n   while (number % 2 == 0) { n     result++; n     number >>= 1; n   } n   return result; n } n long long int solve(long long int start, string s) { n   int tmp = count(n + 1) - 1; n   for (int i = 0; i < s.size(); ++i) { n     int zeros = count(start); n     long long int total = ((long long int)1 << (zeros + 1)) - 1; n     if (s[i] ==  U ) { n       if (zeros < tmp) { n         long long int tmp_one = start + (total - 1) / 2 + 1; n         long long int tmp_two = start - (total - 1) / 2 - 1; n         if (count(tmp_one) == zeros + 1) { n           start = tmp_one; n         } else n           start = tmp_two; n       } n     } else { n       if (s[i] ==  L ) { n         if (zeros) { n           long long int left = (total - 1) / 2; n           start = start - left + ((left - 1) / 2); n         } n       } else { n         if (s[i] ==  R ) { n           if (zeros) { n             long long int right = (total - 1) / 2; n             start = start + (right - 1) / 2 + 1; n           } n         } n       } n     } n   } n   return start; n } n int main() { n   cin >> n >> q; n   for (int i = 0; i < q; ++i) { n     cin >> u >> s; n     cout << solve(u, s) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int boss = 2e5; n map<int, int> used; n queue<int> q; n int n, k, c[2], a[boss + 10]; n int main() { n   int i, answer = 0; n   cin >> n >> k; n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (used[a[i]] || c[a[i] % 2] == n / 2) n       q.push(i); n     else n       used[a[i]] = 1, c[a[i] % 2]++; n   } n   for (i = 1; i <= k && !q.empty(); i++) { n     if (used[i]) continue; n     if (c[i % 2] < n / 2) { n       c[i % 2]++; n       answer++; n       a[q.front()] = i; n       q.pop(); n     } n   } n   if (!q.empty()) n     return printf( -1 ), 0; n   else { n     cout << answer << endl; n     for (i = 1; i <= n; i++) printf( %d  , a[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 2e5 + 10; n int main() { n   int n, arr[mx], temp, z = 0; n   queue<int> visit; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     if (arr[i] == 0) visit.push(i); n   } n   temp = visit.front(); n   visit.pop(); n   for (int i = 0; i < n; i++) { n     if (arr[i] != 0) { n       if (visit.size() > 0) { n         if (abs(visit.front() - i) < abs(i - temp)) { n           temp = visit.front(); n           visit.pop(); n         } n       } n       printf( %d , abs(i - temp)); n     } else { n       if (i != n - 1) { n         if (arr[i + 1] == 0) { n           temp = visit.front(); n           visit.pop(); n         } n       } n       printf( 0 ); n     } n     if (i != n - 1) printf(   ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> inline int len(const T &a) { return a.size(); } n  n int main() { n  ios_base::sync_with_stdio(false); n  cin.tie(nullptr); n  int tc; n  cin >> tc; n  while (tc--) { n   int n, m; n   cin >> n >> m; n   vector<int> k(n); n   vector<int> c(m); n   for (auto &i : k) { n    cin >> i; n    --i; n   } n   for (auto &i : c) { n    cin >> i; n   } n    n   sort(k.rbegin(), k.rend()); n   int ptr = 0; n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n    if (ptr > k[i] || c[k[i]] == c[ptr]) { n     ans += c[k[i]]; n    } else { n     ans += c[ptr++]; n    } n   } n    n   cout << ans <<   n ; n  } n  return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n const long long N = 610001; n const long long inf = (long long)5e16; n struct node { n   long long val, id; n }; n node a[N], b[N]; n bool cmp(node x, node y) { n   if (x.val == y.val) return x.id < y.id; n   return x.val < y.val; n } n long long n, m, ans[N], pos[N]; n signed main() { n   scanf( %lld%lld , &m, &n); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i].val); n     a[i].id = i; n   } n   for (long long i = n + 1; i <= n + n; i++) { n     scanf( %lld , &b[i].val); n     b[i].id = i - n; n     b[i + n].val = b[i].val + m; n     b[i + n].id = i - n; n     b[i - n].val = b[i].val - m; n     b[i - n].id = i - n; n   } n   sort(a + 1, a + n + 1, cmp); n   sort(b + 1, b + 3 * n + 1, cmp); n   for (long long i = 1, j = 1; i <= n; i++) { n     while (a[i].val >= b[j].val && j <= 3 * n) j++; n     ans[0] += a[i].val; n     ans[j - i + 1] -= (a[i].val + a[i].val); n   } n   for (long long i = 1, j = 1; j <= 3 * n; j++) { n     ans[max(j - n + 1, 0LL)] -= b[j].val; n     while (b[j].val > a[i].val && i <= n) i++; n     ans[j - i + 2] += b[j].val + b[j].val; n     ans[j + 1] -= b[j].val; n   } n   long long sum = ans[0], id = 0, mi = inf; n   for (long long i = 1; i <= 2 * n + 1; i++) { n     sum += ans[i]; n     ans[i] += ans[i - 1]; n     if (mi > sum) { n       mi = sum; n       id = i; n     } n   } n   cout << mi << endl; n   for (long long i = 1; i <= n; i++) pos[a[i].id] = b[i + id - 1].id; n   for (long long i = 1; i <= n; i++) printf( %d  , pos[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k, n, a; n int d[1001]; n bool vis[1001], flag; n int main() { n   scanf( %d%d , &n, &k); n   int cut = 0; n   for (int i = 2; i <= k; i++) { n     if (k % i) continue; n     int crt = 1; n     while (k % i == 0) { n       crt *= i; n       k /= i; n     } n     d[cut++] = crt; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &a); n     for (int j = 0; j < cut; j++) { n       if (a % d[j] == 0) { n         vis[j] = true; n       } n     } n   } n   flag = true; n   for (int j = 0; j < cut; j++) { n     flag &= vis[j]; n   } n   if (flag) n     printf( Yes n ); n   else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[] = {0, 1, -1, 0, 0}; n const int dy[] = {0, 0, 0, 1, -1}; n struct MinCostMaxFlow { n   int head[20010], total; n   int next[1000010], aim[1000010], flow[1000010], cost[1000010]; n   int f[20010], from[20010], p[20010]; n   bool v[20010]; n   MinCostMaxFlow() : total(1) {} n   void Add(int x, int y, int f, int c) { n     next[++total] = head[x]; n     aim[total] = y; n     flow[total] = f; n     cost[total] = c; n     head[x] = total; n   } n   void Insert(int x, int y, int f, int c) { n     Add(x, y, f, c); n     Add(y, x, 0, -c); n   } n   bool SPFA() { n     static queue<int> q; n     while (!q.empty()) q.pop(); n     memset(f, 0x3f, sizeof(f)); n     memset(v, false, sizeof(v)); n     f[0] = 0; n     q.push(0); n     while (!q.empty()) { n       int x = q.front(); n       q.pop(); n       v[x] = false; n       for (int i = head[x]; i; i = next[i]) n         if (flow[i] && f[aim[i]] > f[x] + cost[i]) { n           f[aim[i]] = f[x] + cost[i]; n           if (!v[aim[i]]) v[aim[i]] = true, q.push(aim[i]); n           from[aim[i]] = x; n           p[aim[i]] = i; n         } n     } n     return f[(20010 - 1)] != 0x3f3f3f3f; n   } n   int EdmondsKarp() { n     int re = 0; n     while (SPFA()) { n       int max_flow = 0x3f3f3f3f; n       for (int i = (20010 - 1); i != 0; i = from[i]) n         max_flow = ((max_flow) < (flow[p[i]]) ? (max_flow) : (flow[p[i]])); n       for (int i = (20010 - 1); i != 0; i = from[i]) { n         flow[p[i]] -= max_flow; n         flow[p[i] ^ 1] += max_flow; n       } n       re += f[(20010 - 1)] * max_flow; n     } n     return re; n   } n } solver; n int m, n; n int src[110][110]; n int main() { n   cin >> m >> n; n   for (int i = 1; i <= m; ++i) n     for (int j = 1; j <= n; ++j) scanf( %d , &src[i][j]); n   for (int i = 1; i <= m; ++i) n     for (int j = 1; j <= n; ++j) { n       if (i + j & 1) { n         solver.Insert(0, ((i - 1) * n + j), 1, 0); n         for (int k = 1; k <= 4; ++k) { n           int fx = i + dx[k], fy = j + dy[k]; n           if (!fx || !fy || fx > m || fy > n) continue; n           solver.Insert(((i - 1) * n + j), ((fx - 1) * n + fy), 1, n                         src[i][j] != src[fx][fy]); n         } n       } else n         solver.Insert(((i - 1) * n + j), (20010 - 1), 1, 0); n     } n   cout << solver.EdmondsKarp() << endl; n   return 0; n } n