#include <bits/stdc++.h> n using namespace std; n struct Point { n   int x; n   int y; n   int index; n   double angle; n   void Translate(int dx, int dy) { n     x += dx; n     y += dy; n   } n   void CalculateAngle() { angle = atan2(y, x); } n   bool operator<(const Point& p) const { return angle < p.angle; } n }; n std::ostream& operator<<(std::ostream& stream, const Point& p) { n   stream <<  (  << p.x <<  ,  << p.y <<  ) ; n } n int n, m; n Point points[250]; n Point ccPoints[250]; n queue<int> Q[250]; n vector<int> visibleOut[250], visibleIn[250]; n long long dp[250][250][51]; n long long maxArea[51]; n long long sol = 0; n bool IsLeftTurn(Point p1, Point p2, Point p3) { n   Point v1; n   v1.x = p2.x - p1.x; n   v1.y = p2.y - p1.y; n   Point v2; n   v2.x = p3.x - p2.x; n   v2.y = p3.y - p2.y; n   return (long long)v1.x * v2.y - (long long)v1.y * v2.x > 0; n } n inline long long TriangleArea(Point p1, Point p2) { n   return (long long)p1.x * p2.y - (long long)p1.y * p2.x; n } n void Proceed(int i, int j) { n   while (Q[i].size() > 0 && n          IsLeftTurn(ccPoints[Q[i].front()], ccPoints[i], ccPoints[j])) { n     Proceed(Q[i].front(), j); n     Q[i].pop(); n   } n   visibleOut[i].push_back(j); n   visibleIn[j].push_back(i); n   Q[j].push(i); n } n inline void MakeVisibilityGraph(int rightCount) { n   for (int i = 0; i < rightCount; i++) { n     while (!Q[i].empty()) { n       Q[i].pop(); n     } n     visibleIn[i].clear(); n     visibleOut[i].clear(); n   } n   for (int i = 0; i < rightCount - 1; i++) { n     Proceed(i, i + 1); n   } n } n inline void MaxArea(int rightCount) { n   for (int i = 0; i < rightCount; i++) { n     for (int j = 0; j < visibleOut[i].size(); j++) { n       for (int k = 0; k <= m; k++) dp[i][visibleOut[i][j]][k] = 0; n     } n   } n   for (int i = rightCount - 1; i >= 0; i--) { n     for (int j = 0; j <= m; j++) maxArea[j] = 0; n     int outIndex = visibleOut[i].size() - 1; n     for (int inIndex = visibleIn[i].size() - 1; inIndex >= 0; inIndex--) { n       dp[visibleIn[i][inIndex]][i][0] = 0; n       dp[visibleIn[i][inIndex]][i][1] = n           TriangleArea(ccPoints[visibleIn[i][inIndex]], ccPoints[i]); n       for (int j = 2; j <= m; j++) n         dp[visibleIn[i][inIndex]][i][j] = n             maxArea[j - 1] > 0 n                 ? maxArea[j - 1] + n                       TriangleArea(ccPoints[visibleIn[i][inIndex]], ccPoints[i]) n                 : 0; n       while (outIndex >= 0 && n              IsLeftTurn(ccPoints[visibleIn[i][inIndex]], ccPoints[i], n                         ccPoints[visibleOut[i][outIndex]])) { n         for (int j = 1; j <= 50; j++) { n           if (dp[i][visibleOut[i][outIndex]][j - 1] > maxArea[j - 1]) { n             maxArea[j - 1] = dp[i][visibleOut[i][outIndex]][j - 1]; n             dp[visibleIn[i][inIndex]][i][j] = n                 maxArea[j - 1] + n                 TriangleArea(ccPoints[visibleIn[i][inIndex]], ccPoints[i]); n           } n         } n         outIndex--; n       } n     } n   } n   for (int i = 0; i < rightCount; i++) { n     for (int j = 0; j < visibleOut[i].size(); j++) { n       sol = max(sol, dp[i][visibleOut[i][j]][m - 2]); n     } n   } n } n inline void ProcessLeftmostPoint(int leftIndex) { n   int rightCount = 0; n   for (int i = 0; i < n; i++) { n     if (i != leftIndex && points[i].x >= points[leftIndex].x) { n       ccPoints[rightCount] = points[i]; n       ccPoints[rightCount].Translate(-points[leftIndex].x, n                                      -points[leftIndex].y); n       ccPoints[rightCount].CalculateAngle(); n       ++rightCount; n     } n   } n   sort(ccPoints, ccPoints + rightCount); n   MakeVisibilityGraph(rightCount); n   MaxArea(rightCount); n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &points[i].x, &points[i].y); n     points[i].index = i; n   } n   for (int i = 0; i < n; i++) { n     ProcessLeftmostPoint(i); n   } n   cout << sol / 2 << (sol % 2 == 1 ?  .50  :  .00 ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 1e18; n const long long MAXN = 110; n long long N; n vector<string> query(long long a, long long b) { n   cout <<  ?  n        <<     << a <<     << b <<   n ; n   fflush(stdout); n   long long sz = b - a + 1; n   vector<string> vect; n   for (long long i = 0; i < sz * (sz + 1) / 2; i++) { n     string cad; n     cin >> cad; n     vect.push_back(cad); n   } n   return vect; n } n void res(string cad) { n   cout <<  !  n        <<     << cad <<   n ; n   fflush(stdout); n } n struct edge { n   long long nodo; n   char a, b; n }; n vector<edge> L[MAXN]; n char xx, yy; n void solve(vector<string> vect, long long des) { n   vector<string> arr[MAXN]; n   long long sz = 0; n   for (long long i = 0; i < vect.size(); i++) { n     arr[vect[i].size()].push_back(vect[i]); n     sz = max(sz, (long long)vect[i].size()); n   } n   map<char, long long> cont; n   for (long long i = 0; i < sz; i++) { n     cont[arr[sz][0][i]]++; n   } n   vector<edge> vv; n   for (long long i = 2; i <= sz / 2 + 1; i++) { n     map<char, long long> mp; n     map<char, long long> temp; n     for (auto x : cont) { n       temp[x.first] = x.second * i; n     } n     for (long long i = 0; i < vv.size(); i++) { n       temp[vv[i].a] += i + 1; n       temp[vv[i].b] += i + 1; n     } n     for (auto cad : arr[i]) { n       for (long long x = 0; x < i; x++) { n         mp[cad[x]]++; n       } n     } n     char a, b; n     for (auto x : temp) { n       if (x.second > mp[x.first]) { n         a = x.first; n         break; n       } n     } n     temp[a]--; n     for (auto x : temp) { n       if (x.second > mp[x.first]) { n         b = x.first; n         break; n       } n     } n     cont[a]--; n     cont[b]--; n     vv.push_back({0, a, b}); n     L[i - 1 + des].push_back({sz - i + 2 + des, a, b}); n     L[sz - i + 2 + des].push_back({i - 1 + des, a, b}); n     if (i == 2 && des == 0) { n       xx = a; n       yy = b; n     } n   } n } n char ss[MAXN]; n void DFS(long long nodo, long long padre, char c) { n   ss[nodo] = c; n   for (auto v : L[nodo]) { n     if (v.nodo != padre) { n       char temp; n       if (v.a == c) n         temp = v.b; n       else n         temp = v.a; n       DFS(v.nodo, nodo, temp); n     } n   } n } n int32_t main() { n   cin >> N; n   vector<string> vect = query(1, N); n   if (N == 1) { n     res(vect[0]); n     return 0; n   } n   solve(vect, 0); n   vect = query(2, N); n   solve(vect, 1); n   if (xx != yy) { n     vect = query(1, 1); n     DFS(1, -1, vect[0][0]); n     string st; n     for (long long i = 1; i <= N; i++) st += ss[i]; n     res(st); n   } else { n     DFS(1, -1, xx); n     string st; n     for (long long i = 1; i <= N; i++) st += ss[i]; n     for (long long i = 0; i < N; i++) { n       if (st[i] != st[N - i - 1]) { n         vect = query(i + 1, i + 1); n         char cc = vect[0][0]; n         if (st[i] != cc) { n           reverse(st.begin(), st.end()); n         } n         break; n       } n     } n     res(st); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void task(); n int main() { n   srand(time(0)); n   task(); n   return 0; n } n const int INF = 0x3f3f3f3f; n const int N = 1e5 + 10; n const int M = 1e5 + 10; n int n, x; n int l[N]; n int r[N]; n long long int dp[2 * N + 10]; n vector<int> v; n void task() { n   scanf( %d %d , &n, &x); n   v.push_back(x); n   for (int i = 0; i < n; ++i) { n     scanf( %d %d , &l[i], &r[i]); n     v.push_back(l[i]); n     v.push_back(r[i]); n   } n   sort((v.begin()), (v.end())); n   int m = ((int)((v).size())); n   memset(dp, 0x3f, sizeof(dp)); n   for (int i = 0; i < m; ++i) n     if (v[i] == x) dp[i] = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 1; j < m; ++j) dp[j] = min(dp[j], dp[j - 1] + v[j] - v[j - 1]); n     for (int j = m - 2; j >= 0; --j) n       dp[j] = min(dp[j], dp[j + 1] + v[j + 1] - v[j]); n     for (int j = 0; j < m; ++j) n       if (v[j] < l[i]) n         dp[j] += l[i] - v[j]; n       else if (v[j] > r[i]) n         dp[j] += v[j] - r[i]; n   } n   cout << *min_element(dp, dp + m); n } n


#include <bits/stdc++.h> n #define pb push_back n #define mp make_pair n #define fr first n #define sc second n #define MOD 1000000007 n #define len(x) x.size() n #define min3(a, b, c) min(a, min(b, c)) n #define max3(a, b, c) max(a, max(b, c)) n #define all(v) v.begin(), v.end() n #define alla(a,n) a, a + n n  n using namespace std; n  n typedef long long ll; n typedef unsigned long long ull; n typedef pair<ll, ll> pll; n typedef vector<ll> vll; n typedef vector<pll> vpll; n typedef vector<vll> vvll; n typedef vector<string> vs; n  n  n void run() n { n #ifndef ONLINE_JUDGE n  freopen( input.txt ,  r , stdin) ; n  freopen( output.txt ,  w , stdout) ; n #endif n  ios_base::sync_with_stdio(false); n  cin.tie(NULL) ; cout.tie(NULL) ; n } n  n void solve() n { n  int t; n  cin >> t; n  while (t--) n  { n   int n, d; n   cin >> n >> d; n   int arr[n]; n   for (int i = 0; i < n; i++) n   { n    cin >> arr[i]; n   } n  n   int flag = 0, min_val = 0; n  n   for (int i = 0; i < n; i++) n   { n    if (arr[i] > d) n    { n     flag = 1; n    } n   } n  n   if (flag == 0) n   { n    cout <<  YES  << endl; n   } n   else n   { n    sort(arr, arr + n); n    // cout << arr[0] << arr[1]; n    min_val = arr[0] + arr[1]; n  n    if (min_val <= d) n    { n     cout <<  YES  << endl; n    } n    else n    { n     cout <<  NO  << endl; n    } n   } n  } n } n  n int32_t main() n { n  run(); n  solve(); n  n  return 0 ; n }


