#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n long long power(long long x, long long y) { n   if (y == 0) return 1; n   long long a = power(x, y / 2); n   if (y % 2) { n     return (a * ((a * x) % mod)) % mod; n   } else { n     return (a * a) % mod; n   } n } n int main() { n   long long i, j, k, m, n; n   cin >> n >> k; n   long long ans = 0; n   long long arr[n]; n   vector<vector<long long> > ind(11); n   for (i = 0; i < n; i++) cin >> arr[i]; n   for (i = 0; i < n; i++) { n     j = arr[i] % 10; n     ind[j].push_back(i); n   } n   for (j = 9; j >= 1 && k > 0; j--) { n     for (i = 0; i < ind[j].size(); i++) { n       m = ind[j][i]; n       if (k >= (10 - j)) { n         k -= 10 - j; n         arr[m] += 10 - j; n       } n     } n   } n   sort(arr, arr + n); n   for (i = 0; i < n; i++) { n     if (k >= 100 - arr[i]) { n       k -= 100 - arr[i]; n       arr[i] = 100; n     } else { n       long long flag = 1; n       while (flag) { n         if (k >= 10 && 1) { n           k -= 10; n           arr[i] += 10; n         } else n           flag = 0; n       } n     } n   } n   for (i = 0; i < n; i++) { n     ans += arr[i] / 10; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string b[51]; n bool check(pair<int, int> f, pair<int, int> s) { n   bool r1 = true, r2 = true, c1 = true, c2 = true; n   for (int i = f.first; i <= s.first; i++) { n     if (b[i][f.second] ==  W ) c1 = false; n     if (b[i][s.second] ==  W ) c2 = false; n   } n   for (int i = s.first; i <= f.first; i++) { n     if (b[i][f.second] ==  W ) c1 = false; n     if (b[i][s.second] ==  W ) c2 = false; n   } n   for (int i = f.second; i <= s.second; i++) { n     if (b[f.first][i] ==  W ) r1 = false; n     if (b[s.first][i] ==  W ) r2 = false; n   } n   for (int i = s.second; i <= f.second; i++) { n     if (b[f.first][i] ==  W ) r1 = false; n     if (b[s.first][i] ==  W ) r2 = false; n   } n   return (c1 && r2) || (c2 && r1); n } n vector<pair<int, int> > v; n int main() { n   int r, c; n   cin.tie(0); n   cin >> r >> c; n   for (int i = 0; i < r; i++) { n     cin >> b[i]; n     for (int j = 0; j < c; j++) n       if (b[i][j] ==  B ) v.push_back({i, j}); n   } n   int sz = v.size(); n   for (int i = 0; i < sz; i++) n     for (int j = i + 1; j < sz; j++) { n       if (!check(v[i], v[j])) { n         cout <<  NO ; n         return 0; n       }; n     } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n enum var_type { n   TYPE_DOUBLE, n   TYPE_STRING, n   TYPE_INT, n   TYPE_T, n }; n using namespace std; n struct proc { n   string name; n   vector<var_type> types; n }; n vector<proc> procedures; n map<string, var_type> vars; n vector<int> results; n var_type get_type(string type_name) { n   if (type_name ==  double ) return TYPE_DOUBLE; n   if (type_name ==  int ) return TYPE_INT; n   if (type_name ==  string ) return TYPE_STRING; n   if (type_name ==  T ) return TYPE_T; n } n void proc_procedures() { n   int n; n   cin >> n; n   cin.ignore(1,   n ); n   for (int i = 0; i < n; ++i) { n     char buf[200]; n     gets(buf); n     string str(buf); n     int j = 0; n     while (str[j] ==    ) ++j; n     j += 4; n     while (str[j] ==    ) ++j; n     string name; n     while (str[j] !=     && str[j] !=  ( ) { n       name += str[j]; n       ++j; n     } n     while (str[j] !=  ( ) ++j; n     ++j; n     vector<var_type> var_types; n     while (str[j] !=  ) ) { n       if (str[j] ==    ) { n         ++j; n         continue; n       } n       string cur_type; n       while (str[j] !=     && str[j] !=  )  && str[j] !=  , ) { n         cur_type += str[j]; n         ++j; n       } n       var_types.push_back(get_type(cur_type)); n       while (str[j] !=  )  && str[j] !=  , ) ++j; n       if (str[j] ==  , ) ++j; n     } n     proc temp; n     temp.name = name; n     temp.types = var_types; n     procedures.push_back(temp); n   } n } n void proc_variables() { n   int n; n   cin >> n; n   cin.ignore(1,   n ); n   for (int i = 0; i < n; ++i) { n     char buf[200]; n     gets(buf); n     string str(buf); n     int j = 0; n     while (str[j] ==    ) ++j; n     string cur_type; n     while (str[j] !=    ) { n       cur_type += str[j]; n       ++j; n     } n     while (str[j] ==    ) ++j; n     string name; n     while (j < str.size() && str[j] !=    ) { n       name += str[j]; n       ++j; n     } n     vars.insert(pair<string, var_type>(name, get_type(cur_type))); n   } n } n void proc_calls() { n   int n; n   cin >> n; n   cin.ignore(1,   n ); n   for (int i = 0; i < n; ++i) { n     char buf[200]; n     gets(buf); n     string str(buf); n     int j = 0; n     while (str[j] ==    ) ++j; n     string name; n     while (str[j] !=     && str[j] !=  ( ) { n       name += str[j]; n       ++j; n     } n     while (str[j] !=  ( ) ++j; n     ++j; n     vector<var_type> var_types; n     while (str[j] !=  ) ) { n       if (str[j] ==    ) { n         ++j; n         continue; n       } n       string cur_type; n       while (str[j] !=     && str[j] !=  )  && str[j] !=  , ) { n         cur_type += str[j]; n         ++j; n       } n       var_types.push_back(vars[cur_type]); n       while (str[j] !=  )  && str[j] !=  , ) ++j; n       if (str[j] ==  , ) ++j; n     } n     int res = 0; n     for (int j = 0; j < procedures.size(); ++j) { n       if (procedures[j].name != name) continue; n       if (var_types.size() != procedures[j].types.size()) continue; n       bool is_ok = true; n       for (int k = 0; k < var_types.size(); ++k) { n         if (procedures[j].types[k] == TYPE_T) continue; n         if (procedures[j].types[k] != var_types[k]) is_ok = false; n       } n       if (is_ok) ++res; n     } n     results.push_back(res); n   } n } n int main() { n   proc_procedures(); n   proc_variables(); n   proc_calls(); n   for (int i = 0; i < results.size(); ++i) cout << results[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n const int inf = 0x3f3f3f3f; n const long long LL_INF = 0x3f3f3f3f3f3f3f3f; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long power(long long a, long long b, long long m = 1000000007) { n   long long answer = 1; n   while (b) { n     if (b & 1) answer = (answer * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return answer; n } n using namespace std; n bool chk[1048576]; n int main() { n   set<int> st; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     set<int>::iterator it; n     set<int> st2; n     for (it = st.begin(); it != st.end(); it++) { n       int y = *it; n       st2.insert(y | x); n     } n     st2.insert(x); n     st = st2; n     st2.clear(); n     for (it = st.begin(); it != st.end(); it++) { n       chk[*it] = 1; n     } n   } n   int cnt = 0; n   for (int i = 0; i < 1048576; i++) { n     if (chk[i]) cnt++; n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 20; n long long fac[20]; n vector<long long> lst; n vector<long long> lucky; n void prepare(int cnt, int all, long long now) { n   if (cnt == all) { n     lucky.push_back(now); n     return; n   } n   prepare(cnt + 1, all, now * 10 + 4); n   prepare(cnt + 1, all, now * 10 + 7); n } n inline bool chk(long long x) { n   while (x) { n     if (x % 10 != 4 && x % 10 != 7) return false; n     x /= 10; n   } n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   for (int i = 0; i <= 10; i++) prepare(0, i, 0); n   fac[0] = 1; n   for (int i = 1; i < 20; i++) fac[i] = fac[i - 1] * i; n   long long n, k; n   cin >> n >> k; n   long long p = 1; n   while (fac[p] < k) p++; n   if (p > n) return cout << -1 <<   n , 0; n   k--; n   for (long long i = n - p + 1; i <= n; i++) lst.push_back(i); n   while (k) { n     int i; n     for (i = p; ~i; i--) n       if (fac[i] <= k) break; n     int w = k / fac[i]; n     k -= fac[i] * w; n     for (int s = 0; s < w; s++) swap(lst[p - 1 - i], lst[p - i + s]); n   } n   long long res = n       upper_bound(lucky.begin(), lucky.end(), n - p) - lucky.begin() - 1; n   for (int i = 0; i < lst.size(); i++) n     if (chk(n - p + i + 1) && chk(lst[i])) res++; n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int block(long int x) { n   vector<long int> v; n   while (x > 0) { n     v.push_back(x % 2); n     x = x / 2; n   } n   int ret = 0; n   for (int i = 0; i < v.size(); i++) { n     if (v[i] == 1) { n       ret++; n     } n   } n   return ret; n } n void pprint(long int x) { n   vector<long int> v; n   while (x > 0) { n     v.push_back(x % 2); n     x = x / 2; n   } n   cout <<  YES n ; n   int ret = 0; n   for (int i = 0; i < v.size(); i++) { n     if (v[i] == 1) { n       ret++; n       int A = 1 << i; n       cout << A <<    ; n     } n   } n   cout << endl; n } n void solve(long int x, long long k, long long mini) { n   vector<long int> v; n   long long rem = k - mini; n   while (x > 0) { n     v.push_back(x % 2); n     x = x / 2; n   } n   int ret = 0; n   cout <<  YES n ; n   int i = v.size() - 1; n   while (rem > 0) { n     v[i]--; n     v[i - 1] += 2; n     if (v[i] == 0) { n       i--; n     } n     rem--; n   } n   for (int i = 0; i < v.size(); i++) { n     while (v[i] > 0) { n       ret++; n       int A = 1 << i; n       cout << A <<    ; n       v[i]--; n     } n   } n   cout << endl; n } n int main() { n   long long x; n   long long n, k; n   cin >> n >> k; n   if (k > n) { n     cout <<  NO n ; n     return 0; n   } n   if (k == n) { n     cout <<  YES n ; n     for (int i = 0; i < n; i++) { n       cout << 1 <<    ; n     } n     return 0; n   } n   int mini = block(n); n   if (k < mini) { n     cout <<  NO n ; n     return 0; n   } n   if (k == mini) { n     pprint(n); n   } else { n     solve(n, k, mini); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5001; n long long a[N], mem[N][2], b[N]; n int n, m; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   sort(b, b + n); n   m = unique(b, b + n) - b; n   for (int i = n - 1; i >= 0; i--) n     mem[i][1 - m & 1] = n         abs(a[i] - b[m - 1]) + ((i + 1 < n) ? mem[i + 1][1 - m & 1] : 0ll); n   for (int j = m - 2; j >= 0; j--) n     for (int i = n - 1; i >= 0; i--) n       mem[i][j & 1] = n           min(mem[i][1 - j & 1], n               abs(a[i] - b[j]) + ((i + 1 < n) ? mem[i + 1][j & 1] : 0ll)); n   long long ans = 1ll << 50; n   ans = min(mem[0][0], mem[0][1]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class V> n T power(T t, V v) { n   T second = 1; n   while (v--) second *= t; n   return second; n } n void _print(long long t) { cerr << t; } n void _print(int t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(long double t) { cerr << t; } n void _print(double t) { cerr << t; } n void _print(unsigned long long t) { cerr << t; } n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) { n   cerr <<  { ; n   _print(p.first); n   cerr <<    ; n   _print(p.second); n   cerr <<  } ; n } n template <class T> n void _print(vector<T> v) { n   int n = v.size(); n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     n--; n     cerr << (n ?  ,   :    ); n   } n   cerr <<  ] ; n } n template <class T> n void _print(set<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(multiset<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> v) { n   int n = v.size(); n   cerr <<  {  ; n   for (auto p : v) { n     cerr <<  [ ; n     _print(p.first); n     cerr <<  ]= ; n     _print(p.second); n     n--; n     cerr << (n ?  ,   :    ); n   } n   cerr <<  } ; n } n void go() { n   int n; n   cin >> n; n   string a; n   string b; n   cin >> a >> b; n   int sz = 0, so = 0, nsz = 0, nso = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] == b[i]) { n       if (a[i] ==  0 ) n         sz++; n       else n         so++; n     } else if (a[i] ==  0 ) n       nsz++; n     else n       nso++; n   } n   int N = 100000000; n   int ans = N; n   if (so - sz == 1) ans = min(ans, so + sz); n   if (nsz == nso) ans = min(ans, nsz + nso); n   if (ans == N) n     cout <<  -1 n ; n   else n     cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   cin >> t; n   while (t--) { n     go(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n int inf = 0x3f3f3f3f; n long long infl = 0x3f3f3f3f3f3f3f3fLL; n long double infd = 1.0 / 0.0; n const long long MOD = 1e9 + 7; n const double pi = 2 * acos(0.0); n long long modi(long long a, long long n) { n   long long x = 1; n   while (n) { n     if (n & 1) x = (x * a) % MOD; n     n >>= 1; n     a = (a * a) % MOD; n   } n   return x; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int tt = 1; n   while (tt--) { n     long long a, b, n, x; n     cin >> a >> b >> n >> x; n     long long as = (((a * x) % MOD) + b) % MOD; n     long long le = (x * modi(a, n)) % MOD; n     long long ri = a == 1ll ? ((b * (n % MOD)) % MOD) n                             : ((b * (((((modi(a, n) - 1 + MOD) % MOD)) * n                                       (modi(a - 1, MOD - 2))) % n                                      MOD)) % n                                MOD); n     cout << ((((le + ri) % (MOD)) + (MOD)) % (MOD)) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int s1, s2, s3, a, b, c; n   cin >> s1 >> s2 >> s3; n   a = (s1 * s2) / s3; n   a = pow(a, 0.5); n   b = (s2 * s3) / s1; n   b = pow(b, 0.5); n   c = (s1 * s3) / s2; n   c = pow(c, 0.5); n   cout << 4 * (a + b + c) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, ans = 0; n   cin >> n; n   for (;; ans++) { n     if (n == 1) break; n     if (n % 6 == 0) n       n /= 6; n     else if (n % 3 == 0) { n       if ((n * 2) % 3 != 0) { n         cout <<  -1 n ; n         return; n       } n       n *= 2; n     } else { n       cout <<  -1 n ; n       return; n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n void debug() { cerr <<  ] n ; } n template <class H, class... T> n void debug(H h, T... t) { n   cerr << h; n   if (sizeof...(t)) { n     cerr <<  ,  ; n   } n   debug(t...); n } n void solve() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int o, e; n   o = e = 0; n   int f1, f2; n   f1 = f2 = 0; n   for (int i = 0; i < n; i++) { n     if (i & 1) { n       if ((int)(s[i] -  0 ) & 1) { n       } else n         f2 = 1; n     } else { n       if ((int)(s[i] -  0 ) & 1) { n         f1 = 1; n       } n     } n   } n   if (n == 1) { n     cout << (((s[0] -  0 ) & 1) ?  1  :  2 ) <<   n ; n     return; n   } n   if (n & 1) { n     cout << (f1 ?  1  :  2 ) <<   n ; n   } else { n     cout << (f2 ?  2  :  1 ) <<   n ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int _; n   cin >> _; n   for (int i = 1; i <= _; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double g[110][110], x[110]; n int n, m, ues[110]; n pair<pair<int, int>, int> p[5010]; n bool vis[110]; n void read(int &x) { n   char ch = getchar(); n   int mark = 1; n   for (; ch !=  -  && (ch <  0  || ch >  9 ); ch = getchar()) n     ; n   if (ch ==  - ) mark = -1, ch = getchar(); n   for (x = 0; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch - 48; n   x *= mark; n } n double Abs(double x) { n   if (x < 0) return -x; n   return x; n } n int main() { n   read(n); n   read(m); n   g[1][1] = 1; n   g[n][n] = 1; n   g[n][n + 1] = 1; n   for (int i = 1; i <= m; i++) { n     read(p[i].first.first); n     read(p[i].first.second); n     read(p[i].second); n     g[p[i].first.first][p[i].first.first]++; n     g[p[i].first.first][p[i].first.second]--; n     g[p[i].first.second][p[i].first.second]++; n     g[p[i].first.second][p[i].first.first]--; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) n       if (Abs(g[i][j]) && !vis[j]) { n         vis[j] = 1; n         ues[i] = j; n         break; n       } n     for (int j = 1; j <= n; j++) n       if (ues[i] != j && Abs(g[j][i])) { n         double tmp = -g[j][i] / g[ues[i]][i]; n         for (int k = 1; k <= n + 1; k++) g[j][k] += g[ues[i]][k] * tmp; n       } n   } n   for (int i = 1; i <= n; i++) x[i] = g[ues[i]][n + 1] / g[ues[i]][i]; n   double Min = 1e20; n   for (int i = 1; i <= m; i++) { n     if (Abs(x[p[i].first.first] - x[p[i].first.second]) > 1e-6) n       Min = min(Min, n                 p[i].second / Abs(x[p[i].first.first] - x[p[i].first.second])); n   } n   double ans = 0; n   for (int i = 1; i <= m; i++) n     if (p[i].first.first == 1) n       ans += Min * (x[p[i].first.second] - x[p[i].first.first]); n     else if (p[i].first.second == 1) n       ans -= Min * (x[p[i].first.second] - x[p[i].first.first]); n   printf( %.5lf n , ans); n   if (Abs(ans) < 1e-6) memset(x, 0, sizeof(x)); n   for (int i = 1; i <= m; i++) n     printf( %.5lf n , Min * (x[p[i].first.second] - x[p[i].first.first])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int k, a; n   bool operator<(const node &X) const { return a > X.a; } n }; n int n, fa[5010], limi[5010], ans[5010]; n int order[5010], co; n vector<int> edge[5010]; n vector<int> val[5010]; n priority_queue<node> train[5010]; n void dfs(int u, int father, int weight) { n   order[++co] = u; n   fa[u] = father; n   limi[u] = weight; n   for (int i = 0; i < edge[u].size(); i++) { n     int v = edge[u][i]; n     int w = val[u][i]; n     if (v == father) continue; n     dfs(v, u, w); n   } n } n int main() { n   cin >> n; n   node tt; n   for (int i = 1; i <= n; i++) { n     cin >> tt.a; n     tt.k = i; n     train[i].push(tt); n   } n   int x, y, z; n   for (int i = 1; i <= n - 1; i++) { n     cin >> x >> y >> z; n     edge[x].push_back(y); n     val[x].push_back(z); n     edge[y].push_back(x); n     val[y].push_back(z); n   } n   co = 0; n   dfs(1, 0, 0); n   int cnt = 0; n   for (int i = 0; i <= n - 1; i++) { n     while (!train[1].empty()) { n       tt = train[1].top(); n       train[1].pop(); n       ans[tt.k] = i; n       cnt += 1; n     } n     if (cnt == n) break; n     for (int i = 2; i <= n; i++) { n       int u = order[i]; n       int v = fa[u]; n       int w = limi[u]; n       for (int i = 1; i <= w; i++) { n         if (train[u].empty()) break; n         tt = train[u].top(); n         train[u].pop(); n         train[v].push(tt); n       } n     } n   } n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int f = 1; n   int res = 0; n   char ch; n   do { n     ch = getchar(); n     if (ch ==  - ) f = -f; n   } while (ch <  0  || ch >  9 ); n   do { n     res = res * 10 + ch -  0 ; n     ch = getchar(); n   } while (ch >=  0  && ch <=  9 ); n   return f == 1 ? res : -res; n } n const int MAXN = 100010; n const int MAXP = MAXN * 51; n const long long INF = 2000000000000LL; n int n, m, D; n int mod = 1000000007; n int X[MAXN], Y[MAXN]; n char open[MAXN][52]; n int stck[MAXP], in[MAXP], dfsn[MAXP], low[MAXP], idx, top; n int li[MAXN], bak; n bool hv[MAXN]; n int head[MAXP], nxt[MAXP], to[MAXP], tot; n int nid[MAXP], dag[MAXP], sz[MAXP]; n std::queue<int> q; n int dp[MAXP]; n void addEdge(int x, int y) { n   tot++; n   nxt[tot] = head[x]; n   head[x] = tot; n   to[tot] = y; n } n int getIndex(int x, int d) { return x + (d % D) * n; } n void append(int x) { n   --x; n   int c = x % n + 1; n   int d = x / n; n   if (c <= 0 || d >= D) cout <<  x:   << x << endl; n   if (!hv[c] && (c > 0 && d < D && open[c][d] ==  1 )) { n     li[++bak] = c; n     hv[c] = true; n   } n } n void clr() { n   for (int i = 1; i <= bak; i++) hv[li[i]] = false; n   bak = 0; n } n void tarjan(int u) { n   dfsn[u] = low[u] = ++idx; n   in[stck[++top] = u] = true; n   for (int i = head[u]; i; i = nxt[i]) n     if (!dfsn[to[i]]) { n       tarjan(to[i]); n       low[u] = std::min(low[u], low[to[i]]); n     } else if (in[to[i]]) n       low[u] = std::min(low[u], dfsn[to[i]]); n   if (low[u] == dfsn[u]) { n     int v; n     do in[v = stck[top--]] = false, append(v), nid[v] = u; n     while (u != v); n     sz[u] = bak; n     clr(); n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &D); n   for (int i = 0; i < m; i++) { n     int u = read(); n     int v = read(); n     X[i] = u; n     Y[i] = v; n     for (int j = 0; j < D; j++) n       addEdge(getIndex(X[i], j), getIndex(Y[i], j + 1)); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %s , open[i]); n   } n   tarjan(getIndex(1, 0)); n   memset(head, 0, sizeof(head)); n   tot = 0; n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < D; j++) { n       int u = getIndex(X[i], j); n       int v = getIndex(Y[i], j + 1); n       if (!nid[u] || !nid[v]) continue; n       if (nid[u] == nid[v]) continue; n       addEdge(nid[u], nid[v]); n       dag[nid[v]]++; n     } n   } n   q.push(getIndex(1, 0)); n   int ans = 0; n   while (!q.empty()) { n     int t = q.front(); n     q.pop(); n     ans = std::max(ans, dp[t] += sz[t]); n     for (int i = head[t]; i; i = nxt[i]) { n       dp[to[i]] = std::max(dp[to[i]], dp[t]); n       if (!--dag[to[i]]) q.push(to[i]); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[1000005]; n int main() { n   int n; n   cin >> n; n   int a[n], b[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   int l = 0; n   for (int i = 0; i < n; i++) { n     for (int j = i; j < n; j++) { n       if (b[j] == a[i]) { n         for (int k = j; k > i; k--) { n           ans[l++] = k; n           swap(b[k - 1], b[k]); n         } n         break; n       } n     } n   } n   cout << l << endl; n   for (int i = 0; i < l; i++) { n     cout << ans[i] <<     << ans[i] + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   string s1 =  hello ; n   int n1 = s1.size(); n   int i = 0, j = 0; n   int count = 0; n   while (j < n1 && i < n) { n     if (s1[j] == s[i]) { n       j++; n       count++; n     } n     i++; n   } n   if (count == n1) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j = 0, k, sum = 0, ctr = 0, c = 0, ok = 0; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long a, b, c; n   cin >> a >> b >> c; n   for (i = a; i >= 0; i--) { n     if (c >= i * 4 && b >= i * 2) { n       ok = i; n       break; n     } n   } n   cout << ok * 7; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int cen, feet = 0, inch; n   cin >> cen; n   inch = cen / 3; n   if (cen % 3 == 2) inch++; n   if (inch >= 12) { n     feet = inch / 12; n     inch = inch % 12; n   } n   cout << feet <<     << inch << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int nmin, nmax = 1; n   int n, m, a, b; n   scanf( %d%d , &n, &m); n   nmin = n; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &a, &b); n     if (b < a) swap(a, b); n     nmin = min(nmin, b); n     nmax = max(nmax, a); n   } n   if (nmax < nmin) n     printf( %d n , nmin - nmax); n   else n     printf( 0 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int infinity = 10000000; n int railway[401][401]; n int calculatedistance(int start, int end, bool bus) { n   bool visitedtown[401]; n   int distance[401]; n   queue<int> myQueue; n   for (int i = 1; i <= 400; i++) visitedtown[i] = false; n   for (int i = 1; i <= 400; i++) distance[i] = infinity; n   distance[start] = 0; n   visitedtown[start] = true; n   myQueue.push(start); n   while (!myQueue.empty()) { n     int town = myQueue.front(); n     myQueue.pop(); n     for (int i = 1; i <= end; i++) n       if (i != town) { n         if (bus) { n           if (!railway[town][i]) n             if (!visitedtown[i]) n               if (distance[town] + 1 < distance[i]) { n                 distance[i] = distance[town] + 1; n                 visitedtown[i] = true; n                 myQueue.push(i); n               } n         } else { n           if (railway[town][i]) n             if (!visitedtown[i]) n               if (distance[town] + 1 < distance[i]) { n                 distance[i] = distance[town] + 1; n                 visitedtown[i] = true; n                 myQueue.push(i); n               } n         } n       } n   } n   return distance[end]; n } n int main() { n   int n, m, u, v; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) railway[i][j] = 0; n   for (int i = 0; i < m; i++) { n     cin >> u >> v; n     railway[u][v] = 1; n     railway[v][u] = 1; n   } n   int bdis = calculatedistance(1, n, true); n   int tdis = calculatedistance(1, n, false); n   if (bdis == infinity || tdis == infinity) { n     cout <<  -1 ; n     return 0; n   } n   if (bdis != tdis) { n     cout << max(bdis, tdis); n     return 0; n   } n   int disb = infinity; n   for (int i = 2; i < n; i++) n     disb = min(disb, n                calculatedistance(1, i, false) + calculatedistance(i, n, false)); n   int dist; n   for (int i = 2; i < n; i++) n     dist = min(dist, n                calculatedistance(1, i, true) + calculatedistance(i, n, true)); n   cout << min(disb, dist); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int limit[100005] = {0}; n int main() { n   ios::sync_with_stdio(false); n   memset(limit, 0, sizeof(limit)); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x, k; n     cin >> x >> k; n     if (x == limit[k]) n       limit[k]++; n     else if (x > limit[k]) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, w, h; n   string temp; n   vector<string> img; n   cin >> w >> h; n   for (i = 0; i < h; i++) { n     cin >> temp; n     img.push_back(temp); n   } n   for (j = 0; j < w; j++) { n     for (i = 0; i < h; i++) cout << img[i][j] << img[i][j]; n     cout << endl; n     for (i = 0; i < h; i++) cout << img[i][j] << img[i][j]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int a[1005][1005]; n int main() { n   int h, w; n   cin >> h >> w; n   vector<int> r(h), c(w); n   for (int i = 0; i < h; i++) { n     cin >> r[i]; n   } n   for (int i = 0; i < w; i++) { n     cin >> c[i]; n   } n   memset(a, -1, sizeof(a)); n   for (int i = 0; i < h; i++) { n     for (int j = 0; j < r[i]; j++) { n       a[i][j] = 1; n     } n     a[i][r[i]] = 0; n   } n   for (int i = 0; i < w; i++) { n     for (int j = 0; j < c[i]; j++) { n       if (a[j][i] == 0) { n         cout << 0 << endl; n         return 0; n       } n       a[j][i] = 1; n     } n     if (a[c[i]][i] == 1) { n       cout << 0 << endl; n       return 0; n     } n     a[c[i]][i] = 0; n   } n   int ans = 1; n   for (int i = 0; i < h; i++) { n     for (int j = 0; j < w; j++) { n       if (a[i][j] == -1) { n         ans = (1ll * 2 * ans) % mod; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void pv(T a, T b) { n   for (T i = a; i != b; ++i) cout << *i <<    ; n   cout << endl; n } n template <typename T> n void chmin(T &t, const T &f) { n   if (t > f) t = f; n } n template <typename T> n void chmax(T &t, const T &f) { n   if (t < f) t = f; n } n int in() { n   int x; n   scanf( %d , &x); n   return x; n } n const long long MO = 1000000007; n namespace SA { n int n; n char t[300010]; n int ki[300010], ik[300010], is[300010], hh; n bool cmp(const int &a, const int &b) { n   return (a == b)           ? 0 n          : (ik[a] != ik[b]) ? (ik[a] < ik[b]) n                             : (ik[a + hh] < ik[b + hh]); n } n void makeSA() { n   n = strlen(t); n   int i; n   for (i = 0; i <= n; ++i) ik[ki[i] = i] = t[i]; n   is[0] = is[n] = hh = 0; n   sort(ki, ki + n + 1, cmp); n   for (hh = 1; is[n] != n; hh <<= 1) { n     sort(ki, ki + n + 1, cmp); n     for (i = 0; i < n; ++i) is[i + 1] = is[i] + (cmp(ki[i], ki[i + 1]) ? 1 : 0); n     for (i = 0; i <= n; ++i) ik[ki[i]] = is[i]; n   } n } n int lcp[300010]; n void makeHA() { n   int h = 0, i, j; n   for (i = 0; i < n; ++i) { n     for (j = ki[ik[i] - 1]; t[j + h] == t[i + h]; ++h) n       ; n     lcp[ik[i] - 1] = h; n     if (h) --h; n   } n } n }  // namespace SA n int LA, LB, LC; n long long ans[300010]; n int eventsLen; n pair<int, int> events[300010]; n int uf[300010]; n long long cnt[300010][3]; n long long prodSum; n int root(int u) { return (uf[u] < 0) ? u : (uf[u] = root(uf[u])); } n void conn(int u, int v) { n   u = root(u); n   v = root(v); n   if (u == v) return; n   if (uf[u] > uf[v]) swap(u, v); n   uf[u] += uf[v]; n   uf[v] = u; n   prodSum -= cnt[u][0] * cnt[u][1] * cnt[u][2]; n   prodSum -= cnt[v][0] * cnt[v][1] * cnt[v][2]; n   cnt[u][0] += cnt[v][0]; n   cnt[u][1] += cnt[v][1]; n   cnt[u][2] += cnt[v][2]; n   prodSum += cnt[u][0] * cnt[u][1] * cnt[u][2]; n } n int main() { n   using namespace SA; n   for (; ~scanf( %s , t);) { n     LA = strlen(t); n     t[LA] =  0 ; n     scanf( %s , t + LA + 1); n     LB = strlen(t + LA + 1); n     t[LA + 1 + LB] =  1 ; n     scanf( %s , t + LA + 1 + LB + 1); n     LC = strlen(t + LA + 1 + LB + 1); n     t[LA + 1 + LB + 1 + LC] =  2 ; n     t[LA + 1 + LB + 1 + LC + 1] = 0; n     makeSA(); n     makeHA(); n     eventsLen = 0; n     for (int i = 0; i < n; ++i) { n       events[eventsLen++] = make_pair(lcp[i], i); n     } n     sort(events, events + eventsLen); n     reverse(events, events + eventsLen); n     fill(uf, uf + n + 1, -1); n     for (int i = 0; i <= n; ++i) { n       cnt[i][0] = 0; n       cnt[i][1] = 0; n       cnt[i][2] = 0; n       if (0 <= ki[i] && ki[i] < LA) { n         cnt[i][0] = 1; n       } else if (LA + 1 <= ki[i] && ki[i] < LA + 1 + LB) { n         cnt[i][1] = 1; n       } else if (LA + 1 + LB + 1 <= ki[i] && ki[i] < LA + 1 + LB + 1 + LC) { n         cnt[i][2] = 1; n       } n     } n     prodSum = 0; n     for (int h = 0, k = n; k >= 0; --k) { n       for (; h < eventsLen && events[h].first == k; ++h) { n         const int i = events[h].second; n         conn(i, i + 1); n       } n       ans[k] = prodSum; n     } n     const int ansLen = min({LA, LB, LC}); n     for (int k = 1; k <= ansLen; ++k) { n       if (k > 1) printf(   ); n       printf( %d , (int)(ans[k] % MO)); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005; n int l[N], r[N], n, i, ans, a[N], w, j; n char c[N]; n bool bb[N]; n set<int> s; n set<int>::iterator it; n inline void maintain(int i) { n   if ((l[i] && c[i] != c[l[i]]) || (r[i] <= n && c[r[i]] != c[i])) n     s.insert(i); n   else if (s.count(i)) n     s.erase(i); n } n int main() { n   scanf( %s , c + 1); n   n = strlen(c + 1); n   for (i = 1; i <= n; ++i) { n     l[i] = i - 1, r[i] = i + 1; n     r[0] = 1, l[n + 1] = n; n     if ((i > 1 && c[i] != c[i - 1]) || (i < n && c[i] != c[i + 1])) a[++w] = i; n   } n   ans = bool(w); n   for (;;) { n     for (i = 1; i <= w; ++i) bb[a[i]] = 1; n     s.clear(); n     for (i = 1; i <= w; ++i) { n       j = a[i]; n       l[r[j]] = l[j]; n       r[l[j]] = r[j]; n       if (!bb[l[j]] && l[j]) maintain(l[j]); n       if (!bb[r[j]] && r[j] <= n) maintain(r[j]); n     } n     for (i = 1; i <= w; ++i) bb[a[i]] = 0; n     if (s.empty()) break; n     ++ans; n     for (it = s.begin(), w = 0; it != s.end(); ++it) a[++w] = *it; n     s.clear(); n   } n   return printf( %d n , ans), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   vector<int> A(91, 0); n   for (int i = 0; s[i]; i++) { n     A[(int)s[i]]++; n   } n   for (int i = 65; i < 65 + k; i++) n     if (A[i] == 0) { n       cout <<  0 ; n       return 0; n     } n   cout << k * (*min_element(A.begin() + 65, A.begin() + 65 + k)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n struct Matrix { n   long long coef[105][105]; n   Matrix() { n     for (int i = (0); i <= (101); ++i) n       for (int j = (0); j <= (101); ++j) coef[i][j] = 0; n   } n   Matrix operator*(Matrix a) { n     Matrix res; n     for (int i = (0); i <= (101); ++i) { n       for (int j = (0); j <= (101); ++j) { n         for (int k = (0); k <= (101); ++k) { n           res.coef[i][j] = n               (res.coef[i][j] + coef[i][k] * a.coef[k][j] % mod) % mod; n         } n       } n     } n     return res; n   } n }; n Matrix expo(Matrix A, long long n) { n   if (n == 1) return A; n   Matrix Q = expo(A, n / 2); n   if (n % 2 == 0) return Q * Q; n   return Q * Q * A; n } n int a[50001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, b, k, x; n   cin >> n >> b >> k >> x; n   Matrix A; n   for (int i = 1; i <= int(n); i++) cin >> a[i]; n   for (int i = 1; i <= int(n); i++) { n     for (int m = 0; m < int(x); m++) { n       int newm = (m * 10 + a[i]) % x; n       A.coef[m][newm]++; n     } n   } n   Matrix B = expo(A, b); n   cout << B.coef[0][k] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const long long int INF = 1e18 + 2; n bool compareBysecond(pair<long long int, long long int> a, n                      pair<long long int, long long int> b) { n   return a.second > b.second; n } n int log(long long int x) { n   int cnt = 0; n   while (x > 0) { n     cnt++; n     x = x >> 1; n   } n   return cnt; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, x, y; n     cin >> n >> x >> y; n     int k = n; n     while (((y - x) % (k - 1)) != 0) k--; n     int d = (y - x) / (k - 1); n     vector<int> ans; n     for (int i = 0; x + i * d <= y; ++i) { n       ans.push_back(x + i * d); n     } n     if (ans.size() != n) { n       int temp = x - d; n       while (temp > 0 && ans.size() != n) { n         ans.push_back(temp); n         temp -= d; n       } n     } n     if (ans.size() != n) { n       int temp = y + d; n       while (ans.size() != n) { n         ans.push_back(temp); n         temp += d; n       } n     } n     for (auto it : ans) cout << it <<    ; n     cout <<   n ; n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000 + 10; n const int MAXM = 10000 + 10; n const int INF = 1000 * 1000 * 1000; n int n, m, a[MAXM], b[MAXM], c[MAXM], q[MAXN], d[MAXN][MAXN], s, minimum[MAXN]; n int main() { n   int check = 0; n   int sum = 0; n   int pt = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     minimum[i] = INF; n   } n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> a[i] >> b[i] >> c[i]; n     d[a[i]][b[i]] = c[i]; n     minimum[b[i]] = min(c[i], minimum[b[i]]); n   } n   for (int i = 1; i <= n; i++) { n     if (minimum[i] != INF) { n       sum += minimum[i]; n       check++; n     } n   } n   if (check == n - 1) n     cout << sum; n   else n     cout << -1; n   cin >> s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char arr[2][100010]; n int vis[2][100010]; n int k, n, curj1, curj2; n bool valid(int i, int j) { n   if (i >= 0 && i < 2 && j >= 0 && j >= n) return true; n   if (i >= 0 && i < 2 && j >= 0 && j < n && arr[i][j] !=  X  && vis[i][j] == 0) n     return true; n   return false; n } n void bfs(int i, int j) { n   queue<pair<int, int> > q; n   q.push(make_pair(i, j)); n   vis[i][j] = 1; n   while (!q.empty()) { n     int toi = q.front().first; n     int toj = q.front().second; n     q.pop(); n     if (toj >= n) { n       cout <<  YES  << endl; n       return; n     } n     if (valid(toi + 1, toj + k)) { n       q.push(make_pair(toi + 1, toj + k)); n       vis[toi + 1][toj + k] = vis[toi][toj] + 1; n     } n     if (valid(toi - 1, toj + k)) { n       q.push(make_pair(toi - 1, toj + k)); n       vis[toi - 1][toj + k] = vis[toi][toj] + 1; n     } n     if (valid(toi, toj + 1)) { n       q.push(make_pair(toi, toj + 1)); n       vis[toi][toj + 1] = vis[toi][toj] + 1; n     } n     if (valid(toi, toj - 1) && toj - 1 > vis[toi][toj] - 1) { n       q.push(make_pair(toi, toj - 1)); n       vis[toi][toj - 1] = vis[toi][toj] + 1; n     } n   } n   cout <<  NO  << endl; n } n int main() { n   int i, j; n   cin >> n >> k; n   for (i = 0; i < 2; i++) { n     for (j = 0; j < n; j++) cin >> arr[i][j]; n   } n   bfs(0, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Res[2][7010], N[2][7010], Loss[2][7010]; n int n, m, k, OpN[2]; n queue<pair<int, int> > Q; n int main() { n   srand((unsigned)time(0)); n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   cin.get(); n   for (int i = 0; i < 2; i++) { n     cin >> m; n     OpN[i] = m; n     for (int j = 0; j < m; j++) { n       cin >> k; n       N[i][j] = k; n       Res[i][n + 1 - k] = 1; n       Q.push({i, n + 1 - k}); n     } n   } n   pair<int, int>* P; n   while (!Q.empty()) { n     P = &Q.front(); n     int k = !P->first, i = P->second; n     Q.pop(); n     for (int j = 0; j < OpN[k]; j++) { n       int h = N[k][j], l = ((i - h < 1) ? (i - h + n) : (i - h)); n       if (Res[k][l] == 0) { n         Loss[k][l]++; n       } n     } n     for (int i = 2; i < n + 1; i++) { n       if (Res[k][i] == 0) { n         if (Loss[k][i] == OpN[k]) { n           Res[k][i] = 2; n           for (int t = 0; t < OpN[!k]; t++) { n             int h = N[!k][t], l = ((i - h < 1) ? (i - h + n) : (i - h)); n             if (Res[!k][l] == 0) { n               Res[!k][l] = 1; n               Q.push({!k, l}); n             } n           } n         } n       } n     } n   } n   string Out[3] = { Loop ,  Win ,  Lose }; n   for (int k = 0; k < 2; k++) { n     for (int i = 2; i < n + 1; i++) { n       cout << Out[Res[k][i]].c_str() <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long a, b, c, d; n     cin >> a >> b >> c >> d; n     bool prob[5]; n     for (bool& i : prob) { n       i = true; n     } n     if ((a == 0 && b == 0) || (a + b) % 2 == 0) { n       prob[1] = prob[2] = false; n       if (a == 0 && d == 0) { n         prob[4] = false; n       } else if (b == 0 && c == 0) { n         prob[3] = false; n       } n     } else { n       prob[3] = prob[4] = false; n       if (a == 0 && d == 0) { n         prob[1] = false; n       } else if (b == 0 && c == 0) { n         prob[2] = false; n       } n     } n     for (int i = 1; i <= 4; i++) { n       cout << (prob[i] ?  Ya   :  Tidak  ); n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> p[3030]; n long long dist[3030][3030]; n pair<long long, long long> out[3030][5]; n pair<long long, long long> in[3030][5]; n void bfs(int x) { n   dist[x][x] = 0; n   queue<pair<long long, long long> > q; n   q.push(make_pair(0, x)); n   pair<long long, long long> t; n   while (!q.empty()) { n     t = q.front(); n     q.pop(); n     for (int i = 0; i < p[t.second].size(); i++) { n       if (dist[x][p[t.second][i]] > 1 + t.first) { n         dist[x][p[t.second][i]] = 1 + t.first; n         out[x][0] = make_pair(dist[x][p[t.second][i]], p[t.second][i]); n         sort(out[x], out[x] + 5); n         in[p[t.second][i]][0] = make_pair(dist[x][p[t.second][i]], x); n         sort(in[p[t.second][i]], in[p[t.second][i]] + 5); n         q.push(make_pair(dist[x][p[t.second][i]], p[t.second][i])); n       } n     } n   } n } n int main() { n   int n, m, x, y; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &x, &y); n     p[x].push_back(y); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       dist[i][j] = 1e18; n     } n     for (int j = 0; j <= 4; j++) { n       out[i][j] = make_pair(-1, -1); n       in[i][j] = make_pair(-1, -1); n     } n   } n   for (int i = 1; i <= n; i++) bfs(i); n   long long l1, l2; n   int res[5]; n   long long maxx = -1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       l2 = dist[i][j]; n       if (l2 == 1e18) continue; n       for (int k = 4; k >= 0; k--) { n         for (int k1 = 4; k1 >= 0; k1--) { n           l1 = l2; n           x = in[i][k].second; n           y = out[j][k1].second; n           if (x == y) continue; n           if (x == i || x == j) continue; n           if (y == i || y == j) continue; n           if (x == -1 || y == -1) continue; n           l1 += in[i][k].first; n           l1 += out[j][k1].first; n           if (maxx < l1) { n             maxx = l1; n             res[0] = x; n             res[1] = i; n             res[2] = j; n             res[3] = y; n           } n         } n       } n     } n   } n   for (int i = 0; i < 4; i++) printf( %d  , res[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10, mod = 1e9 + 7; n void solve() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   int ans = 0; n   for (int i = 1; i < n; ++i) { n     if (a[i] == 3 && a[i - 1] == 1) { n       ans += 4; n     } else if (a[i] == 1 && a[i - 1] == 3) { n       ans += 4; n     } else if (a[i] == 2 && a[i - 1] == 1) { n       if (i > 1 && a[i - 2] == 3) { n         ans += 2; n       } else { n         ans += 3; n       } n     } else if (a[i] == 1 && a[i - 1] == 2) { n       ans += 3; n     } else if (a[i] == 3 && a[i - 1] == 2) { n       cout <<  Infinite  << endl; n       return; n     } else if (a[i] == 2 && a[i - 1] == 3) { n       cout <<  Infinite  << endl; n       return; n     } n   } n   cout <<  Finite  << endl; n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const int M = 100; n int cal(int t) { return (t >= 0 ? t : -t); } n int n, d, a[M + 5], x[M + 5], y[M + 5], dist[M + 5][M + 5]; n int main() { n   scanf( %d %d , &n, &d); n   for (int i = 2; i < n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) scanf( %d %d , &x[i], &y[i]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       dist[i][j] = n           (i == j ? 0 : cal(x[i] - x[j]) * d + cal(y[i] - y[j]) * d - a[j]); n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); n   printf( %d , dist[1][n]); n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   cin >> n >> k; n   vector<string> s(n); n   for (long long i = 0; i < n; i++) cin >> s[i]; n   vector<long long> g[n * n]; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < n; j++) { n       if (i + 1 < n) g[i * n + j].push_back((i + 1) * n + j); n       if (j + 1 < n) g[i * n + j].push_back(i * n + (j + 1)); n     } n   vector<long long> kval(n * n); n   vector<char> cval(n * n); n   vector<char> cval2(n * n); n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < n; j++) { n       cval[i * n + j] = s[i][j]; n       cval2[i * n + j] = s[i][j]; n     } n   kval[0] = k; n   if (cval[0] !=  a  && kval[0]) { n     cval[0] =  a ; n     kval[0] -= 1; n   } n   string ans; n   queue<long long> q; n   ans += cval[0]; n   q.push(0); n   while (q.size()) { n     set<long long> tq; n     long long maxkval = 0; n     char mincval =  z ; n     while (q.size()) { n       long long x = q.front(); n       q.pop(); n       for (auto y : g[x]) { n         tq.insert(y); n         if (kval[x]) { n           kval[y] = max(kval[y], kval[x] - (cval[y] !=  a )); n           cval2[y] =  a ; n         } n         maxkval = max(maxkval, kval[y]); n         mincval = min(mincval, cval2[y]); n       } n     } n     ans += mincval; n     for (auto v : tq) n       if (cval2[v] == mincval) q.push(v); n   } n   cout << ans.substr(0, 2 * n - 1) <<   n ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using std::abs; n using std::cerr; n using std::cin; n using std::cout; n using std::map; n using std::max; n using std::min; n using std::pair; n using std::set; n using std::string; n using std::swap; n using std::vector; n using ll = long long; n using uint = unsigned int; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n template <typename T> n void err(vector<string>::iterator it, T a) { n   cerr << it->substr((*it)[0] ==    ) <<   =   << a <<   n ; n } n template <typename T, typename... Ts> n void err(vector<string>::iterator it, T a, Ts... as) { n   cerr << it->substr((*it)[0] ==    ) <<   =   << a <<  ,  ; n   err(++it, as...); n } n struct init { n   init() { n     cin.tie(0); n     std::iostream::sync_with_stdio(0); n     cout << std::fixed << std::setprecision(10); n     cerr << std::fixed << std::setprecision(10); n     srand(228); n   } n   ~init() { n     cerr <<  Time elapsed:   << 1.0 * (double)clock() / CLOCKS_PER_SEC n          <<  s. n ; n   } n } init; n const int MAXN = 1e5 + 15; n const ll INF = 1e16; n ll a[MAXN][3]; n ll dp[MAXN][3][2]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i <= n; ++i) n     for (int j = 0; j < 3; ++j) n       for (int k = 0; k < 2; ++k) dp[i][j][k] = -INF; n   for (int i = 0; i < 3; ++i) n     for (int j = 1; j <= n; ++j) cin >> a[j][i]; n   a[0][1] = a[0][2] = -INF; n   dp[0][0][0] = 0; n   for (int i = 1; i <= n; ++i) { n     dp[i][0][0] = n         max({dp[i - 1][0][0] + a[i][0], dp[i - 1][1][0] + a[i - 1][0] + a[i][0], n              dp[i - 1][2][0] + a[i - 1][1] + a[i - 1][0] + a[i][0], n              i > 1 ? dp[i - 1][2][1] + a[i - 1][1] + a[i - 2][1] + a[i - 2][0] + n                          a[i - 1][0] + a[i][0] n                    : -INF}); n     dp[i][0][1] = dp[i - 1][0][0] + a[i][0]; n     dp[i][1][0] = n         max({dp[i - 1][0][0] + a[i - 1][1] + a[i][1], dp[i - 1][1][0] + a[i][1], n              dp[i - 1][2][0] + a[i - 1][1] + a[i][1]}); n     dp[i][1][1] = -INF; n     dp[i][2][0] = n         max({dp[i - 1][0][0] + a[i - 1][1] + a[i - 1][2] + a[i][2], n              dp[i - 1][1][0] + a[i - 1][2] + a[i][2], dp[i - 1][2][0] + a[i][2], n              i > 1 ? dp[i - 1][0][1] + a[i - 1][1] + a[i - 2][1] + a[i - 2][2] + n                          a[i - 1][2] + a[i][2] n                    : -INF}); n     dp[i][2][1] = dp[i - 1][2][0] + a[i][2]; n     for (int j = 0; j < 3; ++j) n       ; n   } n   ll ans = n       max({dp[n][0][0] + a[n][1] + a[n][2], dp[n][1][0] + a[n][2], dp[n][2][0], n            dp[n][0][1] + a[n][1] + a[n - 1][1] + a[n - 1][2] + a[n][2]}); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double EPS = 1e-9; n const long double PI = atan(1) * 4; n const long long M = 1000000007; n struct PT { n   double x, y; n   PT() {} n   PT(double x, double y) : x(x), y(y) {} n   PT(const PT &p) : x(p.x), y(p.y) {} n   PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); } n   PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); } n   PT operator*(double c) const { return PT(x * c, y * c); } n   PT operator/(double c) const { return PT(x / c, y / c); } n }; n double dot(PT p, PT q) { return p.x * q.x + p.y * q.y; } n double dist2(PT p, PT q) { return dot(p - q, p - q); } n double cross(PT p, PT q) { return p.x * q.y - p.y * q.x; } n double cos_u2v(const PT &u, const PT &v) { n   return dot(u, v) / sqrt(dot(u, u) * dot(v, v)); n } n double sin_u2v(const PT &u, const PT &v) { n   return cross(u, v) / sqrt(dot(u, u) * dot(v, v)); n } n ostream &operator<<(ostream &os, const PT &p) { n   os <<  (  << p.x <<  ,  << p.y <<  ) ; n } n double vector2angle(const PT &v) { return atan2(v.y, v.x); } n struct UnionFind { n   vector<long long> C; n   UnionFind(long long n) : C(n) { n     for (int i = 0; i < n; i++) C[i] = i; n   } n   long long find(long long x) { return (C[x] == x) ? x : C[x] = find(C[x]); } n   void merge(long long x, long long y) { C[find(x)] = find(y); } n }; n long long mod(long long x, long long m) { return ((x % m) + m) % m; } n long long gcd(long long a, long long b) { n   for (; b > 0; swap(a, b)) a %= b; n   return a; n } n long long extended_euclid(long long a, long long b, long long &x, n                           long long &y) { n   long long xx = y = 0; n   long long yy = x = 1; n   while (b) { n     long long q = a / b; n     long long t = b; n     b = a % b; n     a = t; n     t = xx; n     xx = x - q * xx; n     x = t; n     t = yy; n     yy = y - q * yy; n     y = t; n   } n   return a; n } n long long mod_inverse(long long a, long long n) { n   long long x, y; n   long long d = extended_euclid(a, n, x, y); n   if (d > 1) return -1; n   return mod(x, n); n } n long long discretize(vector<long long> &x) { n   set<long long> avset(x.begin(), x.end()); n   map<long long, long long> vmap; n   long long dn = 0; n   for (auto it = avset.begin(); it != avset.end(); it++) vmap[*it] = dn++; n   for (long long i = 0, n = x.size(); i < n; i++) x[i] = vmap[x[i]]; n   return dn; n } n struct LPSolver { n   int m, n; n   vector<long long> B, N; n   vector<vector<long double>> D; n   LPSolver(const vector<vector<long double>> &A, const vector<long double> &b, n            const vector<long double> &c) n       : m(b.size()), n         n(c.size()), n         N(n + 1), n         B(m), n         D(m + 2, vector<long double>(n + 2)) { n     for (int i = 0; i < m; i++) n       for (int j = 0; j < n; j++) D[i][j] = A[i][j]; n     for (int i = 0; i < m; i++) { n       B[i] = n + i; n       D[i][n] = -1; n       D[i][n + 1] = b[i]; n     } n     for (int j = 0; j < n; j++) { n       N[j] = j; n       D[m][j] = -c[j]; n     } n     N[n] = -1; n     D[m + 1][n] = 1; n   } n   void Pivot(int r, int s) { n     for (int i = 0; i < m + 2; i++) n       if (i != r) n         for (int j = 0; j < n + 2; j++) n           if (j != s) D[i][j] -= D[r][j] * D[i][s] / D[r][s]; n     for (int j = 0; j < n + 2; j++) n       if (j != s) D[r][j] /= D[r][s]; n     for (int i = 0; i < m + 2; i++) n       if (i != r) D[i][s] /= -D[r][s]; n     D[r][s] = 1.0 / D[r][s]; n     swap(B[r], N[s]); n   } n   bool Simplex(int phase) { n     int x = phase == 1 ? m + 1 : m; n     while (true) { n       int s = -1; n       for (int j = 0; j <= n; j++) { n         if (phase == 2 && N[j] == -1) continue; n         if (s == -1 || D[x][j] < D[x][s] || D[x][j] == D[x][s] && N[j] < N[s]) n           s = j; n       } n       if (D[x][s] > -EPS) return true; n       int r = -1; n       for (int i = 0; i < m; i++) { n         if (D[i][s] < EPS) continue; n         if (r == -1 || D[i][n + 1] / D[i][s] < D[r][n + 1] / D[r][s] || n             (D[i][n + 1] / D[i][s]) == (D[r][n + 1] / D[r][s]) && B[i] < B[r]) n           r = i; n       } n       if (r == -1) return false; n       Pivot(r, s); n     } n   } n   long double solve(vector<long double> &x) { n     int r = 0; n     for (int i = 1; i < m; i++) n       if (D[i][n + 1] < D[r][n + 1]) r = i; n     if (D[r][n + 1] < -EPS) { n       Pivot(r, n); n       if (!Simplex(1) || D[m + 1][n + 1] < -EPS) n         return -numeric_limits<long double>::infinity(); n       for (int i = 0; i < m; i++) n         if (B[i] == -1) { n           int s = -1; n           for (int j = 0; j <= n; j++) n             if (s == -1 || D[i][j] < D[i][s] || n                 D[i][j] == D[i][s] && N[j] < N[s]) n               s = j; n           Pivot(i, s); n         } n     } n     if (!Simplex(2)) return numeric_limits<long double>::infinity(); n     x = vector<long double>(n); n     for (int i = 0; i < m; i++) n       if (B[i] < n) x[B[i]] = D[i][n + 1]; n     return D[m][n + 1]; n   } n }; n void buildadjlist(long long n, const vector<long long> &froms, n                   const vector<long long> &tos, vector<long long> &v2e0, n                   vector<long long> &e2n) { n   v2e0.resize(n); n   v2e0.assign(n, -1); n   long long en = froms.size(); n   e2n.resize(en); n   for (int i = 0; i < en; i++) { n     long long f = froms[i]; n     long long t = tos[i]; n     e2n[i] = v2e0[f]; n     v2e0[f] = i; n   } n } n void dijkstra(const vector<long long> &v2e0, const vector<long long> &e2t, n               const vector<long long> &e2c, const vector<long long> &e2n, n               long long src, vector<long long> &dist, vector<long long> &prec) { n   long long n = v2e0.size(); n   dist.resize(n); n   dist.assign(n, INT_MAX); n   prec.resize(n); n   prec.assign(n, -1); n   vector<bool> arr(n); n   long long sel = src; n   dist[sel] = 0; n   while (sel >= 0) { n     arr[sel] = true; n     for (long long i = v2e0[sel]; i >= 0; i = e2n[i]) { n       long long to = e2t[i]; n       long long cost = e2c[i]; n       if (!arr[to] && dist[to] > dist[sel] + cost) { n         dist[to] = dist[sel] + cost; n         prec[to] = sel; n       } n     } n     sel = -1; n     for (long long i = 0; i < n; i++) n       if (!arr[i] && (sel == -1 || dist[i] < dist[sel])) sel = i; n   } n } n void tarjan_dfs(long long v, long long &timer, long long &cn, n                 vector<long long> &vtime, vector<long long> &rtime, n                 const vector<long long> &v2e0, const vector<long long> &e2t, n                 const vector<long long> &e2n, vector<long long> &v2c, n                 stack<long long> &stk) { n   stk.push(v); n   rtime[v] = vtime[v] = timer++; n   for (long long i = v2e0[v]; i >= 0; i = e2n[i]) { n     long long w = e2t[i]; n     if (vtime[w] == -1) { n       tarjan_dfs(w, timer, cn, vtime, rtime, v2e0, e2t, e2n, v2c, stk); n       rtime[v] = min(rtime[v], rtime[w]); n     } else if (v2c[w] == -1) n       rtime[v] = min(rtime[v], vtime[w]); n   } n   if (vtime[v] == rtime[v]) { n     while (true) { n       long long pv = stk.top(); n       v2c[pv] = cn; n       stk.pop(); n       if (pv == v) break; n     } n     cn++; n   } n } n void tarjan_strong(long long n, const vector<long long> &v2e0, n                    const vector<long long> &e2t, const vector<long long> &e2n, n                    vector<long long> &v2c) { n   v2c.assign(n, -1); n   vector<long long> vtime(n, -1); n   vector<long long> rtime(n, -1); n   stack<long long> stk; n   long long timer = 0, cn = 0; n   for (long long i = 0; i < n; i++) { n     if (vtime[i] >= 0) continue; n     tarjan_dfs(i, timer, cn, vtime, rtime, v2e0, e2t, e2n, v2c, stk); n   } n } n struct FordFulk { n   const vector<long long> &v2e0; n   const vector<long long> &e2f; n   const vector<long long> &e2t; n   const vector<long long> &e2c; n   const vector<long long> &e2n; n   long long s, t; n   FordFulk(const vector<long long> &_v2e0, const vector<long long> &_e2f, n            const vector<long long> &_e2t, const vector<long long> &_e2c, n            const vector<long long> &_e2n, long long _s, long long _t) n       : s(_s), t(_t), v2e0(_v2e0), e2f(_e2f), e2t(_e2t), e2c(_e2c), e2n(_e2n) {} n   long long _find_aug_path(const vector<long long> &v2e0, n                            const vector<long long> &e2f, n                            const vector<long long> &e2t, n                            const vector<long long> &e2c, n                            const vector<long long> &e2n, long long s, n                            long long t, vector<long long> &epath) { n     vector<long long> q2v; n     vector<long long> q2p; n     vector<long long> q2e; n     vector<long long> q2f; n     vector<bool> inq(v2e0.size(), false); n     q2v.push_back(s); n     q2p.push_back(-1); n     q2e.push_back(-1); n     q2f.push_back(INT_MAX); n     inq[s] = true; n     for (long long i = 0; i < q2v.size(); i++) { n       long long crt = q2v[i]; n       long long f = q2f[i]; n       for (long long j = v2e0[crt]; j != -1; j = e2n[j]) { n         long long to = e2t[j]; n         long long rem = e2c[j]; n         if (inq[to]) continue; n         if (rem <= 0) continue; n         q2v.push_back(to); n         q2p.push_back(i); n         q2e.push_back(j); n         q2f.push_back(min(f, rem)); n         inq[to] = true; n       } n     } n     for (long long i = 0; i < q2v.size(); i++) { n       if (q2v[i] != t) continue; n       stack<long long> stk; n       for (long long j = i; j > 0; j = q2p[j]) { n         stk.push(q2e[j]); n       } n       while (!stk.empty()) { n         epath.push_back(stk.top()); n         stk.pop(); n       } n       return q2f[i]; n     } n     return 0; n   } n   long long eval() { n     long long en = e2f.size(); n     map<pair<long long, long long>, long long> rsd; n     for (long long i = 0; i < en; i++) { n       long long f = e2f[i]; n       long long t = e2t[i]; n       rsd[make_pair(f, t)] += e2c[i]; n       rsd[make_pair(t, f)] += 0; n     } n     map<pair<long long, long long>, long long> nei; n     long long ren = 0; n     for (auto er : rsd) { n       nei[er.first] = ren++; n     } n     long long rvn = v2e0.size(); n     vector<long long> rv2e0(rvn, -1); n     vector<long long> re2f(ren); n     vector<long long> re2t(ren); n     vector<long long> re2c(ren); n     vector<long long> re2n(ren); n     vector<long long> re2p(ren); n     long long rei = 0; n     for (auto er : rsd) { n       long long f = er.first.first; n       long long t = er.first.second; n       long long c = er.second; n       re2f[rei] = f; n       re2t[rei] = t; n       re2c[rei] = c; n       re2p[rei] = nei[make_pair(t, f)]; n       re2n[rei] = rv2e0[f]; n       rv2e0[f] = rei; n       ++rei; n     } n     long long sol = 0; n     vector<long long> epath; n     while (true) { n       epath.clear(); n       long long res = n           _find_aug_path(rv2e0, re2f, re2t, re2c, re2n, s, t, epath); n       if (res == 0) break; n       for (auto ei : epath) { n         re2c[ei] -= res; n         re2c[re2p[ei]] += res; n       } n       sol += res; n     } n     return sol; n   } n }; n template <typename T> n struct BIT1D { n   long long n; n   vector<T> v; n   T (*fnx)(long long, T); n   T (*fab)(T, T); n   long long rg(long long k) { return ((k ^ (k + 1)) + 1) / 2; } n   BIT1D(long long _n, T v0, T (*_fnx)(long long, T), T (*_fab)(T, T)) n       : n(_n), v(_n), fnx(_fnx), fab(_fab) { n     for (long long i = 0; i < n; i++) v[i] = fnx(rg(i), v0); n   } n   void update(long long i, T v1) { n     for (; i < n; i += rg(i)) v[i] = fab(v[i], v1); n   } n   T query(long long i) { n     long long j = i - rg(i); n     return (j < 0) ? v[i] : (query(j) + v[i]); n   } n }; n template <typename T> n struct LazySegTree1D { n   vector<T> v; n   vector<long long> opr; n   vector<T> opd; n   vector<long long> nxt; n   long long LEFT; n   long long RGHT; n   T (*fnx)(long long, T); n   T (*fab)(T, T); n   LazySegTree1D(long long _l, long long _r, T _v, T (*_fnx)(long long, T), n                 T (*_fab)(T, T)) n       : v(1), opr(1), opd(1), nxt(1), LEFT(_l), RGHT(_r), fnx(_fnx), fab(_fab) { n     v[0] = fnx(RGHT - LEFT + 1, _v); n     opr[0] = 0; n     opd[0] = _v; n     nxt[0] = -1; n   } n   void _split(long long i, long long l0, long long r0) { n     assert(nxt[i] == -1); n     assert(opr[i] == 0); n     assert(l0 < r0); n     long long n = v.size(); n     nxt[i] = n; n     long long m0 = (l0 + r0) / 2; n     v.push_back(fnx(m0 - l0 + 1, opd[i])); n     v.push_back(fnx(r0 - m0, opd[i])); n     opr.push_back(0); n     opr.push_back(0); n     opd.push_back(opd[i]); n     opd.push_back(opd[i]); n     nxt.push_back(-1); n     nxt.push_back(-1); n   } n   void _pushdown(long long i, long long l0, long long r0) { n     if (opr[i] == -1) return; n     long long m0 = (l0 + r0) / 2; n     if (nxt[i] == -1) n       _split(i, l0, r0); n     else if (opr[i] >= 0) { n       _set(nxt[i], l0, m0, l0, m0, opr[i], opd[i]); n       _set(nxt[i] + 1, m0 + 1, r0, m0 + 1, r0, opr[i], opd[i]); n     } n     opr[i] = -1; n   } n   void _set(long long i, long long l0, long long r0, long long l1, long long r1, n             long long o1, T v1) { n     assert(l1 >= l0 && r1 <= r0); n     if (l0 == l1 && r0 == r1) { n       if (o1 == 0) { n         v[i] = fnx(r0 - l0 + 1, v1); n         opr[i] = 0; n         opd[i] = v1; n       } else if (o1 == 1) { n         v[i] = fab(v[i], fnx(r0 - l0 + 1, v1)); n         if (opr[i] == -1) { n           opr[i] = 1; n           opd[i] = v1; n         } else if (opr[i] == 0) { n           opr[i] = 0; n           opd[i] = fab(opd[i], v1); n         } else if (opr[i] == 1) { n           opr[i] = 1; n           opd[i] = fab(opd[i], v1); n         } n       } n       return; n     } n     long long m0 = (l0 + r0) / 2; n     _pushdown(i, l0, r0); n     if (l1 <= m0) _set(nxt[i], l0, m0, l1, min(r1, m0), o1, v1); n     if (r1 > m0) _set(nxt[i] + 1, m0 + 1, r0, max(l1, m0 + 1), r1, o1, v1); n     v[i] = fab(v[nxt[i]], v[nxt[i] + 1]); n   } n   T _get(long long i, long long l0, long long r0, long long l1, long long r1) { n     assert(l0 <= l1 && r1 <= r0); n     if (l0 == l1 && r1 == r0) return v[i]; n     _pushdown(i, l0, r0); n     long long m0 = (l0 + r0) / 2; n     if (r1 <= m0) return _get(nxt[i], l0, m0, l1, r1); n     if (m0 + 1 <= l1) return _get(nxt[i] + 1, m0 + 1, r0, l1, r1); n     T lv = _get(nxt[i], l0, m0, l1, m0); n     T rv = _get(nxt[i] + 1, m0 + 1, r0, m0 + 1, r1); n     return fab(lv, rv); n   } n   void updres(long long l1, long long r1, T v1) { n     _set(0, LEFT, RGHT, l1, r1, 1, v1); n   } n   void setval(long long l1, long long r1, T v1) { n     _set(0, LEFT, RGHT, l1, r1, 0, v1); n   } n   T get(long long l1, long long r1) { return _get(0, LEFT, RGHT, l1, r1); } n }; n void suffix_array(const string &s, vector<long long> &sa, n                   vector<long long> &rank) { n   long long n = s.size(); n   long long ri = 0; n   vector<vector<long long>> tmprank(2, vector<long long>(n)); n   vector<pair<pair<long long, long long>, long long>> kni(n); n   for (long long g = 1; g / 2 <= n - 1; g *= 2, ri = 1 - ri) { n     for (long long i = 0; i < n; i++) { n       long long key1 = (g == 1) ? s[i] : tmprank[ri][i]; n       long long key2 = (g == 1)          ? 0 n                        : (i + g / 2 < n) ? (tmprank[ri][i + g / 2]) n                                          : -1; n       kni[i] = make_pair(make_pair(key1, key2), i); n     } n     sort(kni.begin(), kni.end()); n     for (long long i = 0, last_rank = -1; i < n; i++) { n       long long this_rank = n           (i > 0 && kni[i].first == kni[i - 1].first) ? last_rank : i; n       last_rank = tmprank[1 - ri][kni[i].second] = this_rank; n     } n   } n   rank.resize(n); n   rank.assign(tmprank[ri].begin(), tmprank[ri].end()); n   sa.resize(n); n   for (long long i = 0; i < n; i++) sa[rank[i]] = i; n } n struct SuffixTree { n   string str; n   vector<unordered_map<long long, long long>> sta; n   vector<unordered_map<long long, long long>> end; n   vector<unordered_map<long long, long long>> nxt; n   vector<long long> suflink; n   long long capacity; n   long long node_count; n   long long act_node; n   long long act_br; n   long long act_len; n   long long todo_count; n   bool _nodehasbr(long long node, long long br) { n     auto m = nxt[node]; n     return m.find(br) != m.end(); n   } n   long long _newnode() { n     if (node_count < capacity) { n       long long newi = node_count++; n       suflink[newi] = -1; n       sta[newi].clear(); n       end[newi].clear(); n       nxt[newi].clear(); n       return newi; n     } n     sta.emplace_back(); n     end.emplace_back(); n     nxt.emplace_back(); n     suflink.push_back(-1); n     ++node_count; n     ++capacity; n     return node_count - 1; n   } n   long long _edgerealend(long long node, long long br) { n     long long tmp = end[node][br]; n     return (tmp == -1) ? (str.size() - 1) : tmp; n   } n   long long _edgelen(long long node, long long br) { n     return (br == -1) ? 0 n            : (!_nodehasbr(node, br)) n                ? 0 n                : (_edgerealend(node, br) - sta[node][br] + 1); n   } n   void clear() { n     node_count = 0; n     act_node = 0; n     act_br = -1; n     act_len = 0; n     todo_count = 0; n     _newnode(); n   } n   SuffixTree(long long est_strlen) { n     capacity = 0; n     if (est_strlen > 0) { n       long long est_nodecnt = est_strlen * 2; n       capacity = est_nodecnt; n       sta = vector<unordered_map<long long, long long>>(est_nodecnt); n       end = vector<unordered_map<long long, long long>>(est_nodecnt); n       nxt = vector<unordered_map<long long, long long>>(est_nodecnt); n       suflink = vector<long long>(est_nodecnt, -1); n     } n     clear(); n   } n   bool _tryextend(long long chr) { n     if (act_len == 0) { n       if (!_nodehasbr(act_node, chr)) return false; n       act_br = chr; n       ++act_len; n       return true; n     } n     if (act_len == _edgelen(act_node, act_br)) { n       long long nxtnode = nxt[act_node][act_br]; n       if (!_nodehasbr(nxtnode, chr)) return false; n       act_node = nxtnode; n       act_br = chr; n       act_len = 1; n       return true; n     } n     long long start = sta[act_node][act_br]; n     if (chr != str[start + act_len]) return false; n     ++act_len; n     return true; n   } n   void _branch(long long node, long long chr) { n     long long newnode = _newnode(); n     sta[node][chr] = str.size() - 1; n     end[node][chr] = -1; n     nxt[node][chr] = newnode; n   } n   long long _actsplit(long long chr) { n     long long new0 = _newnode(); n     long long new1 = _newnode(); n     long long start = sta[act_node][act_br]; n     sta[new0][str[start + act_len]] = start + act_len; n     end[new0][str[start + act_len]] = end[act_node][act_br]; n     nxt[new0][str[start + act_len]] = nxt[act_node][act_br]; n     sta[new0][chr] = str.size() - 1; n     end[new0][chr] = -1; n     nxt[new0][chr] = new1; n     sta[act_node][act_br] = start; n     end[act_node][act_br] = start + act_len - 1; n     nxt[act_node][act_br] = new0; n     return new0; n   } n   long long _splitorbranch(long long chr) { n     if (act_br >= 0 && act_len < _edgelen(act_node, act_br)) n       return _actsplit(chr); n     long long target_node = (act_len == 0) ? 0 : nxt[act_node][act_br]; n     _branch(target_node, chr); n     return target_node; n   } n   string _edgestr(long long node, long long br) { n     long long start = sta[node][br]; n     long long end = _edgerealend(node, br); n     return str.substr(start, end - start + 1); n   } n   void _actsupdate(long long node, long long br, long long len) { n     act_node = node, act_br = br, act_len = len; n   } n   void _actfastfwd(long long start, long long slen) { n     if (act_br == -1) return; n     while (true) { n       long long elen = _edgelen(act_node, act_br); n       if (slen <= elen) return; n       long long new_act_node = nxt[act_node][act_br]; n       long long new_act_br = str[start + elen]; n       long long new_act_len = act_len - elen; n       _actsupdate(new_act_node, new_act_br, new_act_len); n       start += elen; n       slen -= elen; n     } n   } n   bool _actsgotonextsuf() { n     if (act_len == 0) return false; n     long long estart = sta[act_node][act_br]; n     if (act_node == 0) { n       act_br = (act_len == 1) ? (-1) : str[estart + 1]; n       --act_len; n       _actfastfwd(estart + 1, act_len); n     } else { n       act_node = (suflink[act_node] >= 0) ? suflink[act_node] : 0; n       _actfastfwd(estart, act_len); n     } n   } n   void insert(long long chr) { n     str.push_back(chr); n     ++todo_count; n     long long prev_created = -1; n     for (long long waitlink = -1; todo_count > 0; --todo_count) { n       bool suffix_exists = _tryextend(chr); n       if (suffix_exists) { n         waitlink = act_node; n       } else { n         waitlink = _splitorbranch(chr); n       } n       if (prev_created >= 0) suflink[prev_created] = waitlink; n       prev_created = waitlink; n       if (suffix_exists) break; n       _actsgotonextsuf(); n     } n   } n }; n const int N = 100; n int l0[N] = {}; n int l1[N] = {}; n int r0[N] = {}; n int r1[N] = {}; n int main() { n   srand(time(NULL)); n   int n, m; n   cin >> n >> m; n   int p = 1; n   for (int i = 0; i < n; ++i) { n     if (p > m) break; n     l0[i] = p++; n     if (p > m) break; n     r0[i] = p++; n     if (p > m) break; n   } n   for (int i = 0; i < n; ++i) { n     if (p > m) break; n     l1[i] = p++; n     if (p > m) break; n     r1[i] = p++; n     if (p > m) break; n   } n   for (int i = 0; i < n; ++i) { n     if (l1[i] > 0) printf( %d  , l1[i]); n     if (l0[i] > 0) printf( %d  , l0[i]); n     if (r1[i] > 0) printf( %d  , r1[i]); n     if (r0[i] > 0) printf( %d  , r0[i]); n   } n   printf(  n ); n   return 0; n } n


