#include <bits/stdc++.h> n using namespace std; n char string_in_buffer[(int)2e5]; n void fast_scan(int &first) { scanf( %d , &first); } n void fast_scan(long long &first) { scanf( %lld , &first); } n void fast_scan(unsigned long long &first) { scanf( %llu , &first); } n void fast_scan(double &first) { scanf( %lf , &first); } n void fast_scan(long double &first) { scanf( %Lf , &first); } n void fast_scan(char &first) { n   scanf( %c , &first); n   if (first ==   n ) { n     fast_scan(first); n   } n } n void fast_scan(string &first) { n   scanf( %s , string_in_buffer); n   first = string(string_in_buffer); n } n template <class TFirst, class TSecond> n void fast_scan(pair<TFirst, TSecond> &p) { n   fast_scan(p.first); n   fast_scan(p.second); n } n template <class T> n void fast_scan(vector<T> &v) { n   for (auto &first : v) fast_scan(first); n } n void fast_print(const int &first) { printf( %d , first); } n void fast_print(const long long &first) { printf( %lld , first); } n void fast_print(const unsigned long long &first) { printf( %llu , first); } n void fast_print(const double &first) { printf( %.15lf , first); } n void fast_print(const long double &first) { printf( %.15Lf , first); } n void fast_print(const char &first) { printf( %c , first); }; n void fast_print(const string &first) { printf( %s , first.c_str()); } n template <class TFirst, class TSecond> n void fast_print(const pair<TFirst, TSecond> &p) { n   fast_print(p.first); n   fast_print(   ); n   fast_print(p.second); n } n template <class T> n void fast_print(const vector<T> &v) { n   if (v.empty()) return; n   fast_print(v[0]); n   for (int i = 1; i < v.size(); i++) { n     fast_print(   ); n     fast_print(v[i]); n   } n } n template <class T> n void fast_print(const vector<vector<T>> &v) { n   if (v.empty()) return; n   fast_print(v[0]); n   for (int i = 1; i < v.size(); i++) { n     fast_print(  n ); n     fast_print(v[i]); n   } n } n using namespace std; n namespace smart_io { n string print_start =   ; n string sep =    ; n bool first_print = false; n void precall_print() { n   fast_print(print_start); n   print_start =   n ; n   first_print = true; n } n }  // namespace smart_io n template <class T> n ostream &operator,(ostream &os, const T &object) { n   if (!smart_io::first_print) { n     fast_print(smart_io::sep); n   } else { n     smart_io::first_print = false; n   } n   fast_print(object); n   return os; n } n template <class T> n istream &operator,(istream &is, T &object) { n   fast_scan(object); n   return is; n } n namespace typedefs {} n namespace numbers_operation { n template <class T> n T floor_mod(T a, T b) { n   if (a % b == 0) return 0; n   if (a >= 0 && b >= 0) return a % b; n   if (a <= 0 && b <= 0) return a % b; n   return abs(b) - (abs(a) % abs(b)); n } n }  // namespace numbers_operation n using namespace numbers_operation; n using namespace typedefs; n int n; n vector<string> v; n bool expand(string &base, int i, string t) { n   for (int j = 0; j < ((int)t.size()); j++) { n     if (j + i >= ((int)base.size())) { n       base += t[j]; n     } else { n       if (base[j + i] != t[j]) { n         return false; n       } n     } n   } n   return true; n } n signed main(signed argc, char *argv[]) { n   { n     cin, n; n     v.resize(n); n     cin, v; n   } n   { n     for (string s : v) { n       if (((int)set<char>(s.begin(), s.end()).size()) != ((int)s.size())) { n         smart_io::precall_print(); n         cout,  NO ; n         return 0; n       } n     } n   } n   { n     while (true) { n       vector<int> not_first_vec(26); n       vector<int> cnt(26); n       for (int i = 0; i < n; i++) { n         vector<int> local(26); n         for (int j = 0; j < ((int)v[i].size()); j++) { n           local[v[i][j] -  a ]++; n         } n         for (int j = 0; j < 26; j++) { n           if (local[j] > 1) { n             smart_io::precall_print(); n             cout,  NO ; n             return 0; n           } n         } n         cnt[v[i][0] -  a ]++; n         for (int j = 1; j < ((int)v[i].size()); j++) { n           not_first_vec[v[i][j] -  a ]++; n           cnt[v[i][j] -  a ]++; n         } n       } n       char c =  ! ; n       for (int i = 0; i < 26; i++) { n         if (not_first_vec[i] <= 1 && cnt[i] > 1) { n           c = i +  a ; n         } n       } n       if (c ==  ! ) break; n       vector<pair<int, int>> where; n       for (int i = 0; i < n; i++) { n         for (int j = 0; j < ((int)v[i].size()); j++) { n           if (v[i][j] == c) { n             where.emplace_back(i, j); n           } n         } n       } n       if (where.empty()) continue; n       sort(where.begin(), where.end(), [](pair<int, int> a, pair<int, int> b) { n         return ((int)v[a.first].size()) < ((int)v[b.first].size()); n       }); n       int not_first = 0; n       pair<int, int> f = where[0]; n       for (int i = 0; i < ((int)where.size()); i++) { n         if (where[i].second != 0) { n           not_first++; n           f = where[i]; n         } n       } n       if (not_first > 1) { n         smart_io::precall_print(); n         cout,  NO ; n         return 0; n       } n       string base = v[f.first]; n       for (int i = 0; i < ((int)where.size()); i++) { n         if (where[i].first == f.first) continue; n         if (!expand(base, f.second, v[where[i].first])) { n           smart_io::precall_print(); n           cout,  NO ; n           return 0; n         } n       } n       vector<bool> blocked(n, false); n       for (pair<int, int> p : where) { n         blocked[p.first] = true; n       } n       vector<string> _new; n       for (int i = 0; i < n; i++) { n         if (!blocked[i]) { n           _new.push_back(v[i]); n         } n       } n       _new.push_back(base); n       n = ((int)_new.size()); n       v = _new; n       int tt = 0; n     } n   } n   {} { n     sort(v.begin(), v.end()); n     string rez =   ; n     for (string s : v) { n       rez += s; n     } n     if (((int)set<char>(rez.begin(), rez.end()).size()) == ((int)rez.size())) { n       smart_io::precall_print(); n       cout, rez; n     } else { n       smart_io::precall_print(); n       cout,  NO ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int heap(int a, int t) { n   int tmp; n   if (a > t) swap(a, t); n   if (t % a == 0) return a; n   return heap(a, t % a); n } n int main() { n   string x, y; n   cin >> x >> y; n   bool f = true; n   for (int i = 0; i < x.size(); i++) { n     if (x[i] != y[x.size() - 1 - i]) f = false; n   } n   if (f) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, q, l; n int A[N], sum[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , A + i); n     sum[i] = sum[i - 1] + A[i]; n   } n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &l); n     printf( %d n , lower_bound(sum, sum + n + 1, l) - sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 3e5 + 5; n long long a[maxn], ps[maxn], mark[maxn]; n set<pair<long long, long long> > st; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long n, k; n   cin >> n >> k; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   ps[n] = a[n]; n   st.insert(pair<long long, long long>(ps[n], n)); n   for (long long i = n - 1; i >= 1; i--) { n     ps[i] = ps[i + 1] + a[i]; n     if (i == 1) continue; n     st.insert(pair<long long, long long>(ps[i], i)); n   } n   for (long long i = 1; i < k; i++) { n     pair<long long, long long> p = *st.rbegin(); n     mark[p.second]++; n     st.erase(p); n   } n   long long c = 1, ans = 0; n   for (long long i = 1; i <= n; i++) { n     if (mark[i]) { n       c++; n     } n     ans += c * a[i]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200010; n char A[MAXN]; n char B[MAXN]; n int n; n void open() { n   freopen( B.in ,  r , stdin); n   freopen( B.out ,  w , stdout); n } n void close() { n   fclose(stdin); n   fclose(stdout); n } n void init() { n   scanf( %s , A + 1); n   scanf( %s , B + 1); n } n bool equal(int l1, int r1, int l2, int r2) { n   int flag = 1; n   for (int i = 0, END = r1 - l1; i <= END; i++) n     if (A[i + l1] != B[i + l2]) { n       flag = 0; n       break; n     } n   if (flag) n     return true; n   else if ((r1 - l1 + 1) & 1) n     return false; n   int mid1 = (l1 + r1) / 2, mid2 = (l2 + r2) / 2; n   if (equal(l1, mid1, l2, mid2)) { n     if (equal(mid1 + 1, r1, mid2 + 1, r2)) return true; n   } n   if (equal(mid1 + 1, r1, l2, mid2)) { n     if (equal(l1, mid1, mid2 + 1, r2)) return true; n   } n } n int main() { n   init(); n   int n1 = strlen(A + 1), n2 = strlen(B + 1); n   if (n1 != n2) { n     printf( NO n ); n   } else { n     if (equal(1, n1, 1, n2)) n       printf( YES n ); n     else n       printf( NO n ); n   } n   close(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y; n   int num; n   int mark; n }; n point p[100010]; n int ans[100010]; n point pt; n int sum1, sum2; n bool cmp(point a, point b) { n   return (a.x - pt.x) * (b.y - pt.y) - (a.y - pt.y) * (b.x - pt.x) < 0; n } n bool cmp2(point a, point b) { return a.y != b.y ? a.y < b.y : a.x < b.x; } n void work(int l, int r) { n   if (r < l) return; n   int maxi = 0; n   int marki = 0; n   maxi = min_element(p + l, p + r + 1, cmp2) - p; n   swap(p[maxi], p[l]); n   pt = p[l]; n   int k; n   sort(p + l + 1, p + r + 1, cmp); n   sum1 = sum2 = 0; n   for (k = r; !(pt.mark != p[k].mark && sum1 == sum2); k--) n     if (p[k].mark == pt.mark) n       sum1++; n     else n       sum2++; n   marki = k; n   if (pt.mark) n     ans[p[k].num] = pt.num; n   else n     ans[pt.num] = p[k].num; n   work(l + 1, marki - 1); n   work(marki + 1, r); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = (1); i <= (2 * n); ++i) { n     cin >> p[i].x >> p[i].y; n     p[i].mark = (i > n); n     p[i].num = i > n ? i - n : i; n   } n   work(1, 2 * n); n   for (int i = (1); i <= (n); ++i) { n     cout << ans[i] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<vector<pair<int, int>>> g; n vector<int> p; n vector<bool> was; n vector<int> sz; n long long res; n void calcSz(int v, int par = -1) { n   sz[v] = 1; n   for (int i = 0; i < (int)g[v].size(); ++i) { n     int to = g[v][i].first; n     if (to != par && !was[to]) { n       calcSz(to, v); n       sz[v] += sz[to]; n     } n   } n } n int findCentroid(int v, int n, int par = -1) { n   for (int i = 0; i < (int)g[v].size(); ++i) { n     int to = g[v][i].first; n     if (to != par && !was[to] && 2 * sz[to] > n) { n       return findCentroid(to, n, v); n     } n   } n   return v; n } n int getInc(int v, int edge, int par = -1) { n   int res = 1; n   for (int i = 0; i < (int)g[v].size(); ++i) { n     int to = g[v][i].first; n     if (to != par && !was[to] && g[v][i].second >= edge) { n       res += getInc(to, g[v][i].second, v); n     } n   } n   return res; n } n int getDec(int v, int edge, int par = -1) { n   int res = 1; n   for (int i = 0; i < (int)g[v].size(); ++i) { n     int to = g[v][i].first; n     if (to != par && !was[to] && g[v][i].second <= edge) { n       res += getDec(to, g[v][i].second, v); n     } n   } n   return res; n } n int build(int v) { n   calcSz(v); n   v = findCentroid(v, sz[v]); n   was[v] = true; n   int inc0 = 0; n   int inc1 = 0; n   int dec0 = 0; n   int dec1 = 0; n   for (int i = 0; i < (int)g[v].size(); ++i) { n     int to = g[v][i].first; n     if (!was[to]) { n       int inc = getInc(to, g[v][i].second, v); n       int dec = getDec(to, g[v][i].second, v); n       if (g[v][i].second == 0) { n         res += inc * 1ll * (dec0 + 1); n         res += dec * 1ll * (inc0 + inc1 + 1); n         inc0 += inc; n         dec0 += dec; n       } else { n         res += inc * 1ll * (dec0 + dec1 + 1); n         res += dec * 1ll * (inc1 + 1); n         inc1 += inc; n         dec1 += dec; n       } n     } n   } n   for (int i = 0; i < (int)g[v].size(); ++i) { n     int to = g[v][i].first; n     if (!was[to]) { n       p[build(to)] = v; n     } n   } n   return v; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   g.resize(n); n   p.resize(n); n   was.resize(n); n   sz.resize(n); n   for (int i = 0; i < n - 1; ++i) { n     int a, b, c; n     cin >> a >> b >> c; n     --a; n     --b; n     g[a].push_back({b, c}); n     g[b].push_back({a, c}); n   } n   build(0); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> arr[100]; n int res[100]; n int main() { n   int N, W; n   scanf( %d%d , &N, &W); n   for (int i = 0; i < N; i++) { n     scanf( %d , &arr[i].first); n     arr[i].second = i; n   } n   sort(arr, arr + N); n   for (int i = 0; i < N; i++) { n     if (W < (arr[i].first + 1) / 2) { n       printf( -1 n ); n       return 0; n     } n     W -= (arr[i].first + 1) / 2; n     res[arr[i].second] = (arr[i].first + 1) / 2; n   } n   int p = N - 1; n   while (W > 0) { n     int delta = min(W, arr[p].first - res[arr[p].second]); n     W -= delta; n     res[arr[p].second] += delta; n     p--; n   } n   for (int i = 0; i < N; i++) printf( %d  , res[i]); n   putchar(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 10000000; n const int MAX = 150; n string s; n int found(string aux) { n   int ans = 0; n   int cnt = 0; n   for (int i = 0; i < s.size(); i++) { n     for (int j = 0, k = i; j < aux.size(); j++, k++) { n       if (k == s.size()) break; n       if (aux[j] == s[k]) n         cnt++; n       else { n         cnt = 0; n         break; n       } n     } n     if (cnt == aux.size()) ans++; n   } n   return ans; n } n int main() { n   cin >> s; n   int ans = 0; n   ans += found( Danil ); n   ans += found( Olya ); n   ans += found( Slava ); n   ans += found( Ann ); n   ans += found( Nikita ); n   if (ans == 1) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> a[1000005]; n bool fl1, fl2, fl3, fl4; n long long dist(pair<long long, long long> a, pair<long long, long long> b) { n   return abs(a.first - b.first) + abs(a.second - b.second); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long i, j, k, l, m, n, t, r, cnt; n   long long flag = 0; n   long long an = 0, ans = 0; n   t = 1; n   while (t--) { n     cin >> n; n     long long x, y; n     for (i = 0; i < n; i++) { n       cin >> x >> y; n       a[i] = make_pair(x, y); n     } n     long long xmi, xma, yma, ymi; n     xmi = 1LL * 1e18; n     ymi = 1LL * 1e18; n     yma = -1LL * 1e18; n     xma = -1LL * 1e18; n     for (i = 0; i < n; i++) { n       xmi = min(xmi, a[i].first); n       xma = max(xma, a[i].first); n       ymi = min(ymi, a[i].second); n       yma = max(yma, a[i].second); n     } n     ans = 2LL * (xma - xmi + yma - ymi); n     vector<pair<long long, long long> > b; n     for (i = 0; i < n; i++) { n       if (a[i].first == xmi && !fl1) { n         fl1 = true; n         b.push_back(a[i]); n       } n       if (a[i].first == xma && !fl2) { n         fl2 = true; n         b.push_back(a[i]); n       } n       if (a[i].second == ymi && !fl3) { n         fl3 = true; n         b.push_back(a[i]); n       } n       if (a[i].second == yma && !fl4) { n         fl4 = true; n         b.push_back(a[i]); n       } n     } n     b.push_back(make_pair(xmi, ymi)); n     b.push_back(make_pair(xmi, yma)); n     b.push_back(make_pair(xma, yma)); n     b.push_back(make_pair(xma, ymi)); n     an = -1LL * 1e18; n     for (i = 0; i < n; i++) { n       for (j = 0; j < 4; j++) { n         for (k = 0; k < 4; k++) { n           if (b[j] == a[i] || b[k] == a[i]) continue; n           an = max(an, dist(a[i], b[j]) + dist(b[k], a[i]) + dist(b[j], b[k])); n         } n       } n     } n     cout << an <<    ; n     for (i = 4; i <= n; i++) { n       cout << ans <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char arr[1503][1503]; n int inix, iniy; n bool visit[4503][4504]; n int X[] = {0, 0, 1, -1}; n int Y[] = {1, -1, 0, 0}; n int nn, mm; n int colax[4503 * 4503]; n int colay[4503 * 4503]; n bool dentro(int a, int b) { n   if (a < 0 || b < 0 || a >= nn || b >= mm) return false; n   return true; n } n inline void bfs(int x, int y) { n   visit[x][y] = true; n   int xx, yy; n   int q = 0; n   colax[q] = x; n   colay[q++] = y; n   for (int j = 0; j < q; j++) { n     x = colax[j]; n     y = colay[j]; n     for (int i = 0; i < 4; i++) { n       xx = x + X[i]; n       yy = y + Y[i]; n       if (!visit[(xx + nn) % nn][(yy + mm) % mm] && n           arr[(xx + n) % n][(yy + m) % m] !=  # ) { n         visit[(xx + nn) % nn][(yy + mm) % mm] = true; n         colax[q] = (xx + nn) % nn; n         colay[q++] = (yy + mm) % mm; n       } n     } n   } n } n int main() { n   scanf( %d %d , &n, &m); n   nn = n * 3; n   mm = m * 3; n   for (int i = 0; i < n; i++) scanf( %s , arr[i]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (arr[i][j] ==  S ) { n         inix = i, iniy = j; n       }; n   iniy += m; n   inix += n; n   bfs(inix, iniy); n   for (int i = -1; i < 2; i++) n     for (int j = -1; j < 2; j++) { n       if (i == j && i == 0) continue; n       if (visit[inix + i * n][iniy + j * m]) { n         cout <<  Yes  << endl; n         return 0; n       } n     } n   cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, w, a[105], cnt1 = 0, cnt = 0, b[105], c[105], e[105], colour[105]; n   memset(colour, 0, sizeof colour); n   scanf( %d%d , &n, &w); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     c[i] = a[i]; n     cnt1 = a[i] / 2; n     if (a[i] % 2 == 1) cnt1++; n     b[i] = cnt1; n     cnt += cnt1; n   } n   if (cnt > w) { n     printf( -1 n ); n     return 0; n   } n   if (w > cnt) { n     int dif = w - cnt; n     sort(c + 1, c + n + 1); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         if (c[i] == a[j] && colour[j] == 0) { n           e[i] = j; n           colour[j] = 1; n           break; n         } n       } n     } n     int d = n; n     while (d > 0) { n       if (c[d] % 2 == 1) { n         int k = c[d] / 2 + 1; n         int diff = c[d] - k; n         if (dif > diff) { n           dif -= diff; n           b[e[d]] += diff; n         } else n           b[e[d]] += dif, dif = 0; n       } else { n         int k = c[d] / 2; n         int diff = c[d] - k; n         if (dif > diff) { n           dif -= diff; n           b[e[d]] += diff; n         } else n           b[e[d]] += dif, dif = 0; n       } n       if (dif == 0) break; n       d--; n     } n     if (dif > 0) { n       printf( -1 n ); n       return 0; n     } n     for (int i = 1; i <= n; i++) printf( %d  , b[i]); n   } else if (cnt == w) { n     for (int i = 1; i <= n; i++) printf( %d  , b[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n template <typename T> n inline void read(T &AKNOI) { n   T x = 0, flag = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   AKNOI = flag * x; n } n template <typename T> n inline void cmin(T &x, T y) { n   if (y < x) x = y; n } n template <typename T> n inline void cmax(T &x, T y) { n   if (x < y) x = y; n } n char s[MAXN], t[MAXN]; n int n, m, k[MAXN], len[MAXN]; n int tot, ch[MAXN][26], fail[MAXN], ed[MAXN], ff[MAXN]; n int q[MAXN], head, tail; n vector<int> mtch[MAXN]; n void Insert(int id) { n   int p = 0, l = len[id] = strlen(t + 1); n   for (int i = 1; i <= l; ++i) { n     int x = t[i] -  a ; n     if (!ch[p][x]) { n       ch[p][x] = ++tot; n     } n     p = ch[p][x]; n   } n   ed[p] = id; n } n void GetFail() { n   head = 1, tail = 0; n   for (int i = 0; i < 26; ++i) { n     if (ch[0][i]) { n       q[++tail] = ch[0][i]; n     } n   } n   while (head <= tail) { n     int u = q[head++]; n     for (int i = 0; i < 26; ++i) { n       if (ch[u][i]) { n         int v = ch[u][i]; n         fail[v] = ch[fail[u]][i]; n         ff[v] = (ed[fail[v]] ? fail[v] : ff[fail[v]]); n         q[++tail] = v; n       } else { n         ch[u][i] = ch[fail[u]][i]; n       } n     } n   } n } n void Run() { n   int p = 0; n   for (int i = 1; i <= n; ++i) { n     p = ch[p][s[i] -  a ]; n     int u = (ed[p] ? p : ff[p]); n     for (; u; u = ff[u]) { n       mtch[ed[u]].push_back(i); n     } n   } n } n void init() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   read(m); n   for (int i = 1; i <= m; ++i) { n     read(k[i]); n     scanf( %s , t + 1); n     Insert(i); n   } n   GetFail(); n } n void solve() { n   Run(); n   for (int i = 1; i <= m; ++i) { n     if (mtch[i].size() < k[i]) { n       printf( -1 n ); n     } else { n       int ans = n + 1; n       for (int j = 0, sz = mtch[i].size(); j + k[i] - 1 < sz; ++j) { n         cmin(ans, mtch[i][j + k[i] - 1] - mtch[i][j]); n       } n       printf( %d n , ans + len[i]); n     } n   } n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXINT = 1073741823; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t, n, a, b, x; n   bool win; n   cin >> t; n   for (int q = 0; q < t; q++) { n     cin >> n >> a >> b; n     win = false; n     for (int i = 0; i < a; i++) { n       cin >> x; n       if (x == n) win = true; n     } n     for (int i = 0; i < b; i++) cin >> x; n     cout << (win ?  YES  :  NO ) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const char elem[][4] = { n      H ,   He ,  Li ,  Be ,  B ,    C ,   N ,    O ,   F ,    Ne ,   Na ,  Mg , n      Al ,  Si ,  P ,   S ,   Cl ,   Ar ,  K ,    Ca ,  Sc ,   Ti ,   V ,   Cr , n      Mn ,  Fe ,  Co ,  Ni ,  Cu ,   Zn ,  Ga ,   Ge ,  As ,   Se ,   Br ,  Kr , n      Rb ,  Sr ,  Y ,   Zr ,  Nb ,   Mo ,  Tc ,   Ru ,  Rh ,   Pd ,   Ag ,  Cd , n      In ,  Sn ,  Sb ,  Te ,  I ,    Xe ,  Cs ,   Ba ,  La ,   Ce ,   Pr ,  Nd , n      Pm ,  Sm ,  Eu ,  Gd ,  Tb ,   Dy ,  Ho ,   Er ,  Tm ,   Yb ,   Lu ,  Hf , n      Ta ,  W ,   Re ,  Os ,  Ir ,   Pt ,  Au ,   Hg ,  Tl ,   Pb ,   Bi ,  Po , n      At ,  Rn ,  Fr ,  Ra ,  Ac ,   Th ,  Pa ,   U ,   Np ,   Pu ,   Am ,  Cm , n      Bk ,  Cf ,  Es ,  Fm ,  Md ,   No ,  Lr ,   Rf ,  Db ,   Sg ,   Bh ,  Hs , n      Mt ,  Ds ,  Rg ,  Cn ,  Uut ,  Fl ,  Uup ,  Lv ,  Uus ,  Uuo , n }; n const int MAXN = 18; n string ele; n vector<int> can[110]; n map<string, int> ele_map; n int n, m, max_v, vec[MAXN], tar[MAXN], dp[1 << MAXN][MAXN], n     pre[1 << MAXN][MAXN]; n int main() { n   ele_map.clear(); n   for (int i = 1; i <= 100; ++i) ele_map[elem[i - 1]] = i; n   while (cin >> n >> m) { n     for (int i = 0; i < n; ++i) { n       cin >> ele; n       vec[i] = ele_map[ele]; n     } n     max_v = 0; n     for (int i = 0; i < m; ++i) { n       cin >> ele; n       tar[i] = ele_map[ele]; n       max_v = max(max_v, tar[i]); n     } n     for (int i = 0; i <= max_v; ++i) can[i].clear(); n     for (int mask = 0; mask < (1 << n); ++mask) { n       int sum = 0; n       for (int i = 0; i < n; ++i) { n         if (mask & (1 << i)) sum += vec[i]; n       } n       if (sum <= max_v) can[sum].push_back(mask); n     } n     memset(dp, 0, sizeof(dp)); n     dp[0][0] = 1; n     for (int mask = 0; mask < (1 << n); ++mask) { n       for (int i = 0; i < m; ++i) { n         if (dp[mask][i]) { n           int v = tar[i]; n           for (int j = 0; j < can[v].size(); ++j) { n             int nmask = can[v][j]; n             if ((mask & nmask) == 0) { n               dp[mask | nmask][i + 1] = 1; n               pre[mask | nmask][i + 1] = mask; n             } n           } n         } n       } n     } n     int mask = (1 << n) - 1; n     if (dp[mask][m] == 0) { n       puts( NO ); n     } else { n       puts( YES ); n       for (int i = m; i > 0; --i) { n         vector<int> add; n         int nmask = mask ^ pre[mask][i]; n         for (int j = 0; j < n; ++j) { n           if (nmask & (1 << j)) add.push_back(j); n         } n         for (int j = 0; j < add.size(); ++j) { n           if (j) putchar( + ); n           printf( %s , elem[vec[add[j]] - 1]); n         } n         printf( ->%s n , elem[tar[i - 1] - 1]); n         mask = pre[mask][i]; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100005; n int N; n bool vis[MAX]; n vector<pair<int, bool> > neigh[MAX]; n bool islucky(int a) { n   while (a > 0) { n     if (a % 10 != 4 and a % 10 != 7) return false; n     a /= 10; n   } n   return true; n } n long long p3(long long N) { return (N) * (N - 1) * (N - 2); } n long long solve(int a) { n   vector<int> q; n   int qnum = 0; n   q.push_back(a); n   while (qnum < q.size()) { n     int cur = q[qnum]; n     vis[cur] = true; n     for (int i = 0; i < neigh[cur].size(); i++) { n       if (neigh[cur][i].second) continue; n       int cneigh = neigh[cur][i].first; n       if (vis[cneigh]) continue; n       q.push_back(cneigh); n     } n     qnum++; n   } n   long long sz = q.size(); n   return (2 * (sz) * (sz - 1) * ((long long)N - sz) + p3(sz)); n } n int main() { n   cin >> N; n   for (int i = 0; i < N - 1; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     a--; n     b--; n     bool il = islucky(c); n     neigh[a].push_back(pair<int, bool>(b, il)); n     neigh[b].push_back(pair<int, bool>(a, il)); n   } n   long long ans = p3(N); n   for (int i = 0; i < MAX; i++) vis[i] = false; n   for (int i = 0; i < N; i++) { n     if (vis[i]) continue; n     ans -= solve(i); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, l, r, ql, qr; n int w[100010]; n int main() { n   cin >> n >> l >> r >> ql >> qr; n   for (int i = 1; i <= n; ++i) cin >> w[i]; n   int f[100010]; n   f[0] = 0; n   for (int i = 1; i <= n; ++i) f[i] = f[i - 1] + w[i]; n   int ans = INT_MAX; n   for (int i = 0; i <= n; ++i) { n     int v = f[i] * l + (f[n] - f[i]) * r; n     if (i < n / 2) n       v += qr * max(0, (n - 2 * i - 1)); n     else n       v += ql * max(0, (2 * i - n - 1)); n     ans = min(ans, v); n   } n   cout << ans; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, i, j, x, y, a[179000]; n int main() { n   for (cin >> n >> x >> y, i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= n; i++) { n     for (j = max(1, i - x); j <= min(n, i + y); j++) n       if (i != j && a[j] <= a[i]) break; n     if (j > min(n, i + y)) break; n   } n   cout << i; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   double arr[n]; n   double temp = 0; n   for (int i = 0; i < n; i++) { n     cin >> temp; n     arr[i] = temp; n   } n   long long sum = 0; n   double fractpart, intpart; n   int arr2[n]; n   for (int i = 0; i < n; i++) { n     arr2[i] = ((int)floor(arr[i])); n     sum += arr2[i]; n   } n   int count = -sum; n   int total = 0, index = 0; n   while (count != 0) { n     if (arr[index] - floor(arr[index]) < 0.000001) { n       index++; n       continue; n     } n     count--; n     arr2[index]++; n     index++; n   } n   for (int i = 0; i < n; i++) { n     cout << arr2[i] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n string A, B; n int a[N], b[N]; n int main() { n   while (cin >> A >> B) { n     int len1 = A.size(), len2 = B.size(); n     for (int i = 0; i <= len1 - 1; i++) a[i] = A[i] -  0 ; n     for (int i = 0; i <= len2 - 1; i++) b[i] = B[i] -  0 ; n     int sum = 0; n     for (int i = 0; i <= len2 - 1; i++) sum ^= (a[i] ^ b[i]); n     int ans = sum == 0; n     for (int i = 1; i <= len1 - len2; i++) { n       sum ^= a[i - 1]; n       sum ^= a[i - 1 + len2]; n       ans += sum == 0; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( test.in ); n struct intr { n   int l, r, id; n }; n intr q[200005]; n int p, n, qq; n long long sol; n int a[200005]; n long long rez[200005]; n int mp[5 * 200005]; n bool cmp(intr &q1, intr &q2) { n   return (q1.r / p < q2.r / p || (q1.r / p == q2.r / p && q1.l < q2.l)); n } n void add(int x) { n   int v = mp[x]; n   sol = sol - 1LL * v * v * x; n   sol = sol + 1LL * (v + 1) * (v + 1) * x; n   mp[x]++; n } n void del(int x) { n   int v = mp[x]; n   sol = sol - 1LL * v * v * x; n   if (v >= 1) { n     sol = sol + 1LL * (v - 1) * (v - 1) * x; n     mp[x]--; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int i, lx, rx; n   cin >> n >> qq; n   for (i = 1; i <= n; i++) cin >> a[i]; n   p = sqrt(n); n   for (i = 1; i <= qq; i++) { n     cin >> q[i].l >> q[i].r; n     q[i].id = i; n   } n   sort(q + 1, q + qq + 1, cmp); n   lx = 1; n   rx = 0; n   sol = 0; n   for (i = 1; i <= qq; i++) { n     while (q[i].r > rx) add(a[++rx]); n     while (q[i].r < rx) del(a[rx--]); n     while (q[i].l > lx) del(a[lx++]); n     while (q[i].l < lx) add(a[--lx]); n     rez[q[i].id] = sol; n   } n   for (i = 1; i <= qq; i++) cout << rez[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int vis1[1000000]; n long long int ara1[100005], ara2[100005]; n map<long long int, long long int> vis; n map<long long int, long long int> str2; n priority_queue<long long int> pq; n vector<long long int> vec; n int main() { n   long long int a, b, c = 0, sum = 0, n; n   scanf( %lld , &n); n   for (long long int i = 1; i <= n; i++) { n     scanf( %lld , &a); n     long long int mx = 0; n     string str; n     cin >> str; n     c = -1; n     for (long long int i = 0; i < a; i++) { n       if (str[i] !=  A  && c != -1) { n         c++; n         if (c > mx) mx = c; n       } else if (str[i] ==  A ) n         c = 0; n     } n     cout << mx << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int const inf = 1e18; n long long int const maxn = 2e5 + 5; n long long int const mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   vector<long long int> arr(26); n   for (auto &u : arr) cin >> u; n   string s; n   cin >> s; n   long long int n = s.length(); n   map<long long int, long long int> mp[26]; n   vector<long long int> brr(n); n   for (long long int i = 0; i < n; i++) { n     brr[i] = arr[s[i] -  a ]; n     if (i != 0) brr[i] += brr[i - 1]; n   } n   long long int ans = 0; n   mp[s[0] -  a ][brr[0]]++; n   for (long long int i = 1; i < n; i++) { n     ans += mp[s[i] -  a ][brr[i - 1]]; n     mp[s[i] -  a ][brr[i]]++; n   } n   cout << ans <<   n ; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 20; n long long n, m, dp[N][1 << N], B, res, cb[1 << N]; n bool used[N][1 << N], w[N][N]; n long long cbit(long long x) { n   long long c = 0; n   while (x) { n     c += x & 1; n     x /= 2; n   } n   return c; n } n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (long long i = 0; i < m; ++i) { n     long long a, b; n     cin >> a >> b; n     a--; n     b--; n     w[a][b] = 1; n     w[b][a] = 1; n   } n   B = 1 << n; n   for (long long i = 0; i < B; ++i) cb[i] = cbit(i); n   queue<pair<long long, long long>> q; n   for (long long i = 0; i < n; ++i) { n     for (long long j = 0; j < n; ++j) { n       for (long long ma = 0; ma < B; ++ma) { n         dp[j][ma] = 0; n         used[j][ma] = 0; n       } n     } n     q.push({i, 1 << i}); n     dp[i][1 << i] = 1; n     while (!q.empty()) { n       long long v = q.front().first, mask = q.front().second, nmask; n       q.pop(); n       for (long long u = i; u < n; ++u) { n         nmask = mask | (1 << u); n         if (nmask == mask || !w[u][v]) continue; n         dp[u][nmask] += dp[v][mask]; n         if (!used[u][nmask]) { n           used[u][nmask] = 1; n           q.push({u, nmask}); n         } n       } n     } n     long long av = 0; n     for (long long j = 0; j < n; ++j) { n       for (long long mask = 0; mask < B; ++mask) { n         if (cb[mask] >= 3 && w[i][j] && dp[j][mask]) { n           av += dp[j][mask]; n         } n       } n     } n     res += av / 2; n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 4e5 + 100; n vector<int> vec[maxn]; n int n, m, k; n int d[maxn], cnt; n bool vis[maxn]; n void dfs(int x) { n   vis[x] = 1; n   d[++cnt] = x; n   for (int i = 0; i < vec[x].size(); i++) { n     if (vis[vec[x][i]]) continue; n     dfs(vec[x][i]); n     d[++cnt] = x; n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     vec[u].push_back(v); n     vec[v].push_back(u); n   } n   dfs(1); n   int x = (2 * n + k - 1) / k; n   for (int i = 1; i <= k; i++) { n     int c = min(x, cnt); n     if (c == 0) { n       cout <<  1 1  << endl; n       continue; n     } n     cout << c <<    ; n     for (int i = 1; i <= c && cnt != 0; i++, cnt--) { n       cout << d[cnt] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c, d; n   cin >> a >> b >> c >> d; n   if (abs(c - d) > 1) { n     cout <<  -1 ; n     return 0; n   } n   if (max(c, d) > min(a, b)) { n     cout <<  -1 ; n     return 0; n   } n   if (a + b <= c + d) { n     cout <<  -1 ; n     return 0; n   } n   if (c == d) { n     a--; n     for (int i = 1; i <= a - c; i++) cout <<  4 ; n     if (a == c - 1 && b > 1) { n       cout <<  7 ; n       b--; n     } n     for (int i = 1; i <= c; i++) cout <<  47 ; n     for (int i = 1; i <= b - c; i++) cout <<  7 ; n     if (a - c >= 0) cout <<  4 ; n   } n   if (c > d) { n     for (int i = 1; i <= a - c; i++) cout <<  4 ; n     for (int i = 1; i <= c; i++) cout <<  47 ; n     for (int i = 1; i <= b - c; i++) cout <<  7 ; n   } n   if (c < d) { n     if (b > 1) { n       cout <<  7 ; n       b--; n     } n     for (int i = 1; i < a - c; i++) cout <<  4 ; n     for (int i = 1; i <= c; i++) cout <<  47 ; n     for (int i = 1; i <= b - c; i++) cout <<  7 ; n     cout <<  4 ; n   } n } n