#include <bits/stdc++.h> n using namespace std; n long long Answer = 0, idx[300100], edges[300100], dis[300100]; n int n, m, x, y, w, u; n vector<pair<pair<int, int>, int> > v[300100]; n priority_queue<pair<int, int>, vector<pair<int, int> >, n                greater<pair<int, int> > > n     Q; n void dik() { n   for (int i = 1; i <= n; i++) dis[i] = 1e18; n   Q.push({0, u}); n   dis[u] = 0; n   while (!Q.empty()) { n     pair<int, int> x = Q.top(); n     Q.pop(); n     for (int i = 0; i < v[x.second].size(); i++) { n       int V = v[x.second][i].first.first; n       int cost = v[x.second][i].first.second; n       int id = v[x.second][i].second; n       if (dis[V] > dis[x.second] + cost || n           (dis[V] == dis[x.second] + cost && edges[idx[V]] > edges[id])) { n         dis[V] = dis[x.second] + cost; n         idx[V] = id; n         Q.push({dis[V], V}); n       } n     } n   } n } n int main() { n   memset(idx, -1, sizeof(idx)); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> x >> y >> w; n     v[x].push_back({{y, w}, i + 1}); n     v[y].push_back({{x, w}, i + 1}); n     edges[i + 1] = w; n   } n   cin >> u; n   dik(); n   for (int i = 1; i <= n; i++) { n     if (idx[i] != -1) { n       Answer += edges[idx[i]]; n     } n   } n   cout << Answer << endl; n   for (int i = 1; i <= n; i++) { n     if (idx[i] != -1) cout << idx[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int nxt() { n   int x; n   cin >> x; n   return x; n } n int gettm(char c) { n   if (c ==  G ) { n     return 5; n   } else if (c ==  W ) { n     return 3; n   } else if (c ==  L ) { n     return 1; n   } else { n     assert(false); n   } n } n int main() { n   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n   cin.sync_with_stdio(0); n   int n = nxt(); n   vector<long long> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   string s; n   cin >> s; n   deque<pair<int, long long>> water_shit, ground_shit; n   bool was_water = false; n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  G ) { n       ground_shit.push_back({i, a[i]}); n     } else if (s[i] ==  W ) { n       was_water = true; n       water_shit.push_back({i, a[i]}); n     } else { n       long long need = a[i]; n       while (need && !water_shit.empty()) { n         long long tmp = min(need, water_shit.back().second); n         need -= tmp; n         water_shit.back().second -= tmp; n         ans += 4 * tmp; n         if (water_shit.back().second == 0) { n           water_shit.pop_back(); n         } n       } n       while (need && !ground_shit.empty()) { n         long long tmp = min(need, ground_shit[0].second); n         need -= tmp; n         ground_shit[0].second -= tmp; n         ans += 6 * tmp; n         if (ground_shit[0].second == 0) { n           ground_shit.pop_front(); n         } n       } n       if (was_water) { n         ans += 4 * need; n       } else { n         ans += 6 * need; n       } n     } n   } n   long long cur = 0; n   long long totally_used = 0; n   for (int i = 0, j = 0; i < (int)ground_shit.size(); ++i) { n     while (j < (int)water_shit.size() && n            water_shit[j].first < ground_shit[i].first) { n       cur += water_shit[j].second; n       ++j; n     } n     long long tmp = min(cur, ground_shit[i].second); n     ground_shit[i].second -= tmp; n     cur -= tmp; n     totally_used += tmp; n     ans += 4 * tmp; n     ans += ground_shit[i].second * 3; n   } n   totally_used *= -1; n   for (auto p : water_shit) { n     totally_used += p.second; n   } n   ans += 2 * totally_used; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   vector<int> vec(n + 1, 0); n   vector<pair<int, pair<int, int>>> arr; n   for (int i = 0; i < m; i++) { n     int si, di, ci; n     scanf( %d %d %d , &si, &di, &ci); n     arr.push_back(make_pair(di, make_pair(si, ci))); n   } n   bool is_possible = true; n   for (int i = 1; i <= n; i++) { n     bool exam_time = false; n     for (int j = 0; j < m; j++) { n       if (arr[j].first == i) { n         exam_time = true; n         if (arr[j].second.second != 0) { n           is_possible = false; n         } else { n           vec[i] = m + 1; n         } n         break; n       } n     } n     if (exam_time) continue; n     int best = -1, mini = (int)1e9; n     for (int j = 0; j < m; j++) { n       if (arr[j].first > i && arr[j].second.first <= i && n           arr[j].second.second != 0) { n         if (arr[j].first < mini) { n           mini = arr[j].first; n           best = j; n         } n       } n     } n     if (best != -1) { n       vec[i] = best + 1; n       arr[best].second.second--; n     } n   } n   if (is_possible) { n     for (int i = 1; i <= n; i++) { n       printf( %d  , vec[i]); n     } n   } else { n     printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[1005], half; n pair<int, int> info[1005]; n bool cmp(pair<int, int> A, pair<int, int> B) { n   if (A.first == B.first) n     return A.second < B.second; n   else n     return A.first < B.first; n } n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , arr + i); n     half = arr[i] / 2 + (arr[i] % 2); n     info[i] = pair<int, int>(half, arr[i]); n   } n   sort(info, info + n, cmp); n   int lvl = k, cnt = 0; n   for (int i = 0; i < n; i++) { n     if (info[i].first <= lvl) { n       lvl = max(k, info[i].second); n     } else { n       int num = info[i].second; n       while (lvl < num) { n         num = (num / 2) + (num % 2); n         if (lvl < num) cnt++; n       } n       lvl = info[i].second; n     } n   } n   printf( %d , cnt); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, m, i, min, count, a[101], sum, count0; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     count = 0; n     sum = 0; n     count0 = 0; n     for (i = 0; i < (n * m); i++) { n       cin >> a[i]; n       if (a[i] < 0) { n         count++; n         a[i] = -1 * a[i]; n       } n       if (a[i] == 0) count0 = 1; n       sum += a[i]; n     } n     if (count % 2 != 0 && count0 != 1) { n       min = a[0]; n       for (i = 0; i < (n * m); i++) { n         if (a[i] < min) min = a[i]; n       } n       cout << sum - (2 * min) << endl; n     } else n       cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > v; n bool cmp(pair<long long, long long> &a, pair<long long, long long> &b) { n   if (a.first == b.first) return a.second < b.second; n   return b.first < a.first; n } n long long func(pair<long long, long long> &a, pair<long long, long long> &b, n                long long d) {} n int main() { n   long long n; n   long long m, d; n   scanf( %lld %lld %lld , &n, &m, &d); n   pair<long long, long long> a[n]; n   pair<long long, long long> b[m]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n   } n   sort(a, a + n, cmp); n   for (long long i = 0; i < m; i++) { n     cin >> b[i].first >> b[i].second; n   } n   sort(b, b + m, cmp); n   long long posi = 1, totalw = 0, posj = 1, totals = 0; n   totalw = a[0].second + b[0].second; n   totals = a[0].first + b[0].first; n   if (totalw > d) return cout << 0, 0; n   while (posj < m && totalw + b[posj].second <= d) { n     totalw += b[posj].second; n     totals += b[posj].first; n     posj++; n   } n   long long mx = totals; n   while (posi < n) { n     totalw += a[posi].second; n     totals += a[posi].first; n     posi++; n     while (totalw > d) { n       if (posj <= 1) return cout << mx, 0; n       posj--; n       totals -= b[posj].first; n       totalw -= b[posj].second; n     } n     mx = max(mx, totals); n   } n   cout << mx << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 10; n const long long inf = 1e15; n const int mod = 1e6; n void faster() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n const long long N = 3e5 + 10; n int n, m; n long long a[N]; n int main() { n   faster(); n   cin >> n; n   if (n % 10 < 10 - (n % 10)) n     cout << n - n % 10 << endl; n   else n     cout << n + (10 - (n % 10)) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000000 + 10, MOD = (int)1e+9 + 7; n long long fac[MAXN], Ifac[MAXN], T; n int k, w; n long long Pow(long long a, long long b) { n   long long d = 1; n   for (; b; d = (b & 1) ? d * a % MOD : d, a = a * a % MOD, b /= 2) n     ; n   return d; n } n long long A(int x, int y) { return fac[x] * Ifac[x - y] % MOD; } n int main() { n   cin >> k >> w; n   fac[0] = Ifac[0] = 1; n   for (int i = 1; i <= k; ++i) n     fac[i] = fac[i - 1] * i % MOD, n     Ifac[i] = Ifac[i - 1] * Pow(i, MOD - 2) % MOD; n   long long CC = 0, t = Pow(k, w - 2); n   for (int i = 1; i <= k; ++i) { n     if (i < w) n       CC = (CC + A(k, i) * A(k, i) % MOD * t % MOD * i % MOD) % MOD; n     else n       CC = (CC + A(k, i) * A(k + w - 1 - i, w - 1) % MOD * w) % MOD; n     if (i < k) { n       t = t * Pow(k, MOD - 2) % MOD; n       if (i + 2 <= w) n         CC = (CC + A(k, i) * A(k, i + 1) % MOD * t % MOD * i % MOD) % MOD; n       else n         CC = n             (CC + A(k, i + 1) * A(k - i + w - 2, w - 2) % MOD * (w - 1) % MOD) % n             MOD; n     } n   } n   cout << (CC + MOD) % MOD << endl; n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long int a, b, c, d, x; n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %lld%lld%lld%lld , &a, &b, &c, &d); n     x = a / d + (((a / d) / b) * c); n     printf( %lld n , x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[4]; n   int t = 0; n   int s = 0; n   for (int i = 0; i < 4; i++) cin >> arr[i]; n   for (int m = 0; m < 4; m++) { n     for (int j = 0; j < 4; j++) { n       for (int k = 0; k < 4; k++) { n         if (arr[m] < arr[j] + arr[k] && arr[j] < arr[m] + arr[k] && n             arr[k] < arr[m] + arr[j] && m != j && j != k && k != m) { n           t += 1; n         } else if (arr[m] == arr[j] + arr[k] && m != j && j != k && k != m) { n           s += 1; n         } n       } n     } n   } n   if (t == 0 && s != 0) n     cout <<  SEGMENT ; n   else if (t != 0) n     cout <<  TRIANGLE ; n   else if (t == 0 && s == 0) n     cout <<  IMPOSSIBLE ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9 + 7; n const double EPS = 1e-9; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long x, y, z; n   cin >> x >> y >> z; n   long long sasha = x % z; n   long long masha = y % z; n   if ((sasha + masha) >= z) { n     cout << ((x + y) / z) <<     << (z - max(sasha, masha)); n   } else n     cout << (x / z + y / z) <<     << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n struct state { n   int l, r, v, d; n } node[N]; n int n, m, cnt, bit[2][N * 2]; n vector<int> g[N]; n void add(int x, int val, int *b) { n   while (x <= n * 2) { n     b[x] += val; n     x += (x & (-x)); n   } n } n int get(int x, int *b) { n   int ans = 0; n   while (x > 0) { n     ans += b[x]; n     x -= (x & (-x)); n   } n   return ans; n } n void dfs(int c, int fa, int d) { n   node[c].l = cnt++; n   node[c].d = d; n   for (int i = 0; i < g[c].size(); i++) { n     if (g[c][i] == fa) continue; n     dfs(g[c][i], c, 1 - d); n   } n   node[c].r = cnt++; n } n void init() { n   cnt = 1; n   scanf( %d%d , &n, &m); n   memset(bit, 0, sizeof(bit)); n   for (int i = 1; i <= n; i++) scanf( %d , &node[i].v); n   int a, b; n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &a, &b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs(1, -1, 0); n } n void solve() { n   int o, a, b; n   for (int i = 0; i < m; i++) { n     scanf( %d , &o); n     if (o == 1) { n       scanf( %d%d , &a, &b); n       add(node[a].l, b, bit[node[a].d]); n       add(node[a].r + 1, -b, bit[node[a].d]); n     } else { n       scanf( %d , &a); n       printf( %d n , node[a].v + get(node[a].l, bit[node[a].d]) - n                          get(node[a].l, bit[1 - node[a].d])); n     } n   } n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t, n, x; n   cin >> t; n   while (t--) { n     cin >> n; n     long long m[n + 5]; n     bool flag[n + 5]; n     memset(m, 0, sizeof(m)); n     memset(flag, false, sizeof(flag)); n     for (long long i = 0; i < n; i++) { n       cin >> x; n       v.push_back(x); n       m[x]++; n     } n     long long mx = *max_element(v.begin(), v.end()); n     for (long long i = 0; i < n; i++) { n       long long sum = v[i]; n       for (long long j = i + 1; j < n; j++) { n         sum += v[j]; n         if (sum > mx) break; n         if (m[sum] && !flag[sum]) flag[sum] = true; n       } n     } n     long long ans = 0; n     for (long long i = 0; i <= n; i++) { n       if (flag[i]) ans += m[i]; n     } n     cout << ans << endl; n     v.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n const int MAX_N = 100005; n long long cost[MAX_N]; n int r[MAX_N]; n struct dish { n   int idx, cnt; n   long long c; n   bool operator<(const dish &d) const { n     if (c != d.c) return c < d.c; n     return idx < d.idx; n   } n }; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> r[i]; n   for (int i = 0; i < n; ++i) cin >> cost[i]; n   multiset<dish> s; n   for (int i = 0; i < n; ++i) { n     s.insert({i, r[i], cost[i]}); n   } n   vector<long long> ans(m); n   for (int i = 0; i < m; ++i) { n     long long tempCost = 0; n     int dIdx, dCnt; n     cin >> dIdx >> dCnt; n     --dIdx; n     auto it = s.find({dIdx, r[dIdx], cost[dIdx]}); n     if (it != s.end()) { n       dish d = *it; n       s.erase(it); n       int buy = min(dCnt, r[dIdx]); n       d.cnt -= buy; n       r[dIdx] -= buy; n       dCnt -= buy; n       tempCost += d.c * buy; n       if (d.cnt) s.insert(d); n     } n     while (!s.empty() && dCnt) { n       auto it = s.begin(); n       dish d = *it; n       s.erase(it); n       int buy = min(dCnt, d.cnt); n       d.cnt -= buy; n       r[d.idx] -= buy; n       dCnt -= buy; n       tempCost += d.c * buy; n       if (d.cnt) s.insert(d); n     } n     if (dCnt) break; n     ans[i] = tempCost; n   } n   for (int i = 0; i < m; ++i) cout << ans[i] <<   n ; n   return 0; n } n