// Codeforces Round #720 (Div. 2) n // Problem link: https://codeforces.com/problemset/problem/1521/B n // Problem tags:         constructive algorithms    math    *1300 n  n   n   n // CODE n  n #include <iostream> n #include <vector> n #include <limits.h> n  n using namespace std; n  n int main()  n { n    int t; n    cin >> t; n    while(t--) n    { n         int n; n         cin >> n; n         n         vector<int> arr(n+1); n         int min = INT_MAX, pos = -1;  n         for(int i = 1; i <= n; i++) n         { n             cin >> arr[i]; n             if(arr[i] < min) n             { n                min = arr[i]; n                pos = i; n             } n         } n         n         cout << n-1 <<   n ; n         n         int next = min+1; n         for(int i = pos-1; i > 0; i--) n         { n             arr[i] = next; n             cout << pos <<     << i <<     << min <<     << next <<   n ;  n             next++; n         } n         next = min+1; n         for(int i = pos+1; i <= n; i++) n         { n             arr[i] = next; n             cout << pos <<     << i <<     << min <<     << next <<   n ;  n             next++; n         } n         n         // cout <<  array:  ; n         // for(int i=1; i<=n; i++) n         //     cout << arr[i] <<    ; n         // cout <<   n ; n    } n     n    return 0; n } n   n