#include<bits/stdc++.h> n #define rep(i,a,b) for(int i=a;i<=b;i++) n #define pre(i,a,b) for(int i=a;i>=b;i--) n #define N 505 n using namespace std; n int f[52][95][905][2],n,mx=450,cc=45; n char s[N]; n int calc(int x,int cur,int res,int op){ n  if(x==n+1&&!cur)return 0; n  if(abs(cur)>cc||abs(res)>mx||x==n+1)return 0x3f3f3f3f; n  if(~f[x][cur+cc][res+mx][op==1])return f[x][cur+cc][res+mx][op==1]; n  f[x][cur+cc][res+mx][op==1]=min(calc(x,cur,res+op,op)+n-x+1,min(calc(x+1,cur*10+s[x+1]- 0 -res,res,1),calc(x+1,cur*10+s[x+1]- 0 -res,res,-1))); n  return f[x][cur+cc][res+mx][op==1]; n } n int main(){ n  scanf( %s ,s+2);n=strlen(s+2);s[1]= 0 ; n  memset(f,~0,sizeof(f)); n  printf( %d n ,calc(0,0,0,1)); n  return 0; n }  n /* n #include<bits/stdc++.h> n #define rep(i,a,b) for(int i=a;i<=b;i++) n #define pre(i,a,b) for(int i=a;i>=b;i--) n #define N 505 n using namespace std; n int f[52][95][905][2],n,mx=450,cc=45; n char s[N]; n int calc(int x,int cur,int res,int op){ n  if(x==n+1&&!cur)return 0; n  if(abs(cur)>cc||abs(res)>mx||x==n+1)return 0x3f3f3f3f; n  if(~f[x][cur+cc][res+mx][op==1])return f[x][cur+cc][res+mx][op==1]; n  f[x][cur+cc][res+mx][op==1]=min(calc(x,cur,res+op,op)+n-x+1,min(calc(x+1,cur*10+s[x+1]- 0 -res,res,1),calc(x+1,cur*10+s[x+1]- 0 -res,res,-1))); n  return f[x][cur+cc][res+mx][op==1]; n } n int main(){ n  scanf( %s ,s+2);n=strlen(s+2);s[1]= 0 ; n  memset(f,~0,sizeof(f)); n  printf( %d n ,calc(0,0,0,1)); n  return 0; n }  n */ n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n long long int dp1[N], dp2[N], Sa[N], Sw[N], dp[20][N], A[N]; n int P[20][N], H[N], n, q; n vector<pair<int, int> > adj[N]; n void preDFS(int v, int p = -1) { n   if (!~p) P[0][v] = v; n   for (int i = 1; i < 20; i++) P[i][v] = P[i - 1][P[i - 1][v]]; n   for (pair<int, int> u : adj[v]) { n     if (u.first != p) { n       P[0][u.first] = v; n       H[u.first] = H[v] + 1; n       Sw[u.first] = Sw[v] + u.second, Sa[u.first] = Sa[v] + A[u.first]; n       preDFS(u.first, v); n     } n   } n } n int getpar(int v, int h) { n   for (int i = h; i; i -= i & -i) v = P[__builtin_ctz(i)][v]; n   return v; n } n int LCA(int u, int v) { n   if (H[u] < H[v]) swap(u, v); n   u = getpar(u, H[u] - H[v]); n   if (u == v) return v; n   for (int i = 19; ~i; i--) n     if (P[i][u] != P[i][v]) u = P[i][u], v = P[i][v]; n   return P[0][v]; n } n void DFSdn(int v, int p = -1) { n   for (pair<int, int> u : adj[v]) { n     if (u.first != p) n       DFSdn(u.first, v), n           dp1[v] += max(0ll, dp1[u.first] - 2 * u.second + A[u.first]); n   } n } n void DFSup(int v, int p = -1) { n   for (pair<int, int> u : adj[v]) { n     if (u.first != p) { n       dp2[u.first] = n           max(0ll, A[v] + dp2[v] + dp1[v] - n                        max(0ll, dp1[u.first] - 2 * u.second + A[u.first])); n       dp2[u.first] = max(0ll, dp2[u.first] - 2 * u.second); n       DFSup(u.first, v); n     } n   } n } n void NIMA(int v, int p = -1) { n   for (pair<int, int> u : adj[v]) { n     if (u.first != p) { n       dp[0][u.first] = n           dp1[v] - max(0ll, dp1[u.first] - 2 * u.second + A[u.first]); n       NIMA(u.first, v); n     } n   } n } n long long int get(int v, int h) { n   long long int ret = 0; n   for (int i = h; i; i -= i & -i) n     ret += dp[__builtin_ctz(i)][v], v = P[__builtin_ctz(i)][v]; n   return ret; n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %lld , &A[i]); n   for (int i = 1; i < n; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     adj[u].push_back({v, w}); n     adj[v].push_back({u, w}); n   } n   preDFS(1); n   DFSdn(1); n   DFSup(1); n   NIMA(1); n   for (int i = 1; i < 20; i++) n     for (int j = 1; j <= n; j++) n       dp[i][j] = dp[i - 1][j] + dp[i - 1][P[i - 1][j]]; n   for (; q; q--) { n     int u, v; n     scanf( %d%d , &u, &v); n     if (H[u] < H[v]) swap(u, v); n     if (u == v) { n       printf( %lld n , dp1[u] + dp2[u] + A[u]); n       continue; n     } n     int p = LCA(u, v); n     long long int a = get(u, H[u] - H[p]), b = get(v, H[v] - H[p]); n     long long int ret = n         Sa[u] - Sa[p] + A[p] + Sa[v] - Sa[p] - (Sw[u] - Sw[p] + Sw[v] - Sw[p]); n     printf( %lld n , a + b + dp2[p] - dp1[p] + ret + dp1[u] + dp1[v]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e6 + 10, mod = 1e9 + 7, inf = 2e9, lg = 22, n                 maskm = (1 << lg) - 1, maxm = (1 << lg) + 7; n long long a[maxn], dp[maxm]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     dp[a[i]] = a[i]; n   } n   for (int i = 1; i < maxm; i++) { n     for (int j = i; j && dp[i] == 0; j -= (j & -j)) { n       dp[i] = dp[i ^ (j & -j)]; n     } n   } n   for (int i = 0; i < n; i++) { n     long long tmp = maskm ^ a[i]; n     if (dp[tmp] == 0) n       cout << -1 <<    ; n     else n       cout << dp[tmp] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[100010]; n vector<int> dp[5][100010]; n int k, l; n int a[6][6]; n int dfs(int now) { n   int i, j, x = 0, sum = 0; n   for (i = 0; i < now; i++) x = x * 10 + a[now][i]; n   if (i == l - 1) return dp[1][x].size(); n   for (i = 0; i < dp[l - now][x].size(); i++) { n     int tem = dp[l - now][x][i]; n     for (j = l - 1; j >= now; j--) { n       a[j][now] = a[now][j] = tem % 10; n       tem /= 10; n     } n     sum += dfs(now + 1); n   } n   return sum; n } n int main() { n   int i, j, n; n   int m = sqrt(100000.5); n   for (i = 2; i <= m; i++) n     if (!vis[i]) { n       for (j = i * i; j <= 100000; j += i) vis[j] = 1; n     } n   for (i = 2; i < 100000; i++) n     if (!vis[i]) { n       int tem = i; n       tem /= 10; n       for (j = 1; j <= 4; j++) { n         dp[j][tem].push_back(i); n         tem /= 10; n       } n     } n   scanf( %d , &n); n   char str[7]; n   for (j = 1; j <= n; j++) { n     scanf( %s , str); n     l = strlen(str); n     for (i = 0; str[i] !=   0 ; i++) { n       a[0][i] = a[i][0] = str[i] -  0 ; n     } n     printf( %d n , dfs(1)); n   } n } n