/* -> Written by <- n    ----------- n   |K_A_Z_A_M_A| n   |___________| n   |    ___    | n   |   (^_^)   | n   |  /( | )  | n   |____|_|____| n     H O A N G n */ n  n #include <bits/stdc++.h> n  n #define Task             running  n #define F               first n #define S               second n #define pb              push_back n #define bit(x, i)       ((x >> (i)) & 1) n #define inf             1e9 + 7 n #define INF             1e18 + 7 n #define ll              long long n #define pii             pair <int, int> n #define debug(x)        cerr << #x <<   is   << x <<   n ; n  n using namespace std; n  n const int MOD = 1e9 + 7; n const int maxn = 1e5 + 5; n const int base = 3e5 + 1; n  n int n, q; n int a[300005]; n vector <int> p[300005]; n  n void Solve(int test_case) { n     cin >> n >> q; n     for (int i = 1; i <= n; ++ i) { n         cin >> a[i]; n         p[a[i]].pb(i); n     } n     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n     for (int i = 1; i <= q; ++ i) { n         int l, r; cin >> l >> r; n         int ans = 1; n         for (int times = 1; times <= 40; ++ times) { n             int u = a[uniform_int_distribution<int>(l,r)(rng)]; n             int f = upper_bound(p[u].begin(), p[u].end(), r) - lower_bound(p[u].begin(), p[u].end(), l); n             ans = max(ans, 2 * f - (r - l + 1)); n         } n         cout << ans <<   n ; n     } n } n  n  n int main(){ n     ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); n     if (fopen(Task .inp ,  r )) { n         freopen(Task .inp ,  r , stdin); n         freopen(Task .out ,  w , stdout); n     } n //    Prepare(); n     int test_case = 1; n //     cin >> test_case; n     for (int i = 1; i <= test_case; ++ i) { n         Solve(i); n     } n     return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int m, n; n   char a[100001], b[100001]; n   scanf( %s , a); n   scanf( %s , b); n   n = strlen(a); n   m = strlen(b); n   if (n == m) { n     if (strcmp(a, b) == 0) n       printf( -1 ); n     else n       printf( %d , m); n   } else if (n > m) { n     printf( %d , n); n   } else n     printf( %d , m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<int, int> m; n   int sum = 0, a; n   for (int i = 0; i < 5; i++) { n     cin >> a; n     m[a]++; n     sum += a; n   } n   int temp = 0; n   for (auto i : m) { n     if (i.second == 2) temp = max(temp, i.first * 2); n     if (i.second >= 3) temp = max(temp, i.first * 3); n   } n   cout << sum - temp; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 5005, inf = 1011111111; n int n, p[max_n], b[max_n]; n int pos[max_n]; n vector<pair<vector<int>, pair<vector<int>, vector<int>>>> all; n vector<pair<vector<int>, pair<vector<int>, vector<int>>>> my_all; n void find_b() { n   for (int i = 0; i < n; ++i) { n     pos[p[i]] = i; n   } n   for (int i = 0; i < n; ++i) { n     b[i] = pos[i]; n   } n } n int ans[2][max_n]; n int res[max_n], tm, used[max_n]; n int main() { n   cin >> n; n   for (int i = 0; i + 1 < n; ++i) { n     for (int j = 0; j < 2; ++j) { n       cout <<  ?   << i + j <<     << i << endl; n       cout.flush(); n       cin >> ans[j][i]; n     } n   } n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     p[0] = i; n     for (int j = 0; j + 1 < n; ++j) { n       int x = ans[0][j] ^ ans[1][j]; n       p[j + 1] = p[j] ^ x; n     } n     find_b(); n     int f = 1; n     for (int j = 0; j + 1 < n; ++j) { n       for (int k = 0; k < 2; ++k) { n         if (ans[k][j] != (p[j + k] ^ b[j])) { n           f = 0; n           break; n         } n       } n     } n     ++tm; n     for (int j = 0; j < n; ++j) { n       if (p[j] >= n) { n         f = 0; n         break; n       } n       if (used[p[j]] == tm) { n         f = 0; n         break; n       } n       used[p[j]] = tm; n     } n     if (0) { n       for (int j = 0; j < n; ++j) { n         cout << p[j] <<    ; n       } n       cout << endl; n       for (int j = 0; j < n; ++j) { n         cout << b[j] <<    ; n       } n       cout <<  f =   << f << endl; n     } n     cnt += f; n     if (f) { n       for (int j = 0; j < n; ++j) { n         res[j] = p[j]; n       } n     } n   } n   cout <<  !  << endl; n   cout << cnt << endl; n   for (int i = 0; i < n; ++i) { n     cout << res[i] <<    ; n   } n   cout << endl; n   cout.flush(); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long int ll; n int main() n { n     ll t,i,maxi,sumi,mini; n     cin>>t; n     while(t--) n     { n         ll n; n         cin>>n; n         ll a[n]; n         for(i=0;i<n;i++) n         cin>>a[i]; n         maxi=-1; n         mini=a[0]; n         sumi=0; n         for(i=0;i<n;i++) n         { n             sumi=sumi+a[i]; n             if(maxi<a[i]) n             maxi=a[i]; n             if(mini>a[i]) n             mini=a[i]; n         } n         mini=0; n         if(maxi*(n-1)>sumi) n             { n                 mini+=maxi*(n-1)-sumi; n                 sumi=maxi*(n-1); n             }            n         mini+=((n-1)-sumi%(n-1))%(n-1); n         cout<<mini<<endl; n  n     } n }


#include <bits/stdc++.h> n using namespace std; n long long PosMod(long long a) { return a >= 0 ? a : a + 998244353; } n long long AddMod(long long a, long long b) { n   return PosMod((a + b) % 998244353); n } n long long MultMod(long long a, long long b) { n   return PosMod((a * b) % 998244353); n } n long long powll(long long a, long long n) { n   long long r = 1; n   for (long long b = n; b > 0; b >>= 1, a = MultMod(a, a)) n     if (b & 1) r = MultMod(r, a); n   return r; n } n long long InvMod(long long a) { return powll(a, 998244353 - 2); } n long long Frac(long long a, long long b) { return MultMod(a, InvMod(b)); } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int n, m, k; n int COL[2][2][1000100]; n int ROW[2][2][1000100]; n map<std::pair<int, int>, int> writtenValue; n long long col_posCnt[3]; n long long row_posCnt[3]; n long long valid1Cnt[2]; n long long valid2Cnt[2]; n int calcRPos(int i) { n   int pos = 0; n   pos += ROW[1][1][i] == 0 && ROW[0][0][i] == 0; n   pos += ROW[0][1][i] == 0 && ROW[1][0][i] == 0; n   return pos; n } n int calcCPos(int i) { n   int pos = 0; n   pos += COL[1][1][i] == 0 && COL[0][0][i] == 0; n   pos += COL[0][1][i] == 0 && COL[1][0][i] == 0; n   return pos; n } n std::pair<int, int> calcValid(int i) { n   bool valid1 = ROW[1][1][i] == 0 && ROW[0][0][i] == 0; n   bool valid2 = ROW[0][1][i] == 0 && ROW[1][0][i] == 0; n   if (i % 2 == 0) swap(valid1, valid2); n   return {(int)valid1, (int)valid2}; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   cin >> n >> m >> k; n   col_posCnt[2] = m; n   row_posCnt[2] = n; n   valid1Cnt[1] = n; n   valid2Cnt[1] = n; n   for (int i = 0; i < k; i++) { n     int r, c, t; n     cin >> r >> c >> t; n     int prevRPos = calcRPos(r); n     int prevCPos = calcCPos(c); n     std::pair<int, int> prevValid = calcValid(r); n     row_posCnt[prevRPos]--; n     col_posCnt[prevCPos]--; n     valid1Cnt[prevValid.first]--; n     valid2Cnt[prevValid.second]--; n     std::pair<int, int> p = {r, c}; n     if (writtenValue.count(p) > 0) { n       int val = writtenValue[p]; n       COL[r % 2][val][c]--; n       ROW[c % 2][val][r]--; n       writtenValue.erase(p); n     } n     if (t == 0 || t == 1) { n       writtenValue[p] = t; n       COL[r % 2][t][c]++; n       ROW[c % 2][t][r]++; n     } n     int curRPos = calcRPos(r); n     int curCPos = calcCPos(c); n     std::pair<int, int> curValid = calcValid(r); n     row_posCnt[curRPos]++; n     col_posCnt[curCPos]++; n     valid1Cnt[curValid.first]++; n     valid2Cnt[curValid.second]++; n     long long ansR = row_posCnt[0] == 0 ? powll(2, row_posCnt[2]) : 0; n     long long ansC = col_posCnt[0] == 0 ? powll(2, col_posCnt[2]) : 0; n     long long dup = n         (long long)(valid1Cnt[0] == 0) + (long long)(valid2Cnt[0] == 0); n     cout << AddMod(AddMod(ansR, ansC), -dup) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int lcd = 2 * 2 * 2 * 3 * 3 * 5 * 7 * 11 * 13; n long long a, b, k, d[lcd], y = 0, l, i; n int main() { n   cin >> a >> b >> k; n   while (a > b) { n     for (i = 2, l = 1; i <= k; i++) n       if (a % i > l && a - a % i >= b) l = a % i; n     y++; n     a -= l; n     if (d[a % lcd]) { n       int c = y - d[a % lcd]; n       y += c * ((a - b) / lcd); n       a -= ((a - b) / lcd) * lcd; n     } n     d[a % lcd] = y; n   } n   cout << y; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, K; n   int O = 0, E = 0; n   scanf( %d%d , &N, &K); n   for (int i = 0; i < N; i++) { n     int t; n     scanf( %d , &t); n     if (t % 2 == 0) n       E++; n     else n       O++; n   } n   if (N == K) { n     if (O % 2) n       puts( Stannis ); n     else n       puts( Daenerys ); n     return 0; n   } n   if (K % 2 == 0 && (N - K) % 2 == 0) { n     puts( Daenerys ); n     return 0; n   } n   if (K % 2 == 0 && (N - K) % 2 == 1) { n     if ((N - K) / 2 >= min(O, E)) n       puts( Daenerys ); n     else n       puts( Stannis ); n     return 0; n   } n   if (K % 2 == 1 && (N - K) % 2 == 1) { n     if ((N - K) / 2 >= O) n       puts( Daenerys ); n     else n       puts( Stannis ); n     return 0; n   } n   if (K % 2 == 1 && (N - K) % 2 == 0) { n     if ((N - K) / 2 >= E) n       puts( Stannis ); n     else n       puts( Daenerys ); n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int v[1000001]; n int main() { n   int n; n   cin >> n; n   for (int i = 2; i * i <= 1000000; i++) n     if (v[i] == 0) n       for (int j = i * i; j <= 1000000; j += i) v[j] = 1; n   if (n > 2) n     cout <<  2  << endl; n   else n     cout << 1 << endl; n   for (int i = 2; i <= n + 1; i++) n     if (v[i] == 0) n       cout << 1 <<    ; n     else n       cout <<  2  ; n   return 0; n } n