#include <bits/stdc++.h> n using namespace std; n const int MX = (int)1e5 + 5; n int test; n vector<int> con[MX]; n int dp[MX], sz[MX], prv[MX]; n void dfs(int u) { n   sz[u] = 1; n   for (int v : con[u]) { n     dp[v] = dp[u] + 1; n     dfs(v); n     sz[u] += sz[v]; n   } n } n int fst[MX], st[MX], en[MX], pn; n void hld(int u, int hd) { n   fst[u] = hd; n   st[u] = ++pn; n   int w = 0; n   for (int v : con[u]) n     if (sz[w] < sz[v]) w = v; n   if (w) hld(w, hd); n   for (int v : con[u]) n     if (v != w) hld(v, v); n   en[u] = pn; n } n struct Node { n   int a, b, clr, len; n   Node(int a = 0, int b = 0, int clr = 0) : a(a), b(b), clr(clr) {} n   Node operator+(const Node &d) const { n     if (b >= d.a) n       return Node(a, b - d.a + d.b); n     else n       return Node(a + d.a - b, d.b); n   } n   void clear() { a = len, b = 0, clr = 1; } n } node[1 << 20]; n void push_down(int id) { n   if (node[id].clr) { n     node[id + id].clear(); n     node[id + id + 1].clear(); n     node[id].clr = 0; n   } n } n void push_up(int id) { n   node[id] = node[id + id] + node[id + id + 1]; n   node[id].len = node[id + id].len + node[id + id + 1].len; n } n void build(int id, int st, int en) { n   if (st == en) { n     node[id] = Node(1, 0, 0); n     node[id].len = 1; n     return; n   } n   int mid = st + en >> 1; n   build(id + id, st, mid); n   build(id + id + 1, mid + 1, en); n   push_up(id); n } n void add(int id, int st, int en, int pos, int val) { n   if (pos <= st && en <= pos) { n     node[id].b += val; n     return; n   } n   push_down(id); n   int mid = st + en >> 1; n   if (pos <= mid) add(id + id, st, mid, pos, val); n   if (pos > mid) add(id + id + 1, mid + 1, en, pos, val); n   push_up(id); n } n Node get(int id, int st, int en, int l, int r) { n   if (l <= st && en <= r) { n     return node[id]; n   } n   push_down(id); n   Node res = Node(0, 0); n   int mid = st + en >> 1; n   if (l <= mid) res = res + get(id + id, st, mid, l, r); n   if (r > mid) res = res + get(id + id + 1, mid + 1, en, l, r); n   push_up(id); n   return res; n } n vector<pair<int, int> > cur; n int get_val(int u, int v, int n) { n   int fu, fv; n   Node res = Node(0, 0); n   cur.clear(); n   while (true) { n     fu = fst[u], fv = fst[v]; n     if (fu == fv) break; n     if (dp[fu] > dp[fv]) swap(u, v), swap(fu, fv); n     cur.push_back(make_pair(st[fv], st[v])); n     v = prv[fv]; n   } n   if (st[u] > st[v]) swap(u, v); n   cur.push_back(make_pair(st[u], st[v])); n   for (int i = cur.size() - 1; i >= 0; i--) n     res = res + get(1, 1, n, cur[i].first, cur[i].second); n   return res.b; n } n void clear(int id, int st, int en, int l, int r) { n   if (l <= st && en <= r) { n     node[id].clear(); n     return; n   } n   push_down(id); n   int mid = st + en >> 1; n   if (l <= mid) clear(id + id, st, mid, l, r); n   if (r > mid) clear(id + id + 1, mid + 1, en, l, r); n   push_up(id); n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, q; n   cin >> n >> q; n   for (int i = 2; i <= n; i++) { n     int u; n     cin >> u; n     con[u].push_back(i); n     prv[i] = u; n   } n   dp[1] = 1; n   dfs(1); n   hld(1, 1); n   build(1, 1, n); n   while (q--) { n     int type, v; n     cin >> type >> v; n     if (type == 1) n       add(1, 1, n, st[v], 1); n     else { n       int val = get_val(v, 1, n); n       if (type == 3) n         cout << (val > 0 ?  black  :  white ) << endl; n       else n         add(1, 1, n, st[v], -val), clear(1, 1, n, st[v] + 1, en[v]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t, n, i, s, a1, b1, m; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     if (n < m) { n       cout <<  0 n ; n       continue; n     } n     if (n == m) n       cout << n % 10 <<   n ; n     else { n       long long int a[10] = {}, b[11] = {}; n       for (i = 1; i <= 10; i++) { n         a[i - 1] = (m * i) % 10; n         b[i] += a[i - 1] + b[i - 1]; n       } n       a1 = n / m; n       b1 = a1 % 10; n       a1 = a1 / 10; n       s = a1 * b[10] + b[b1]; n       cout << s <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getInt(); n int getNegInt(); n int T, N; n int get_nearest(int p, int a, vector<vector<int> >& path, n                 unordered_set<int>& me) { n   if (me.find(a) != me.end()) return a; n   for (auto b : path[a]) { n     if (b == p) continue; n     int tar = get_nearest(a, b, path, me); n     if (tar != -1) return tar; n   } n   return -1; n } n int main(int argc, char** argv) { n   int a, b, k1, k2; n   T = getInt(); n   while (T--) { n     unordered_set<int> me, he; n     N = getInt(); n     vector<vector<int> > path(N + 1, vector<int>()); n     for (int i = 1; i < N; i++) { n       scanf( %d%d , &a, &b); n       path[a].push_back(b); n       path[b].push_back(a); n     } n     k1 = getInt(); n     int start; n     for (int i = 1; i <= k1; i++) { n       a = getInt(); n       me.insert(a); n     } n     k2 = getInt(); n     for (int i = 1; i <= k2; i++) { n       b = getInt(); n       start = b; n       he.insert(b); n     } n     int ans = -1; n     for (int i = 1; i <= 1; i++) { n       cout <<  B   << start << endl; n       fflush(stdout); n       int index = getInt(); n       if (me.find(index) != me.end()) { n         ans = index; n         break; n       } n       int nearest = get_nearest(index, index, path, me); n       cout <<  A   << nearest << endl; n       fflush(stdout); n       index = getInt(); n       if (he.find(index) != he.end()) { n         ans = nearest; n         break; n       } n     } n     cout <<  C   << ans << endl; n     fflush(stdout); n   } n   return 0; n } n int getInt() { n   int ret = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) ret *= 10, ret += c -  0 , c = getchar(); n   return ret; n } n int getNegInt() { n   int ret = 0, sign = 1; n   char c = getchar(); n   while ((c <  0  && c !=  - ) || c >  9 ) c = getchar(); n   if (c ==  - ) sign = -1, c = getchar(); n   while (c >=  0  && c <=  9 ) ret *= 10, ret += c -  0 , c = getchar(); n   return ret * sign; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> a; n bool valido; n vector<vector<long long>> adj; n long long d; n void dfs(long long u, long long p = 0) { n   if (!valido) return; n   for (long long v : adj[u]) n     if (v != p) dfs(v, u); n   if (a[u] % d == 0) { n     a[p]++; n   } else { n     a[u]++; n     if (p == 0 || a[u] % d != 0) valido = 0; n   } n } n int32_t main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     a.resize(n + 1); n     adj = vector<vector<long long>>(n + 1); n     for (long long i = 0; i < n - 1; i++) { n       long long a, b; n       cin >> a >> b; n       adj[a].push_back(b); n       adj[b].push_back(a); n     } n     vector<long long> freq(n + 1); n     freq[1] = 2; n     for (long long i = 1; i < n - 1; i++) freq[1] = 2 * freq[1] % 998244353; n     for (d = 2; d <= n - 1; d++) { n       if ((n - 1) % d == 0) { n         valido = 1; n         fill(a.begin(), a.end(), 0); n         dfs(1); n         freq[d] = valido; n       } n     } n     vector<long long> res(n + 1); n     for (long long d = n; d >= 1; d--) { n       res[d] = freq[d]; n       for (long long mult = 2 * d; mult <= n; mult += d) { n         res[d] = res[d] - res[mult] + 998244353; n         res[d] %= 998244353; n       } n     } n     for (long long d = 1; d <= n; d++) { n       cout << res[d] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> nums; n     for (long long i = 0; i < n; i++) { n       int num; n       cin >> num; n       nums.push_back(num); n     } n     sort(nums.begin(), nums.end()); n     long long c = 0; n     for (int i = 0; i < n; i++) { n       if (nums[i] == 0) { n         nums[i] += 1; n         c++; n       } n     } n     long long sum = 0; n     for (int i = 0; i < n; i++) { n       sum += nums[i]; n     } n     if (sum == 0) { n       c++; n     } n     cout << c << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int l, r; n   node() { l = r = -1; } n } p[30]; n int vis[30], sum[1000005]; n int main() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   for (int i = 0; i < s.length(); i++) { n     int id = s[i] -  A ; n     if (!vis[id]) { n       p[id].l = p[id].r = i; n       vis[id] = 1; n     } else n       p[id].r = i; n   } n   for (int i = 0; i < 26; i++) { n     if (vis[i]) { n       sum[p[i].l]++; n       sum[p[i].r + 1]--; n     } n   } n   int ans = sum[0]; n   if (ans > k) { n     cout <<  YES ; n     return 0; n   } n   for (int i = 1; i < n; i++) { n     sum[i] = sum[i - 1] + sum[i]; n     if (sum[i] > k) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int arr[1000010], lazy[1000010 << 2]; n string str; n struct data { n   int n4, n7, n47, n74; n   data() { n4 = n7 = n47 = n74 = 0; } n } tree[1000010 << 2]; n void build(int node, int l, int r) { n   if (l == r) { n     tree[node].n4 = (arr[l] == 4); n     tree[node].n7 = (arr[l] == 7); n     tree[node].n47 = 1; n     tree[node].n74 = 1; n     return; n   } n   int mid = (l + r) >> 1; n   build(node << 1, l, mid); n   build(node << 1 | 1, mid + 1, r); n   tree[node].n4 = tree[node << 1].n4 + tree[node << 1 | 1].n4; n   tree[node].n7 = tree[node << 1].n7 + tree[node << 1 | 1].n7; n   tree[node].n47 = max(max(tree[node << 1].n47 + tree[node << 1 | 1].n7, n                            tree[node << 1].n4 + tree[node << 1 | 1].n47), n                        tree[node << 1].n4 + tree[node << 1 | 1].n7); n   tree[node].n74 = max(max(tree[node << 1].n7 + tree[node << 1 | 1].n74, n                            tree[node << 1].n74 + tree[node << 1 | 1].n4), n                        tree[node << 1].n7 + tree[node << 1 | 1].n4); n } n void propagate(int node) { n   if (lazy[node]) { n     lazy[node << 1] ^= 1, lazy[node << 1 | 1] ^= 1; n     swap(tree[node << 1].n4, tree[node << 1].n7); n     swap(tree[node << 1 | 1].n4, tree[node << 1 | 1].n7); n     swap(tree[node << 1].n47, tree[node << 1].n74); n     swap(tree[node << 1 | 1].n47, tree[node << 1 | 1].n74); n   } n   lazy[node] = 0; n } n void update(int node, int l, int r, int i, int j) { n   if (r < i || l > j) return; n   if (i <= l && r <= j) { n     lazy[node] ^= 1; n     swap(tree[node].n4, tree[node].n7); n     swap(tree[node].n47, tree[node].n74); n     return; n   } n   int mid = (l + r) >> 1; n   propagate(node); n   update(node << 1, l, mid, i, j); n   update(node << 1 | 1, mid + 1, r, i, j); n   tree[node].n4 = tree[node << 1].n4 + tree[node << 1 | 1].n4; n   tree[node].n7 = tree[node << 1].n7 + tree[node << 1 | 1].n7; n   tree[node].n47 = max(max(tree[node << 1].n47 + tree[node << 1 | 1].n7, n                            tree[node << 1].n4 + tree[node << 1 | 1].n47), n                        tree[node << 1].n4 + tree[node << 1 | 1].n7); n   tree[node].n74 = max(max(tree[node << 1].n7 + tree[node << 1 | 1].n74, n                            tree[node << 1].n74 + tree[node << 1 | 1].n4), n                        tree[node << 1].n7 + tree[node << 1 | 1].n4); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> q; n   for (int i = 0; i < n; ++i) { n     char c; n     cin >> c; n     arr[i + 1] = c -  0 ; n   } n   build(1, 1, n); n   while (q--) { n     cin >> str; n     if (str ==  count ) { n       cout << tree[1].n47 <<   n ; n     } else { n       int l, r; n       cin >> l >> r; n       update(1, 1, n, l, r); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct FenwickTree { n   int N; n   vector<int> A; n   FenwickTree(int N) : N(N), A(N) {} n   int rsq(int b) { n     int sum = 0; n     for (; b; b -= (b & (-b))) sum += A[b]; n     return sum; n   } n   int rsq(int a, int b) { return rsq(b) - (a == 1000000000 ? 0 : rsq(a - 1)); } n   void adjust(int k, int v) { n     for (; k < A.size(); k += k & -k) A[k] += v; n   } n }; n int n, k, a, b, q; n int main() { n   cin >> n >> k >> a >> b >> q; n   FenwickTree bef(n + 5), aft(n + 5); n   vector<int> befa(n + 1), afta(n + 1); n   int i, j, k1; n   for (int c = 0; c < q; c++) { n     scanf( %d , &i); n     if (i == 1) { n       scanf( %d%d , &j, &k1); n       int befk = min(k1 + befa[j], b); n       int aftk = min(k1 + afta[j], a); n       bef.adjust(j, befk - befa[j]); n       befa[j] = befk; n       aft.adjust(j, aftk - afta[j]); n       afta[j] = aftk; n     } else { n       scanf( %d , &j); n       printf( %d n , bef.rsq(1, j - 1) + aft.rsq(k + j, n)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, s, q; n   cin >> t >> s >> q; n   int ans = 0; n   while (s < t) { n     s = s * q; n     ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n inline int read() { n   static char ch; n   while (ch = getchar(), ch <  0  || ch >  9 ) n     ; n   int res = ch - 48; n   while (ch = getchar(), ch >=  0  && ch <=  9 ) res = res * 10 + ch - 48; n   return res; n } n inline unsigned int Rand() { n   static unsigned int x = 998666131; n   return x += x << 2 | 1; n } n const int N = 5001, M = 1e5 + 5, T = 2e6; n int n, m, Q, ans[M]; n bool vis[M]; n struct QueryT { n   int op, x, y, dir, len, lab; n   inline void Reset1(const int &_op, const int &_dir, const int &_x, n                      const int &_y, const int &_len) { n     op = _op; n     dir = _dir; n     x = _x; n     y = _y; n     len = _len; n   } n   inline void Reset2(const int &_op, const int &_dir, const int &_x, n                      const int &_y, const int &_lab) { n     op = _op; n     dir = _dir; n     x = _x; n     y = _y; n     lab = _lab; n     len = x + y; n   } n   inline bool operator<=(const QueryT &rhs) { n     if (x != rhs.x) return x < rhs.x; n     return op <= rhs.op; n   } n } qry[M * 4], tmp[M * 4]; n struct node { n   node *lc, *rc; n   int val, sze; n   unsigned int pri; n   inline void Update() { sze = (lc ? lc->sze : 0) + (rc ? rc->sze : 0) + 1; } n } used[T], *pool = used, *unused[T], **top = unused; n node *rt[5][N]; n inline node *new_node() { n   node *res; n   if (top != unused) n     res = *--top; n   else n     res = pool++; n   res->lc = res->rc = NULL; n   return res; n } n inline void del_node(node *&u) { n   if (!u) return; n   del_node(u->lc); n   del_node(u->rc); n   *top++ = u; n   u = NULL; n } n inline void Zig(node *&u) { n   node *v = u->lc; n   u->lc = v->rc; n   v->rc = u; n   v->sze = u->sze; n   u->Update(); n   u = v; n } n inline void Zag(node *&u) { n   node *v = u->rc; n   u->rc = v->lc; n   v->lc = u; n   v->sze = u->sze; n   u->Update(); n   u = v; n } n inline void Insert(node *&u, const int &val) { n   if (!u) { n     u = new_node(); n     u->val = val; n     u->sze = 1; n     u->pri = Rand(); n     return; n   } n   ++u->sze; n   if (val <= u->val) { n     Insert(u->lc, val); n     if (u->lc->pri < u->pri) Zig(u); n   } else { n     Insert(u->rc, val); n     if (u->rc->pri < u->pri) Zag(u); n   } n } n inline int moreQuery(node *u, const int &val) { n   if (!u) return 0; n   if (u->val < val) return moreQuery(u->rc, val); n   return (u->rc ? u->rc->sze + 1 : 1) + moreQuery(u->lc, val); n } n inline void add(node **R, int x, const int &val) { n   for (; x <= n; x += x & -x) Insert(R[x], val); n } n inline int Query(node **R, int x, const int &val) { n   int res = 0; n   for (; x > 0; x -= x & -x) res += moreQuery(R[x], val); n   return res; n } n inline void Clear(node **R, int x) { n   for (; x <= n; x += x & -x) del_node(R[x]); n } n inline void cdqSolve(const int &l, const int &r) { n   if (l == r) return; n   int mid = l + r >> 1; n   cdqSolve(l, mid); n   cdqSolve(mid + 1, r); n   int ql = l, qr = mid + 1; n   for (int i = (l); i <= (r); ++i) { n     if (qr > r || (ql <= mid && qry[ql] <= qry[qr])) { n       tmp[i] = qry[ql++]; n       if (tmp[i].op == 1) add(rt[tmp[i].dir], tmp[i].y, tmp[i].len); n     } else { n       tmp[i] = qry[qr++]; n       if (tmp[i].op == 2) n         ans[tmp[i].lab] += Query(rt[tmp[i].dir], tmp[i].y, tmp[i].len); n     } n   } n   for (int i = (l); i <= (r); ++i) { n     qry[i] = tmp[i]; n     if (qry[i].op == 1) Clear(rt[qry[i].dir], qry[i].y); n   } n } n int main() { n   n = read(); n   Q = read(); n   int op, x, y, len, dir; n   for (int i = (1); i <= (Q); ++i) { n     op = read(); n     if (op == 1) { n       dir = read(), x = read(), y = read(), len = read(); n       if (dir == 2) y = n - y + 1; n       if (dir == 3) x = n - x + 1; n       if (dir == 4) x = n - x + 1, y = n - y + 1; n       len += x + y; n       qry[++m].Reset1(op, dir, x, y, len); n     } else { n       x = read(), y = read(); n       qry[++m].Reset2(op, 1, x, y, i); n       qry[++m].Reset2(op, 2, x, n - y + 1, i); n       qry[++m].Reset2(op, 3, n - x + 1, y, i); n       qry[++m].Reset2(op, 4, n - x + 1, n - y + 1, i); n       vis[i] = true; n     } n   } n   cdqSolve(1, m); n   for (int i = (1); i <= (Q); ++i) n     if (vis[i]) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7, A = 1e5 + 2; n bool v[20][20]; n long long dp[20][1 << 20]; n int n, m, start = 0; n long long solve(int node, int mask) { n   if (~dp[node][mask]) return dp[node][mask]; n   long long ret = 0; n   if (v[node][start] && __builtin_popcount(mask) > 2) ret++; n   for (int i = start + 1; i < n; i++) { n     if (!(mask >> i & 1) && v[node][i]) ret += solve(i, mask | (1 << i)); n   } n   return dp[node][mask] = ret; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     v[x][y] = v[y][x] = 1; n   } n   long long ans = 0; n   memset(dp, -1, sizeof dp); n   for (int i = 0; i < n; i++) start = i, ans += solve(i, 1 << i); n   cout << ans / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n int n, m, q, t, r, c, x; n int R[N], C[N], ans[N][N]; n pair<int, pair<int, int> > arr[N][N], temp[N]; n int main() { n   cin >> n >> m >> q; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       arr[i][j].second.first = i; n       arr[i][j].second.second = j; n     } n   while (q--) { n     scanf( %d , &t); n     if (t == 1) { n       scanf( %d , &r); n       r--; n       for (int i = 0; i < m; i++) temp[(i - 1 + m) % m] = arr[r][i]; n       for (int i = 0; i < m; i++) arr[r][i] = temp[i]; n     } n     if (t == 2) { n       scanf( %d , &c); n       c--; n       for (int i = 0; i < n; i++) temp[(i - 1 + n) % n] = arr[i][c]; n       for (int i = 0; i < n; i++) arr[i][c] = temp[i]; n     } n     if (t == 3) { n       scanf( %d %d %d , &r, &c, &x); n       r--; n       c--; n       arr[r][c].first = x; n     } n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       ans[arr[i][j].second.first][arr[i][j].second.second] = arr[i][j].first; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) printf( %d  , ans[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const int N = 1e5 + 10; n vector<int> v[N]; n void init() { n   int tot = 0; n   for (int i = 1; i < N; i++) { n     for (int j = i; j < N; j += i) { n       v[j].push_back(i); n     } n     tot = max(tot, (int)v[i].size()); n   } n } n int C(int n, int m) { n   int U = 1, D = 1; n   while (m) { n     U *= n++; n     D *= m--; n   } n   return U / D; n } n void solve(int n) { n   int ret = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= n; j++) { n       ret++; n     } n   } n   cout << n <<     << ret << endl; n } n int main() { n   map<tuple<int, int, int>, int> mp; n   for (int i = 1; i < 8; i++) { n     if (((i >> 0) & 1) == 0) continue; n     for (int j = 1; j < 8; j++) { n       if (((j >> 1) & 1) == 0) continue; n       for (int k = 1; k < 8; k++) { n         if (((k >> 2) & 1) == 0) continue; n         int a = i, b = j, c = k; n         if (a > b) swap(a, b); n         if (b > c) swap(b, c); n         if (a > b) swap(a, b); n         mp[{a, b, c}] = 1; n       } n     } n   } n   init(); n   int ncase; n   scanf( %d , &ncase); n   while (ncase--) { n     map<int, int> mc; n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     vector<int> v1, v2; n     merge(v[a].begin(), v[a].end(), v[b].begin(), v[b].end(), n           back_inserter(v1)); n     merge(v[c].begin(), v[c].end(), v1.begin(), v1.end(), back_inserter(v2)); n     v2.erase(unique(v2.begin(), v2.end()), v2.end()); n     int sz[8]; n     memset((sz), (0), sizeof(sz)); n     for (auto &x : v2) { n       int f = 0; n       if (a % x == 0) f |= 1; n       if (b % x == 0) f |= 2; n       if (c % x == 0) f |= 4; n       sz[f]++; n     } n     int ans = 0; n     for (auto &t : mp) { n       int a, b, c; n       tie(a, b, c) = t.first; n       if (a == c) n         ans += C(sz[a], 3); n       else if (a == b) n         ans += C(sz[a], 2) * sz[c]; n       else if (b == c) n         ans += C(sz[b], 2) * sz[a]; n       else n         ans += sz[a] * sz[b] * sz[c]; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 5; n int n, a[MAX], bk[3]; n int ans[MAX]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i], bk[a[i]]++; n   sort(a + 1, a + n + 1, greater<int>()); n   int cur = 0, top = 0, tar; n   for (int i = 1; i <= n; i++) { n     cur += a[i]; n     bk[a[i]]--; n     ans[++top] = a[i]; n     if (cur == 2) { n       tar = i; n       break; n     } n   } n   if (bk[1] > 0) bk[1]--, cur += 1, ans[++top] = 1, n--; n   for (int i = tar + 1; i <= n; i++) { n     cur += a[i]; n     bk[a[i]]--; n     ans[++top] = a[i]; n   } n   for (int i = 1; i <= top; i++) printf( %d%c , ans[i], i == top ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class U> n ostream& operator<<(ostream& os, const pair<T, U>& p) { n   os <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   os <<  { ; n   for (int i = 0; i < (int)v.size(); i++) { n     if (i) os <<  ,  ; n     os << v[i]; n   } n   os <<  } ; n   return os; n } n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const int mod = 1e9 + 7; n const int N = 2005; n inline int add(int x, int y) { n   x += y; n   if (x >= mod) x -= mod; n   return x; n } n inline int sub(int x, int y) { n   x -= y; n   if (x < 0) x += mod; n   return x; n } n inline int mul(int x, int y) { return (x * 1ll * y) % mod; } n inline int powr(int a, long long b) { n   int x = 1 % mod; n   while (b) { n     if (b & 1) x = mul(x, a); n     a = mul(a, a); n     b >>= 1; n   } n   return x; n } n inline int inv(int a) { return powr(a, mod - 2); } n char s[N][N]; n int dp[2][N][N], dp2[2][N][N]; n int num[2][N][N]; n int main() { n   int n = 2000, m = 2000; n   scanf( %d , &(n)), scanf( %d , &(m)); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s[i] + 1); n   } n   for (int i = n; i >= 1; i--) { n     for (int j = m; j >= 1; j--) { n       num[0][i][j] = num[0][i][j + 1] + (s[i][j] ==  R ); n       num[1][i][j] = num[1][i + 1][j] + (s[i][j] ==  R ); n       if (i == n) dp[0][i][j] = (num[0][i][j] == 0); n       if (j == m) dp[1][i][j] = (num[1][i][j] == 0); n       int mxj = m - num[0][i][j]; n       int mxi = n - num[1][i][j]; n       if (i != n) dp[0][i][j] = sub(dp2[1][i + 1][j], dp2[1][i + 1][mxj + 1]); n       if (j != m) dp[1][i][j] = sub(dp2[0][i][j + 1], dp2[0][mxi + 1][j + 1]); n       dp2[0][i][j] = add(dp[0][i][j], dp2[0][i + 1][j]); n       dp2[1][i][j] = add(dp[1][i][j], dp2[1][i][j + 1]); n     } n   } n   assert(dp[0][1][1] == dp[1][1][1]); n   printf( %d n , dp[0][1][1]); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char mat[100][100]; n char c; n vector<int> us; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> mat[i]; n   for (int i = 0; i < n; i++) { n     us.clear(); n     for (int j = 0; j < m; j++) { n       if (mat[i][j] ==  # ) us.push_back(j); n     } n     if (us.size() > 0) { n       for (int j = 0; j < n; j++) { n         c = mat[j][us[0]]; n         for (int k = 1; k < (int)us.size(); k++) { n           if (mat[j][us[k]] != c) { n             cout <<  No ; n             return 0; n           } n         } n       } n     } n   } n   for (int i = 0; i < m; i++) { n     us.clear(); n     for (int j = 0; j < n; j++) { n       if (mat[j][i] ==  # ) us.push_back(j); n     } n     if (us.size() > 0) { n       for (int j = 0; j < m; j++) { n         c = mat[us[0]][j]; n         for (int k = 1; k < (int)us.size(); k++) { n           if (mat[us[k]][j] != c) { n             cout <<  No ; n             return 0; n           } n         } n       } n     } n   } n   cout <<  Yes ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<long long int> dif; n vector<long long int> vec; n long long int a[1000000], b[1000000]; n int main() { n   long long int x, d, contador = 0, i; n   long long int pot, exp; n   cin >> x >> d; n   while (x > 0) { n     x++; n     pot = 1; n     exp = 0; n     while (pot <= x) { n       pot = pot * 2; n       exp++; n     } n     for (i = 0; i < exp - 1; i++) { n       vec.push_back(d * contador + 1); n     } n     contador++; n     x = x - pot / 2; n   } n   cout << vec.size() <<   n ; n   for (i = 0; i < vec.size(); i++) cout << vec[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long pw10[] = {1, 10, 100, 1000, 10000, 100000}; n long long K, F[7], Q, dp[7][2000000]; n signed main() { n   cin >> K; n   for (long long i = 1; i <= 6; ++i) cin >> F[i]; n   memset(dp, -0x3f, sizeof(dp)); n   dp[0][0] = 0; n   for (long long i = 1; i <= 6; ++i) { n     for (long long j = 0; j < 3 * pw10[i - 1]; ++j) { n       deque<pair<long long, long long> > q; n       for (long long k = j, cnt = 0; k < 1e6; k += 3 * pw10[i - 1], ++cnt) { n         long long val = dp[i - 1][k] - cnt * F[i]; n         while (q.size() && q.back().first <= val) q.pop_back(); n         q.push_back(make_pair(val, cnt)); n         if (cnt - q.front().second > 3 * K) q.pop_front(); n         dp[i][k] = max(dp[i][k], cnt * F[i] + q.front().first); n         long long pos = 0; n         while (cnt - q[pos].second > 3 * (K - 1)) ++pos; n         long long v = cnt * F[i] + q[pos].first; n         for (long long t = 0; t < 10; ++t) n           dp[i][k + t * pw10[i - 1]] = max(dp[i][k + t * pw10[i - 1]], v); n       } n     } n   } n   for (cin >> Q; Q; --Q) { n     long long x; n     cin >> x; n     cout << max(0ll, dp[6][x]) <<   n ; n   } n   return 0; n } n