#include <bits/stdc++.h> n const int maxn = 5e5 + 10; n using namespace std; n int main() { n   string s; n   long long n; n   cin >> n; n   while (n--) { n     cin >> s; n     int b = 0, e = s.size() - 1; n     while (b < e && s[b] == s[e]) b++, e--; n     if (b >= e) n       cout << s <<   n ; n     else { n       string s1, s2, cmax; n       for (int i = b; i <= e; i++) { n         s1 += s[i]; n         s2 = s1; n         reverse(s2.begin(), s2.end()); n         if (s1 == s2) cmax = s1; n       } n       string s3, s4, cmax2; n       for (int i = e; i >= b; i--) { n         s3 += s[i]; n         s4 = s3; n         reverse(s4.begin(), s4.end()); n         if (s3 == s4) cmax2 = s3; n       } n       if (cmax.size() > cmax2.size()) n         cout << s.substr(0, b) << cmax << s.substr(e + 1) << endl; n       else n         cout << s.substr(0, b) << cmax2 << s.substr(e + 1) << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   x *= f; n } n const int MAXN = 2e3 + 7; n int n; n string ch[MAXN][2]; n vector<int> g[MAXN]; n inline int neg(int u) { return (u > n ? u - n : n + u); } n inline void eadd(int u, int v) { g[u].push_back(v); } n inline void iadd(int u, int v) { n   eadd(u, v); n   eadd(neg(v), neg(u)); n } n bool mk[MAXN]; n int dt[MAXN], cmp[MAXN], t = 0, cc = 0; n stack<int> stk; n int scc(int u) { n   stk.push(u); n   mk[u] = true; n   int lw = dt[u] = ++t; n   for (auto v : (g[u])) { n     if (!dt[v]) { n       int nl = scc(v); n       lw = min(lw, nl); n     } else if (mk[v]) n       lw = min(lw, dt[v]); n   } n   mk[u] = false; n   if (dt[u] == lw) { n     ++cc; n     while (stk.top() != u) { n       cmp[stk.top()] = cc; n       if (cmp[neg(stk.top())] == cc) return -1; n       stk.pop(); n     } n     if (cmp[neg(stk.top())] == cc) return -1; n     cmp[stk.top()] = cc; n     stk.pop(); n   } n   return lw; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = (1); i <= (n); ++i) { n     string s0, s1; n     cin >> s0 >> s1; n     ch[i][0] = s0.substr(0, 3); n     ch[i][1] = s0.substr(0, 2) + s1.substr(0, 1); n   } n   for (int i = (1); i <= (n); ++i) n     for (int j = (1); j <= (n); ++j) n       if (i != j) { n         if (ch[i][0] == ch[j][0]) iadd(i, neg(j)), iadd(neg(i), neg(j)); n         ; n         if (ch[i][1] == ch[j][1]) iadd(neg(i), j); n         if (ch[i][0] == ch[j][1]) iadd(i, j); n       } n   for (int i = (1); i <= (2 * n); ++i) n     if (!dt[i]) n       if (scc(i) < 0) { n         cout <<  NO n ; n         return 0; n       } n   cout <<  YES n ; n   for (int i = (1); i <= (n); ++i) { n     if (cmp[i] > cmp[neg(i)]) n       cout << ch[i][1] <<   n ; n     else n       cout << ch[i][0] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7, N = 100007; n int n; n long long ts, tf, t, a[N], ans = (long long)1e18, id; n int main() { n   cin >> ts >> tf >> t; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   while (a[n] > tf - t) n--; n   a[++n] = tf - t + 1; n   long long cur = 0; n   for (int i = 1; i <= n; i++) { n     cur = max(cur, ts); n     if (ans > cur - (a[i] - 1)) { n       ans = cur - (a[i] - 1); n       id = a[i] - 1; n     } n     if (cur < a[i]) { n       cout << cur; n       return 0; n     } n     cur = max(cur, a[i]); n     cur += t; n     if (cur >= tf - t + 1) break; n   } n   cout << id; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n int n, m, q; n long long suma[N], b[N], sumb[N]; n long long solve(long long x) { n   int pos = lower_bound(sumb + n, sumb + m + 1, x) - sumb; n   if (pos > m) return x - sumb[m]; n   if (pos == n) return sumb[pos] - x; n   return min(x - sumb[pos - 1], sumb[pos] - x); n } n int main() { n   scanf( %d %d %d , &n, &m, &q); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &suma[i]); n     if (i & 1) n       suma[i] = suma[i - 1] + suma[i]; n     else n       suma[i] = suma[i - 1] - suma[i]; n   } n   for (int i = 1; i <= m; i++) { n     scanf( %lld , &b[i]); n   } n   for (int i = 1; i <= n; i++) { n     if (i & 1) n       sumb[i] = sumb[i - 1] + b[i]; n     else n       sumb[i] = sumb[i - 1] - b[i]; n   } n   int f = -1; n   if (n & 1) f = 1; n   for (int i = 1; i <= m - n; i++) { n     sumb[i + n] = -(sumb[i + n - 1] - b[i]) + b[i + n] * f; n   } n   sort(sumb + n, sumb + m + 1); n   long long a = suma[n]; n   printf( %lld n , solve(a)); n   for (int i = 1; i <= q; i++) { n     int l, r, x; n     scanf( %d %d %d , &l, &r, &x); n     if ((r - l + 1) & 1) { n       if (l & 1) n         a += x; n       else n         a -= x; n     } n     printf( %lld n , solve(a)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000005; n int n; n char s1[maxn], s2[maxn]; n int main() { n   scanf( %d%s%s , &n, s1 + 1, s2 + 1); n   reverse(s1 + 1, s1 + n); n   for (int i = 1; i < n; ++i) { n     if (s1[i] ==  N ) n       s1[i] =  S ; n     else if (s1[i] ==  S ) n       s1[i] =  N ; n     else if (s1[i] ==  W ) n       s1[i] =  E ; n     else n       s1[i] =  W ; n   } n   unsigned long long h1 = 0, h2 = 0, fa = 1; n   for (int i = 1; i < n; ++i) { n     h1 = h1 + s1[i] * fa; n     h2 = h2 * 233u + s2[n - i]; n     fa *= 233u; n     if (h1 == h2) return puts( NO ), 0; n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y, a, b; n }; n point rotate(point p) { n   point p1 = p; n   int x = p1.x - p1.a, y = p1.y - p1.b; n   p1.x = -y + p1.a; n   p1.y = x + p1.b; n   return p1; n } n bool comp(point p1, point p2) { n   if (p1.x == p2.x) return p1.y < p2.y; n   return p1.x < p2.x; n } n bool issq(point p[]) { n   sort(p, p + 4, comp); n   if (p[0].x == p[3].x && p[0].y == p[3].y) return false; n   if (p[0].x == p[1].x && p[0].y == p[1].y) return false; n   int d11 = (p[0].x - p[1].x) * (p[0].x - p[1].x) + n             (p[0].y - p[1].y) * (p[0].y - p[1].y); n   int d22 = (p[0].x - p[2].x) * (p[0].x - p[2].x) + n             (p[0].y - p[2].y) * (p[0].y - p[2].y); n   int d1 = (p[0].x - p[3].x) * (p[0].x - p[3].x) + n            (p[0].y - p[3].y) * (p[0].y - p[3].y); n   int d2 = (p[1].x - p[2].x) * (p[1].x - p[2].x) + n            (p[1].y - p[2].y) * (p[1].y - p[2].y); n   if (p[0].x + p[3].x == p[1].x + p[2].x && n       p[0].y + p[3].y == p[1].y + p[2].y && d11 == d22 && d1 == d2) n     return true; n   return false; n } n int main() { n   int n; n   cin >> n; n   while (n--) { n     point p[4]; n     for (int i = 0; i < 4; i++) cin >> p[i].x >> p[i].y >> p[i].a >> p[i].b; n     int ans = 17; n     for (int i = 0; i < 4; i++) { n       for (int j = 0; j < 4; j++) { n         for (int k = 0; k < 4; k++) { n           for (int l = 0; l < 4; l++) { n             point pc[4]; n             for (int il = 0; il < 4; il++) pc[il] = p[il]; n             if (issq(pc)) ans = min(ans, i + j + k + l); n             p[3] = rotate(p[3]); n           } n           p[2] = rotate(p[2]); n         } n         p[1] = rotate(p[1]); n       } n       p[0] = rotate(p[0]); n     } n     if (ans == 17) n       cout << -1; n     else n       cout << ans; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   string s; n   cin >> n >> s; n   n += 3; n   vector<int> a(n), la(n), ra(n), lmi(n), lma(n), rmi(n), rma(n); n   int j = 2; n   for (int i = 0; i < n - 3; i++) { n     if (s[i] ==  ( ) { n       a[j] = 1; n     } else if (s[i] ==  ) ) { n       a[j] = -1; n     } else if (s[i] ==  R ) { n       j++; n     } else if (s[i] ==  L ) { n       j--; n       j = max(j, 2); n     } else { n       a[j] = 0; n     } n     la[j] = la[j - 1] + a[j]; n     ra[j] = ra[j + 1] - a[j]; n     lmi[j] = min(lmi[j - 1], la[j]); n     lma[j] = max(lma[j - 1], la[j]); n     rmi[j] = min(rmi[j + 1], ra[j]); n     rma[j] = max(rma[j + 1], ra[j]); n     if (lmi[j] >= 0 && rmi[j + 1] >= 0 && la[j] == ra[j + 1]) { n       cout << max(lma[j], rma[j + 1]) <<    ; n     } else { n       cout <<  -1  ; n     } n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ; n const long long maxn = 1e5 + 7; n const long double pi = 3.14159265358979323846; n struct pirog { n   long long r; n   long long h; n   long long i; n }; n long long V[4 * maxn]; n pirog A[maxn]; n vector<pirog> C; n void update(long long v, long long tl, long long tr, long long i, n             long long color) { n   if (tl == tr) { n     V[v] = color; n     return; n   } n   long long tm = (tl + tr) / 2; n   if (tm >= i) { n     update(2 * v, tl, tm, i, color); n   } else { n     update(2 * v + 1, tm + 1, tr, i, color); n   } n   V[v] = max(V[2 * v], V[2 * v + 1]); n } n long long mx(long long v, long long tl, long long tr, long long l, n              long long r) { n   if (tl > r || l > tr) { n     return 0; n   } n   if (tl == l && tr == r) { n     return V[v]; n   } n   long long tm = (tl + tr) / 2; n   return max(mx(2 * v, tl, tm, l, min(tm, r)), n              mx(2 * v + 1, tm + 1, tr, max(l, tm + 1), r)); n } n bool cmp(pirog a, pirog b) { return a.h * a.r * a.r < b.h * b.r * b.r; } n long long X[maxn]; n int main() { n   long long q, w, e, k, n; n   cin >> n; n   cout << fixed << setprecision(10); n   for (q = 0; q < n; q++) { n     cin >> A[q].r >> A[q].h; n     A[q].i = q; n     C.push_back(A[q]); n   } n   sort(C.begin(), C.end(), cmp); n   long long l = 0; n   for (q = 0; q < n; q++) { n     long long x = mx(1, 0, n - 1, 0, C[q].i) + C[q].h * C[q].r * C[q].r; n     X[q] = x; n     if (C[q].r * C[q].r * C[q].h == C[q + 1].r * C[q + 1].r * C[q + 1].h) { n       continue; n     } n     for (w = l; w <= q; w++) { n       update(1, 0, n - 1, C[w].i, X[w]); n     } n     l = q + 1; n   } n   cout << mx(1, 0, n - 1, 0, n - 1) * pi; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n int main() { n   int flag = 0; n   long long n, a, b; n   cin >> n >> a >> b; n   long long x = a, y = b; n   if (a > b) { n     swap(a, b); n     flag = 1; n   } n   if (a * b > 6 * n) { n     printf( %lld n , a * b); n     printf( %lld %lld n , a, b); n     return 0; n   } n   long long ans = 0x3f3f3f3f3f3f3f3f, l = 0, r = 0; n   long long len = sqrt(6 * n) + 1; n   for (int i = a; i <= len + 5; i++) { n     if (6 * n % i == 0 && 6 * n / i >= b) { n       l = i; n       r = 6 * n / i; n       break; n     } n     y = 6 * n / i + 1; n     y = max(y, b); n     if (i * y < ans) { n       l = i; n       r = y; n       ans = l * r; n     } n   } n   if (flag) swap(l, r); n   printf( %lld n , l * r); n   printf( %lld %lld n , l, r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e4 + 7; n const int inf = 0x3f3f3f3f; n int a[maxn]; n char s[150]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     scanf( %s , s); n     int x = 0, y = 0; n     for (int i = 0; i < n; i++) { n       if (s[i] ==  < ) n         x++; n       else n         break; n     } n     for (int i = n - 1; i >= 0; i--) { n       if (s[i] ==  > ) n         y++; n       else n         break; n     } n     cout << min(x, y) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n void print(int n) { n   if (n == 4) { n     printf( 1 * 2 = 2 n ); n     printf( 3 * 4 = 12 n ); n     printf( 2 * 12 = 24 n ); n   } else if (n == 5) { n     printf( 5 * 4 = 20 n ); n     printf( 3 + 2 = 5 n ); n     printf( 5 - 1 = 4 n ); n     printf( 20 + 4 = 24 n ); n   } n } n int main() { n   int n; n   while (~scanf( %d , &n)) { n     if (n <= 3) { n       printf( NO n ); n       continue; n     } n     printf( YES n ); n     if (n == 4) n       print(4); n     else if (n == 5) n       print(5); n     else if (n % 2) { n       print(5); n       for (int i = n; i > 5; i = i - 2) { n         printf( %d - %d = 1 n , i, i - 1); n         printf( 24 * 1 = 24 n ); n       } n     } else { n       print(4); n       for (int i = n; i > 4; i = i - 2) { n         printf( %d - %d = 1 n , i, i - 1); n         printf( 24 * 1 = 24 n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 666; n const int inf = 1e9; n struct edge { n   int pr; n   int to; n   int cap; n } e[111111]; n int tot = 1, la[N]; n void clr() { n   tot = 1; n   memset(la, 0, sizeof(la)); n } n void adde(int x, int y, int z) { n   e[++tot].pr = la[x]; n   la[x] = tot; n   e[tot].to = y; n   e[tot].cap = z; n } n void addf(int x, int y, int z = inf) { n   adde(x, y, z); n   adde(y, x, 0); n } n int d[N]; n int bfs(int s, int t) { n   queue<int> Q; n   int i, x, y; n   memset(d, 0, sizeof(d)); n   d[s] = 1; n   Q.push(s); n   while (!Q.empty()) { n     x = Q.front(); n     Q.pop(); n     for (i = la[x]; i; i = e[i].pr) { n       if (!e[i].cap) continue; n       y = e[i].to; n       if (!d[y]) { n         d[y] = d[x] + 1; n         Q.push(y); n       } n     } n   } n   return d[t]; n } n int dfs(int s, int t, int f) { n   if (s == t) return f; n   int i, x, y, r = 0; n   for (i = la[s]; i; i = e[i].pr) { n     if (!e[i].cap || !f) continue; n     y = e[i].to; n     if (d[y] != d[s] + 1) continue; n     x = dfs(y, t, min(f, e[i].cap)); n     if (x) { n       f -= x; n       r += x; n       e[i].cap -= x; n       e[i ^ 1].cap += x; n     } n   } n   if (!r) d[s] = 0; n   return r; n } n int maxflow(int s, int t) { n   int x, r = 0; n   while (bfs(s, t)) { n     x = dfs(s, t, inf); n     if (x) n       r += x; n     else n       break; n   } n   return r; n } n int vis[N], w[N]; n bool hungary(int x) { n   int i, y; n   for (i = la[x]; i; i = e[i].pr) { n     y = e[i].to; n     if (!vis[y]) { n       vis[y] = 1; n       if (!w[y] || hungary(w[y])) { n         w[y] = x; n         return true; n       } n     } n   } n   return false; n } n int n, a[N][N]; n int main() { n   int i, j, o, s, t, ans; n   scanf( %d , &n); n   clr(); n   for (i = 1; i <= n; i = i + 1) { n     scanf( %d , &o); n     while (o--) { n       scanf( %d , &j); n       a[i][j] = 1; n       adde(i, j, 0); n     } n   } n   for (i = 1; i <= n; i = i + 1) { n     memset(vis, 0, sizeof(vis)); n     hungary(i); n   } n   clr(); n   s = 0, t = n + 1; n   for (i = 1; i <= n; i = i + 1) n     for (j = 1; j <= n; j = j + 1) n       if (a[i][j]) addf(i, w[j]); n   ans = 0; n   for (i = 1; i <= n; i = i + 1) { n     scanf( %d , &o); n     if (o > 0) n       addf(i, t, o); n     else { n       ans += o; n       addf(s, i, -o); n     } n   } n   printf( %d , ans + maxflow(s, t)); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int x, h, m, hh, mm; n   scanf( %d , &x); n   scanf( %d%d , &h, &m); n   hh = h, mm = m; n   for (int i = 0; i <= 24 * 60; i++) { n     int tmp = x * i; n     h = hh, m = mm; n     while (m < tmp) { n       m += 60; n       h--; n       if (h == -1) h = 23; n     } n     m -= tmp; n     if (m % 10 == 7 || m / 10 == 7 || h % 10 == 7 || h / 10 == 7) { n       printf( %d n , i); n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T gcd(T a, T b) { n   return b == 0 ? a : gcd(b, a % b); n } n template <typename T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <class T> n T my_pow(T n, T p) { n   if (p == 0) return 1; n   T x = my_pow(n, p / 2); n   x = (x * x); n   if (p & 1) x = (x * n); n   return x; n } n template <class T> n T big_mod(T n, T p, T m) { n   if (p == 0) return (T)1; n   T x = big_mod(n, p / 2, m); n   x = (x * x) % m; n   if (p & 1) x = (x * n) % m; n   return x; n } n template <class T> n inline T Mod(T n, T m) { n   return (n % m + m) % m; n } n template <class T> n T extract(string s, T ret) { n   stringstream ss(s); n   ss >> ret; n   return ret; n } n template <class T> n string itos(T n) { n   ostringstream ost; n   ost << n; n   ost.flush(); n   return ost.str(); n } n long long stoi(string s) { n   long long r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n double toDouble(string s) { n   double r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n long long ar[2000005], ar1[200005]; n long long a = 0, b = 0, c = 0, r = 0, rr = 0, res = 0, n, m; n string s, ss; n long long fun(long long val) { n   r = 0; n   for (__typeof(n) i = 0; i < (n); i++) { n     if (val < ar[i]) return false; n     r += (val - ar[i]); n   } n   if (r >= val) n     return true; n   else n     return false; n } n int main() { n   cin >> n; n   for (__typeof(n) i = 0; i < (n); i++) { n     cin >> ar[i]; n   } n   long long lo = 0, hi = 10000000000ll; n   while (hi - lo > 1) { n     long long mid = (lo + hi) >> 1; n     if (fun(mid)) n       hi = mid; n     else n       lo = mid; n   } n   cout << hi; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, i, a[100100], b[100100]; n priority_queue<pair<long long, int>> q; n long long m, mx, mn, cur, cnt, le, ri, h; n int fnd(int i, long long v) { n   int lft = 0, rgh = a[i]; n   while (lft < rgh) { n     int mid = (lft + rgh) / 2 + 1; n     cur = a[i] - 3LL * mid * (mid + 1LL) - 1LL; n     if (cur < v) n       rgh = mid - 1; n     else n       lft = mid; n   } n   return rgh; n } n int main() { n   scanf( %d%I64d , &n, &m); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     q.push({a[i] - 1LL, i}); n     cur = a[i] - 3LL * a[i] * (a[i] + 1LL) - 1LL; n     if (i) { n       mn = min(mn, cur); n       mx = max(mx, a[i] - 1LL); n     } else { n       mn = cur; n       mx = a[i] - 1LL; n     } n   } n   le = 0; n   ri = mx - mn; n   while (le < ri) { n     h = (le + ri) / 2; n     for (cnt = i = 0; i < n; i++) cnt += fnd(i, mx - h); n     if (cnt < m) n       le = h + 1; n     else n       ri = h; n   } n   for (i = 0; i < n; i++) { n     b[i] = fnd(i, mx - ri + 1); n     m -= b[i]; n   } n   for (i = 0; i < n && m > 0; i++) { n     x = min(1LL * fnd(i, mx - ri) - b[i], m); n     b[i] += x; n     m -= x; n   } n   for (i = 0; i < n; i++) printf( %d  , b[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const long long INFLL = 1e18 + 1; n const int MAX = 200001; n const long long MOD = 1000000007; n long long inq(long long k, long long q) { n   if (q == 0) return 1; n   long long l = inq(k, q / 2); n   if (q % 2 == 0) n     return l * l % MOD; n   else n     return l * l * k % MOD; n } n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long cubr(long long a) { n   long long l = -1, r = 1e6 + 2; n   while (l < r - 1) { n     long long mid = (l + r) / 2; n     if (mid * mid * mid > a) n       r = mid; n     else n       l = mid; n   } n   return l; n } n long long max(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long min(long long a, long long b) { return -1 * max(-a, -b); } n long long possible(long long q) { n   if (q == INF) return -1; n   return q; n } n bool correct(int x, int xx) { n   if (x < 0) return 0; n   if (x >= xx) return 0; n   return 1; n } n long long dsumm(long long x, long long k) { n   long long y = 1; n   long long z = 0; n   for (int i = 0; y < 1e18; i++) { n     z += x / y % k; n     y *= k; n   } n   return z; n } n long long dcount(long long x) { n   long long y = 1; n   long long z = 0; n   int c[100]; n   for (int i = 0; i < 10; i++) c[i] = 0; n   for (int i = 0; x > 0; i++) { n     if (c[x / y % 10] == 0) z++; n     c[x / y % 10] = 1; n     x /= 10; n   } n   return z; n } n long long lg10(long long x) { n   if (x == 0) return 0; n   return lg10(x / 10) + 1; n } n string months[12] = { January ,    February ,  March ,     April , n                       May ,        June ,      July ,      August , n                       September ,  October ,   November ,  December }; n int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n vector<int> cx, cy; n long long n, m; n long long x[50]; n long long y[50]; n long long x2[50]; n long long y2[50]; n long long a[52][52]; n long long b[52][52]; n vector<pair<pair<int, int>, pair<int, int>>> ans1, ans2, ans3; n void Move_left_p1(int num) { n   int i = 0; n   int j = 0; n   for (int i1 = 1; i1 <= n; i1++) { n     for (int j1 = 1; j1 <= n; j1++) { n       if (a[i1][j1] == num) { n         i = i1; n         j = j1; n         break; n       } n     } n   } n   while (j > 1) { n     if (a[i][j - 1] == 0) { n       ans1.push_back({{i, j}, {i, j - 1}}); n       j--; n       a[i][j] = num; n       a[i][j + 1] = 0; n     } else n       break; n   } n } n void Move_right_p1(int num) { n   int i = 0; n   int j = 0; n   for (int i1 = 1; i1 <= n; i1++) { n     for (int j1 = 1; j1 <= n; j1++) { n       if (a[i1][j1] == num) { n         i = i1; n         j = j1; n         break; n       } n     } n   } n   while (j < n) { n     if (a[i][j + 1] == 0) { n       ans1.push_back({{i, j}, {i, j + 1}}); n       j++; n       a[i][j] = num; n       a[i][j - 1] = 0; n     } else n       break; n   } n } n void Move_up_p1(int num) { n   int i = 0; n   int j = 0; n   for (int i1 = 1; i1 <= n; i1++) { n     for (int j1 = 1; j1 <= n; j1++) { n       if (a[i1][j1] == num) { n         i = i1; n         j = j1; n         break; n       } n     } n   } n   while (i > 1) { n     if (a[i - 1][j] == 0) { n       ans1.push_back({{i, j}, {i - 1, j}}); n       i--; n       a[i][j] = num; n       a[i + 1][j] = 0; n     } else n       break; n   } n } n void collectA() { n   for (int i = 1; i <= 1; i++) { n     for (int j = 1; j <= n; j++) { n       if (a[i][j] != 0) { n         Move_left_p1(a[i][j]); n       } n     } n   } n   for (int i = 2; i <= n; i++) { n     for (int j = n; j >= 1; j--) { n       if (a[i][j] != 0) { n         int id = a[i][j]; n         Move_right_p1(id); n         Move_up_p1(id); n         Move_left_p1(id); n       } n     } n   } n } n void Move_left_p3(int num) { n   int i = 0; n   int j = 0; n   for (int i1 = 1; i1 <= n; i1++) { n     for (int j1 = 1; j1 <= n; j1++) { n       if (b[i1][j1] == num) { n         i = i1; n         j = j1; n         break; n       } n     } n   } n   while (j > 1) { n     if (b[i][j - 1] == 0) { n       ans3.push_back({{i, j}, {i, j - 1}}); n       j--; n       b[i][j] = num; n       b[i][j + 1] = 0; n     } else n       break; n   } n } n void Move_right_p3(int num) { n   int i = 0; n   int j = 0; n   for (int i1 = 1; i1 <= n; i1++) { n     for (int j1 = 1; j1 <= n; j1++) { n       if (b[i1][j1] == num) { n         i = i1; n         j = j1; n         break; n       } n     } n   } n   while (j < n) { n     if (b[i][j + 1] == 0) { n       ans3.push_back({{i, j}, {i, j + 1}}); n       j++; n       b[i][j] = num; n       b[i][j - 1] = 0; n     } else n       break; n   } n } n void Move_up_p3(int num) { n   int i = 0; n   int j = 0; n   for (int i1 = 1; i1 <= n; i1++) { n     for (int j1 = 1; j1 <= n; j1++) { n       if (b[i1][j1] == num) { n         i = i1; n         j = j1; n         break; n       } n     } n   } n   while (i > 1) { n     if (b[i - 1][j] == 0) { n       ans3.push_back({{i, j}, {i - 1, j}}); n       i--; n       b[i][j] = num; n       b[i + 1][j] = 0; n     } else n       break; n   } n } n void collectB() { n   for (int i = 1; i <= 1; i++) { n     for (int j = 1; j <= n; j++) { n       if (b[i][j] != 0) { n         Move_left_p3(b[i][j]); n       } n     } n   } n   for (int i = 2; i <= n; i++) { n     for (int j = n; j >= 1; j--) { n       if (b[i][j] != 0) { n         int id = b[i][j]; n         Move_right_p3(id); n         Move_up_p3(id); n         Move_left_p3(id); n       } n     } n   } n } n void Move_right_p2(int num) { n   int i = 0; n   int j = 0; n   for (int i1 = 1; i1 <= n; i1++) { n     for (int j1 = 1; j1 <= n; j1++) { n       if (a[i1][j1] == num) { n         i = i1; n         j = j1; n         break; n       } n     } n   } n   while (j < n) { n     if (a[i][j + 1] == 0) { n       ans2.push_back({{i, j}, {i, j + 1}}); n       j++; n       a[i][j] = num; n       a[i][j - 1] = 0; n     } else n       break; n   } n } n void Move_left_p2(int num) { n   int i = 0; n   int j = 0; n   for (int i1 = 1; i1 <= n; i1++) { n     for (int j1 = 1; j1 <= n; j1++) { n       if (a[i1][j1] == num) { n         i = i1; n         j = j1; n         break; n       } n     } n   } n   while (j > 1) { n     if (a[i][j - 1] == 0) { n       ans2.push_back({{i, j}, {i, j - 1}}); n       j--; n       a[i][j] = num; n       a[i][j + 1] = 0; n     } else n       break; n   } n } n void Move_right_all(int line) { n   for (int i = n; i >= 1; i--) { n     if (a[line][i] != 0) { n       int id = a[line][i]; n       Move_right_p2(id); n     } n   } n } n void Push(int id) { n   for (int i = 1; i <= n; i++) { n     if (a[2][i] == id) { n       a[1][i] = id; n       a[2][i] = 0; n       ans2.push_back({{2, i}, {1, i}}); n       Move_left_p2(id); n     } n   } n } n void reOrder() { n   for (int i = 1; i <= m; i++) { n     a[2][i] = a[1][i]; n     a[1][i] = 0; n     ans2.push_back({{1, i}, {2, i}}); n   } n   Move_right_all(2); n   for (int i = 1; i <= m; i++) { n     int id = b[1][i]; n     Push(id); n     Move_right_all(2); n   } n } n int main() { n   cin >> n >> m; n   if (n == 1) { n     cout << 0; n     return 0; n   } n   for (int(i) = 0; (i) != (m); i++) { n     cin >> x[i] >> y[i]; n     a[x[i]][y[i]] = i + 1; n   } n   collectA(); n   for (int(i) = 0; (i) != (m); i++) { n     cin >> x2[i] >> y2[i]; n     b[x2[i]][y2[i]] = i + 1; n   } n   collectB(); n   reOrder(); n   if (ans1.size() + ans2.size() + ans3.size() < 10800) { n     cout << ans1.size() + ans2.size() + ans3.size() <<   n ; n     for (int(i) = 0; (i) != (ans1.size()); i++) { n       cout << ans1[i].first.first <<     << ans1[i].first.second <<     n            << ans1[i].second.first <<     << ans1[i].second.second <<   n ; n     } n     for (int(i) = 0; (i) != (ans2.size()); i++) { n       cout << ans2[i].first.first <<     << ans2[i].first.second <<     n            << ans2[i].second.first <<     << ans2[i].second.second <<   n ; n     } n     for (int(i) = (ans3.size()) - 1; (i) != -1; i--) { n       cout << ans3[i].second.first <<     << ans3[i].second.second <<     n            << ans3[i].first.first <<     << ans3[i].first.second <<   n ; n     } n   } else { n     for (int i = 1; i <= 50; i++) { n       for (int j = 1; j <= 50; j++) { n         a[i][j] = 0; n         b[i][j] = 0; n       } n     } n     ans1.resize(0); n     ans2.resize(0); n     ans3.resize(0); n     for (int(i) = 0; (i) != (m); i++) { n       a[n + 1 - x[i]][y[i]] = i + 1; n     } n     collectA(); n     for (int(i) = 0; (i) != (m); i++) { n       b[n + 1 - x2[i]][y2[i]] = i + 1; n     } n     collectB(); n     reOrder(); n     if (ans1.size() + ans2.size() + ans3.size() < 10800) { n       cout << ans1.size() + ans2.size() + ans3.size() <<   n ; n       for (int(i) = 0; (i) != (ans1.size()); i++) { n         cout << n + 1 - ans1[i].first.first <<     << ans1[i].first.second n              <<     << n + 1 - ans1[i].second.first <<     n              << ans1[i].second.second <<   n ; n       } n       for (int(i) = 0; (i) != (ans2.size()); i++) { n         cout << n + 1 - ans2[i].first.first <<     << ans2[i].first.second n              <<     << n + 1 - ans2[i].second.first <<     n              << ans2[i].second.second <<   n ; n       } n       for (int(i) = (ans3.size()) - 1; (i) != -1; i--) { n         cout << n + 1 - ans3[i].second.first <<     << ans3[i].second.second n              <<     << n + 1 - ans3[i].first.first <<     n              << ans3[i].first.second <<   n ; n       } n     } else { n       for (int i = 1; i <= 50; i++) { n         for (int j = 1; j <= 50; j++) { n           a[i][j] = 0; n           b[i][j] = 0; n         } n       } n       ans1.resize(0); n       ans2.resize(0); n       ans3.resize(0); n       for (int(i) = 0; (i) != (m); i++) { n         a[y[i]][x[i]] = i + 1; n       } n       collectA(); n       for (int(i) = 0; (i) != (m); i++) { n         b[y2[i]][x2[i]] = i + 1; n       } n       collectB(); n       reOrder(); n       if (ans1.size() + ans2.size() + ans3.size() < 10800) { n         cout << ans1.size() + ans2.size() + ans3.size() <<   n ; n         for (int(i) = 0; (i) != (ans1.size()); i++) { n           cout << ans1[i].first.second <<     << ans1[i].first.first <<     n                << ans1[i].second.second <<     << ans1[i].second.first <<   n ; n         } n         for (int(i) = 0; (i) != (ans2.size()); i++) { n           cout << ans2[i].first.second <<     << ans2[i].first.first <<     n                << ans2[i].second.second <<     << ans2[i].second.first <<   n ; n         } n         for (int(i) = (ans3.size()) - 1; (i) != -1; i--) { n           cout << ans3[i].second.second <<     << ans3[i].second.first <<     n                << ans3[i].first.second <<     << ans3[i].first.first <<   n ; n         } n       } else { n         for (int i = 1; i <= 50; i++) { n           for (int j = 1; j <= 50; j++) { n             a[i][j] = 0; n             b[i][j] = 0; n           } n         } n         ans1.resize(0); n         ans2.resize(0); n         ans3.resize(0); n         for (int(i) = 0; (i) != (m); i++) { n           a[n + 1 - y[i]][x[i]] = i + 1; n         } n         collectA(); n         for (int(i) = 0; (i) != (m); i++) { n           b[n + 1 - y2[i]][x2[i]] = i + 1; n         } n         collectB(); n         reOrder(); n         if (ans1.size() + ans2.size() + ans3.size() < 10800) { n           cout << ans1.size() + ans2.size() + ans3.size() <<   n ; n           for (int(i) = 0; (i) != (ans1.size()); i++) { n             cout << n + 1 - ans1[i].first.second <<     << ans1[i].first.first n                  <<     << n + 1 - ans1[i].second.second <<     n                  << ans1[i].second.first <<   n ; n           } n           for (int(i) = 0; (i) != (ans2.size()); i++) { n             cout << n + 1 - ans2[i].first.second <<     << ans2[i].first.first n                  <<     << n + 1 - ans2[i].second.second <<     n                  << ans2[i].second.first <<   n ; n           } n           for (int(i) = (ans3.size()) - 1; (i) != -1; i--) { n             cout << n + 1 - ans3[i].second.second <<     << ans3[i].second.first n                  <<     << n + 1 - ans3[i].first.second <<     n                  << ans3[i].first.first <<   n ; n           } n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n, m, arr[303][303]; n     scanf( %d %d , &n, &m); n     bool can = true; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         scanf( %d , &arr[i][j]); n         if (i == 0 || i == n - 1) { n           if (j > 0 && j < m - 1) { n             if (arr[i][j] > 3) can = false; n             arr[i][j] = 3; n           } else { n             if (arr[i][j] > 2) can = false; n             arr[i][j] = 2; n           } n         } else if (j == 0 || j == m - 1) { n           if (i > 0 && i < n - 1) { n             if (arr[i][j] > 3) can = false; n             arr[i][j] = 3; n           } else { n             if (arr[i][j] > 2) can = false; n             arr[i][j] = 2; n           } n         } else { n           if (arr[i][j] > 4) can = false; n           arr[i][j] = 4; n         } n       } n     } n     if (!can) { n       printf( NO n ); n       continue; n     } n     printf( YES n ); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         printf( %d%c , arr[i][j], j >= m - 1 ?   n  :    ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> g, sg; n vector<bool> vis; n void dfs(int u) { n   vis[u] = 1; n   for (int v : g[u]) { n     if (!vis[v]) { n       sg[u].push_back(v); n       sg[v].push_back(u); n       dfs(v); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   g.resize(n); n   sg.resize(n); n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   vis.assign(n, 0); n   dfs(0); n   auto find_path = [&](int u, int v) { n     swap(u, v); n     queue<int> qu; n     vector<int> p(n, -1); n     vis.assign(n, 0); n     vis[u] = 1; n     qu.push(u); n     while (!qu.empty()) { n       int x = qu.front(); n       qu.pop(); n       for (int y : sg[x]) { n         if (!vis[y]) { n           vis[y] = 1; n           qu.push(y); n           p[y] = x; n         } n       } n     } n     vector<int> ret; n     for (int x = v; x != u; x = p[x]) { n       ret.push_back(x); n     } n     ret.push_back(u); n     return ret; n   }; n   int q; n   cin >> q; n   vector<pair<int, int>> queries(q); n   vector<int> deg(n, 0); n   vector<vector<int>> ans(q); n   for (int i = 0; i < q; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     deg[u]++; n     deg[v]++; n     queries[i] = {u, v}; n     ans[i] = find_path(u, v); n   } n   int cnt_odd = 0; n   for (int u = 0; u < n; u++) cnt_odd += deg[u] % 2; n   if (cnt_odd) { n     cout <<  NO n  << cnt_odd / 2 <<   n ; n   } else { n     cout <<  YES n ; n     for (auto v : ans) { n       cout << v.size() <<   n ; n       for (int x : v) cout << x + 1 <<    ; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<set<int> > graph; n vector<int> eliminar; n set<int>::iterator ite; n void elim(int N) { n   eliminar.clear(); n   for (int i = 1; i <= N; i++) n     if (graph[i].size() == 1) eliminar.push_back(i); n } n int main() { n   int n, m, n1, n2, temp, contador; n   cin >> n >> m; n   contador = 0; n   graph.resize(n + 10); n   while (m--) { n     cin >> n1 >> n2; n     graph[n1].insert(n2); n     graph[n2].insert(n1); n   } n   elim(n); n   while (!eliminar.empty()) { n     for (int i = 0; i < eliminar.size(); i++) { n       graph[eliminar[i]].clear(); n       for (int j = 1; j <= n; j++) n         if (graph[j].find(eliminar[i]) != graph[j].end()) n           graph[j].erase(eliminar[i]); n     } n     contador++; n     elim(n); n   } n   cout << contador << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, c, f = 0; n int main() { n   cin >> n >> a >> b >> c; n   for (int i = 0; i <= c; i++) { n     for (int j = 0; j <= b; j++) { n       if (n - j - 2 * i >= 0 && n - j - 2 * i <= a / 2) { n         f++; n       } n     } n   } n   cout << f; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n int a[MAXN]; n int main() { n   int n, tot = 0, flag = -1, num = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   sort(a, a + n); n   for (int i = 0; i < n - 1; ++i) { n     if (i != 0 && a[i] == a[i + 1] && a[i - 1] == a[i] - 1) flag = 1; n     if (a[i] == a[i + 1]) num++; n     tot += a[i] - i; n   } n   tot += a[n - 1] - (n - 1); n   if (num >= 2) flag = 1; n   if (a[0] == 0 && a[1] == 0) flag = 1; n   if (flag == -1) { n     if (tot % 2 == 0) n       flag = 1; n     else n       flag = 0; n   } n   if (flag) n     printf( cslnb ); n   else n     printf( sjfnb ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename S, typename T> n ostream& operator<<(ostream& out, pair<S, T> const& p) { n   out <<  (  << p.first <<  ,   << p.second <<  ) ; n   return out; n } n template <typename T> n ostream& operator<<(ostream& out, vector<T> const& v) { n   int l = v.size(); n   for (int i = 0; i < l - 1; i++) out << v[i] <<    ; n   if (l > 0) out << v[l - 1]; n   return out; n } n long long n; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   if (n == 0) { n     cout <<  0 0  << endl; n     return 0; n   } n   long long lo = 0, hi = 600000000, mid; n   while (lo + 1 < hi) { n     mid = (lo + hi) / 2; n     if (1 + (mid * (mid + 1)) * 3 <= n + 1) n       lo = mid; n     else n       hi = mid; n   } n   long long x, y; n   if (lo == 0) { n     x = 2, y = 0; n     lo++; n   } else { n     n -= (lo * (lo + 1) * 3); n     lo++; n     x = 2 * (lo), y = 0; n   } n   if (n == 0) { n     x -= 2; n   } n   for (int i = 0; i < 6 and n > 0; i++) { n     if (lo <= n) { n       if (i == 0) { n         x -= lo; n         y += 2 * lo; n       } else if (i == 1) { n         x -= 2 * lo; n       } else if (i == 2) { n         x -= lo; n         y -= 2 * lo; n       } else if (i == 3) { n         x += lo; n         y -= 2 * lo; n       } else if (i == 4) { n         x += 2 * lo; n       } else if (i == 5) { n         x += lo; n         y += 2 * lo; n       } n       n -= lo; n     } else { n       lo = n; n       if (i == 0) { n         x -= lo; n         y += 2 * lo; n       } else if (i == 1) { n         x -= 2 * lo; n       } else if (i == 2) { n         x -= lo; n         y -= 2 * lo; n       } else if (i == 3) { n         x += lo; n         y -= 2 * lo; n       } else if (i == 4) { n         x += 2 * lo; n       } else if (i == 5) { n         x += lo; n         y += 2 * lo; n       } n       break; n     } n   } n   cout << x <<     << y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n void dump(const vector<int>& a) { n   for (int i = 0; i < a.size(); i++) { n     cerr << a[i] <<    ; n   } n   cerr << endl; n } n vector<pair<int, int>> factorise(long long m) { n   vector<pair<int, int>> res; n   for (long long i = 2; i * i <= m; i++) { n     if (m % i == 0) { n       int cnt = 0; n       while (m % i == 0) { n         cnt++; n         m /= i; n       } n       res.emplace_back(i, cnt); n     } n   } n   return res; n }; n using ll = long long; n ll gcd(ll a, ll b, ll& x, ll& y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   ll x1, y1; n   ll d = gcd(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return d; n } n bool debug = false; n using pii = pair<int, int>; n using pll = pair<long long, long long>; n const int magic = 3; n const int INF = 1e9; n void solve(); n void init(); n template <typename T, int N> n void dump(const array<T, N>& a) { n   for (int i = 0; i < a.size(); i++) { n     cerr << a[i] <<    ; n   } n   cerr << endl; n } n int main(int argc, char** argv) { n   cout.precision(15); n   int t = 1; n   cin >> t; n   init(); n   for (int tt = 1; tt <= t; tt++) { n     solve(); n   } n   return 0; n } n const int MX = 100500; n vector<int> delim[MX]; n int p[MX]; n long long troek[MX]; n long long dvoek(long long n) { return n * (n + 1) / 2; } n void go(int n) { n   auto& curr = delim[n]; n   int pr = p[n]; n   if (pr == 0) { n     if (n == 1) { n       curr = {1}; n     } else { n       curr = {1, n}; n     } n     return; n   } n   int cnt = 0; n   while (n % pr == 0) { n     n /= pr; n     cnt++; n   } n   curr.reserve((cnt + 1) * delim[n].size()); n   int mult = 1; n   for (int i = 0; i <= cnt; i++) { n     for (auto div : delim[n]) { n       curr.push_back(mult * div); n     } n     mult *= pr; n   } n   sort(curr.begin(), curr.end()); n } n void init() { n   for (int i = 1; i < MX; i++) { n     troek[i] = troek[i - 1] + dvoek(i); n   } n   for (int i = 2; i * i < MX; i++) { n     if (p[i] == 0) { n       for (int j = i; j * i < MX; j++) { n         p[i * j] = i; n       } n     } n   } n   for (int n = 1; n < MX; n++) { n     go(n); n   } n   for (int n = 1; n < MX; n++) { n     delim[n].push_back(int(1e9)); n   } n } n int cnt[10]; n int cnt2[10]; n int sum[10]; n int sum2[10]; n long long solve_true(int a, int b, int c) { n   vector<decltype(delim[a].cbegin())> x(3); n   x[0] = delim[a].begin(); n   x[1] = delim[b].begin(); n   x[2] = delim[c].begin(); n   memset(cnt, 0, sizeof(cnt)); n   while (true) { n     int z = *x[0]; n     for (int i = 1; i < 3; i++) { n       z = min(z, *x[i]); n     } n     if (z > MX) { n       break; n     } n     int mask = 0; n     for (int i = 0; i < 3; i++) { n       if (*x[i] == z) { n         mask |= 1 << i; n         x[i]++; n       } n     } n     cnt[mask]++; n   } n   for (int i = 1; i < 8; i++) { n     if (debug) cerr << cnt[i] <<    ; n     cnt2[i] = cnt[i] * cnt[i]; n     sum[i] = cnt[i] + sum[i - 1]; n     sum2[i] = cnt2[i] + sum2[i - 1]; n   } n   if (debug) cerr << endl; n   long long res = troek[cnt[7]]; n   res += dvoek(cnt[7]) * sum[6]; n   res += cnt[7] * (dvoek(cnt[3]) + dvoek(cnt[5]) + dvoek(cnt[6])); n   res += cnt[7] * (sum[6] * sum[6] - sum2[6]) / 2; n   for (int i = 0; i < 3; i++) { n     int bit = 1 << i; n     res += (cnt[bit | 1] + cnt[bit | 2] + cnt[bit | 4]) * dvoek(cnt[7 - bit]); n   } n   res += cnt[3] * cnt[5] * cnt[6]; n   res += (cnt[3] * cnt[5] + cnt[3] * cnt[6] + cnt[5] * cnt[6]) * n          (cnt[1] + cnt[2] + cnt[4]); n   res += (cnt[3] + cnt[5] + cnt[6]) * n          (cnt[1] * cnt[2] + cnt[1] * cnt[4] + cnt[2] * cnt[4]); n   res -= cnt[3] * cnt[1] * cnt[2] + cnt[5] * cnt[1] * cnt[4] + n          cnt[6] * cnt[2] * cnt[4]; n   res += cnt[1] * cnt[2] * cnt[4]; n   return res; n } n long long slow(int a, int b, int c) { n   set<vector<int>> s; n   for (int x = 1; x <= a; x++) { n     if (a % x) continue; n     for (int y = 1; y <= b; y++) { n       if (b % y) continue; n       for (int z = 1; z <= c; z++) { n         if (c % z) continue; n         vector<int> t = {x, y, z}; n         sort(begin(t), end(t)); n         s.insert(t); n       } n     } n   } n   return s.size(); n } n void stress(int n = 60) { n   debug = false; n   for (int a = 1; a <= n; a++) { n     for (int b = a; b <= n; b++) { n       for (int c = b; c <= n; c++) { n         if (slow(a, b, c) != solve_true(a, b, c)) { n           cerr << a <<     << b <<     << c << endl; n           cerr << slow(a, b, c) <<     << solve_true(a, b, c) << endl; n         } n       } n     } n   } n } n void solve() { n   int a, b, c; n   cin >> a >> b >> c; n   cout << solve_true(a, b, c) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, h[100005], p[100005], f[100005]; n bool check(long long x) { n   int now = 1; n   for (int i = 1; i <= n; i++) { n     long long rest = x - abs(h[i] - p[now]); n     if (rest < 0) continue; n     long long nowp = p[now]; n     while ((p[now] <= h[i] || p[now] - nowp <= rest || n             2 * (p[now] - h[i]) <= rest) && n            now <= m) n       now++; n     if (now == m + 1) return 1; n   } n   return now == m + 1; n } n int main() { n   scanf( %I64d%I64d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %I64d , &h[i]); n   for (int i = 1; i <= m; i++) scanf( %I64d , &p[i]); n   long long l = 0, r = 100000000000; n   while (l <= r) { n     long long mid = l + r >> 1; n     if (check(mid)) n       r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %I64d n , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[3][3], t[3][3]; n vector<char> vs, vt; n int main() { n   scanf( %s%s%s%s , s[0], s[1], t[0], t[1]); n   if (s[0][0] !=  X ) vs.push_back(s[0][0]); n   if (s[0][1] !=  X ) vs.push_back(s[0][1]); n   if (s[1][1] !=  X ) vs.push_back(s[1][1]); n   if (s[1][0] !=  X ) vs.push_back(s[1][0]); n   if (t[0][0] !=  X ) vt.push_back(t[0][0]); n   if (t[0][1] !=  X ) vt.push_back(t[0][1]); n   if (t[1][1] !=  X ) vt.push_back(t[1][1]); n   if (t[1][0] !=  X ) vt.push_back(t[1][0]); n   for (int i = 0; i < 3; i++) vs.push_back(vs[i]); n   for (int i = 0; i < 3; i++) { n     if (vs[i] == vt[0]) { n       bool ok = 1; n       for (int j = 0; j < 3; j++) { n         if (vs[i + j] != vt[j]) { n           ok = 0; n         } n       } n       printf( %s n , ok ?  YES  :  NO ); n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF32 = 0x3f3f3f3f; n const long long INF64 = 0x3f3f3f3f3f3f3f3f; n const int mod2 = 1e9 + 9; n const int mod = 1e9 + 7; n const int N = 2e5 + 5; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, k, m; n   long long sum = 0; n   cin >> n >> k >> m; n   vector<int> cost(k + 1); n   for (int i = 1; i <= k; ++i) { n     cin >> cost[i]; n     sum += cost[i]; n   } n   sort(cost.begin(), cost.end()); n   int ans = 0, temp, t, tm = m; n   for (int i = 0; i <= n && sum * i <= tm; ++i) { n     temp = (k + 1) * i; n     tm -= sum * i; n     for (int j = 1; j <= k && tm >= cost[j]; ++j) { n       t = min(tm / cost[j], n - i); n       temp += t; n       if (j == k) temp += t; n       tm -= t * cost[j]; n     } n     tm = m; n     ans = max(ans, temp); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool DBG = 0; n const int dbg = 0; n using ii = pair<int, int>; n using cd = int64_t; n cd gcd(cd a, cd b) { return b ? gcd(b, a % b) : a; } n using _LD = long double; n struct ld { n   cd a, b; n   ld(cd a, cd b = 1) : a(a), b(b) { reduce(); } n   ld operator-() const { return {-a, b}; } n   ld& reduce() { n     if (a) { n       cd g = gcd(a < 0 ? -a : a, b); n       a /= g, b /= g; n     } else n       b = 1; n     return *this; n   } n   ld& operator+=(ld o) { n     a = a * o.b + b * o.a, b *= o.b; n     return reduce(); n   } n   ld& operator-=(ld o) { n     a = a * o.b - b * o.a, b *= o.b; n     return reduce(); n   } n   ld& operator*=(ld o) { n     a *= o.a, b *= o.b; n     return reduce(); n   } n   ld operator*(ld o) const { return o *= *this; } n   ld operator+(ld o) const { return o += *this; } n   ld operator-(ld o) const { n     ld t = *this; n     return t -= o; n   } n   bool operator==(ld o) const { return a * o.b == b * o.a; } n   bool operator<(ld o) const { return (_LD(a) / b) < (_LD(o.a) / o.b); } n   bool operator<=(ld o) const { return *this == o || *this < o; } n   bool operator>(ld o) const { return !(*this <= o); } n   bool operator>=(ld o) const { return !(*this < o); } n   operator _LD() const { return _LD(a) / b; } n }; n ld abs(ld a) { return {a.a < 0 ? -a.a : a.a, a.b}; } n using cord = ld; n using vi = vector<int>; n using vii = vector<ii>; n const ld eps = {0, 1}; n struct po { n   cord X, Y; n   po operator-(po b) { return {X - b.X, Y - b.Y}; } n   po& operator-=(po b) { n     X -= b.X, Y -= b.Y; n     return *this; n   } n   po operator-() const { return {-X, -Y}; } n   ld cross(po b) const { return X * b.Y - Y * b.X; } n   bool operator<(const po& a) const { n     return (X == a.X) ? (Y < a.Y) : (X < a.X); n   } n   bool operator<=(const po& a) const { n     return (X == a.X) ? (Y <= a.Y) : (X <= a.X); n   } n   bool operator==(const po& a) const { return X == a.X && Y == a.Y; } n   operator complex<_LD>() const { return complex<_LD>(X, Y); } n   _LD arg() const { return ::arg(complex<_LD>(*this)); } n }; n const _LD _EPS = 1e-9; n bool nb = 0; n struct acmp { n   bool operator()(const po& a, const po& b) const { n     if (a == b) return 0; n     _LD A = a.arg(), B = b.arg(); n     return nb ? (A < B - _EPS) : (A < B); n   } n } _acmp; n bool argeq(po a, po b) { return (!_acmp(a, b)) && (!_acmp(b, a)); } n using ms = map<po, int, acmp>; n using poly = vector<po>; n ld abs(po a) { return a.X * a.X + a.Y * a.Y; } n using i64 = int64_t; n ostream& operator<<(ostream& os, ld first) { n   ld t = first; n   t *= {1, 1}; n   return os <<  (  << i64(t.a) <<  /  << i64(t.b) <<  ) ; n } n ostream& operator<<(ostream& os, po a) { n   return os << (arg(complex<_LD>(a)) * 180 / 3.14159265358979323846); n } n template <class T> n ostream& operator<<(ostream& os, vector<T> v) { n   os <<  [ ; n   for (auto& V : v) os << V <<  , ; n   return os <<   b  <<  ] ; n } n const ld ZERO = {0, 1}; n int orient(po a, po b, po c) { n   cord r = (b - a).cross(c - a); n   return r == ZERO ? 0 : (r < ZERO ? -1 : 1); n } n const po Z = {0, 0}; n int isfar(ms& m, ms::iterator it) { n   if (m.size() == 1) return 0; n   auto nx = it; n   ++nx; n   _LD theta = 0; n   if (nx == m.end()) nx = m.begin(), theta = 2 * 3.14159265358979323846; n   theta += nx->first.arg() - it->first.arg(); n   if (abs(theta - 3.14159265358979323846) <= 1e-8 || n       theta >= 3.14159265358979323846 + 1e-8) n     return 1; n   return 0; n } n ms::iterator prev(ms& m, ms::iterator it) { n   if (it == m.begin()) return --m.end(); n   return --it; n } n void insert(ms& m, po p, int& far) { n   if (!m.empty()) { n     auto it = m.find(p); n     if (it != m.end()) return void(++it->second); n     far -= isfar(m, prev(m, m.lower_bound(p))); n     ++m[p]; n     far += isfar(m, it = m.find(p)); n     far += isfar(m, prev(m, it)); n     if (far) far = 1; n   } else n     ++m[p], far = 1; n } n void remove(ms& m, po p, int& far) { n   if (m.size() == 1) { n     far = 1; n     if (!(--m[p])) m.erase(p); n     return; n   } n   auto it = prev(m.find(p)); n   far -= isfar(m, it); n   if (!(--m[p])) m.erase(m.find(p)); n   far |= isfar(m, prev(m, m.lower_bound(p))); n   if (far) far = 1; n } n struct S { n   int zcnt, far; n   int64_t ls; n   po z; n   map<po, int, acmp> ps; n   S(po z) : zcnt(0), far(1), ls(0), z(z) {} n   void insert(po p) { n     if (DBG) cout <<  inserting   << p - z << endl; n     if ((p -= z) == Z) return void(++zcnt); n     auto it = ps.find(-p); n     if (it != ps.end()) { n       if (DBG) cout <<  ls receives   << it->second << endl; n       ls += it->second; n     } else if (DBG) n       cout <<  ls receieves 0 n ; n     ::insert(ps, p, far); n   } n   void erase(po p) { n     if (DBG) cout <<  erasing   << p - z << endl; n     if ((p -= z) == Z) return void(--zcnt); n     auto it = ps.find(-p); n     if (DBG) cout <<  ls -= ps.find(  << -p <<  ) n ; n     if (it != ps.end()) { n       if (DBG) cout <<  ls loses   << it->second << endl; n       ls -= it->second; n     } else if (DBG) n       cout <<  ls loses 0 n ; n     ::remove(ps, p, far); n   } n   int query() { n     if (DBG) cout <<  zcnt=  << zcnt <<  ,ls=  << ls << endl; n     if (zcnt) return 1; n     if (ls) return 2; n     int N = ps.size(); n     if (N < 3) return 0; n     return far ? 0 : 3; n   } n }; n po cnv(int s, int p, int g) { return {ld(s, s + p + g), ld(p, s + p + g)}; } n int main() { n   if (dbg) n     freopen( inb.txt ,  r , stdin); n   else n     cin.tie(0)->sync_with_stdio(0); n   int s, p, g; n   cin >> s >> p >> g; n   S d(cnv(s, p, g)); n   int n; n   cin >> n; n   if (n <= 5000) nb = 1; n   char op; n   vector<tuple<int, int, int>> bs; n   for (auto i = 0; i != n; ++i) { n     if (DBG) cout <<  i=  << i << endl; n     cin >> op; n     if (op ==  A ) { n       cin >> s >> p >> g; n       bs.emplace_back(s, p, g); n       d.insert(cnv(s, p, g)); n     } else { n       cin >> s; n       tie(s, p, g) = bs[s - 1]; n       d.erase(cnv(s, p, g)); n     } n     cout << d.query() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAXINT = 2147483640; n const long long MAXLL = 9223372036854775800LL; n const long long MAXN = 1123456; n const long long MOD = 1123456769; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   srand(time(0)); n   vector<int> v; n   int n, m, i, h; n   char c; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> h >> c >> m; n     v.push_back(h * 60 + m); n   } n   sort(v.begin(), v.end()); n   v.push_back(24 * 60 + v[0]); n   int mx = 0; n   for (i = 1; i < v.size(); i++) { n     mx = max(mx, v[i] - v[i - 1] - 1); n   } n   h = mx / 60; n   m = mx % 60; n   if (h < 10) cout << 0; n   cout << h <<  : ; n   if (m < 10) cout << 0; n   cout << m <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000100; n int N; n long long arr[MAXN]; n long long csum; n long long nv[MAXN]; n long long dp[MAXN][2]; n long long gogo(long long x) { n   nv[0] = 0; n   for (int i = 0; i < N; i++) { n     nv[i + 1] = (nv[i] + arr[i]) % x; n   } n   for (int i = 0; i <= N; i++) { n     dp[i][0] = dp[i][1] = 3e18; n   } n   dp[0][0] = nv[1]; n   dp[0][1] = (x - nv[1]); n   for (int i = 1; i < N; i++) { n     if ((x - nv[i]) + nv[i + 1] <= arr[i]) n       dp[i][0] = min(dp[i][0], dp[i - 1][1] + nv[i + 1]); n     if (nv[i + 1] - nv[i] <= arr[i]) n       dp[i][0] = min(dp[i][0], dp[i - 1][0] + nv[i + 1]); n     if ((x - nv[i]) + (nv[i + 1] - x) <= arr[i]) n       dp[i][1] = min(dp[i][1], dp[i - 1][1] + (x - nv[i + 1])); n     dp[i][1] = min(dp[i][1], dp[i - 1][0] + (x - nv[i + 1])); n   } n   return min(dp[N - 1][1], dp[N - 1][0]); n } n int main() { n   ios_base::sync_with_stdio(0); n   csum = 0; n   cin >> N; n   for (int i = 0; i < N; i++) { n     cin >> arr[i]; n     csum += arr[i]; n   } n   vector<long long> v; n   for (long long i = 2; i < MAXN; i++) { n     if (csum % i == 0) { n       v.push_back(i); n       while (csum % i == 0) csum /= i; n     } n   } n   if (csum > 1) v.push_back(csum); n   long long ans = 3e18; n   for (long long f : v) ans = min(ans, gogo(f)); n   if (ans > 2e18) n     cout <<  -1 n ; n   else n     cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200033; n int n; n bool col[MAXN]; n struct edge { n   int t, nx; n } E[MAXN << 1], E_[MAXN << 1]; n int first[MAXN], edges_nums = 0; n inline void add(int f, int t) { n   E[++edges_nums].t = t; n   E[edges_nums].nx = first[f]; n   first[f] = edges_nums; n } n int first_[MAXN], edges_nums_ = 0; n inline void add_(int f, int t) { n   E_[++edges_nums_].t = t; n   E_[edges_nums_].nx = first_[f]; n   first_[f] = edges_nums_; n } n bool check[MAXN]; n int nums_of_gp, bel[MAXN]; n void uni(int e, int c, int fa) { n   int t; n   for (int i = first[e]; i; i = E[i].nx) { n     t = E[i].t; n     if (col[t] == c && !check[t] && t != fa) { n       check[t] = 1; n       bel[t] = nums_of_gp; n       uni(t, c, e); n     } n   } n } n int fa[MAXN]; n int find(int e) { return fa[e] == e ? e : fa[e] = find(fa[e]); } n void union_(int a, int b) { n   int e1 = find(a); n   int e2 = find(b); n   if (e1 != e2) fa[e1] = e2; n } n int MX, S; n void dfs(int sum, int e, int fa) { n   if (sum > MX) { n     MX = sum; n     S = e; n   } n   int t; n   for (int i = first_[e]; i; i = E_[i].nx) { n     t = E_[i].t; n     if (t != fa) dfs(sum + 1, t, e); n   } n } n int main() { n   int a, b; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &col[i]); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &a, &b); n     add(a, b); n     add(b, a); n   } n   for (int i = 1; i <= n; i++) { n     fa[i] = i; n     if (!check[i]) { n       nums_of_gp++; n       bel[i] = nums_of_gp; n       uni(i, col[i], 0); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = first[i]; j; j = E[j].nx) { n       int f = bel[i]; n       int t = bel[E[j].t]; n       if (find(f) != find(t)) { n         union_(f, t); n         add_(f, t); n         add_(t, f); n       } n     } n   } n   dfs(0, 1, 0); n   MX = 0; n   dfs(0, S, 0); n   if (nums_of_gp > 1) n     printf( %d , (MX + 1) >> 1); n   else n     printf( 0 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[100030]; n int main() { n   while (gets(str) != NULL) { n     int len = strlen(str), m, n1 = 0, n2 = 0; n     for (int i = 0; i < len; i++) n       if (str[i] >=  a  && str[i] <=  z ) n         n2++; n       else n         n1++; n     int m1 = 0, m2 = 0; n     m = n1; n     for (int i = 1; i <= len; i++) { n       int k = 0; n       if (str[i - 1] >=  a  && str[i - 1] <=  z ) n         m2++; n       else n         m1++; n       k += n1 - m1; n       k += m2; n       if (k < m) m = k; n     } n     printf( %d n , m); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 100 + 10; n pair<int, int> a[MAX_N]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i].first, a[i].second = i; n   sort(a, a + n); n   int ans = 0; n   for (int i = 0; i < n && k >= a[i].first; i++, ans++) k -= a[i].first; n   cout << ans << endl; n   for (int i = 0; i < ans; i++) cout << a[i].second + 1 <<    ; n } n