#include <bits/stdc++.h> n using ll = long long; n using namespace std; n const long double PI = acos(-1); n const long long M = 998244353; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n inline long long mod(long long n, long long m = M) { n   long long ret = n % m; n   if (ret < 0) ret += m; n   return ret; n } n long long exp(long long n, long long k, long long m = M) { n   if (k == 0) return 1; n   if (k == 1) return n; n   long long ax = exp(n, k / 2, m); n   ax = mod(ax * ax, m); n   if (k % 2) ax = mod(ax * n, m); n   return ax; n } n long long gcd(long long a, long long b) { n   if (a == 0) n     return b; n   else n     return gcd(b % a, a); n } n void solution() { n   list<long long> l; n   long long n, k; n   cin >> n >> k; n   vector<list<long long>::iterator> elems(n + 1); n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     l.push_back(x); n     auto ed = l.end(); n     ed--; n     elems[x] = ed; n   } n   long long ans = 1; n   vector<bool> mark(n + 1, true); n   vector<long long> b(k); n   for (long long i = 0; i < k; i++) { n     cin >> b[i]; n     mark[b[i]] = false; n   } n   for (long long i = 0; i < k; i++) { n     auto it = elems[b[i]]; n     long long mult = 0; n     if (it != l.begin()) { n       auto it2 = it; n       it2--; n       mult += mark[*it2]; n     } n     auto it2 = it; n     it2++; n     if (it2 != l.end()) mult += mark[*it2]; n     ans *= mult; n     ans = mod(ans); n     mark[b[i]] = true; n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) solution(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)5e6 + 100; n const int maxm = (int)1e6 + 100; n const int mod = (int)1e9 + 7; n const int P = (int)1e6 + 7; n const double pi = acos(-1.0); n int n, t[2][maxn], id = 1; n long long dp[2][30]; n vector<int> g[maxn]; n void add(int x, int pos) { n   int v = 0; n   for (int i = (29); i >= (0); --i) { n     int bit = ((x >> i) & 1); n     if (!t[bit][v]) t[bit][v] = id++; n     v = t[bit][v]; n     g[v].push_back(pos); n   } n } n void go(int v, int b = 29) { n   int l = t[0][v], r = t[1][v]; n   if (l) go(l, b - 1); n   if (r) go(r, b - 1); n   if (!l || !r) return; n   long long res = 0; n   int ptr = 0; n   for (auto x : g[l]) { n     while (ptr < ((int)(g[r]).size()) && g[r][ptr] < x) ptr++; n     res += ptr; n   } n   dp[0][b] += res; n   dp[1][b] += ((int)(g[l]).size()) * 1ll * ((int)(g[r]).size()) - res; n } n void solve() { n   scanf( %d , &n); n   for (int i = (1); i <= (n); ++i) { n     int x; n     scanf( %d , &x); n     add(x, i); n   } n   go(0); n   long long inv = 0; n   int res = 0; n   for (int i = (0); i <= (29); ++i) { n     inv += min(dp[0][i], dp[1][i]); n     if (dp[1][i] < dp[0][i]) res += (1 << i); n   } n   printf( %lld %d , inv, res); n } n int main() { n   int t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   int x[n], x1[m], x2[m], y[m]; n   for (int i = 0; i < n; i++) cin >> x[i]; n   sort(x, x + n); n   vector<int> v; n   for (int i = 0; i < m; i++) { n     cin >> x1[i] >> x2[i] >> y[i]; n     if (x1[i] == 1) { n       v.push_back(x2[i]); n     } n   } n   sort(v.rbegin(), v.rend()); n   if (v.size() == 0) { n     cout <<  0 ; n     return 0; n   } n   int answer = 1000000000; n   for (int i = 0; i <= n; i++) { n     int right = 1000000000; n     if (i != n) right = x[i]; n     if (v[0] < right) n       answer = min(answer, i); n     else { n       int l = 0; n       int r = (int)v.size() - 1; n       while (r - l > 1) { n         int sr = (r + l) / 2; n         if (v[sr] >= right) n           l = sr; n         else n           r = sr; n       } n       if (v[r] < right) r = l; n       answer = min(answer, i + r + 1); n     } n   } n   cout << answer; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, result, count = 0, i; n   scanf( %d , &n); n   char arr[n]; n   for (i = 0; i < n; i++) { n     scanf(  n%c , &arr[i]); n   } n   i = 0; n   for (i = 0; i < n; i++) { n     if (arr[i] != arr[i - 1]) { n       count++; n     } n   } n   if (count + 2 < n) { n     result = count + 2; n   } n   if (count + 2 >= n) { n     result = n; n   } n   printf( %d , result); n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct x { n   int cor; n   int power; n   bool operator<(x T) const { return cor < T.cor; } n } beacon; n beacon arr[2000009]; n vector<int> tmp; n int dp[2000009]; n int main() { n   int n, m, k; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &arr[i].cor, &arr[i].power); n   } n   sort(arr, arr + n); n   for (int i = 0; i < n; i++) tmp.push_back(arr[i].cor); n   dp[0] = 0; n   for (int i = 1; i < n; i++) { n     int p = arr[i].power; n     int left = arr[i].cor - p; n     vector<int>::iterator it = lower_bound(tmp.begin(), tmp.end(), left); n     int l = it - tmp.begin(); n     dp[i] = (i - l); n     if (l > 0) dp[i] += dp[l - 1]; n   } n   int ans = INT_MAX; n   for (int i = 0; i < n; i++) { n     ans = min(ans, dp[i] + (n - i - 1)); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n template <class X = int> n inline X min(X a, X b, X c) { n   return min(min(a, b), c); n } n template <class X = int> n inline X min(X a, X b, X c, X d) { n   return min(min(a, b, c), d); n } n template <class X = int> n inline X max(X a, X b, X c) { n   return max(max(a, b), c); n } n template <class X = int> n inline X max(X a, X b, X c, X d) { n   return max(max(a, b, c), d); n } n template <class X = int> n inline X mid(X s, X e) { n   return (s + (e - s) / 2); n } n template <class X = int> n inline X len(X s, X e) { n   return (e - s + 1); n } n const int MOD = 1e9 + 7; n const int INF = 987654321; n const bool TESTCASES = 0; n const bool CODEJAM = 0; n void solve() { n   int n; n   string s; n   cin >> n; n   cin >> s; n   int ans = 0; n   int temp = 0, br = 0; n   for (int i = 0; i < n; i++) { n     br += s[i] ==  )  ? -1 : 1; n     if (br < 0) n       temp++; n     else if (br == 0 and temp != 0) { n       ans += temp + 1; n       temp = 0; n     } n   } n   if (br != 0) n     cout <<  -1 n ; n   else n     cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int T = 1; n   if (TESTCASES) cin >> T; n   for (int t = 1; t <= T; t++) { n     if (CODEJAM) cout <<  Case #  << t <<  :  ; n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s1[50], s2[50]; n   cin >> s1 >> s2; n   long long left = 0, right = 0, mid = 0, l; n   l = strlen(s1); n   for (long long i = 0; i < l; i++) n     if (s1[i] ==  | ) { n       mid = i; n       break; n     } n   left = mid; n   right = l - (mid + 1); n   if ((strlen(s1) + strlen(s2) - 1) % 2 == 1) { n     printf( Impossible ); n     return 0; n   } n   if (left == right) { n     if (strlen(s2) % 2 == 0) { n       for (int j = 0; j < left; j++) printf( %c , s1[j]); n       for (int j = 0; j < strlen(s2) / 2; j++) printf( %c , s2[j]); n       printf( | ); n       for (int j = left + 1; j < l; j++) printf( %c , s1[j]); n       for (int j = strlen(s2) / 2; j < strlen(s2); j++) printf( %c , s2[j]); n       return 0; n     } else { n       printf( Impossible ); n       return 0; n     } n   } n   if (left < right) { n     if (right - left > strlen(s2)) { n       printf( Impossible ); n       return 0; n     } else { n       long long dif = right - left; n       dif += (strlen(s2) - dif) / 2; n       for (int j = 0; j < left; j++) printf( %c , s1[j]); n       for (int j = 0; j < dif; j++) printf( %c , s2[j]); n       printf( | ); n       for (int j = left + 1; j < strlen(s1); j++) printf( %c , s1[j]); n       for (int j = dif; j < strlen(s2); j++) printf( %c , s2[j]); n       return 0; n     } n   } n   if (right < left) { n     if (left - right > strlen(s2)) { n       printf( Impossible ); n       return 0; n     } else { n       long long dif = left - right; n       dif = (strlen(s2) - dif) / 2; n       for (int j = 0; j < left; j++) printf( %c , s1[j]); n       for (int j = 0; j < dif; j++) printf( %c , s2[j]); n       printf( | ); n       for (int j = left + 1; j < strlen(s1); j++) printf( %c , s1[j]); n       for (int j = dif; j < strlen(s2); j++) printf( %c , s2[j]); n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, x, y, z, ans = 0; n   cin >> n >> x >> y >> z; n   for (long long i = 0; i <= x / 2; i++) { n     for (long long j = 0; j <= y; j++) { n       if ((n - (i + j)) % 2 == 0 && (n - (i + j)) / 2 <= z && n           (n - (i + j)) / 2 >= 0) n         ans++; n     } n   } n   cout << ans; n } n int main() { n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, a, b, k, p[N]; n int main() { n   cin >> n >> a >> b >> k; n   int sum = a + b; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n     if (p[i] % sum == 0) n       p[i] = sum; n     else n       p[i] %= sum; n     p[i] -= a; n   } n   int ans = 0; n   sort(p + 1, p + 1 + n); n   for (int i = 1; i <= n; i++) { n     if (p[i] <= 0) n       ans++; n     else { n       int t = ceil(p[i] * 1.0 / a); n       if (t <= k) k -= t, ans++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t maxn = 1e3 + 42; n vector<int64_t> g[maxn]; n int64_t par[maxn], sz[maxn]; n basic_string<int64_t> leaves[maxn]; n void dfs(int64_t v) { n   sz[v] = 1; n   for (auto u : g[v]) { n     if (u != par[v]) { n       par[u] = v; n       dfs(u); n       sz[v] += sz[u]; n       leaves[v] += leaves[u][0]; n     } n   } n   if (g[v].size() == 1) { n     leaves[v] = {v}; n   } n } n vector<tuple<int64_t, int64_t, int64_t>> ans; n int64_t need[maxn]; n tuple<int64_t, int64_t, int64_t> changers[maxn]; n void change(int64_t v, int64_t added = 0) { n   for (auto u : g[v]) { n     if (u != par[v]) { n       int64_t a, b, c; n       tie(a, b, c) = changers[u]; n       int64_t d = need[u] - added * (a == leaves[v][0]); n       ans.emplace_back(a, b, d / 2); n       ans.emplace_back(a, c, d / 2); n       ans.emplace_back(b, c, -d / 2); n       change(u, added * (a == leaves[v][0]) + d); n     } n   } n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int64_t n; n   cin >> n; n   int64_t u[n], v[n], val[n]; n   for (int64_t i = 1; i < n; i++) { n     cin >> u[i] >> v[i] >> val[i]; n     u[i]--, v[i]--; n     g[u[i]].push_back(v[i]); n     g[v[i]].push_back(u[i]); n   } n   bool ok = true; n   for (int64_t i = 0; i < n; i++) { n     ok &= g[i].size() == 1 || g[i].size() >= 3; n   } n   if (!ok) { n     cout <<  NO  << endl; n   } else if (n == 2) { n     cout <<  YES  << endl; n     cout << 1 << endl; n     cout << 1 <<     << 2 <<     << val[1] << endl; n     return 0; n   } else { n     cout <<  YES  << endl; n     int64_t root = 0; n     while (g[root].size() == 1) { n       root++; n     } n     par[root] = root; n     dfs(root); n     for (int64_t i = 1; i < n; i++) { n       if (sz[u[i]] > sz[v[i]]) { n         swap(u[i], v[i]); n       } n       int64_t a = leaves[u[i]][0]; n       int64_t b = 0; n       while (leaves[v[i]][b] == a) { n         b++; n       } n       int64_t c = b + 1; n       b = leaves[v[i]][b]; n       while (c < (int64_t)leaves[v[i]].size() && leaves[v[i]][c] == a) { n         c++; n       } n       if (c == (int64_t)leaves[v[i]].size()) { n         int64_t w = par[v[i]]; n         c = 0; n         while (leaves[w][c] == a || leaves[w][c] == b) { n           c++; n         } n         c = leaves[w][c]; n       } else { n         c = leaves[v[i]][c]; n       } n       changers[u[i]] = {a, b, c}; n       need[u[i]] = val[i]; n     } n     change(root); n     cout << ans.size() << endl; n     for (auto it : ans) { n       int64_t a, b, c; n       tie(a, b, c) = it; n       cout << a + 1 <<     << b + 1 <<     << c << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d, h, i, j; n int main() { n   cin >> n >> d >> h; n   if (d > h * 2) { n     cout << -1 << endl; n     return 0; n   } n   if (d == 1 && h == 1 && n > 2) { n     cout << -1 << endl; n     return 0; n   } n   for (i = 1; i <= h; i++) cout << i <<     << i + 1 << endl; n   if (d != 1 && d != h) { n     cout << 1 <<     << i + 1 << endl; n     i++; n   } n   int k = 1; n   if (d == h) k++; n   for (j = i; j <= d; j++) cout << j <<     << j + 1 << endl; n   for (i = j + 1; i <= n; i++) cout << k <<     << i << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[1000001]; n int a[1000001]; n vector<int> v[1000001]; n vector<int> an; n int main() { n   memset((dp), (0), sizeof(dp)); n   memset((a), (0), sizeof(a)); n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     if (x <= m) { n       a[x]++; n       v[x].push_back(i + 1); n     } n   } n   for (int i = 1; i <= m; i++) { n     int x = i; n     while (x <= m) { n       dp[x] += a[i]; n       x += i; n     } n   } n   int ans = -1, ind = -1; n   for (int i = 1; i < 1000001; i++) n     if (ans < dp[i]) ans = dp[i], ind = i; n   printf( %d , ind); n   printf(   ); n   printf( %d , dp[ind]); n   printf(  n ); n   for (int i = 1; i <= m; i++) n     if (ind % i == 0 && a[i] > 0) n       for (int j : v[i]) an.push_back(j); n   sort(an.begin(), an.end()); n   for (int i : an) printf( %d , i), printf(   ); n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, k, pres[300005], cont[300005]; n vector<long long int> fcts[300005]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   long long int tot = 0; n   pres[1] = 1; n   for (long long int i = 1; i <= n; ++i) n     for (int j = 2 * i; j <= n; j += i) fcts[j].push_back(i); n   for (long long int i = 2; i <= n; ++i) { n     if (tot >= k) { n       n = i - 1; n       break; n     } n     tot += fcts[i].size(); n     cont[i] += fcts[i].size(); n     for (long long int v : fcts[i]) cont[v]++; n     pres[i] = 1; n   } n   if (tot < k) { n     cout <<  No n ; n     return 0; n   } n   set<pair<long long int, long long int> > conts; n   for (long long int i = 1; i <= n; ++i) conts.insert(make_pair(cont[i], i)); n   long long int rmv = tot - k; n   while (rmv) { n     auto it = conts.lower_bound(make_pair(rmv + 1, -1LL)); n     if (it == conts.begin()) { n       cout <<  No n ; n       return 0; n     } n     it--; n     pair<long long int, long long int> xp = *it; n     pres[xp.second] = 0; n     rmv -= xp.first; n     conts.erase(it); n     for (long long int v : fcts[xp.second]) { n       if (pres[v]) { n         conts.erase(make_pair(cont[v], v)); n         cont[v]--; n         conts.insert(make_pair(cont[v], v)); n       } n     } n     for (long long int v = 2 * xp.second; v <= n; v += xp.second) n       if (pres[v]) { n         conts.erase(make_pair(cont[v], v)); n         cont[v]--; n         conts.insert(make_pair(cont[v], v)); n       } n   } n   cout <<  Yes n ; n   vector<long long int> ans; n   for (long long int i = 1; i <= n; ++i) n     if (pres[i]) ans.push_back(i); n   cout << ans.size() <<   n ; n   for (long long int v : ans) cout << v <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n struct Point { n   double x, y; n   Point(double x = 0, double y = 0) : x(x), y(y) {} n }; n Point operator+(Point a, Point b) { return Point(a.x + b.x, a.y + b.y); } n Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } n Point operator*(Point a, double p) { return Point(a.x * p, a.y * p); } n Point operator/(Point a, double p) { return a * (1 / p); } n bool operator<(const Point& a, const Point& b) { n   return a.x < b.x || (a.x == b.x && a.y < b.y); n } n const double EPS = 1e-10; n int dcmp(double x) { n   if (fabs(x) < EPS) n     return 0; n   else n     return x < 0 ? -1 : 1; n } n bool operator==(const Point& a, const Point& b) { n   return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; n } n double ang(Point v) { return atan2(v.y, v.x); } n double Dot(Point A, Point B) { return A.x * B.x + A.y * B.y; } n double Length(Point A) { return sqrt(Dot(A, A)); } n double Angle(Point A, Point B) { n   return acos(Dot(A, B) / Length(A) / Length(B)); n } n double Cross(Point A, Point B) { return A.x * B.y - A.y * B.x; } n Point CirCenter(Point a, Point b, Point c) { n   double a1 = b.x - a.x, b1 = b.y - a.y, c1 = (a1 * a1 + b1 * b1) / 2; n   double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2 * a2 + b2 * b2) / 2; n   double d = a1 * b2 - a2 * b1; n   return Point(a.x + (c1 * b2 - c2 * b1) / d, a.y + (a1 * c2 - a2 * c1) / d); n } n vector<Point> ConvexHull(vector<Point> p) { n   sort(p.begin(), p.end()); n   p.erase(unique(p.begin(), p.end()), p.end()); n   int n = p.size(); n   vector<Point> ch(n + 1); n   int m = 0; n   for (int i = 0; i < n; i++) { n     while (m > 1 && dcmp(Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) <= 0) n       m--; n     ch[m++] = p[i]; n   } n   int k = m; n   for (int i = n - 2; i >= 0; i--) { n     while (m > k && dcmp(Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) <= 0) n       m--; n     ch[m++] = p[i]; n   } n   if (n > 1) m--; n   ch.resize(m); n   return ch; n } n bool judge(const vector<Point>& a, const vector<Point>& b, int i, int j) { n   double minr = -DBL_MAX, maxr = DBL_MAX; n   int mid = -1; n   int na = a.size(), nb = b.size(); n   bool flag = true; n   for (int k = i + 1; k < j; k++) { n     if (dcmp(Cross(a[k] - a[i], a[k] - a[j])) == 0) { n       flag = false; n       break; n     } n     Point center = CirCenter(a[i], a[j], a[k]); n     double r = Length(center - (a[i] + a[j]) / 2.0); n     if (dcmp(Cross(center - a[i], a[j] - a[i])) * n             dcmp(Cross(a[k] - a[i], a[j] - a[i])) < n         0) n       r = -r; n     if (dcmp(r - minr) > 0) minr = r, mid = k; n   } n   if (flag) { n     for (int k = (j + 1) % na;; k = (k + 1) % na) { n       if (k == i) break; n       if (dcmp(Cross(a[k] - a[i], a[k] - a[j])) == 0) { n         flag = false; n         break; n       } n       Point center = CirCenter(a[i], a[j], a[k]); n       double r = Length(center - (a[i] + a[j]) / 2.0); n       if (dcmp(Cross(center - a[i], a[j] - a[i])) * n               dcmp(Cross(a[k] - a[i], a[j] - a[i])) > n           0) n         r = -r; n       if (dcmp(r - maxr) < 0) maxr = r; n     } n   } n   bool ok = true; n   if (flag) { n     for (int k = 0; k < nb; k++) { n       if (dcmp(Cross(b[k] - a[i], a[j] - a[i])) == 0 && n           dcmp(Dot(a[i] - b[k], a[j] - b[k])) <= 0) { n         ok = false; n         break; n       } n       Point center = CirCenter(a[i], a[j], b[k]); n       double r = Length(center - (a[i] + a[j]) / 2.0); n       if (dcmp(Cross(center - a[i], a[j] - a[i])) * n               dcmp(Cross(b[k] - a[i], a[j] - a[i])) < n           0) n         r = -r; n       if (dcmp(Cross(b[k] - a[i], a[j] - a[i])) > 0) { n         if (dcmp(r - maxr) < 0) maxr = r; n       } else if (dcmp(Cross(b[k] - a[i], a[j] - a[i])) < 0) { n         r = -r; n         if (dcmp(minr - r) < 0) minr = r; n       } n       if (dcmp(maxr - minr) <= 0) { n         ok = false; n         break; n       } n     } n   } else n     ok = false; n   return ok || (mid >= 0 && (judge(a, b, i, mid) || judge(a, b, mid, j))); n } n int n, m; n int main() { n   scanf( %d%d , &n, &m); n   vector<Point> ina(n), inb(m); n   int x, y; n   for (int i = 0; i < n; i++) n     scanf( %d%d , &x, &y), ina[i].x = (double)x, ina[i].y = (double)y; n   for (int i = 0; i < m; i++) n     scanf( %d%d , &x, &y), inb[i].x = (double)x, inb[i].y = (double)y; n   vector<Point> a = ConvexHull(ina), b = ConvexHull(inb); n   puts((judge(a, inb, 0, a.size() - 1) || judge(b, ina, 0, b.size() - 1)) n            ?  YES  n            :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int qtd[200007]; n int vogal(char ch) { n   return ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u  || n          ch ==  A  || ch ==  E  || ch ==  I  || ch ==  O  || ch ==  U ; n } n map<int, int> prim; n int main() { n   string s; n   cin >> s; n   int resp = 0; n   int qtdr = 0; n   for (int i = 1; i <= s.length(); i++) { n     qtd[i] = qtd[i - 1]; n     qtd[i] += vogal(s[i - 1]) ? -1 : 2; n     if (qtd[i] >= 0) n       resp = i; n     else { n       if (!prim.count(qtd[i])) prim[qtd[i]] = i; n       resp = max(resp, i - prim[qtd[i]]); n     } n   } n   if (resp == 0) { n     cout <<  No solution n ; n     return 0; n   } n   for (int i = resp; i <= s.length(); i++) { n     if (qtd[i - resp] <= qtd[i]) qtdr++; n   } n   cout << resp <<     << qtdr <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long inf = 1e18; n const long double pie = acos((long double)-1); n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, 1, -1}; n unordered_map<int, vector<int>> mp; n int indegree[1001]; n void addedge(int x, int y) { n   mp[x].push_back(y); n   mp[y].push_back(x); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   auto start = std::chrono::high_resolution_clock::now(); n   int n, m; n   cin >> n >> m; n   while (m--) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     addedge(x, y); n   } n   int cnt = 0; n   for (auto x : mp) { n     vector<int> v = x.second; n     for (auto y : v) { n       indegree[y]++; n     } n   } n   while (true) { n     int c = 0; n     vector<int> ans; n     for (int i = 0; i < n; ++i) { n       if (indegree[i] == 1) { n         ans.push_back(i); n         indegree[i] = 0; n         c++; n       } n     } n     if (c != 0) { n       cnt++; n     } n     for (auto x : ans) { n       for (auto y : mp[x]) { n         if (indegree[y] > 0) indegree[y]--; n       } n     } n     int flag = 0; n     for (int i = 0; i < n; ++i) { n       if (indegree[i] == 1) { n         flag++; n         break; n       } n     } n     if (flag == 0) { n       break; n     } n   } n   cout << cnt <<   n ; n   auto stop = std::chrono::high_resolution_clock::now(); n   auto duration = n       std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxm = 1.5e7; n const int maxn = 1e5 + 5; n int lson[maxm], rson[maxm]; n long long sum[maxm]; n int T[maxn]; n int clk, n; n void modity(int &rt, int L, int R, int pos, int val) { n   if (!rt) rt = ++clk; n   if (L == R) { n     sum[rt] += val; n   } else { n     int mid = L + R >> 1; n     if (pos <= mid) n       modity(lson[rt], L, mid, pos, val); n     else n       modity(rson[rt], mid + 1, R, pos, val); n     sum[rt] = sum[lson[rt]] + sum[rson[rt]]; n   } n } n long long query(int rt, int L, int R, int l, int r) { n   if (!rt) return 0; n   if (l <= L && R <= r) n     return sum[rt]; n   else { n     int mid = L + R >> 1; n     long long ret = 0; n     if (l <= mid) ret += query(lson[rt], L, mid, l, r); n     if (mid < r) ret += query(rson[rt], mid + 1, R, l, r); n     return ret; n   } n } n int lowbit(int x) { return x & -x; } n void modity(int pos, pair<int, int> val) { n   for (int i = pos; i <= n; i += lowbit(i)) { n     modity(T[i], 1, n, val.first, val.second); n   } n } n long long query(int l, int r) { n   long long ret = 0; n   for (int i = r; i > 0; i -= lowbit(i)) ret += query(T[i], 1, n, l, n); n   for (int i = l - 1; i > 0; i -= lowbit(i)) ret -= query(T[i], 1, n, l, n); n   return ret; n } n set<int> s[maxn]; n int a[maxn]; n int main() { n   int m; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (s[a[i]].size()) { n       int prev = *s[a[i]].rbegin(); n       modity(i, make_pair(prev, i - prev)); n     } n     s[a[i]].insert(i); n   } n   while (m--) { n     int opt; n     scanf( %d , &opt); n     if (opt == 1) { n       int pos, val; n       scanf( %d %d , &pos, &val); n       if (val == a[pos]) continue; n       auto it = s[a[pos]].find(pos); n       auto is = it; n       it++; n       if (it != s[a[pos]].end()) { n         modity(*it, make_pair(*is, *is - *it)); n         if (is != s[a[pos]].begin()) { n           auto ix = is; n           ix--; n           modity(*it, make_pair(*ix, *it - *ix)); n         } n       } n       if (is != s[a[pos]].begin()) { n         auto ix = is; n         ix--; n         modity(*is, make_pair(*ix, *ix - *is)); n       } n       s[a[pos]].erase(pos); n       a[pos] = val; n       is = s[a[pos]].insert(pos).first; n       if (is != s[a[pos]].begin()) { n         auto ix = is; n         ix--; n         modity(*is, make_pair(*ix, *is - *ix)); n       } n       it = is; n       it++; n       if (it != s[a[pos]].end()) { n         if (is != s[a[pos]].begin()) { n           auto ix = is; n           ix--; n           modity(*it, make_pair(*ix, *ix - *it)); n         } n         modity(*it, make_pair(*is, *it - *is)); n       } n     } else { n       int l, r; n       scanf( %d %d , &l, &r); n       long long ret = query(l, r); n       printf( %lld n , ret); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long maxn = 1e5 + 10; n vector<long long> a[maxn]; n long long lo[maxn], hi[maxn]; n const long long inf = 1e15; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout << fixed << setprecision(9); n   long long n; n   cin >> n; n   long long cnt = 0; n   map<long long, long long> mp; n   vector<long long> keep; n   for (long long i = 0; i < n; ++i) { n     long long len; n     cin >> len; n     bool ok = false; n     long long mn = inf; n     long long mx = -inf; n     for (long long j = 0; j < len; ++j) { n       long long cur; n       cin >> cur; n       a[i].push_back(cur); n       if (cur > mn) ok = true; n       mn = min(mn, cur); n       mx = max(mx, cur); n     } n     lo[i] = mn; n     hi[i] = mx; n     if (ok) { n       ++cnt; n       hi[i] = inf; n     } else { n       keep.push_back(mx); n     } n   } n   sort(keep.begin(), keep.end()); n   long long res = 0; n   for (long long i = 0; i < n; ++i) { n     if (hi[i] == inf) { n       res += n; n     } else { n       long long cur = cnt; n       auto it = upper_bound(keep.begin(), keep.end(), lo[i]); n       if (it != keep.end()) { n         long long ind = it - keep.begin(); n         long long is = (long long)keep.size() - ind; n         cur += is; n       } n       res += cur; n     } n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n int a[maxn], ans[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m, c; n   cin >> n >> m >> c; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) { n     int val; n     cin >> val; n     ans[i] += val; n     ans[i + n - m + 1] -= val; n   } n   ans[0] %= c; n   for (int i = 1; i < n; i++) ans[i] = (ans[i] + ans[i - 1]) % c; n   for (int i = 0; i < n; i++) cout << ((a[i] + ans[i]) % c + c) % c <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, k, l, n, m, s, tot, ss, t; n const int pp = 1000000007; n long long a[100010], an; n void dfs(int x, bitset<120> y, long long s) { n   if (x > m / 2) { n     an = (an + s) % pp; n     return; n   } n   dfs(x + 1, y, s); n   if ((a[x]) && (!y[x])) n     dfs(x + 1, y | (y << x) | (y >> x) | (y << (m - x)) | (y >> (m - x)), n         (s * a[x]) % pp); n } n int main() { n   scanf( %d%d%d , &n, &m, &t); n   for (int i = 1; i <= t; i++) { n     int x; n     scanf( %d , &x); n     x %= m; n     a[min(x, m - x)]++; n   } n   dfs(0, bitset<120>(1), 1); n   printf( %I64d , an); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, k; n     cin >> n >> k; n     vector<set<long long int>> v(k); n     for (long long int i = 0; i < n; ++i) { n       char c; n       cin >> c; n       if (c ==  ? ) continue; n       if (c ==  1 ) n         v[i % k].insert(1); n       else n         v[i % k].insert(2); n     } n     bool ans = true; n     long long int count_0 = 0, count_1 = 0, c_q = 0; n     for (auto s : v) { n       if (s.size() > 1) ans = false; n     } n     for (auto s : v) { n       if (s.empty()) { n         c_q++; n         continue; n       } n       if (*s.begin() == 1) { n         count_1++; n         continue; n       } else { n         count_0++; n         continue; n       } n     } n     if (c_q < abs(count_0 - count_1)) { n       ans = false; n     } n     if (!ans) { n       cout <<  NO  n            <<   n ; n     } else { n       cout <<  YES  n            <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n #define ll long long n  n  n  n int main() n { n  n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     int t; n     cin >> t; n     int n, k; n     while (t--) n     { n         char a[3] = { a , b , c }; n         cin >> n >> k;  n         int temp = k;  n         while(k--) { n             cout<<a[0]; n         } n         int i = 1; n         n = n - temp; n         //cout << n; n         while(n--) { n              n             if(i == 3){ n                 i = 0; n             } n             cout<<a[i]; n             i++; n         }    n         cout << endl; n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n bool check(long long mid, vector<long long> &d, vector<long long> &prep) { n   long long n = d.size(); n   long long m = prep.size() - 1; n   vector<long long> pos(m + 1, -1); n   for (long long i = 0; i <= mid; i++) pos[d[i]] = i; n   for (long long i = 1; i <= m; i++) n     if (pos[i] == -1) return false; n   long long prep_days = 0; n   for (long long i = 0; i <= mid; i++) { n     if (d[i] == 0) n       prep_days++; n     else if (pos[d[i]] == i) { n       if (prep_days < prep[d[i]]) n         return false; n       else n         prep_days -= prep[d[i]]; n     } else n       prep_days++; n   } n   return true; n } n int32_t main() { n   long long n, m; n   cin >> n >> m; n   vector<long long> d(n); n   for (long long &i : d) cin >> i; n   vector<long long> prep(m + 1); n   for (long long i = 1; i <= m; i++) cin >> prep[i]; n   long long l = 0, r = n - 1; n   while (l < r) { n     long long mid = (l + r) / 2; n     if (check(mid, d, prep)) { n       r = mid; n     } else { n       l = mid + 1; n     } n   } n   if (r == n - 1) { n     if (check(r, d, prep)) n       cout << n <<   n ; n     else n       cout << -1 <<   n ; n   } else n     cout << r + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n int sz(const T &a) { n   return (int)a.size(); n } n long long int am[101]; n int main() { n   int n; n   cin >> n; n   long long int trip; n   long long int str; n   cin >> trip >> str; n   long long int a, b; n   bool lastinc = false; n   cout <<  +   << 1 << endl; n   cin >> trip >> str; n   cout <<  +   << 2 << endl; n   cin >> a >> b; n   if (b > str) n     lastinc = true; n   else n     lastinc = false; n   trip = a, str = b; n   cout <<  +   << 3 << endl; n   cin >> a >> b; n   if (a > trip) { n     am[3] = (long long int)sqrt(2 * (a - trip)) + 2; n   } else { n     if (lastinc) n       am[3] = 2; n     else n       am[3] = 1; n   } n   long long int diff = b - str; n   trip = a, str = b; n   cout <<  +   << 1 << endl; n   cin >> a >> b; n   am[1] = (long long int)sqrt(2 * (a - trip)) + 2; n   am[2] = (b - str) / am[3]; n   if (diff > (am[1] - 1) * (am[2])) n     lastinc = true; n   else n     lastinc = false; n   trip = a, str = b; n   for (int i = 4; i < n; i++) { n     cout <<  +   << i << endl; n     cin >> a >> b; n     if (a > trip) { n       am[i] = (long long int)sqrt(2 * (a - trip)) + 1; n     } else { n       if (lastinc) n         am[i] = 1; n       else n         am[i] = 0; n     } n     am[i]++; n     if (b - str > (am[i - 2]) * (am[i - 1])) n       lastinc = true; n     else n       lastinc = false; n     trip = a, str = b; n   } n   for (int i = 3; i < n; i++) { n     str -= am[i] * am[i - 1] * am[i - 2]; n   } n   am[n] = (str / am[n - 1] / am[n - 2]) + 1; n   am[1]--; n   cout <<  !  ; n   for (int i = 1; i <= n; i++) cout << am[i] - 1 <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Ss[10005], Ee[10005]; n int pre[10005 * 200][26]; n char A[10005], s[10005], e[10005]; n int main() { n   scanf( %s%s%s , A, s, e); n   int n = strlen(A); n   int lens = strlen(s); n   int lene = strlen(e); n   int i, j; n   memset(Ss, 0, sizeof(Ss)); n   memset(Ee, 0, sizeof(Ee)); n   memset(pre, -1, sizeof(pre)); n   for (i = 0; i <= n - lens; i++) { n     for (j = 0; j < lens; j++) n       if (A[i + j] != s[j]) break; n     if (j >= lens) Ss[i] = 1; n   } n   for (i = 0; i <= n - lene; i++) { n     for (j = 0; j < lene; j++) n       if (A[i + j] != e[j]) break; n     if (j >= lene) Ee[i + lene - 1] = 1; n   } n   int m = (lens > lene ? lens : lene); n   int cnt = 0; n   int alloc = 1; n   for (i = 0; i <= n - m; i++) { n     if (!Ss[i]) continue; n     int cur = 0; n     for (j = i; j < i + m - 1; j++) { n       if (pre[cur][A[j] -  A ] == -1) pre[cur][A[j] -  A ] = alloc++; n       cur = pre[cur][A[j] -  A ]; n     } n     for (j = i + m - 1; j < n; j++) { n       if (Ee[j]) n         if (pre[cur][A[j] -  A ] == -1) cnt++; n       if (pre[cur][A[j] -  A ] == -1) pre[cur][A[j] -  A ] = alloc++; n       cur = pre[cur][A[j] -  A ]; n     } n   } n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   cout << a + b; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e3 + 7; n long long a[N][N]; n int32_t main() { n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < n; j++) cin >> a[i][j]; n   long long a12 = a[0][1], a23 = a[1][2], a13 = a[0][2]; n   long double aa123 = (sqrt(a12 * 1.0) * sqrt(a23 * 1.0) * sqrt(a13 * 1.0)); n   long long a123 = round(aa123); n   long long a1 = a123 / a23; n   cout << a1 <<    ; n   for (long long i = 1; i < n; i++) cout << a[0][i] / a1 <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n const int mod = 1e9 + 7; n int n; n struct block { n   vector<int> v; n   int id; n }; n vector<block> b; n int a[maxn]; n void get_blocks() { n   b.clear(); n   block cur; n   int j = 1; n   while (j <= n) { n     cur.v.clear(); n     while (1) { n       cur.v.push_back(a[j]); n       if (j == n || a[j + 1] != a[j] + 1) { n         ++j; n         break; n       } n       ++j; n     } n     b.push_back(cur); n   } n   vector<int> bv; n   for (auto& w : b) bv.push_back(w.v[0]); n   sort(bv.begin(), bv.end()); n   for (auto& w : b) { n     int u = lower_bound(bv.begin(), bv.end(), w.v[0]) - bv.begin() + 1; n     w.id = u; n   } n } n vector<vector<int>> tv; n void tr() { n   vector<vector<block>> seq; n   int la = b.back().id; n   int en = b.size() - 1; n   vector<block> last; n   last.push_back(b.back()); n   vector<int> e; n   vector<block> mid; n   if (la == b.size()) { n     la = b[b.size() - 2].id; n     vector<block> q; n     q.push_back(b[b.size() - 2]); n     --en; n     for (int i = 0; i < b.size(); ++i) { n       if (b[i].id == la + 1) { n         for (int j = i; j < en; ++j) mid.push_back(b[j]); n         vector<block> fr; n         for (int j = 0; j < i; ++j) fr.push_back(b[j]); n         if (fr.size()) seq.push_back(fr); n         seq.push_back(mid); n         seq.push_back(q); n         seq.push_back(last); n         break; n       } n     } n     goto N; n   } n   for (int i = 0; i < b.size(); ++i) { n     if (b[i].id == la + 1) { n       for (int j = i; j < en; ++j) mid.push_back(b[j]); n       vector<block> fr; n       for (int j = 0; j < i; ++j) fr.push_back(b[j]); n       if (fr.size()) seq.push_back(fr); n       seq.push_back(mid); n       seq.push_back(last); n       break; n     } n   } n N:; n   e.push_back(seq.size()); n   for (auto& w : seq) { n     int cnt = 0; n     for (auto& bb : w) cnt += bb.v.size(); n     e.push_back(cnt); n   } n   tv.push_back(e); n   reverse(seq.begin(), seq.end()); n   int j = 1; n   for (auto& w : seq) { n     for (auto& ee : w) { n       for (auto& ii : ee.v) a[j++] = ii; n     } n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   while (1) { n     get_blocks(); n     if (b.size() == 1) break; n     tr(); n   } n   cout << tv.size() <<   n ; n   for (auto& w : tv) { n     for (int i = 0; i < w.size(); ++i) { n       cout << w[i]; n       if (i < w.size() - 1) n         cout <<    ; n       else n         cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> dp(2 * n); n     int mx = -1; n     int c = 0; n     dp[0] = 1; n     for (int i = 0; i < 2 * n; i++) { n       int u; n       cin >> u; n       if (mx > u) { n         c++; n       } else { n         for (int j = n + 1; j >= c; j--) { n           dp[j] |= dp[j - c]; n         } n         c = 1; n         mx = u; n       } n     } n     for (int j = n; j >= c; j--) { n       dp[j] |= dp[j - c]; n     } n     if (dp[n]) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int A[101]; n   int B[101]; n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> A[i]; n   int t = n / k; n   for (int i = 0; i < k; i++) { n     int max1 = 0; n     int max2 = 0; n     for (int j = 0; j < t; j++) n       if (A[i + j * k] == 1) n         max1++; n       else n         max2++; n     if (max2 > max1) n       B[i] = 2; n     else n       B[i] = 1; n   } n   int d = 0; n   for (int i = 0; i < t; i++) { n     for (int j = 0; j < k; j++) n       if (A[i * k + j] != B[j]) d++; n   } n   cout << d; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void A() { n   cout <<  sjfnb  << endl; n   exit(0); n } n void B() { n   cout <<  cslnb  << endl; n   exit(0); 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   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   sort(a.begin(), a.end()); n   if (n == 1) { n     if (a[0] % 2 == 0) { n       B(); n     } else { n       A(); n     } n   } n   if (a[1] == 0) { n     B(); n   } n   vector<int> b = a; n   if (unique(b.begin(), b.end()) - b.begin() <= n - 2) { n     B(); n   } n   int t = 0; n   for (int i = 0; i < n - 1; ++i) { n     if (a[i] == a[i + 1]) { n       if (i > 0 && a[i - 1] == a[i] - 1) { n         B(); n       } n       --a[i]; n       t = 1; n     } n   } n   for (int i = 0; i < n; ++i) { n     t = (t + a[i] - i) % 2; n   } n   if (t % 2 == 0) { n     B(); n   } else { n     A(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bins(long long l, long long r) { n   while (l + 1 < r) { n     long long m = (l + r) >> 1; n     cout <<  ?   << l <<     << m << endl; n     cout.flush(); n     char c; n     cin >> c; n     if (c ==  e ) return 0; n     if (c ==  y ) { n       l = m; n     } else { n       r = m; n     } n   } n   return r; n } n signed main() { n   string s; n   cin >> s; n   long long x, y; n   while (s ==  start ) { n     x = 0, y = 1; n     char c =  y ; n     bool z = false; n     while (c !=  x ) { n       if (!z) n         z = true; n       else n         x = y, y *= 2; n       cout <<  ?   << x <<     << y << endl; n       cout.flush(); n       cin >> c; n       if (c ==  e ) return 0; n     } n     long long p = bins(x, y); n     cout <<  !   << p << endl; n     cout.flush(); n     cin >> s; n     if (s ==  mistake ) { n       return 0; n     } n     if (s ==  end ) return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 2 * acos(0.0); n const int inf = 0x3f3f3f3f; n const double infd = 1.0 / 0.0; n long long power(long long x, long long y, long long MOD) { n   long long res = 1; n   x = x % MOD; n   while (y > 0) { n     if (y & 1) res = (res * x) % MOD; n     y = y >> 1; n     x = (x * x) % MOD; n   } n   return res; n } n long long mul(long long a, long long b, long long MOD) { n   if (b == 1) { n     return a; n   } n   if (b % 2 == 0) { n     return 2 * (mul(a, b / 2, MOD) % MOD); n   } else { n     return (a + (2 * (mul(a, b / 2, MOD)) % MOD)) % MOD; n   } n } n bool ispow2(long long n) { n   if (((n & (n - 1)) == 0) && n != 0) { n     return true; n   } n   return false; n } n bool prime(int x) { n   if (x == 1) { n     return false; n   } n   if (x == 2) { n     return true; n   } n   if (x % 2 == 0) { n     return false; n   } n   for (int i = 3; i * i <= x; i += 2) { n     if (x % i == 0) { n       return false; n     } n   } n   return true; n } n int __gcd(int a, int b) { n   if (b == 0) return a; n   return __gcd(b, a % b); n } n int coprime(int a, int b) { n   if (a == b) return 0; n   if (prime(a) && prime(b)) return 1; n   if ((a == 2 && b % 2 != 0) || (b == 2 && a % 2 != 0)) return 1; n   if ((a % 2 != 0 && prime(b) && a < b) || (b % 2 != 0 && prime(a) && a > b)) n     return 1; n   if (abs(a - b) == 1) return 1; n   if (a == 1 || b == 1) return 1; n   return __gcd(a, b); n } n unsigned long long lcm(unsigned a, unsigned b) { n   return ((unsigned long long)a) * (b / __gcd(a, b)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, diff; n   string s; n   cin >> n; n   cin >> s; n   map<int, int> m; n   int ans = 0; n   m[0] = -1; n   diff = 0; n   for (int i = 0; i < n; i++) { n     int tmp = (s[i] ==  0  ? -1 : 1); n     diff += tmp; n     if (m.find(diff) == m.end()) { n       m[diff] = i; n     } n     ans = max(ans, i - m[diff]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 25; n int main(int argc, char *argv[]) { n   ios ::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   string s; n   cin >> n >> k; n   cin >> s; n   if (k > (n + 1) / 2) { n     --k; n     while (k < n - 1) { n       cout <<  RIGHT  <<   n ; n       ++k; n     } n     while (k > -1) { n       cout <<  PRINT   << s[k] <<   n ; n       if (k - 1 >= 0) cout <<  LEFT  <<   n ; n       --k; n     } n   } else { n     --k; n     while (k > 0) { n       cout <<  LEFT  <<   n ; n       --k; n     } n     while (k < n) { n       cout <<  PRINT   << s[k] <<   n ; n       if (k + 1 < n) cout <<  RIGHT  <<   n ; n       ++k; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[212345], ans = (1ll << 62); n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (long long i = 0; i < 2 * n; ++i) { n     cin >> a[i]; n   } n   sort(a, a + 2 * n); n   for (long long i = n; i < 2 * n - 1; ++i) { n     ans = min(ans, (a[2 * n - 1] - a[0]) * (a[i] - a[i - n + 1])); n   } n   ans = min(ans, (a[n - 1] - a[0]) * (a[2 * n - 1] - a[n])); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005, mod = 998244353; n int n, a[N][N], f[N][N], jie[N], ans, vis1[N], vis2[N], c[N], c2[N], vis3[N]; n inline int qpow(int a, int b, int res = 1) { n   for (; b; b >>= 1, a = 1ll * a * a % mod) n     if (b & 1) res = 1ll * res * a % mod; n   return res; n } n void init() { n   jie[0] = 1; n   for (register int i = 1; i <= n; ++i) jie[i] = 1ll * jie[i - 1] * i % mod; n   f[0][0] = 1, f[1][1] = 1; n   for (register int i = 2; i <= n; i++) { n     f[i][0] = 1ll * (i - 1) * (f[i - 1][0] + f[i - 2][0]) % mod; n     for (register int j = 1; j <= i; j++) n       f[i][j] = (f[i][j - 1] + f[i - 1][j - 1]) % mod; n   } n } n inline int lowbit(int x) { return x & -x; } n inline void add(int x, int y) { n   for (; x <= n; x += lowbit(x)) c[x] += y; n } n inline int get(int x, int res = 0) { n   for (; x; x -= lowbit(x)) res += c[x]; n   return res; n } n inline void add2(int x, int y) { n   for (; x <= n; x += lowbit(x)) c2[x] += y; n } n inline int get2(int x, int res = 0) { n   for (; x; x -= lowbit(x)) res += c2[x]; n   return res; n } n signed main() { n   scanf( %d , &n); n   init(); n   for (register int i = 1; i <= n; ++i) { n     int tmp = qpow(f[n][0], n - i), res = 0, num = 0; n     memset(vis1, 0, sizeof(vis1)), memset(vis2, 0, sizeof(vis2)); n     memset(vis3, 0, sizeof(vis3)); n     memset(c, 0, sizeof(c)), memset(c2, 0, sizeof(c2)); n     for (register int j = 1; j <= n; ++j) add(j, 1), add2(j, 1); n     for (register int j = 1; j <= n; ++j) { n       scanf( %d , &a[i][j]); n       if (i == 1) { n         int cnt = get(a[i][j] - 1); n         res = (res + 1ll * cnt * jie[n - j] % mod) % mod; n         add(a[i][j], -1); n       } else { n         vis1[a[i - 1][j]] = 1; n         if (vis3[a[i - 1][j]] == 0) n           add2(a[i - 1][j], -1), vis3[a[i - 1][j]] = 1; n         if (vis2[a[i - 1][j]]) num--; n         int cnt = get(a[i][j] - 1); n         if (a[i - 1][j] < a[i][j] && get(a[i - 1][j]) - get(a[i - 1][j] - 1)) n           cnt--; n         int cnt1 = get2(a[i][j] - 1), cnt2 = 0; n         if (a[i - 1][j] < a[i][j] && get2(a[i - 1][j]) - get2(a[i - 1][j] - 1)) n           cnt1--; n         cnt2 = cnt - cnt1; n         res = (res + 1ll * cnt1 * f[n - j][num + 1] % mod) % mod; n         res = (res + 1ll * cnt2 * f[n - j][num] % mod) % mod; n         vis2[a[i][j]] = 1; n         add(a[i][j], -1); n         if (!vis3[a[i][j]]) add2(a[i][j], -1), vis3[a[i][j]] = 1; n         if (!vis1[a[i][j]]) num++; n       } n     } n     ans = (ans + 1ll * res * tmp % mod) % mod; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int X[4] = {0, 0, -1, 1}; n const int Y[4] = {-1, 1, 0, 0}; n int dp[1001][1001], Vis[1001][1001]; n char c[1001][1001]; n int vis[1001][1001], i, j, m, n, p, k, ans; n bool check(int x, int y, int xx, int yy) { n   if (x < 0 || y < 0 || x > n || y >= m) return false; n   if (c[x][y] ==  D  && c[xx][yy] ==  A ) return true; n   if (c[x][y] ==  I  && c[xx][yy] ==  D ) return true; n   if (c[x][y] ==  M  && c[xx][yy] ==  I ) return true; n   if (c[x][y] ==  A  && c[xx][yy] ==  M ) return true; n   return false; n } n int dfs(int x, int y) { n   if (Vis[x][y]) return dp[x][y]; n   if (vis[x][y]) { n     printf( Poor Inna! n ); n     exit(0); n   } n   vis[x][y] = 1; n   int i; n   for (i = 0; i < 4; i++) n     if (check(x + X[i], y + Y[i], x, y)) n       dp[x][y] = max(dp[x][y], dfs(x + X[i], y + Y[i])); n   if (c[x][y] ==  A ) dp[x][y]++; n   vis[x][y] = 0; n   Vis[x][y] = 1; n   return dp[x][y]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) scanf( %s , &c[i]); n   for (i = 1; i <= n; i++) n     for (j = 0; j < m; j++) n       if (c[i][j] ==  D ) ans = max(ans, dfs(i, j)); n   if (!ans) n     printf( Poor Dima! n ); n   else n     printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 1e6 + 5; n struct comp { n   bool operator()(pair<int, int> &a, pair<int, int> &b) { n     if (a.first == b.first) return (a.second < b.second); n     return (a.first > b.first); n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<pair<int, int>> ed(m); n   map<pair<int, int>, queue<int>> pos; n   priority_queue<pair<int, int>, vector<pair<int, int>>, comp> p; n   for (int i = 0; i < m; i++) { n     cin >> ed[i].first >> ed[i].second; n     p.push(ed[i]); n     pos[ed[i]].push(i); n   } n   vector<int> q; n   int i = 1, x = 2; n   queue<int> avl; n   q.push_back(i++); n   int s = q.size(); n   vector<pair<int, int>> res(m); n   bool check = true; n   while (!p.empty()) { n     pair<int, int> v = p.top(); n     p.pop(); n     int j = pos[v].front(); n     pos[v].pop(); n     if (v.second) { n       res[j] = {q.back(), i}; n       q.push_back(i++); n       if (q.size() > 2) avl.push(q.size() - 1); n     } else if (!avl.empty()) { n       int t = avl.front(); n       if (t - x >= 0) { n         res[j] = {q[t], q[t - x]}; n         x++; n       } else if (avl.size() == 1) { n         check = false; n         break; n       } else { n         avl.pop(); n         t = avl.front(); n         x = 2; n         res[j] = {q[t], q[t - x]}; n         x++; n       } n     } else { n       check = false; n       break; n     } n   } n   if (check) { n     for (pair<int, int> t : res) cout << t.first <<     << t.second <<   n ; n   } else n     cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500005; n int n, m, a[N], b[N]; n priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, n                greater<pair<long long, long long> > > n     Q; n long long ans; n int solve(int x) { n   ans = 0; n   int num = 0; n   for (int i = (int)(1); i <= (int)(n); i++) { n     Q.push(pair<long long, long long>(a[i], 0)); n     long long t = Q.top().first, now = b[i] - x; n     if (now + t < 0) { n       ans += now + t; n       Q.pop(); n       Q.push(pair<long long, long long>(-now, 1)); n     } n   } n   for (; !Q.empty(); num += Q.top().second, Q.pop()) n     ; n   return num; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = (int)(1); i <= (int)(n); i++) scanf( %d , &a[i]); n   for (int i = (int)(1); i <= (int)(n); i++) scanf( %d , &b[i]); n   long long l = 0, r = 2e9; n   while (l <= r) { n     long long mid = (l + r) / 2; n     int sum = solve(mid); n     if (sum == m) { n       printf( %lld n , ans + 1ll * m * mid); n       return 0; n     } n     if (sum < m) n       l = mid + 1; n     else n       r = mid - 1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long getnum() { n   char c = getchar(); n   long long num, sign = 1; n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) sign = -1; n   for (num = 0; c >=  0  && c <=  9 ;) { n     c -=  0 ; n     num = num * 10 + c; n     c = getchar(); n   } n   return num * sign; n } n pair<int, int> P[200050]; n int A[200050]; n stack<int> Stack; n int main() { n   int d = getnum(), tot = getnum(), n = getnum() + 2; n   P[1] = make_pair(0, 0); n   P[2] = make_pair(d, 0); n   for (int i = 3; i <= n; i++) { n     int x = getnum(), y = getnum(); n     P[i] = make_pair(x, y); n   } n   sort(P + 1, P + n + 1); n   Stack.push(n); n   for (int i = n - 1; i >= 1; i--) { n     while (P[Stack.top()].second > P[i].second) Stack.pop(); n     A[i] = Stack.top(); n     Stack.push(i); n   } n   long long ans = 0; n   int cur = 0; n   for (int i = 1; i <= n - 1; i++) { n     int x = 0, y = A[i]; n     int to = P[y].first - P[i].first; n     if (to > cur) { n       int a = to - cur; n       if (a + cur > tot) a = (tot - cur); n       ans += (long long)a * P[i].second; n       cur += a; n     } n     cur -= (P[i + 1].first - P[i].first); n     if (cur < 0) { n       puts( -1 ); n       return -0; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000000; n int z[N]; n void computeZ(char s[]) { n   int n = strlen(s), L = 0, R = 0; n   for (int i = 1; i < n; i++) { n     if (i > R) { n       L = R = i; n       while (R < n && s[R - L] == s[R]) R++; n       z[i] = R - L; n       R--; n     } else { n       int k = i - L; n       if (z[k] < R - i + 1) n         z[i] = z[k]; n       else { n         L = i; n         while (R < n && s[R - L] == s[R]) R++; n         z[i] = R - L; n         R--; n       } n     } n   } n } n char s[N + 1]; n int suffix[N + 1], size; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> s; n   computeZ(s); n   int n = strlen(s), mx = 0; n   int size = 0; n   for (int i = 1; i < n; ++i) n     if (z[i] == n - i) suffix[z[i]]++; n   for (int i = 1; i < n; ++i) { n     if (suffix[i]) n       suffix[i] = i; n     else n       suffix[i] = suffix[i - 1]; n   } n   for (int i = 1; i < n; ++i) { n     int len = z[i] - (z[i] == n - i); n     mx = max(mx, suffix[len]); n   } n   if (mx == 0) n     cout <<  Just a legend  << endl; n   else { n     s[mx] =   0 ; n     cout << s << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n vector<vector<int>> v; n void sit(int x, int y) { n   while (1) { n     int u = max(1, x - v[x][y]), d = min(n, x + v[x][y]); n     for (; u <= d; ++u) { n       if (y - v[x][y] + abs(u - x) > 0) { n         if (v[u][y - v[x][y] + abs(u - x)] == 0) { n           cout << u <<     << y - v[x][y] + abs(u - x) <<   n ; n           ++v[u][y - v[x][y] + abs(u - x)]; n           return; n         } else if (v[x][y] < v[u][y - v[x][y] + abs(u - x)] - v[x][y]) { n           v[x][y] = v[u][y - v[x][y] + abs(u - x)] - v[x][y]; n           break; n         } n       } n       if (y + v[x][y] - abs(u - x) <= m) { n         if (v[u][y + v[x][y] - abs(u - x)] == 0) { n           cout << u <<     << y + v[x][y] - abs(u - x) <<   n ; n           ++v[u][y + v[x][y] - abs(u - x)]; n           return; n         } else if (v[x][y] < v[u][y + v[x][y] - abs(u - x)] - v[x][y]) { n           v[x][y] = v[u][y + v[x][y] - abs(u - x)] - v[x][y]; n           break; n         } n       } n     } n     if (u > d) ++v[x][y]; n   } n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m >> k; n   v.resize(n + 1, vector<int>(m + 1, 0)); n   while (k--) { n     int a, b; n     cin >> a >> b; n     sit(a, b); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = (long long)(1e9 + 7); n const long long N = (long long)(1e5 + 7); n long long p[N]; n long long h[N]; n int n, m; n bool fun(long long dist) { n   queue<long long> q; n   for (int i = 0; i < m; i++) { n     q.push(p[i]); n   } n   for (int i = 0; i < n; i++) { n     if (q.empty()) { n       return true; n     } n     if (q.front() >= h[i]) { n       while (!q.empty() && q.front() <= h[i] + dist) { n         q.pop(); n       } n     } else { n       long long x = h[i] - q.front(); n       if (x > dist) { n         return false; n       } n       long long y = max(dist - (2 * x), (dist - x) / 2); n       y = max(y, (long long)0); n       while (!q.empty() && q.front() <= h[i]) { n         q.pop(); n       } n       while (!q.empty() && q.front() - h[i] <= y) { n         q.pop(); n       } n     } n   } n   if (q.empty()) { n     return true; n   } else { n     return false; n   } n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> h[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> p[i]; n   } n   sort(h, h + n); n   sort(p, p + m); n   long long l = 0; n   long long r = 20000000007; n   long long ans = -1; n   while (l <= r) { n     long long dist = (l + r) / 2; n     if (fun(dist)) { n       ans = dist; n       r = dist - 1; n     } else { n       l = dist + 1; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long maxm = -1e18; n long long minm = 1e18; n bool isP(long long n) { n   if (n == 2LL) return 1; n   if (n % 2LL == 0LL) n     return 0; n   else if (n == 1) n     return 0; n   for (long long i = 3; i * i <= n; ++i) n     if (n % i == 0) return 0; n   return 1; n } n void solve() { n   int n; n   cin >> n; n   string res; n   while (n > 0) { n     if (n == 3) { n       res = res +  7 ; n       n -= 3; n     } else { n       res = res +  1 ; n       n -= 2; n     } n   } n   string result; n   for (int i = res.length() - 1; i >= 0; i--) { n     result = result + res[i]; n   } n   cout << result << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[5005][5005]; n deque<int> dq[5005]; n int a[5005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, k, x; n   cin >> n >> k >> x; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   memset(dp, -1, sizeof(dp)); n   dp[0][0] = 0; n   dq[0].push_back(0); n   for (int i = 1; i <= n; i++) { n     for (int l = x - 1; l >= 0; l--) { n       while (!dq[l].empty() && i - dq[l].front() > k) dq[l].pop_front(); n       if (dq[l].empty()) continue; n       dp[i][l + 1] = max(dp[i][l + 1], dp[dq[l].front()][l] + a[i]); n       while (!dq[l + 1].empty() && dp[dq[l + 1].back()][l + 1] <= dp[i][l + 1]) n         dq[l + 1].pop_back(); n       dq[l + 1].push_back(i); n     } n   } n   long long ans = -1; n   for (int i = n; i >= n - k + 1; i--) ans = max(ans, dp[i][x]); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int t, f, w; n   bool operator<(const edge& e) const { return w > e.w; } n   edge(int from, int to, int weight) { n     t = to; n     f = from; n     w = weight; n   } n }; n const double EPS = (1e-5); n long long binpow(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a; n     a = a * a; n     b >>= 1; n   } n   return res; n } n int findXOR(int n) { n   switch (n % 4) { n     case 0: n       return n; n     case 1: n       return 1; n     case 2: n       return n + 1; n     case 3: n       return 0; n   } n } n int rangeXOR(int l, int r) { return (findXOR(l - 1) ^ findXOR(r)); } n int getbit(int mask, int bit) { return (mask & (1 << bit)); } n void setbit(int& mask, int bit, int val) { n   if (val) n     mask |= (1 << bit); n   else n     mask &= ~(1 << bit); n } n const int N = 100 + 10; n const int M = 101; n const long long INF = 1000 * 1e9 + 1; n void solve() { n   int x; n   cin >> x; n   if (x == 3) { n     cout << 5 <<   n ; n     return; n   } n   if (x == 1) { n     cout << 1 <<   n ; n     return; n   } n   int i = 1; n   while (x > i * i / 2 + 1) { n     i += 2; n   } n   cout << i <<   n ; n } n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int tc = 1; n   while (tc--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n char st[300][100], sy[10000], ss[1000000]; n int main() { n   long a, i, j, len, po, k; n   strcpy(st[ > ],  1000 ); n   strcpy(st[ < ],  1001 ); n   strcpy(st[ + ],  1010 ); n   strcpy(st[ - ],  1011 ); n   strcpy(st[ . ],  1100 ); n   strcpy(st[ , ],  1101 ); n   strcpy(st[ [ ],  1110 ); n   strcpy(st[ ] ],  1111 ); n   while (gets(sy)) { n     ss[0] = 0; n     len = strlen(sy); n     for (i = 0; i < len; i++) { n       strcat(ss, st[sy[i]]); n     } n     j = a = 0; n     len = strlen(ss); n     for (i = len - 1; i >= 0; i--) { n       po = 1; n       for (k = 0; k < j; k++) { n         po = po * 2; n         po = po % 1000003; n       } n       a += (ss[i] - 48) * po; n       j++; n       a = a % 1000003; n     } n     printf( %ld n , a); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m, a, b, c; n   cin >> n >> m; n   vector<int> ans(n, 0); n   set<int> s; n   for (int i = 1; i <= n; ++i) { n     s.insert(i); n   } n   while (m--) { n     cin >> a >> b >> c; n     auto lt = s.lower_bound(a), ht = prev(s.upper_bound(b)); n     auto ct = lt; n     while (ct != next(ht)) { n       if (*ct != c) { n         ans[*ct - 1] = c; n       } n       ct = next(ct); n     } n     s.erase(lt, next(ht)); n     s.insert(c); n   } n   for (int x : ans) cout << x <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n long long q[500009]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   long long sum = 0; n   long long mini = 0; n   for (long long i = 1; i < n; i++) { n     cin >> q[i]; n     sum += q[i]; n     if (sum < mini) mini = sum; n   } n   bool ok = 1; n   long long p1 = 1 - mini; n   set<long long> vis; n   vector<long long> p; n   p.push_back(p1); n   vis.insert(p1); n   long long prev = p1; n   for (long long i = 1; i < n; i++) { n     long long x = prev + q[i]; n     if (x < 1 or x > n or vis.count(x)) { n       ok = 0; n       break; n     } n     p.push_back(x); n     vis.insert(x); n     prev = x; n   } n   if (p[0] > n or p[0] < 1) ok = 0; n   if (!ok) { n     cout <<  -1 n ; n     return 0; n   } n   for (auto i : p) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e3 + 100; n struct node { n   int a, b, c; n } p[N]; n int nex[N]; n vector<int> pre[N]; n int dp[N][N]; n int main() { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   int sum = k; n   for (int i = 1; i <= n; ++i) n     scanf( %d %d %d , &p[i].a, &p[i].b, &p[i].c), sum += p[i].b; n   for (int i = 1; i <= n; ++i) nex[i] = i; n   for (int i = 1; i <= m; ++i) { n     int u, v; n     scanf( %d %d , &u, &v); n     nex[v] = max(nex[v], u); n   } n   for (int i = 1; i <= n; ++i) { n     pre[nex[i]].push_back(p[i].c); n   } n   memset(dp, -0x3f3f3f3f, sizeof(dp)); n   dp[1][k] = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = p[i].a; j <= 5000; ++j) { n       int nj = j + p[i].b; n       if (dp[i][j] >= 0) dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j]); n     } n     for (int k = 0; k < pre[i].size(); ++k) { n       int x = pre[i][k]; n       for (int j = 0; j <= 5000; ++j) { n         dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j + 1] + x); n       } n     } n   } n   int ans = -1; n   for (int i = 0; i <= sum; ++i) ans = max(ans, dp[n + 1][i]); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, m; n   cin >> n >> m; n   if (n == 1) n     cout << 0; n   else if (n == 2) n     cout << m; n   else n     cout << 2 * m; n   puts(  ); n } n int main() { n   int q; n   for (cin >> q; q--;) solve(); n   return 0; n } n