#include <bits/stdc++.h> n FILE *in = stdin, *out = stdout; n using namespace std; n int n; n vector<int> son[5005]; n int tot; n vector<int> edge[5005]; n int dy[5005][2][5005]; n int leaf[5005]; n int visit[5005]; n void input() { n   fscanf(in,  %d , &n); n   for (int i = (1); i <= (n - 1); i++) { n     int x, y; n     fscanf(in,  %d , &x), fscanf(in,  %d , &y); n     edge[x].push_back(y); n     edge[y].push_back(x); n   } n } n int temp[2][5005]; n void go(int x) { n   visit[x] = 1; n   int flag = 0; n   for (auto &i : edge[x]) { n     if (visit[i]) continue; n     go(i); n     if (flag == 0) dy[x][0][0] = dy[x][1][0] = 0; n     flag = 1; n     for (int j = (0); j <= (leaf[x] + leaf[i]); j++) n       temp[0][j] = temp[1][j] = 0x7fffffff; n     for (int p1 = (0); p1 <= (1); p1++) n       for (int p2 = (0); p2 <= (1); p2++) n         for (int j = (0); j <= (leaf[x]); j++) n           for (int k = (0); k <= (leaf[i]); k++) n             if (dy[x][p1][j] != 0x7fffffff && dy[i][p2][k] != 0x7fffffff) n               temp[p1][j + k] = n                   min(temp[p1][j + k], n                       dy[x][p1][j] + dy[i][p2][k] + (p1 == p2 ? 0 : 1)); n     for (int j = (0); j <= (leaf[x] + leaf[i]); j++) n       for (int k = (0); k <= (1); k++) dy[x][k][j] = temp[k][j]; n     leaf[x] += leaf[i]; n   } n   if (flag == 0) { n     leaf[x] = 1; n     dy[x][0][0] = 0; n     dy[x][1][1] = 0; n     tot++; n   } n } n void pro() { n   for (int i = (0); i <= (n); i++) n     for (int j = (0); j <= (n); j++) dy[i][0][j] = dy[i][1][j] = 0x7fffffff; n   for (int i = (1); i <= (n); i++) n     if (edge[i].size() > 1) { n       go(i); n       fprintf(out,  %d , dy[i][1][tot / 2]); n       return; n     } n } n int main() { n   input(); n   if (n == 2) n     fprintf(out,  1 ); n   else n     pro(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n const int maxn = 2e5 + 5; n struct node { n   int idx, p; n   bool operator<(const node &b) const { return p < b.p; } n } ver[100005], hor[100005]; n int vcnt, hcnt; n vector<node> V1[maxn], V2[maxn]; n int tempx[100005], tempy[100005]; n int ansx[100005], ansy[100005]; n int main() { n   int n, w, h; n   cin >> n >> w >> h; n   int g, p, t; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &g); n     if (g == 1) { n       vcnt++; n       scanf( %d%d , &p, &t); n       V1[p - t + 100000].push_back({i, p}); n     } else { n       hcnt++; n       scanf( %d%d , &p, &t); n       V2[p - t + 100000].push_back({i, p}); n     } n   } n   for (int i = 0; i < maxn; i++) { n     sort(V1[i].begin(), V1[i].end()); n     sort(V2[i].begin(), V2[i].end()); n   } n   for (int i = 0; i < maxn; i++) { n     int cnt = 0; n     for (int j = 0; j < V1[i].size(); j++) { n       tempx[cnt] = V1[i][j].p; n       tempy[cnt] = h; n       cnt++; n     } n     for (int j = V2[i].size() - 1; j >= 0; j--) { n       tempx[cnt] = w; n       tempy[cnt] = V2[i][j].p; n       cnt++; n     } n     cnt = 0; n     for (int j = V2[i].size() - 1; j >= 0; j--) { n       ansx[V2[i][j].idx] = tempx[cnt]; n       ansy[V2[i][j].idx] = tempy[cnt]; n       cnt++; n     } n     for (int j = 0; j < V1[i].size(); j++) { n       ansx[V1[i][j].idx] = tempx[cnt]; n       ansy[V1[i][j].idx] = tempy[cnt]; n       cnt++; n     } n   } n   for (int i = 1; i <= n; i++) { n     printf( %d %d n , ansx[i], ansy[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105][105], n, m, k; n int res, c; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) cin >> a[i][j]; n   for (int j = 1; j <= m; ++j) { n     int cnt = 0; n     int maxx = 0; n     int now = 0; n     for (int i = 1; i <= n; ++i) n       if (a[i][j] == 1) { n         int tmp = 0; n         cnt++; n         for (int h = i; h <= i + min(k, n - i + 1) - 1; ++h) n           if (a[h][j] == 1) ++tmp; n         if (tmp > maxx) { n           maxx = tmp; n           now = cnt; n         } n       } n     res += maxx; n     c += max(0, now - 1); n   } n   cout << res <<     << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long x, long long y, long long z) { n   long long ret = 1; n   while (y > 0) { n     if (y & 1) ret = (ret * x) % z; n     x = (x * x) % z; n     y >>= 1; n   } n   return ret; n } n long long q; n map<long long, long long> getFactors(long long num) { n   long long i; n   map<long long, long long> answer; n   for (i = 2; i * i <= num; i++) { n     while (num > 1 && num % i == 0) { n       answer[i]++; n       num /= i; n     } n   } n   if (num > 1) answer[num]++; n   return answer; n } n int main() { n   long long i, j, t; n   scanf( %lld , &q); n   ; n   map<long long, long long> factors = getFactors(q); n   long long sum = 0, xorVal = 0, flag = 0, ting; n   for (auto it = factors.begin(); it != factors.end(); it++) { n     long long temp = it->second; n     sum += temp; n   } n   xorVal = factors.size(); n   cerr <<  sum  n        <<   ::   << sum <<   t  n        <<  flag  n        <<   ::   << flag <<   t  n        <<  xorVal  n        <<   ::   << xorVal <<   n ; n   ; n   if (sum == 2) { n     cout <<  2  << endl; n     return 0; n   } n   if (sum <= 1) n     cout <<  1  << endl <<  0  << endl; n   else { n     long long answer = 1, cutter = 0; n     for (auto it = factors.begin(); it != factors.end(); it++) { n       long long siz = it->second; n       for (j = 0; j < siz; j++) { n         if (cutter >= 2) { n           cout <<  1  << endl << answer << endl; n           return 0; n         } n         answer = answer * (it->first); n         cutter++; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, k; n     cin >> n >> k; n     long long int arr[n]; n     long long int i; n     for (i = 0; i <= n - 1; i++) { n       cin >> arr[i]; n     } n     long long int temp = k; n     vector<long long int> v; n     long long int sum = 0; n     for (i = 0; i <= n - 1; i++) { n       sum = sum + arr[i]; n       if (sum % 2 != 0 && temp > 1) { n         v.push_back(i); n         sum = 0; n         temp--; n       } n     } n     if (sum % 2 == 0) { n       cout <<  NO  n            <<   n ; n     } else { n       v.push_back(n - 1); n       cout <<  YES  n            <<   n ; n       for (i = 0; i < v.size(); i++) { n         cout << v[i] + 1 <<    ; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 111111; n const int INF = 1000000000, mod = 1000000007; n const long long LLINF = 1000000000000000000ll; n pair<string, string> a[N]; n int p[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second; n   for (int i = 0; i < n; ++i) { n     cin >> p[i]; n     --p[i]; n   } n   string mx =   ; n   for (int j = 0; j < n; ++j) { n     int i = p[j]; n     if (j == 0) { n       mx = min(a[i].first, a[i].second); n       continue; n     } n     if (a[i].first > a[i].second) swap(a[i].first, a[i].second); n     if (a[i].first < mx) { n       if (a[i].second < mx) { n         puts( NO ); n         return 0; n       } n       mx = a[i].second; n     } else n       mx = a[i].first; n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n int n, d, m, cnta = 0, cntb = 0; n long long a[MAXN], b[MAXN]; n inline bool cmp(long long x, long long y) { return x > y; } n int main() { n   scanf( %d%d%d , &n, &d, &m); n   for (int i = 1; i <= n; i++) { n     long long x; n     scanf( %lld , &x); n     if (x > m) n       a[++cnta] = x; n     else n       b[++cntb] = x; n   } n   sort(a + 1, a + cnta + 1, cmp); n   sort(b + 1, b + cntb + 1, cmp); n   for (int i = 1; i <= cnta; i++) a[i] += a[i - 1]; n   for (int i = 1; i <= cntb; i++) b[i] += b[i - 1]; n   long long ans = b[cntb]; n   for (int i = 1; i <= cnta; i++) { n     if ((i - 1) * (d + 1) + 1 > n) break; n     int ind = min(n - (i - 1) * (d + 1) - 1, cntb); n     ans = max(ans, a[i] + b[ind]); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5, MOD = 1e9 + 7; n int main() { n   vector<int> a(27, -1), b(27, -1), c(27, -1); n   string str; n   cin >> str; n   for (int i = 0; i < (int)str.size(); i++) { n     if (a[str[i] -  a ] == -1) { n       a[str[i] -  a ] = i; n       c[str[i] -  a ] = i; n     } else { n       b[str[i] -  a ] = max(i - a[str[i] -  a ], b[str[i] -  a ]); n       a[str[i] -  a ] = i; n     } n   } n   int x = (int)str.size() - 1; n   int ans = (int)str.size() / 2 + 1; n   for (int i = 0; i < 27; i++) { n     if (b[i] != -1) { n       b[i] = max({b[i], x - a[i] + 1, c[i] + 1}); n       ans = min(b[i], ans); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class U> n void ckmin(T &a, U b) { n   if (a > b) a = b; n } n template <class T, class U> n void ckmax(T &a, U b) { n   if (a < b) a = b; n } n int N, K; n pair<int, int> arr[213]; n vector<pair<int, int> > blocks; n int dp[100013][2]; n int tmp[100013][2]; n int ans; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(12); n   cerr << fixed << setprecision(4); n   cin >> N >> K; n   for (auto i = (0); i < (K); i++) { n     cin >> arr[i].first >> arr[i].second; n     if (i == 0 && arr[i].first > 0) { n       blocks.push_back({arr[i].first, 0}); n     } n     if (i > 0) { n       blocks.push_back({arr[i].first - arr[i - 1].second, 0}); n     } n     blocks.push_back({arr[i].second - arr[i].first, 1}); n   } n   if (arr[K - 1].second != 2 * N) { n     blocks.push_back({2 * N - arr[K - 1].second, 0}); n   } n   for (auto i = (0); i < (N + 1); i++) { n     dp[i][0] = 1000000007; n     dp[i][1] = 1000000007; n   } n   dp[0][0] = 0; n   dp[0][1] = 0; n   for (pair<int, int> p : blocks) { n     int sz = p.first; n     if (!p.second) { n       for (auto i = (N + 1) - 1; i >= (sz); i--) { n         dp[i][0] = dp[i - sz][0]; n       } n       for (auto i = (sz)-1; i >= (0); i--) { n         dp[i][0] = 1000000007; n       } n     } else { n       for (auto i = (0); i < (N + 1); i++) { n         tmp[i][0] = 1000000007; n         tmp[i][1] = 1000000007; n       } n       multiset<int> dp0, dp1; n       for (auto i = (0); i < (N + 1); i++) { n         dp0.insert(dp[i][0]); n         dp1.insert(dp[i][1]); n         ckmin(tmp[i][0], *dp0.begin() + 2); n         ckmin(tmp[i][1], *dp1.begin() + 2); n         ckmin(tmp[i][1], *dp0.begin() + 1); n         ckmin(tmp[i][0], *dp1.begin() + 1); n         if (i >= sz) { n           dp0.erase(dp0.find(dp[i - sz][0])); n           dp1.erase(dp1.find(dp[i - sz][1])); n         } n       } n       for (auto i = (0); i < (N + 1); i++) { n         ckmin(tmp[i][1], dp[i][1]); n       } n       for (auto i = (0); i < (N + 1 - sz); i++) { n         ckmin(tmp[i + sz][0], dp[i][0]); n       } n       for (auto i = (0); i < (N + 1); i++) { n         dp[i][0] = tmp[i][0]; n         dp[i][1] = tmp[i][1]; n       } n     } n   } n   ans = min(dp[N][0], dp[N][1]); n   if (ans >= 1000000007) { n     cout <<  Hungry n ; n     return 0; n   } n   cout <<  Full n  << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T bigmod(T p, T e, T M) { n   long long int ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int arr[105], cnt; n int moveee(int n) { n   for (int i = 3, j = 1; i <= 99; i += 2, j++) n     if (n == i) return j; n   return -1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   ; n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   int mov = moveee(n); n   if (mov == -1) return cout << -1 << endl, 0; n   for (int i = mov; i >= 1; i--) { n     int a = i, b = i << 1, c = b + 1; n     int maxi = max(arr[b], arr[c]); n     arr[a] = max(0, arr[a] - maxi); n     arr[b] = max(0, arr[b] - maxi); n     arr[c] = max(0, arr[c] - maxi); n     cnt += maxi; n   } n   if (arr[1] > 0) cnt += arr[1]; n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1); n const int maxn = 1e6 + 10; n const int mod = 1e9 + 7; n typedef struct p { n   long long x, y; n }; n p a[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lld %lld , &a[i].x, &a[i].y); n   } n   int isok = 1; n   if (n % 2) { n     printf( NO n ); n     return 0; n   } n   for (int i = 0; i < n / 2; i++) { n     int a1 = i, b1 = i + 1, a2 = (i + n / 2 + n) % n, n         b2 = (i + n / 2 + n + 1) % n; n     if (abs(a[a1].x - a[b1].x) != abs(a[a2].x - a[b2].x) || n         abs(a[a1].y - a[b1].y) != abs(a[a2].y - a[b2].y)) { n       isok = 0; n       break; n     } n   } n   if (isok) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r, c, n, k; n   cin >> r >> c >> n >> k; n   int x[n], y[n], res = 0, xi, yi; n   for (int i = 0; i < n; i++) { n     cin >> xi >> yi; n     x[i] = xi - 1; n     y[i] = yi - 1; n   } n   for (int i = 0; i < r; i++) { n     for (int j = 0; j < c; j++) { n       for (int i2 = i; i2 < r; i2++) { n         for (int j2 = j; j2 < c; j2++) { n           int violasenimg = 0; n           for (int v = 0; v < n; v++) { n             if (i <= x[v] && x[v] <= i2 && j <= y[v] && y[v] <= j2) n               violasenimg++; n           } n           if (violasenimg >= k) res++; n         } n       } n     } n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 5; n long long v[MAX]; n long long GCD(long long a, long long b) { n   if (!b) return a; n   if (!a) return b; n   return GCD(b, a % b); n } n int main() { n   long long mx = 0; n   long long n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> v[i]; n     mx = max(v[i], mx); n   } n   long long G = 0; n   for (int i = 1; i <= n; i++) { n     G = GCD(mx - v[i], G); n   } n   long long ans1 = 0; n   for (int i = 1; i <= n; i++) { n     ans1 += (mx - v[i]) / G; n   } n   cout << ans1 <<     << G <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 11; n long long cs[4][N], n, dp[N][4][4]; n vector<int> g[N], ord; n bool u[N]; n void dfs(int v) { n   u[v] = 1; n   ord.push_back(v); n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (!u[to]) dfs(to); n   } n } n pair<long long, vector<int> > f(int x, int y) { n   long long res = 0; n   vector<int> g(n + 1); n   g[ord[0]] = x; n   g[ord[1]] = y; n   res += cs[x][ord[0]] + cs[y][ord[1]]; n   for (int i = 2; i < n; i++) { n     int cur = ord[i]; n     int col = 6 - x - y; n     res += cs[col][cur]; n     g[cur] = col; n     x = y; n     y = col; n   } n   return make_pair(res, g); n } n void solve() { n   cin >> n; n   for (int i = 1; i <= 3; i++) n     for (int j = 1; j <= n; j++) cin >> cs[i][j]; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   int cnt = 0; n   for (int i = 1; i <= n; i++) n     if (g[i].size() > 2) { n       cout << -1; n       return; n     } else if (g[i].size() == 1) n       cnt++; n   if (cnt != 2) { n     cout << -1; n     return; n   } n   int st = 0; n   for (int i = 1; i <= n; i++) n     if (g[i].size() == 1) st = i; n   dfs(st); n   pair<long long, vector<int> > ans = min( n       min(f(1, 2), f(1, 3)), min(min(f(2, 1), f(2, 3)), min(f(3, 1), f(3, 2)))); n   cout << ans.first <<   n ; n   for (int i = 1; i <= n; i++) cout << ans.second[i] <<    ; n } n int main() { n   ; n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int tt = 1; n   for (int i = 1; i <= tt; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int sum = 0; n   for (int i = 0; i < n; ++i) { n     int l, r; n     cin >> l >> r; n     sum += r - l + 1; n   } n   int ct = 0; n   while (sum % k != 0) { n     ct++; n     sum++; n   } n   cout << ct << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char buff[100010]; n int calc() { n   scanf( %s , buff); n   int mask = 0; n   int len = strlen(buff); n   for (int i = 0; i < len; ++i) mask |= 1 << (buff[i] -  a ); n   return mask; n } n int main() { n   int n; n   scanf( %d , &n); n   int mask = (1 << 26) - 1; n   bool flag = false; n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %s , buff); n     char op = buff[0]; n     if (!flag) { n       if (op ==  ! ) { n         mask &= calc(); n       } else if (op ==  . ) { n         mask &= ~calc(); n       } else { n         mask &= ~calc(); n       } n       if (mask == (mask & -mask)) flag = true; n     } else { n       if (op ==  ! ) { n         if (mask & calc()) cnt++; n       } else if (op ==  . ) { n         calc(); n       } else { n         if (calc() != mask) cnt++; n       } n     } n   } n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e5 + 5; n char str[N]; n int n, s[N]; n long long f[N][11], ans; n inline int nex(int i, int j) { return (9 + i * (i - 1) / 2 + j + 1) % 11; } n int main() { n   scanf( %s , str + 1); n   n = strlen(str + 1); n   for (int i = 1; i <= n; ++i) s[i] = str[i] -  0 ; n   for (int i = 1; i <= n; ++i) n     if (s[i]) ++f[i][s[i]]; n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j <= 10; ++j) { n       if (s[i] < j) f[i][nex(j, s[i])] += f[i - 1][j]; n     } n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j <= 10; ++j) ans += f[i][j]; n   printf( %I64d n , ans); n   return 0; n } n


#include<bits/stdc++.h> n #define llu unsigned long long   n #define ll long long  n #define l1 int n #define u_m unordered_map n #define M map n #define answer cout<<ans<<endl; n #define Sum cout<<sum<<endl; n #define sort1(s) sort(s.begin(),s.end()) n #define pb(x) push_back(x) n #define yes cout<< YES <<endl; n #define no cout<< NO <<endl; n using namespace std; n /*  n vector<vector<int>> v(101,vector<int>()); n vector<bool> b(101,false); n vector<int> ans(101); n   n ll power(ll x, ll y)   n {   n     ll res = 1;         n     while (y > 0)   n     {   n         if (y & 1)   n             res = (res*x)%mod;   n         y = y/2;   n         x = (x*x)%mod; n     }   n     return res%mod;   n } n int dfs(int p,int k) n { n     a[p]=true; n     int i,j=1; n     for(i=0;i<v[p].size();i++) n     { n         if(a[v[p][i]]==false) n         { n             if(k==4) n             { n                 ans[v[p][i]]=5; n                 a[v[p][i]]=true; n                 j=dfs(v[p][i],5); n             } n             else n             { n                 ans[v[p][i]]=4; n                 a[v[p][i]]=true; n                 j=dfs(v[p][i],4); n             } n         } n     } n     return j; n } n ll prime(ll n) n { n     ll mx=n+1;//cout<<mx<<endl; n     for(ll i=2;i*i<=n;i++) n     { n         if(n%i==0 && n/i!=i) n         { n             mx=min(mx,(n/i) + i); n             //cout<<mx<<endl; n         } n     } n     return mx; n } n ll modinv(ll x) n { n     return power(x,mod-2)%mod;     n } n void swap(ll *xp, ll *yp)  n {  n     ll temp = *xp;  n     *xp = *yp;  n     *yp = temp;  n } n void print(vector<ll> v) n { n     ll i,j,n=v.size(); n     for(i=0;i<n;i++) n     { n         cout<<v[i]<<   ; n     } n     cout<<endl; n } n bool sortcol( const vector<int>& v1,  n                const vector<int>& v2 ) {  n  return v1[1] < v2[1];  n }  n   n bool sortcol1( const vector<int>& v1,  n                const vector<int>& v2 ) {  n  return v1[0] < v2[0];  n } n void scan(vll &v,ll n) n { n     for(ll i=0;i<n;i++) n     cin>>v[i]; n } n ll gcd(ll a, ll b)  n {  n     if (b == 0)  n         return a;  n     return gcd(b, a % b);   n } n ll lcm(ll a,ll b) n { n     return (a*b)/gcd(a,b); n } n */  n  n int main() n { n     ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); n     /*l1 t; n     cin>>t; n     while(t--)*/ n     { n      ll n; n      cin>>n; n      if(n==1) n      { n       cout<<4; n       return 0; n      } n      if(n%2==0) n      { n       n=(n/2 + 1)*(n/2 + 1); n      } n      else n      { n       n=2*(n/2 + 1)*(n/2 + 2); n      } n      cout<<n; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T, class T1> n inline void gn(T &first, T1 &second) { n   gn(first); n   gn(second); n } n template <class T, class T1, class T2> n inline void gn(T &first, T1 &second, T2 &z) { n   gn(first); n   gn(second); n   gn(z); n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <class T> n inline void printsp(T first) { n   print(first); n   putchar(   ); n } n template <class T> n inline void println(T first) { n   print(first); n   putchar(  n ); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n struct Node { n   int sz, val; n   Node *ls, *rs, *fa; n } buf[1111111]; n int used; n struct Splay_Tree { n   Node *root, *NIL, *minf, *pinf; n   Node *create_node(int first = 0) { n     Node *node = &buf[used++]; n     node->sz = 1; n     node->val = first; n     node->ls = node->rs = node->fa = NIL; n     return node; n   } n   void init() { n     used = 0; n     NIL = create_node(0); n     NIL->sz = 0; n     Node *first = create_node(-0x3f3f3f3f); n     Node *second = create_node(0x3f3f3f3f); n     first->rs = second; n     second->fa = first; n     root = first; n     minf = first; n     pinf = second; n     update(first); n   } n   void update(Node *first) { first->sz = first->ls->sz + first->rs->sz + 1; } n   void zig(Node *first) { n     Node *second = first->rs, *p = first->fa; n     first->rs = second->ls; n     second->ls->fa = first; n     second->ls = first; n     first->fa = second; n     second->fa = p; n     if (p == NIL) n       root = second; n     else if (p->ls == first) n       p->ls = second; n     else n       p->rs = second; n     update(first); n   } n   void zag(Node *first) { n     Node *second = first->ls, *p = first->fa; n     first->ls = second->rs; n     second->rs->fa = first; n     second->rs = first; n     first->fa = second; n     second->fa = p; n     if (p == NIL) n       root = second; n     else if (p->rs == first) n       p->rs = second; n     else n       p->ls = second; n     update(first); n   } n   void splay(Node *first, Node *g) { n     Node *second, *z; n     while (first->fa != g) { n       second = first->fa; n       z = second->fa; n       if (z == g) { n         if (first == second->rs) n           zig(second); n         else n           zag(second); n       } else if (second == z->rs) { n         if (first == second->rs) n           zig(z), zig(second); n         else n           zag(second), zig(z); n       } else { n         if (first == second->rs) n           zig(second), zag(z); n         else n           zag(z), zag(second); n       } n     } n     update(first); n   } n   Node *find(Node *first, int k) { n     if (k <= first->ls->sz) return find(first->ls, k); n     if (k == first->ls->sz + 1) return first; n     return find(first->rs, k - first->ls->sz - 1); n   } n   Node *next(Node *first, int val) { n     if (first == NIL) return first; n     if (first->val > val) { n       Node *ans = next(first->ls, val); n       if (ans == NIL) ans = first; n       return ans; n     } else n       return next(first->rs, val); n   } n   Node *prev(Node *first, int val) { n     if (first == NIL) return first; n     if (first->val < val) { n       Node *ans = prev(first->rs, val); n       if (ans == NIL) ans = first; n       return ans; n     } else n       return prev(first->ls, val); n   } n   Node *insert(int val) { n     Node *first = create_node(val); n     Node *z = prev(root, val); n     Node *second = next(root, val); n     splay(z, NIL); n     splay(second, z); n     first->fa = second; n     second->ls = first; n     update(second); n     update(z); n     return first; n   } n   void removeFront(int val) { n     Node *first = minf; n     Node *second = next(root, val); n     splay(first, NIL); n     splay(second, first); n     second->ls = NIL; n     update(second); n     update(first); n   } n   void removeBack(int val) { n     Node *first = pinf; n     Node *second = prev(root, val); n     splay(first, NIL); n     splay(second, first); n     second->rs = NIL; n     update(second); n     update(first); n   } n   void remove(int k) { n     Node *z = find(root, k), *second = find(root, k + 2); n     splay(z, NIL); n     splay(second, z); n     second->ls = NIL; n     update(second); n     update(z); n   } n   void remove(Node *first) { n     splay(first, NIL); n     remove(first->ls->sz); n   } n   void dup(Node *first) { n     if (first == NIL) return; n     dup(first->ls); n     printf( %d  , first->val); n     dup(first->rs); n   } n } tree; n int N, s1, s2; n int first[111111]; n bool possible(int d) { n   tree.init(); n   tree.insert(s1); n   int cur = s2; n   for (int i = 0; i < N; i++) { n     if (abs(cur - first[i]) <= d) tree.insert(cur); n     tree.removeFront(max(first[i] - d - 1, -0x3f3f3f3f + 1)); n     tree.removeBack(min(first[i] + d + 1, 0x3f3f3f3f - 1)); n     if (tree.root->sz == 2) { n       return false; n     } n     cur = first[i]; n   } n   return true; n } n int main() { n   gn(N, s1, s2); n   for (int i = 0; i < N; i++) gn(first[i]); n   int high = 1000000000, low = abs(s1 - s2); n   while (high > low + 1) { n     int mid = (high + low) / 2; n     if (possible(mid)) n       high = mid; n     else n       low = mid; n   } n   if (possible(low)) n     printf( %d n , low); n   else n     printf( %d n , high); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> sx; n vector<string> vx; n int ft; n void generate(char str[], int k, int l) { n   if (k < 0) { n     str[l] =   0 ; n     bool status = true; n     for (int i = 0; i < strlen(str); i++) { n       for (int j = i + 1; j < strlen(str); j++) { n         if (str[i] == str[j]) { n           status = false; n           break; n         } n       } n       if (status == false) break; n     } n     if (status) { n       vx.push_back(string(str)); n     } n     return; n   } else { n     for (int i = 0; i <= 9; i++) { n       str[l] = i +  0 ; n       generate(str, k - 1, l + 1); n     } n   } n } n set<string> check(string sno, int a, int b, set<string> fx) { n   set<string> px; n   for (int i = 0; i < vx.size(); i++) { n     int c = 0, d = 0; n     for (int j = 0; j < 4; j++) { n       if (vx[i][j] == sno[j]) n         c++; n       else { n         for (int k = 0; k < 4; k++) n           if (vx[i][j] == sno[k]) { n             d++; n             break; n           } n       } n     } n     if (c == a && b == d) { n       if (ft == 0 || fx.find(vx[i]) != fx.end()) px.insert(vx[i]); n     } n   } n   ft = 1; n   return px; n } n int main() { n   char str[10]; n   generate(str, 3, 0); n   set<string> fx; n   ft = 0; n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     char inp[100]; n     getchar(); n     scanf( %[^ n] , inp); n     string sno =   ; n     int b, c; n     b = c = -1; n     bool stat = false; n     for (int i = 0; i < strlen(inp); i++) { n       if (inp[i] ==    ) n         stat = true; n       else { n         if (!stat) n           sno += inp[i]; n         else if (b == -1) n           b = inp[i] -  0 ; n         else if (c == -1) n           c = inp[i] -  0 ; n       } n     } n     fx = check(sno, b, c, fx); n   } n   if (fx.size() == 1) { n     string req = *(fx.begin()); n     cout << req << endl; n   } else if (fx.size() > 1) n     cout <<  Need more data  << endl; n   else if (fx.size() == 0) n     cout <<  Incorrect data  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, int> > road; n   for (int a = (0); a < (m); a++) { n     int p, q; n     cin >> p >> q; n     if (p > q) swap(p, q); n     road.push_back(make_pair(p, q)); n   } n   int field[101][101]; n   for (int a = (0); a < (101); a++) n     for (int b = (0); b < (101); b++) field[a][b] = 10000; n   for (int a = (0); a < (m); a++) n     for (int b = (0); b < (m); b++) { n       int p = road[b].first; n       int q = road[b].second; n       int r = road[a].first; n       int s = road[a].second; n       if (p != r && p != s & q != r && q != s) { n         int count = 0; n         if (p < r && r < q) count++; n         if (p < s && s < q) count++; n         if (count == 1) { n           field[a + 1][b + 1] = 1; n           field[b + 1][a + 1] = 1; n         } n       } n     } n   int check[101]; n   for (int j = 1; j <= m; j++) n     for (int i = 1; i <= m; i++) n       for (int k = 1; k <= m; k++) n         field[i][k] = min(field[i][k], field[i][j] + field[j][k]); n   int flag = 0; n   for (int a = 1; a <= m; a++) n     if (field[a][a] % 2 == 1) flag = 1; n   if (flag == 1) n     cout <<  Impossible  << endl; n   else { n     int out[101]; n     for (int a = (0); a < (101); a++) out[a] = -1; n     for (int a = 1; a <= m; a++) n       if (out[a] == -1) { n         out[a] = 0; n         for (int b = 1; b <= m; b++) n           if (field[a][b] < 200) { n             out[b] = field[a][b] % 2; n           } n       } n     int fflag = 0; n     for (int a = 1; a <= m; a++) n       for (int b = 1; b <= m; b++) { n         if (field[a][b] == 1) n           if (out[a] + out[b] != 1) fflag = 1; n       } n     if (fflag == 1) n       cout <<  Impossible  << endl; n     else { n       for (int a = 1; a <= m; a++) { n         if (out[a] == 0) n           cout <<  o ; n         else n           cout <<  i ; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e6 + 00; n const long long inf = 1e9 + 800; n long long a[maxn]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, r, sum = 0; n   ; n   cin >> n >> r; n   for (long long i = 0; i < (1 << n); i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   cout << fixed << setprecision(10); n   for (long long i = 0; i < r; i++) { n     cout << (long double)sum / (1 << n) << endl; n     long long p, v; n     cin >> p >> v; n     sum -= a[p]; n     a[p] = v; n     sum += a[p]; n   } n   cout << (long double)sum / (1 << n) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double EPS = 1e-11; n void fast() { std::ios_base::sync_with_stdio(0); } n int n; n vector<int> arr; n string s; n vector<vector<int>> dp; n int calc(int i, int state) { n   if (i == n) return 0; n   int &ret = dp[i][state]; n   if (ret != -1) { n     return ret; n   } n   if (state == 1) return ret = calc(i + 1, 1) + arr[i]; n   if (s[i] ==  0 ) return ret = calc(i + 1, 0); n   int x = calc(i + 1, 0) + arr[i]; n   int y = calc(i + 1, 1); n   return ret = max(x, y); n } n int main() { n   scanf( %d , &n); n   arr.resize(n); n   dp.resize(n); n   for (int i = 0; i < n; i++) dp[i].resize(2, -1); n   for (int &i : arr) scanf( %d , &i); n   cin >> s; n   reverse(arr.begin(), arr.end()); n   reverse(s.begin(), s.end()); n   printf( %d n , calc(0, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int head[100005], fa[100005], a[100005]; n struct edge { n   int to, next; n } edges[200005]; n int em; n void addedge(int u, int v) { n   edges[em].next = head[u]; n   edges[em].to = v; n   head[u] = em++; n   edges[em].next = head[v]; n   edges[em].to = u; n   head[v] = em++; n } n void dfs(int x, int f) { n   fa[x] = f; n   for (int e = head[x]; e != -1; e = edges[e].next) { n     int v = edges[e].to; n     if (v != f) { n       dfs(v, x); n     } n   } n } n int gcd(int a, int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   int m = n - 1; n   memset(head, -1, sizeof(head)); n   while (m--) { n     int x, y; n     scanf( %d%d , &x, &y); n     addedge(x, y); n   } n   dfs(1, -1); n   while (q--) { n     int qq; n     scanf( %d , &qq); n     if (qq == 1) { n       int v; n       scanf( %d , &v); n       int in = -1; n       int tmp = fa[v]; n       while (tmp != -1) { n         int g = gcd(a[tmp], a[v]); n         if (g > 1) { n           in = tmp; n           break; n         } n         tmp = fa[tmp]; n       } n       printf( %d n , in); n     } else if (qq == 2) { n       int v, w; n       scanf( %d%d , &v, &w); n       a[v] = w; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   string second; n   cin >> n >> second; n   string t = second; n   bool ok = 1; n   vector<int> vec; n   for (int i = 0; i < n; i++) { n     if (second[i] ==  W ) continue; n     if (i + 1 < n && second[i + 1] == second[i]) { n       vec.push_back(i); n       second[i] = second[i + 1] =  W ; n       continue; n     } n     bool foo = 1; n     int to = -1; n     for (int j = i + 1; j < n; j++) n       if (second[j] == second[i]) { n         to = j; n         break; n       } n     for (int j = i + 1; j < to; j++) n       if (second[j] == second[i]) foo = 0; n     if (to == -1) foo = 0; n     if (foo) { n       for (int j = i; j < to; j++) { n         vec.push_back(j); n         second[j] =  W ; n       } n       second[to] =  W ; n       continue; n     } n     ok = 0; n     break; n   } n   if (ok) { n     cout << (int)vec.size() << endl; n     for (auto i : vec) cout << i + 1 <<    ; n     cout << endl; n     return 0; n   } n   ok = 1; n   vec.clear(); n   second = t; n   for (int i = 0; i < n; i++) { n     if (second[i] ==  B ) continue; n     if (i + 1 < n && second[i + 1] == second[i]) { n       vec.push_back(i); n       second[i] = second[i + 1] =  B ; n       continue; n     } n     bool foo = 1; n     int to = -1; n     for (int j = i + 1; j < n; j++) n       if (second[j] == second[i]) { n         to = j; n         break; n       } n     for (int j = i + 1; j < to; j++) n       if (second[j] == second[i]) foo = 0; n     if (to == -1) foo = 0; n     if (foo) { n       for (int j = i; j < to; j++) { n         vec.push_back(j); n         second[j] =  B ; n       } n       second[to] =  B ; n       continue; n     } n     ok = 0; n     break; n   } n   if (ok) { n     cout << (int)vec.size() << endl; n     for (auto i : vec) cout << i + 1 <<    ; n     cout << endl; n     return 0; n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int oo = 0x3f3f3f3f; n using namespace std; n inline int read() { n   int x = 0, f = 1, hhh = getchar(); n   while (!isdigit(hhh)) { n     if (hhh ==  - ) f = -1; n     hhh = getchar(); n   } n   while (isdigit(hhh)) { n     x = x * 10 + hhh -  0 ; n     hhh = getchar(); n   } n   return x * f; n } n const int c[] = {0, 1, 2, 3, 4, 6, 12}; n const int r[] = {0, 12, 6, 4, 3, 2, 1}; n char a[100][100]; n bool ans[10]; n int main() { n   int T = read(); n   while (T--) { n     memset(a, 0, sizeof(a)); n     memset(ans, false, sizeof(ans)); n     string s; n     cin >> s; n     for (int cas = (1); cas <= (6); ++cas) { n       int now = 0; n       for (int i = (1); i <= (c[cas]); ++i) n         for (int j = (1); j <= (r[cas]); ++j) { n           a[i][j] = s[now++]; n         } n       for (int i = (1); i <= (r[cas]); ++i) { n         bool flag = false; n         for (int j = (1); j <= (c[cas]); ++j) { n           if (a[j][i] ==  O ) { n             flag = true; n             break; n           } n         } n         if (!flag) { n           ans[cas] = true; n           break; n         } n       } n     } n     int ret = 0; n     for (int i = (1); i <= (6); ++i) ret += (ans[i] == true); n     printf( %d  , ret); n     for (int i = (1); i <= (6); ++i) n       if (ans[i] == true) printf( %dx%d  , c[i], r[i]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f, oo = inf; n inline long long read() { n   register long long x = 0, f = 1; n   register char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); n   return x * f; n } n inline void write(long long x) { n   if (x < 0) x = -x, putchar( - ); n   if (x >= 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n inline void writeln(long long x) { n   write(x); n   puts(  ); n } n const int Mod = 1e9 + 7; n inline int ppow(int a, int b) { n   int ans = 1; n   for (; b; b >>= 1, a = 1ll * a * a % Mod) n     if (b & 1) ans = 1ll * ans * a % Mod; n   return ans; n } n const int maxnode = 323333; n struct ACautomto { n   struct node { n     int nxt[26], fail, v, end; n   } T[maxnode]; n   int tot; n   int front, rear, q[maxnode], root[22]; n   ACautomto() { n     tot = 0; n     memset(T, 0, sizeof(T)); n   } n   inline void merge(int o1, int o2) { n     T[o2].end |= T[o1].end; n     for (register int i = (0); i < (26); ++i) { n       if (!T[o1].nxt[i]) n         continue; n       else if (!T[o2].nxt[i]) n         T[o2].nxt[i] = T[o1].nxt[i]; n       else n         merge(T[o1].nxt[i], T[o2].nxt[i]); n     } n   } n   inline void insert(int o, char s[]) { n     for (int i = 0; s[i]; ++i) { n       if (!T[o].nxt[s[i] -  a ]) T[o].nxt[s[i] -  a ] = ++tot; n       o = T[o].nxt[s[i] -  a ]; n     } n     ++T[o].end; n   } n   inline void build(int rt) { n     int u, v, p; n     front = rear = 0; n     q[rear++] = rt; n     while (front < rear) { n       u = q[front++]; n       for (register int i = (0); i < (26); ++i) n         if (T[u].nxt[i]) { n           v = T[u].nxt[i], p = T[u].fail; n           while (p && !T[p].nxt[i]) p = T[p].fail; n           T[v].fail = p ? T[p].nxt[i] : rt; n           T[v].v = T[v].end + T[T[v].fail].v; n           q[rear++] = v; n         } n     } n   } n   inline int query(int rt, char s[]) { n     int ans = 0, o = rt; n     for (int i = 0; s[i]; ++i) { n       while (o && !T[o].nxt[s[i] -  a ]) o = T[o].fail; n       if (o == 0) o = rt; n       o = T[o].nxt[s[i] -  a ]; n       ans += T[o].v; n     } n     return ans; n   } n   inline void add(char s[]) { n     int rt = ++tot; n     insert(rt, s); n     for (int i = 0; i < 20; ++i) { n       if (!root[i]) { n         root[i] = rt; n         build(root[i]); n         return; n       } else { n         merge(root[i], rt); n         root[i] = 0; n       } n     } n   } n   inline int get(char s[]) { n     int ans = 0; n     for (int i = 0; i < 20; ++i) ans += query(root[i], s); n     return ans; n   } n } add, del; n char s[300005]; n int main() { n   int T = read(); n   while (T--) { n     int op = read(); n     scanf( %s , s); n     if (op == 1) { n       add.add(s); n     } else if (op == 2) { n       del.add(s); n     } else { n       writeln(add.get(s) - del.get(s)); n     } n     fflush(stdout); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n struct sl { n   int SC, LC, TS, TL; n   double p; n } a[1005]; n struct csl { n   long double score, tim; n } f[1005][1561], ans; n double a1, a2, p; n int n, m, z, x, y; n inline bool cmp(sl x, sl y) { n   return x.TL * x.p * (1 - y.p) < y.TL * y.p * (1 - x.p); n } n inline csl Max(csl x, int l, int k, int t) { n   csl y; n   y.score = y.tim = 0; n   if (t == 1) { n     y.score = f[l - 1][k - a[l].TS].score + a[l].SC; n     y.tim = f[l - 1][k - a[l].TS].tim + a[l].TS; n   } else { n     y.score = f[l - 1][k - a[l].TS - a[l].TL].score + a[l].SC + n               a[l].LC * (1 - a[l].p); n     y.tim = a[l].p * (f[l - 1][k - a[l].TS - a[l].TL].tim + a[l].TS) + n             k * (1 - a[l].p); n   } n   if (x.score + eps < y.score || n       (abs(x.score - y.score) < eps && x.tim > y.tim)) n     return y; n   else n     return x; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d%d%d%lf , &a[i].SC, &a[i].LC, &a[i].TS, &a[i].TL, &a[i].p); n   } n   sort(a + 1, a + n + 1, cmp); n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j <= m; ++j) { n       f[i][j] = f[i - 1][j]; n       if (j >= a[i].TS) f[i][j] = Max(f[i][j], i, j, 1); n       if (j >= a[i].TS + a[i].TL) f[i][j] = Max(f[i][j], i, j, 2); n     } n   } n   for (int i = 0; i <= m; ++i) n     if (ans.score + eps < f[n][i].score || n         (abs(ans.score - f[n][i].score) < eps && ans.tim > f[n][i].tim)) n       ans = f[n][i]; n   printf( %.10lf %.10lf , (double)ans.score, (double)ans.tim); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int maxn = 205; n int fail[maxn + 5]; n int nxt[maxn + 5][maxn + 5]; n long long dp[maxn + 5][maxn + 5][maxn + 5]; n char s[maxn + 5]; n int n, len; n void Get_fail() { n   fail[0] = -1; n   int j = 0, k = -1; n   while (j <= len) { n     if (k == -1 || s[j] == s[k]) n       fail[++j] = ++k; n     else n       k = fail[k]; n   } n } n void Get_nxt() { n   for (int i = 0; i <= len; i++) { n     for (int j = 0; j <= 1; j++) { n       if (i == len) n         nxt[i + 1][j] = len; n       else { n         char ch = (j == 0 ?  )  :  ( ); n         int r = i; n         while (r >= 0 && s[r] != ch) r = fail[r]; n         if (s[r] == ch || r == -1) r++; n         nxt[i + 1][j] = r; n       } n     } n   } n } n long long DP() { n   dp[0][0][0] = 1; n   for (int i = 0; i <= 2 * n; i++) { n     for (int j = 0; j <= min(i, n); j++) { n       for (int k = 0; k <= len; k++) { n         for (int p = 0; p <= 1; p++) { n           if (dp[i][j][k] == 0) continue; n           int nj = j - (p == 0 ? 1 : -1); n           if (nj >= 0 && nj <= n) { n             int nk = nxt[k + 1][p]; n             dp[i + 1][nj][nk] = (dp[i + 1][nj][nk] + dp[i][j][k]) % mod; n           } n         } n       } n     } n   } n   return dp[2 * n][0][len]; n } n void Init() { n   len = strlen(s); n   Get_fail(); n   Get_nxt(); n } n int main() { n   scanf( %d , &n); n   scanf( %s , s); n   Init(); n   printf( %lld n , DP()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n vector<int> rating; n void reading() { n   cin >> N; n   rating.resize(N); n   for (int i = 0; i < N; i++) cin >> rating[i]; n } n void processing() { n   for (int i = 0; i < N; i++) { n     int res = 0; n     for (int j = 0; j < N; j++) n       if (rating[i] < rating[j]) res++; n     cout << res + 1 <<    ; n   } n } n int main() { n   reading(); n   processing(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXW = 1000000; n const int INF = 0x3f3f3f3f; n const int MOD = 1000000007; n const char test[] =  test n ; n int in(); n int tr[100010]; n int ls[100010]; n int n, m, k; n int hav[100010]; n int lowbit(int x) { return (-x) & x; } n void updata(int in, int val) { n   while (in <= n) { n     if (val > tr[in]) { n       tr[in] = val; n     } n     in += lowbit(in); n   } n } n int ask(int to) { n   int mx = 0; n   while (to) { n     mx = max(mx, tr[to]); n     to -= lowbit(to); n   } n   return mx; n } n int main() { n   cin >> n >> m; n   hav[1]++; n   hav[m + 1]--; n   for (int i = 0; i < n; ++i) { n     int u, v; n     cin >> u >> v; n     hav[u]++; n     hav[v + 1]--; n   } n   for (int i = 1; i <= m; ++i) { n     hav[i] += hav[i - 1]; n     int val = ask(hav[i]) + 1; n     updata(hav[i], val); n     ls[i] = val; n   } n   memset(tr, 0, sizeof tr); n   int ans = 0; n   for (int i = m; i > 0; --i) { n     int val = ask(hav[i]) + 1; n     updata(hav[i], val); n     ans = max(ans, ls[i] + val - 1); n   } n   cout << ans; n   return 0; n } n int in() { n   int input; n   scanf( %d , &input); n   return input; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream &operator<<(ostream &o, vector<T> &vec) { n   cout <<  [ ; n   for (int i = 0; i < ((int)(vec).size()); i++) { n     if (i) cout <<  ,  ; n     cout << vec[i]; n   } n   cout <<  ] ; n   return o; n } n template <typename T1, typename T2> n ostream &operator<<(ostream &o, pair<T1, T2> &pair) { n   cout <<  (  << pair.first <<  ,   << pair.second <<  ) ; n   return o; n } n const long long MX = 312312; n const long long SQMX = 1000; n const long long MD = 1000000007; n long long N; n vector<long long> ip; n bool isp[MX]; n vector<long long> prime; n long long sm[MX], tmp[MX]; n long long C[MX]; n long long fac[MX]; n long long ps[MX]; n long long fp(long long a, long long b) { n   if (!b) return 1 % MD; n   long long ret = fp(a * a % MD, b / 2); n   if (b & 1) ret = ret * a % MD; n   return ret; n } n inline long long rev(long long a) { return fp(a, MD - 2); } n void pre() { n   fill(isp + 2, isp + MX, true); n   for (long long i = 0; i < MX; i++) { n     if (isp[i]) { n       prime.push_back(i); n       for (long long j = i + i; j < MX; j += i) { n         isp[j] = false; n       } n     } n   } n   fac[0] = 1; n   for (long long i = 1; i <= N - 1; i++) fac[i] = fac[i - 1] * i % MD; n   for (long long i = 0; i <= N - 1; i++) n     C[i] = fac[N - 1] * rev(fac[i]) % MD * rev(fac[N - i - 1]) % MD; n   for (long long i = 0; i <= N - 1; i++) { n     ps[i + 1] = ps[i] + C[i]; n   } n } n int32_t main() { n   cin >> N; n   pre(); n   ip.resize(N); n   for (long long i = 0; i < N; i++) { n     cin >> ip[i]; n   } n   long long ans = 0; n   for (long long i = 0; i < ((long long)(prime).size()); i++) { n     long long p = prime[i]; n     if (p > SQMX) break; n     fill(tmp, tmp + N, 0); n     for (long long j = 0; j < N; j++) { n       long long t = 0, &a = ip[j]; n       while (a % p == 0) { n         a /= p; n         t++; n       } n       tmp[j] = t; n     } n     sort(tmp, tmp + N); n     for (long long j = 0; j < N; j++) sm[j] += tmp[j]; n   } n   sort(ip.begin(), ip.end()); n   for (long long i = 0, j = 0; i < N; i = j) { n     while (ip[j] == ip[i]) j++; n     if (ip[i] == 1) continue; n     long long c = j - i; n     for (long long k = 0; k < c; k++) { n       sm[N - k - 1]++; n     } n   } n   for (long long j = 0; j < N; j++) { n     long long f1 = ps[j] - ps[0], f2 = ps[N] - ps[j + 1]; n     long long f = f1 - f2; n     f = (f + MD) % MD; n     ans += f * sm[j]; n     ans %= MD; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int dx[4] = {-1, 0, 1, 0}; n int dy[4] = {0, -1, 0, 1}; n int n; n int map[2010][2010]; n int vis[2010][2010]; n int as, ac; n int go(int x, int y) { n   vis[y][x] = 1; n   int r = 1; n   for (int i = 0; i < 4; ++i) { n     int nx = x + dx[i], ny = y + dy[i]; n     if (map[ny][nx] == 0 || vis[ny][nx] == 1) continue; n     r += go(nx, ny); n   } n   return r; n } n int chk(int x, int y, int r) { n   int ok = 1; n   for (int i = y; i < y + r && ok == 1; ++i) n     for (int j = x; j < x + r && ok == 1; ++j) n       if (map[i][j] != 1) ok = 0; n   return ok == 1; n } n int main(void) { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) scanf( %d , &map[i][j]); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       if (map[i][j] == 0 || vis[i][j] == 1) continue; n       int nd = go(j, i); n       int r = (int)sqrt(nd); n       if (nd != r * r) n         ++ac; n       else { n         if (chk(j, i, r) == 1) n           ++as; n         else n           ++ac; n       } n     } n   } n   printf( %d %d n , ac, as); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 6e3 + 5; n const int mod = 1e9 + 7; n const int inf = 0x3f3f3f3f; n int main() { n   int t, n, k; n   char x; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     for (int i = 0; i < n % k; i++) { n       cout <<  a ; n     } n     for (int i = 0; i < k; i++) { n       x =  a  + i; n       for (int j = 0; j < n / k; j++) { n         cout << x; n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, first[200050], nxt[200050], to[200050], tot; n void add(int x, int y) { nxt[++tot] = first[x], first[x] = tot, to[tot] = y; } n double w[200050], ans, sum[200050]; n int fa[200050]; n void dfs(int u, int f) { n   ans += w[f] * (1 - w[u]); n   for (int i = first[u]; i; i = nxt[i]) { n     int t = to[i]; n     if (t == f) continue; n     fa[t] = u; n     sum[u] += (1 - w[t]); n     dfs(t, u); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lf , &w[i]); n   } n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x++; n     y++; n     add(x, y); n     add(y, x); n   } n   w[0] = 1; n   dfs(1, 0); n   scanf( %d , &m); n   while (m--) { n     int u; n     double v; n     scanf( %d%lf , &u, &v); n     u++; n     ans -= (1 - w[u]) * w[fa[u]]; n     ans -= w[u] * sum[u]; n     sum[fa[u]] -= (1 - w[u]); n     w[u] = v; n     sum[fa[u]] += (1 - w[u]); n     ans += w[u] * sum[u]; n     ans += (1 - w[u]) * w[fa[u]]; n     printf( %0.5lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const long long INF = 1e17, LINF = 1e12; n int n, m, q, x, y; n vector<int> g[N], s[N]; n int top[N], p[N], dp[N], son[N], sz, d[N]; n int tin1[N], tin2[N], tout[N], timer, num[N]; n pair<long long, int> og[N]; n pair<long long, int> t[4 * N]; n long long ob[4 * N]; n void dfs(int v, int pr = 0, int depth = 0) { n   d[v] = depth; n   p[v] = pr; n   dp[v] = 1; n   for (int to : g[v]) { n     if (to != pr) { n       dfs(to, v, depth + 1); n       dp[v] += dp[to]; n       if (dp[to] > dp[son[v]]) { n         son[v] = to; n       } n     } n   } n } n void hld(int v, int pr = 0) { n   tin1[v] = ++timer; n   og[timer] = make_pair(INF, 0); n   for (auto it : s[v]) { n     timer++; n     og[timer] = make_pair(it, it); n     num[it] = timer; n   } n   tin2[v] = timer; n   if (son[v]) { n     top[son[v]] = top[v]; n     hld(son[v], v); n   } n   for (int to : g[v]) { n     if (to != pr && to != son[v]) { n       top[to] = to; n       hld(to, v); n     } n   } n   tout[v] = timer; n } n void build(int v, int l, int r) { n   if (l == r) { n     t[v] = og[l]; n     return; n   } n   int mid = (r + l) >> 1; n   build(v + v, l, mid); n   build(v + v + 1, mid + 1, r); n   t[v] = min(t[v + v], t[v + v + 1]); n } n inline void push(int v) { n   if (ob[v]) { n     ob[v + v] += ob[v]; n     ob[v + v + 1] += ob[v]; n     t[v + v].first += ob[v]; n     t[v + v + 1].first += ob[v]; n     ob[v] = 0; n   } n } n void update(int v, int l, int r, int tl, int tr, long long val) { n   if (l > r || l > tr || tl > r) { n     return; n   } n   if (tl <= l && r <= tr) { n     t[v].first += val; n     ob[v] += val; n     return; n   } n   push(v); n   int mid = (r + l) >> 1; n   update(v + v, l, mid, tl, tr, val); n   update(v + v + 1, mid + 1, r, tl, tr, val); n   t[v] = min(t[v + v], t[v + v + 1]); n } n pair<long long, int> get(int v, int l, int r, int tl, int tr) { n   if (l > r || l > tr || tl > r) { n     return make_pair(INF, 0); n   } n   if (tl <= l && r <= tr) { n     return t[v]; n   } n   push(v); n   int mid = (r + l) >> 1; n   return min(get(v + v, l, mid, tl, tr), get(v + v + 1, mid + 1, r, tl, tr)); n } n pair<long long, int> get(int x, int y) { n   pair<long long, int> mn = make_pair(INF, 1e9); n   while (top[x] != top[y]) { n     if (d[top[x]] > d[top[y]]) { n       mn = min(mn, get(1, 1, timer, tin1[top[x]], tin2[x])); n       x = p[top[x]]; n     } else { n       mn = min(mn, get(1, 1, timer, tin1[top[y]], tin2[y])); n       y = p[top[y]]; n     } n   } n   if (d[x] > d[y]) { n     swap(x, y); n   } n   mn = min(mn, get(1, 1, timer, tin1[x], tin2[y])); n   return mn; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> q; n   for (int i = 1; i < n; i++) { n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int i = 1; i <= m; i++) { n     cin >> x; n     s[x].push_back(i); n   } n   dfs(1); n   top[1] = 1; n   hld(1); n   build(1, 1, timer); n   while (q--) { n     int t; n     cin >> t; n     if (t == 1) { n       int x, y, k; n       cin >> x >> y >> k; n       vector<int> ans; n       while (k) { n         auto it = get(x, y); n         if (it.first >= INF) { n           break; n         } n         k--; n         ans.push_back(it.second); n         update(1, 1, timer, num[it.second], num[it.second], INF); n       } n       cout << ans.size() <<    ; n       for (auto it : ans) { n         cout << it <<    ; n       } n       cout <<   n ; n     } else { n       int x, y; n       cin >> x >> y; n       update(1, 1, timer, tin1[x], tout[x], y); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)1e5 + 5; n int x[3]; n int y[3]; n int buf; n int main() { n   buf = 0; n   cin >> x[0] >> y[0] >> x[1] >> y[1] >> x[2] >> y[2]; n   for (long long i = 0; i < 3; ++i) { n     if (x[i] < y[i]) { n       buf = x[i]; n       x[i] = y[i]; n       y[i] = buf; n     } n   } n   int n = max(x[0], x[1]); n   n = max(n, x[2]); n   bool eq = (x[0] == x[1] && x[1] == x[2]); n   if (eq && x[0] == (y[0] + y[1] + y[2])) { n     cout << n <<   n ; n     for (long long i = 0; i < 3; ++i) { n       for (long long j = 0; j < y[i]; ++j) { n         for (long long p = 0; p < n; ++p) { n           cout << (char)(65 + i); n         } n         cout <<   n ; n       } n     } n     return 0; n   } n   int max = 0; n   int med = 1; n   int min = 2; n   if (n == x[1]) { n     max = 1; n     med = 0; n   } n   if (n == x[2]) { n     max = 2; n     min = 0; n     med = 1; n   } n   if (n - y[max] == x[med] && n - y[max] == x[min] && n == y[med] + y[min]) { n     cout << n <<   n ; n     for (long long j = 0; j < y[max]; ++j) { n       for (long long p = 0; p < n; ++p) { n         cout << (char)(65 + max); n       } n       cout <<   n ; n     } n     for (long long j = 0; j < n - y[max]; ++j) { n       for (long long p = 0; p < y[med]; ++p) { n         cout << (char)(65 + med); n       } n       for (long long p = 0; p < y[min]; ++p) { n         cout << (char)(65 + min); n       } n       cout <<   n ; n     } n     return 0; n   } n   if (n - y[max] == y[med] && n - y[max] == y[min] && n == x[med] + x[min]) { n     cout << n <<   n ; n     for (long long j = 0; j < y[max]; ++j) { n       for (long long p = 0; p < n; ++p) { n         cout << (char)(65 + max); n       } n       cout <<   n ; n     } n     for (long long j = 0; j < n - y[max]; ++j) { n       for (long long p = 0; p < x[med]; ++p) { n         cout << (char)(65 + med); n       } n       for (long long p = 0; p < x[min]; ++p) { n         cout << (char)(65 + min); n       } n       cout <<   n ; n     } n     return 0; n   } n   if (n - y[max] == y[med] && n - y[max] == x[min] && n == x[med] + y[min]) { n     cout << n <<   n ; n     for (long long j = 0; j < y[max]; ++j) { n       for (long long p = 0; p < n; ++p) { n         cout << (char)(65 + max); n       } n       cout <<   n ; n     } n     for (long long j = 0; j < n - y[max]; ++j) { n       for (long long p = 0; p < x[med]; ++p) { n         cout << (char)(65 + med); n       } n       for (long long p = 0; p < y[min]; ++p) { n         cout << (char)(65 + min); n       } n       cout <<   n ; n     } n     return 0; n   } n   if (n - y[max] == x[med] && n - y[max] == y[min] && n == y[med] + x[min]) { n     cout << n <<   n ; n     for (long long j = 0; j < y[max]; ++j) { n       for (long long p = 0; p < n; ++p) { n         cout << (char)(65 + max); n       } n       cout <<   n ; n     } n     for (long long j = 0; j < n - y[max]; ++j) { n       for (long long p = 0; p < y[med]; ++p) { n         cout << (char)(65 + med); n       } n       for (long long p = 0; p < x[min]; ++p) { n         cout << (char)(65 + min); n       } n       cout <<   n ; n     } n     return 0; n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[200005]; n int main() { n   memset(x, 0, sizeof(x)); n   int n = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &x[i]); n   } n   sort(x, x + n); n   double eps = 1e-10, l = 0.0, r = (double)(x[n - 1] - x[0]) / 6.0; n   int p1 = 0, p2 = 0; n   while (r - l > eps) { n     double mid = l + (r - l) / 2.0; n     p1 = upper_bound(x, x + n, x[0] + 2 * mid) - x; n     if (p1 == n) { n       r = mid; n       continue; n     } n     p2 = upper_bound(x, x + n, x[p1] + 2 * mid) - x; n     if (p2 == n || (double)(x[n - 1] - x[p2]) <= 2 * mid - eps) { n       r = mid; n     } else { n       l = mid; n     } n   } n   double tmp = x[0] + 2 * l; n   int idx = lower_bound(x, x + n, (int)(tmp - 1)) - x; n   for (; idx < n; ++idx) { n     if (x[idx] > x[0] + (int)(2 * l + 0.1)) { n       break; n     } n   } n   printf( %.6lf n , l); n   printf( %.6lf %.6lf %.6lf , (double)x[0] + l, (double)x[idx] + l, n          (double)x[n - 1] - l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[400006]; n vector<int> dis; n int main() { n   int i, j, m, n, c, x, l, y, t, bytes; n   cin >> n >> bytes; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   int total = 1; n   for (i = 1; i < n; i++) { n     while (i < n && a[i] == a[i - 1]) { n       total++; n       i++; n     } n     dis.push_back(total); n     total = 1; n   } n   if (a[n - 1] != a[n - 2]) dis.push_back(1); n   bytes *= 8; n   int bits = bytes / n; n   if (bits > 31) { n     cout << 0; n     return 0; n   } n   total = pow(2, bits); n   if (total >= dis.size()) { n     cout << 0; n     return 0; n   } n   int ans = 0; n   int temp = 0; n   for (i = 1; i < dis.size(); i++) { n     dis[i] += dis[i - 1]; n   } n   ans = dis[total - 1]; n   for (i = total; i < dis.size(); i++) { n     ans = max(ans, dis[i] - dis[i - total]); n   } n   cout << n - ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 5; n int n, f[N]; n struct node { n   int t, d, p, id; n   bool operator<(const node& other) const { return d < other.d; } n } a[N]; n vector<int> g[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     scanf( %d %d %d , &a[i].t, &a[i].d, &a[i].p), a[i].id = i; n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; i++) { n     for (int j = a[i].d - 1; j >= a[i].t; j--) { n       if (f[j - a[i].t] + a[i].p > f[j]) { n         f[j] = f[j - a[i].t] + a[i].p; n         g[j].clear(); n         g[j] = g[j - a[i].t]; n         g[j].push_back(a[i].id); n       } n     } n   } n   int ans = 0; n   for (int i = 1; i <= 2000; i++) n     if (f[i] > f[ans]) ans = i; n   printf( %d n , f[ans]); n   int len = g[ans].size(); n   printf( %d n , len); n   for (int i = 0; i < len; i++) printf( %d  , g[ans][i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 0x3f3f3f3f; n const int N = 3e5 + 10; n const long long mod = 1e9 + 7; n const double PI = acos(-1); n inline long long mm(long long x, long long p) { n   return x >= p ? x -= p : ((x < 0) ? x += p : x); n } n inline long long ab(long long x) { return x < 0 ? -x : x; } n char s[N]; n int main() { n   int n, x, y; n   long long cnt = 0; n   scanf( %d%d%d , &n, &x, &y); n   scanf( %s , s); n   bool flag = true; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  1 ) n       flag = true; n     else { n       if (flag) cnt++; n       flag = false; n     } n   } n   if (cnt) n     printf( %lld n , min(cnt * y, (cnt - 1) * x + y)); n   else n     puts( 0 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long limit = 1e9 + 7; n bool cmp(long long a, long long b) { return a > b; } n long long myceil(long long a, long long b) { return a / b + bool(a % b); } n long long mylog(long long num, long long base) { n   long long ans = 0; n   while (num) { n     ans++; n     num /= base; n   } n   return ans - 1; n } n long long Pow(long long a, long long b) { n   long long ans = 1, mul = a; n   while (b) { n     if (b & 1) ans = (ans * mul) % limit; n     mul = (mul * mul) % limit; n     b /= 2; n   } n   return ans; n } n template <typename T> n ostream &operator<<(ostream &os, vector<T> &v) { n   int n = v.size(); n   for (int i = 0; i < n; i++) os << v[i] <<    ; n   os << endl; n   return os; n } n template <typename T> n istream &operator>>(istream &is, vector<T> &v) { n   int n = v.size(); n   for (int i = 0; i < n; i++) is >> v[i]; n   return is; n } n string lps(string s) { n   int n = s.length(); n   int lps[n]; n   lps[0] = 0; n   int len = 0; n   int i = 1; n   while (i < n) { n     if (s[i] == s[len]) { n       len++; n       lps[i] = len; n       i++; n     } else { n       if (len != 0) { n         len = lps[len - 1]; n       } else { n         lps[i] = 0; n         i++; n       } n     } n   } n   int res = lps[n - 1]; n   return s.substr(0, res); n } n vector<int> primes; n long long fact[100005], inv[100005]; n ; n void pre() { n   fact[0] = 1; n   inv[0] = 1; n   for (int i = 1; i < 100005; i++) fact[i] = (fact[i - 1] * i) % limit; n   for (int i = 1; i < 100005; i++) n     inv[i] = (inv[i - 1] * Pow(i, limit - 2)) % limit; n } n long long comb(long long n, long long r) { n   long long ans = fact[n]; n   ans *= inv[n - r]; n   ans %= limit; n   ans *= inv[r]; n   ans %= limit; n   return ans; n } n int main() { n   int t = 1; n   cin >> t; n   for (int test = 1; test < t + 1; test++) { n     long long n, k; n     cin >> n >> k; n     if ((n - (k - 1) * (k - 1)) >= (2 * k - 1) && (n - (k - 1) * (k - 1)) % 2) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int n; n int seat[100]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> seat[i]; n   int sum = 0; n   for (int i = 0; i < n; i++) sum += seat[i]; n   vector<int> ans; n   ans.push_back(0); n   int curSum = seat[0]; n   for (int i = 1; i < n; i++) { n     if (seat[i] * 2 <= seat[0]) { n       ans.push_back(i); n       curSum += seat[i]; n     } n   } n   if (curSum > sum / 2) { n     cout << ans.size() <<   n ; n     for (int i = 0; i < ans.size(); i++) cout << ans[i] + 1 <<    ; n   } else n     cout << 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, a[200010], m = INT_MAX; n   string s; n   cin >> n >> s; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long int j = -1; n   for (long long int i = 0; i < s.length(); i++) { n     if (s[i] ==  R ) n       j = i; n     else { n       if (j != -1) { n         long long int temp = abs(a[i] - a[j]) / 2; n         if (temp < m) m = temp; n       } n     } n   } n   if (m == INT_MAX) n     cout << -1; n   else n     cout << m; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n void solve() { n   int n, p, k; n   cin >> n >> p >> k; n   auto f = [&](int x) -> int { n     return ((ll)x * x % p * x % p * x % p - (ll)k * x % p + p) % p; n   }; n   map<int, int> cnt; n   for (int _ = 0; _ < n; _++) { n     int x; n     cin >> x; n     cnt[f(x)]++; n   } n   ll res = 0; n   for (auto& kp : cnt) { n     int i = kp.second; n     res += (ll)i * (i - 1) / 2; n   } n   cout << res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1 << 16; n map<pair<int, int>, int> dp[maxn]; n int n, c[20], r[20], b[20], ed, rrr, bbb, ans = 0x3f3f3f3f; n int main() { n   cin >> n; n   getchar(); n   for (int i = 1; i <= n; i++) { n     char ch; n     cin >> ch >> r[i] >> b[i]; n     if (ch ==  R ) c[i] = 1; n   } n   ed = (1 << n) - 1; n   dp[0][pair<int, int>(0, 0)] = 0; n   for (int i = 0; i <= ed; i++) { n     int rr = 0, bb = 0; n     for (int t = 0; t < n; t++) n       if (i & (1 << t)) ++(c[t + 1] ? rr : bb); n     for (int j = 0; j < n; j++) { n       if (i & (1 << j)) continue; n       for (auto it : dp[i]) { n         int next = i | (1 << j), need = 0; n         if (it.first.first < max(r[j + 1] - rr, 0)) n           need = max(need, r[j + 1] - rr - it.first.first); n         if (it.first.second < max(b[j + 1] - bb, 0)) n           need = max(need, b[j + 1] - bb - it.first.second); n         int rmr = it.first.first + need - max(r[j + 1] - rr, 0), n             rmb = it.first.second + need - max(b[j + 1] - bb, 0); n         if (!dp[next].count(pair<int, int>(rmr, rmb))) n           dp[next][pair<int, int>(rmr, rmb)] = 0x3f3f3f3f; n         dp[next][pair<int, int>(rmr, rmb)] = n             min(dp[next][pair<int, int>(rmr, rmb)], it.second + need); n       } n     } n   } n   for (auto it : dp[ed]) ans = min(ans, it.second); n   cout << ans + n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char p[120], A[10100][120], B[10100][120], res[120]; n int SA[110], SB[110], Z[110], Pre[110]; n int SS[110], Suf[110]; n void Do(int n) { n   int i, j; n   printf( ? %d %d n , 1, n); n   fflush(stdout); n   for (i = 0; i < n * (n + 1) / 2; i++) { n     scanf( %s , A[i]); n     int c = 0; n     for (j = 0; A[i][j]; j++) c += A[i][j] -  a ; n     int L = j; n     SA[L] += c; n   } n   printf( ? %d %d n , 1, n - 1); n   fflush(stdout); n   for (i = 0; i < n * (n - 1) / 2; i++) { n     scanf( %s , B[i]); n     int c = 0; n     for (j = 0; B[i][j]; j++) c += B[i][j] -  a ; n     int L = j; n     SB[L] += c; n   } n   for (i = 1; i <= n; i++) { n     Z[i] = SA[i] - SB[i]; n     res[n - i] = Z[i] - Z[i - 1] +  a ; n   } n } n int main() { n   int i, j, k; n   int n; n   scanf( %d , &n); n   if (n == 1) { n     printf( ? %d %d n , 1, n); n     fflush(stdout); n     scanf( %s , p); n     printf( ! %s n , p); n     fflush(stdout); n     return 0; n   } n   if (n == 2) { n     printf( ? %d %d n , 1, 1); n     fflush(stdout); n     scanf( %s , p); n     res[0] = p[0]; n     printf( ? %d %d n , 2, 2); n     fflush(stdout); n     scanf( %s , p); n     res[1] = p[0]; n     res[2] = 0; n     printf( ! %s n , res); n     return 0; n   } n   Do((n + 1) / 2); n   printf( ? %d %d n , 1, n); n   fflush(stdout); n   for (i = 0; i < (n + 1) / 2; i++) { n     Pre[i + 1] = Pre[i] + res[i] -  a ; n   } n   for (i = 0; i < n * (n + 1) / 2; i++) { n     scanf( %s , A[i]); n     int c = 0; n     for (j = 0; A[i][j]; j++) c += A[i][j] -  a ; n     int L = j; n     SS[L] += c; n   } n   int tot = SS[n]; n   for (i = 1; i <= n / 2; i++) { n     int z = SS[n - i]; n     for (j = 1; j < i; j++) { n       int s = 0; n       for (k = 0; k < i - j; k++) { n         s += res[n - k - 1] -  a ; n       } n       z -= tot - (s + Pre[j]); n     } n     z -= (tot - Pre[i]); n     Suf[i] = tot - z; n     res[n - i] = Suf[i] - Suf[i - 1] +  a ; n   } n   printf( ! %s n , res); n   fflush(stdout); n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* na, Arg1&& arg1, Args&&... args) { n   const char* c = strchr(na + 1,  , ); n   cerr.write(na, c - na) <<   :   << arg1 <<   ,  ; n   __f(c + 1, args...); n } n const double pi = acos(-1.0); n const double eps = 1e-8; n const int inf = 1e9; n const long long INF = 1e18; n const int mod = 1e9 + 7; n const int N = 3e5 + 5; n void solve() { n   int len, q; n   cin >> len >> q; n   string s; n   cin >> s; n   s =  #  + s; n   int ans = 0; n   for (int i = 1; i <= len;) { n     if (s[i] ==  . ) { n       int j = i; n       while (j <= len && s[j] ==  . ) j++; n       ans += (j - i - 1); n       i = j; n     } else { n       i++; n     } n   } n   while (q--) { n     int pos; n     string ch; n     cin >> pos >> ch; n     if (s[pos] !=  .  && ch[0] ==  . ) { n       if (pos - 1 >= 1 && s[pos - 1] ==  . ) ans++; n       if (pos + 1 <= len && s[pos + 1] ==  . ) ans++; n       s[pos] =  . ; n     } else if (s[pos] ==  .  && ch[0] !=  . ) { n       if (pos - 1 >= 1 && s[pos - 1] ==  . ) ans--; n       if (pos + 1 <= len && s[pos + 1] ==  . ) ans--; n       s[pos] = ch[0]; n     } n     cout << ans <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   for (int tc = 1; tc <= t; tc++) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double Pi = acos(-1.0); n inline int cmp(double x, double y = 0, double tol = 1e-8) { n   return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; n } n struct point { n   double x, y; n   point(double x = 0, double y = 0) : x(x), y(y) {} n   point operator+(point q) { return point(x + q.x, y + q.y); } n   point operator-(point q) { return point(x - q.x, y - q.y); } n   point operator*(double t) { return point(x * t, y * t); } n   point operator/(double t) { return point(x / t, y / t); } n   double operator*(point q) { return x * q.x + y * q.y; } n   double operator%(point q) { return x * q.y - y * q.x; } n   double mod() { return sqrt(x * x + y * y); } n   point rotate(double t) { n     return point(x * cos(t) - y * sin(t), x * sin(t) + y * cos(t)); n   } n   int cmp(point q) const { n     if (int t = ::cmp(x, q.x)) return t; n     return ::cmp(y, q.y); n   } n   bool operator==(point q) const { return cmp(q) == 0; } n   bool operator!=(point q) const { return cmp(q) != 0; } n   bool operator<(point q) const { return cmp(q) < 0; } n   static point pivot; n }; n point point::pivot; n double poly_area(vector<point>& T) { n   double s = 0; n   int n = T.size(); n   for (int i = 0; i < n; i++) s += T[i] % T[(i + 1) % n]; n   return s / 2; n } n point line_intersect(point p, point q, point r, point s) { n   point a = q - p, b = s - r, c = point(p % q, r % s); n   return point(point(a.x, b.x) % c, point(a.y, b.y) % c) / (a % b); n } n vector<point> halfplane(vector<point>& p, pair<point, point> semiplano) { n   vector<point> q; n   point p1 = semiplano.first, p2 = semiplano.second; n   int n = p.size(); n   for (int i = 0; i < n; i++) { n     double c = (p2 - p1) % (p[i] - p1); n     double d = (p2 - p1) % (p[(i + 1) % n] - p1); n     if (cmp(c) >= 0) q.push_back(p[i]); n     if (cmp(c * d) < 0) n       q.push_back(line_intersect(p1, p2, p[i], p[(i + 1) % n])); n   } n   return q; n } n vector<point> semi_plano_cruzado(vector<pair<point, point> > semiplano) { n   vector<point> p; n   p.push_back(point(-1e9, -1e9)); n   p.push_back(point(-1e9, +1e9)); n   p.push_back(point(+1e9, +1e9)); n   p.push_back(point(+1e9, -1e9)); n   int n = semiplano.size(); n   for (int i = 0; i < n; i++) { n     p = halfplane(p, semiplano[i]); n     if (p.size() == 0) break; n   } n   return p; n } n int main() { n   double w, h, a; n   cin >> w >> h >> a; n   double gama = atan(w / h); n   double beta = atan(h / w); n   vector<point> p, q; n   p.push_back(point(-0.5 * w, -0.5 * h)); n   p.push_back(point(-0.5 * w, 0.5 * h)); n   p.push_back(point(0.5 * w, 0.5 * h)); n   p.push_back(point(0.5 * w, -0.5 * h)); n   a = a * Pi / 180; n   double teta = 0.5 * Pi + a - gama - beta; n   q.push_back(point(-0.5 * w, -0.5 * h)); n   q.push_back(point(-0.5 * w, 0.5 * h)); n   q.push_back(point(0.5 * w, 0.5 * h)); n   q.push_back(point(0.5 * w, -0.5 * h)); n   for (int i = 0; i < 4; i++) q[i] = q[i].rotate(teta); n   for (int i = 0; i < 4; i++) { n     p = halfplane(p, make_pair(q[(i + 1) % 4], q[i])); n   } n   printf( %.9lf n , fabs(poly_area(p))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s1; n long long arr2[2000006]; n long long dsu(long long x) { return arr2[x] == x ? x : arr2[x] = dsu(arr2[x]); } n int main() { n   char ans[2000006]; n   long long n, t1, t2, t3, t4, t5; n   cin >> n; n   for (int i = 0; i < 2000006; i++) { n     ans[i] =  a ; n     arr2[i] = i; n   } n   t3 = 0; n   while (n--) { n     cin >> s1; n     t1 = s1.length(); n     cin >> t2; n     while (t2--) { n       cin >> t4; n       t4--; n       if (t3 < t4 + t1 - 1) t3 = t4 + t1 - 1; n       for (int i = dsu(t4); i < t4 + t1;) { n         ans[i] = s1[i - t4]; n         arr2[i] = i + 1; n         i = dsu(i); n       } n     } n   } n   t5 = 0; n   while (1) { n     cout << ans[t5++]; n     if (t5 > t3) break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n inline long long int rl() { n   long long int res = 0, neg = 0; n   char c = getchar(); n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   if (c ==  - ) neg = 1, c = getchar(); n   while (c >=  0  && c <=  9 ) { n     res = (res << 1) + (res << 3) + c - 48; n     c = getchar(); n   } n   return neg ? -res : res; n } n inline void read(string &s) { n   s =   ; n   char c = getchar(); n   while (c !=   n ) { n     s += c; n     c = getchar(); n   } n } n int main() { n   vector<std::vector<char> > a(9); n   for (int i = 0; i < 9; ++i) { n     for (int j = 0; j < 9; ++j) { n       char c; n       cin >> c; n       a[i].push_back(c); n     } n   } n   long long int x = rl() - 1, y = rl() - 1; n   int i; n   for (i = 3 * (x % 3); i < 3 * (x % 3 + 1); ++i) n     for (int j = 3 * (y % 3); j < 3 * (y % 3 + 1); ++j) { n       if (a[i][j] ==  . ) goto exit; n     } n exit:; n   if (i == 3 * (x % 3 + 1)) { n     for (int i = 0; i < 9; ++i) { n       for (int j = 0; j < 9; ++j) { n         if (a[i][j] ==  . ) n           cout <<  ! ; n         else n           cout << a[i][j]; n         if (j % 3 == 2 && j != 8) cout <<    ; n       } n       cout <<   n ; n       if (i % 3 == 2 && i != 8) cout <<   n ; n     } n   } else { n     for (i = 3 * (x % 3); i < 3 * (x % 3 + 1); ++i) n       for (int j = 3 * (y % 3); j < 3 * (y % 3 + 1); ++j) { n         if (a[i][j] ==  . ) a[i][j] =  ! ; n       } n     for (int i = 0; i < 9; ++i) { n       for (int j = 0; j < 9; ++j) { n         cout << a[i][j]; n         if (j % 3 == 2 && j != 8) cout <<    ; n       } n       cout <<   n ; n       if (i % 3 == 2 && i != 8) cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q, p[200005], a[200005], pi[200005], mra[200005], t; n int mar[200005], su[200005], pr[200005], mm[200005], l, lr, r, Ilmm[200005]; n vector<int> v[200005]; n bool pa; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m >> q; n   for (int i = 1; i <= n; ++i) { n     cin >> p[i]; n     pi[p[i]] = i; n   } n   for (int i = 1; i <= m; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i <= m; ++i) { n     mar[a[i]] = i; n     if (pi[a[i]] == 1) { n       pr[i] = i; n     } else if (mar[p[pi[a[i]] - 1]]) { n       pr[i] = pr[mar[p[pi[a[i]] - 1]]]; n     } n   } n   for (int i = 1; i <= n; ++i) mar[i] = 0; n   for (int i = m; i > 0; --i) { n     v[a[i]].push_back(i); n     mar[a[i]] = i; n     if (pi[a[i]] == n) { n       su[i] = i; n     } else if (mar[p[pi[a[i]] + 1]]) { n       su[i] = su[mar[p[pi[a[i]] + 1]]]; n     } n     if (!su[i]) continue; n     if (pi[a[i]] > 1) { n       t = p[pi[a[i]] - 1]; n       l = 0; n       r = v[t].size(); n       --r; n       int tp = 0; n       while (l <= r) { n         lr = (l + r) / 2; n         if (pr[v[t][lr]] > su[i]) { n           tp = v[t][lr]; n           l = lr + 1; n         } else { n           r = lr - 1; n         } n       } n       mm[i] = tp; n     } else { n       mm[i] = su[i]; n     } n   } n   for (int i = m; i > 0; --i) { n     Ilmm[i] = Ilmm[i + 1]; n     if ((mm[i] && mm[i] < Ilmm[i]) || !Ilmm[i]) { n       Ilmm[i] = mm[i]; n     } n   } n   for (int i = 1; i <= q; ++i) { n     cin >> l >> r; n     pa = 0; n     if (Ilmm[l] && Ilmm[l] <= r) { n       pa = 1; n     } n     cout << pa; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const int MOD = 1e9 + 7; n vector<string> vec_splitter(string s) { n   s +=  , ; n   vector<string> res; n   while (!s.empty()) { n     res.push_back(s.substr(0, s.find( , ))); n     s = s.substr(s.find( , ) + 1); n   } n   return res; n } n void debug_out(vector<string> __attribute__((unused)) args, n                __attribute__((unused)) int idx, n                __attribute__((unused)) int LINE_NUM) { n   cerr <<   n ; n } n template <typename Head, typename... Tail> n void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { n   if (idx > 0) n     cerr <<  ,  ; n   else n     cerr <<  Line(  << LINE_NUM <<  )  ; n   stringstream ss; n   ss << H; n   cerr << args[idx] <<   =   << ss.str(); n   debug_out(args, idx + 1, LINE_NUM, T...); n } n void run() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n long long gcd(long long n1, long long n2) { n   if (n2 != 0) n     return gcd(n2, n1 % n2); n   else n     return n1; n } n bool checkPalindrome(string s) { n   int n = s.size(); n   for (int i = 0; i < n / 2; i++) { n     if (s[i] != s[n - i - 1]) return false; n   } n   return true; n } n long long digSum(long long n) { n   long long sum = 0; n   while (n > 0) { n     sum += n % 10; n     n /= 10; n   } n   return sum; n } n bool compare(pair<int, int> a, pair<int, int> b) { n   if (a.first == a.second) n     return true; n   else if (b.first == b.second) n     return false; n   else { n     int adiff = a.second - a.first; n     int bdiff = b.second - b.first; n     if (adiff > bdiff) n       return false; n     else n       return true; n   } n } n int main() { n   run(); n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     vector<int> v(n + 1); n     for (int i = 1; i <= n; i++) { n       cin >> v[i]; n     } n     sort(v.begin(), v.end()); n     long long ans = 0; n     int ind = n - 2 * k; n     int half = ind + k; n     for (int i = 1; i <= ind; i++) { n       ans += v[i]; n     } n     for (int i = ind + 1; i <= half; i++) { n       ans = ans + (v[i] / v[i + k]); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n const long long pri = 1e9 + 7; n long long myPow(long long a, long long b) { n   long long ret = 1; n   while (b != 0) { n     if (b % 2 != 0) { n       ret = (ret * a) % pri; n     } n     a = (a * a) % pri; n     b /= 2; n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   long long precision = numeric_limits<double>::digits10; n   cin >> n >> m >> k; n   if (k == -1 && (n + m) % 2 != 0) n     cout << 0 <<   n ; n   else { n     long long ans = myPow(2, m - 1); n     ans = myPow(ans, n - 1); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n int main() { n   int x, y; n   scanf( %d%d , &x, &y); n   int r2 = x * x + y * y; n   int r = floor(sqrt(static_cast<double>(r2))); n   if (r * r < r2 && (r + 1) * (r + 1) > r2 && n       (r % 2 == 1 && x * y > 0 || r % 2 == 0 && x * y < 0)) { n     printf( white n ); n   } else { n     printf( black n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 250005; n vector<int> g[maxn]; n int pa[maxn]; n int v[maxn]; n int dep[maxn]; n vector<int> leaf; n int n, m, k; n void build(int now) { n   int x = 0, y = 0; n   for (auto &i : g[now]) { n     if (i == pa[now]) { n       continue; n     } else if (x == 0) { n       x = i; n     } else { n       y = i; n     } n   } n   vector<int> ans; n   if ((dep[now] - dep[x] + 1) % 3) { n     ans.push_back(now); n     while (ans.back() != x) { n       ans.push_back(pa[ans.back()]); n     } n   } else if ((dep[now] - dep[y] + 1) % 3) { n     ans.push_back(now); n     while (ans.back() != y) { n       ans.push_back(pa[ans.back()]); n     } n   } else { n     if (dep[x] < dep[y]) { n       swap(x, y); n     } n     ans.push_back(now); n     ans.push_back(x); n     while (ans.back() != y) { n       ans.push_back(pa[ans.back()]); n     } n   } n   cout << ans.size() <<   n ; n   for (auto &i : ans) { n     cout << i <<    ; n   } n   cout <<   n ; n } n void dfs(int now) { n   v[now] = 1; n   int f = 0; n   if (dep[now] * k >= n) { n     cout <<  PATH n ; n     vector<int> ans{now}; n     while (ans.back() != 1) { n       ans.push_back(pa[ans.back()]); n     } n     cout << ans.size() <<   n ; n     for (auto &i : ans) { n       cout << i <<    ; n     } n     cout <<   n ; n     exit(0); n   } n   for (auto &i : g[now]) { n     if (v[i]) { n       continue; n     } n     dep[i] = dep[now] + 1; n     pa[i] = now; n     dfs(i); n     f = 1; n   } n   if (!f) { n     leaf.push_back(now); n   } n } n void go() { n   cin >> n >> m >> k; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dep[1] = 1; n   dfs(1); n   cout <<  CYCLES n ; n   for (int i = 0; i < k; i++) { n     build(leaf[i]); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int c = 0; n   int t; n   if (!c) { n     t = 1; n   } else { n     cin >> t; n   } n   while (t--) { n     go(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int KMAX = 8; n const int NMAX = 2505; n char mat[NMAX][NMAX]; n int psm[NMAX][NMAX], pt1[KMAX], pt2[KMAX]; n inline int sum(int x1, int y1, int x2, int y2) { n   return psm[x2][y2] - psm[x2][y1 - 1] - psm[x1 - 1][y2] + psm[x1 - 1][y1 - 1]; n } n long long solve(int x1, int y1, int x2, int y2, int k) { n   if (x1 == x2 and y1 == y2) return sum(x1, y1, x2, y2) == k; n   long long ans; n   if (x2 - x1 <= y2 - y1) { n     int ym = (y1 + y2) / 2; n     ans = solve(x1, y1, x2, ym, k) + solve(x1, ym + 1, x2, y2, k); n     for (int i = x1; i <= x2; ++i) { n       for (int k1 = 0; k1 <= k; ++k1) pt1[k1] = y1, pt2[k1] = y2; n       for (int j = i; j <= x2; ++j) { n         for (int k1 = 0; k1 <= k; ++k1) { n           while (pt1[k1] <= ym and sum(i, pt1[k1], j, ym) > k1) ++pt1[k1]; n           while (pt2[k1] > ym and sum(i, ym + 1, j, pt2[k1]) > k1) --pt2[k1]; n         } n         for (int k1 = 0; k1 <= k; ++k1) n           ans += 1LL * (((k1 > 0) ? pt1[k1 - 1] : (ym + 1)) - pt1[k1]) * n                  (pt2[k - k1] - ((k - k1 > 0) ? pt2[k - k1 - 1] : ym)); n       } n     } n   } else { n     int xm = (x1 + x2) / 2; n     ans = solve(x1, y1, xm, y2, k) + solve(xm + 1, y1, x2, y2, k); n     for (int i = y1; i <= y2; ++i) { n       for (int k1 = 0; k1 <= k; ++k1) pt1[k1] = x1, pt2[k1] = x2; n       for (int j = i; j <= y2; ++j) { n         for (int k1 = 0; k1 <= k; ++k1) { n           while (pt1[k1] <= xm and sum(pt1[k1], i, xm, j) > k1) ++pt1[k1]; n           while (pt2[k1] > xm and sum(xm + 1, i, pt2[k1], j) > k1) --pt2[k1]; n         } n         for (int k1 = 0; k1 <= k; ++k1) n           ans += 1LL * (((k1 > 0) ? pt1[k1 - 1] : (xm + 1)) - pt1[k1]) * n                  (pt2[k - k1] - ((k - k1 > 0) ? pt2[k - k1 - 1] : xm)); n       } n     } n   } n   return ans; n } n int main(void) { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %s , mat[i] + 1); n     for (int j = 1; j <= m; ++j) n       psm[i][j] = n           psm[i - 1][j] + psm[i][j - 1] - psm[i - 1][j - 1] + (mat[i][j] -  0 ); n   } n   printf( %I64d n , solve(1, 1, n, m, k)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[2001][2001]; n int f(int i, int j) { n   if (j < 0 || i < 0 || j > i) return 0; n   if (dp[i][j] != -1) return dp[i][j]; n   if (j == i) n     dp[i][j] = n         (((long long)i - 1) * (f(i - 1, i - 1) + (long long)f(i - 2, i - 2))) % n         1000000007; n   else n     dp[i][j] = n         (f(i - 1, j - 1) * ((long long)j) + ((long long)i - j) * f(i - 1, j)) % n         1000000007; n   return dp[i][j]; n } n int used[2001] = {}; n int a[2001]; n int main() { n   int x, n, bound = 0, total = 0, i, j; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     if (a[i] == -1) n       total++; n     else n       used[a[i]] = 1; n   } n   for (i = 0; i < n; i++) { n     if (used[i + 1] == 0 && a[i] == -1) bound++; n   } n   memset(dp, -1, sizeof(dp)); n   dp[0][0] = 1; n   dp[1][0] = 1; n   dp[1][1] = 0; n   printf( %d n , f(total, bound)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 205; n const int INF = 0x3f3f3f3f; n string a, b; n int dp[MAX][MAX][405]; n int call(int x, int y, int s) { n   if (x >= a.size() and y >= b.size()) return s; n   if (dp[x][y][s] != -1) return dp[x][y][s]; n   int ret = INF; n   if (x < a.size() and y < b.size() and a[x] == b[y]) { n     if (a[x] ==  ( ) { n       ret = min(ret, 1 + call(x + 1, y + 1, s + 1)); n     } else { n       if (s) { n         ret = min(ret, 1 + call(x + 1, y + 1, s - 1)); n       } else { n         ret = min(ret, 2 + call(x + 1, y + 1, s)); n       } n     } n   } else { n     if (x < a.size()) { n       if (a[x] ==  ( ) { n         ret = min(ret, 1 + call(x + 1, y, s + 1)); n       } else { n         if (s) { n           ret = min(ret, 1 + call(x + 1, y, s - 1)); n         } else { n           ret = min(ret, 2 + call(x + 1, y, s)); n         } n       } n     } n     if (y < b.size()) { n       if (b[y] ==  ( ) { n         ret = min(ret, 1 + call(x, y + 1, s + 1)); n       } else { n         if (s) { n           ret = min(ret, 1 + call(x, y + 1, s - 1)); n         } else { n           ret = min(ret, 2 + call(x, y + 1, s)); n         } n       } n     } n   } n   return dp[x][y][s] = ret; n } n void go(int x, int y, int s) { n   if (x >= a.size() and y >= b.size()) { n     for (int i = 0; i < s; i++) cout <<  ) ; n     cout << endl; n     return; n   } n   int ret = call(x, y, s); n   if (x < a.size() and y < b.size() and a[x] == b[y]) { n     if (a[x] ==  ( ) { n       if (ret == 1 + call(x + 1, y + 1, s + 1)) { n         cout <<  ( ; n         go(x + 1, y + 1, s + 1); n         return; n       } n     } else { n       if (s) { n         if (ret == 1 + call(x + 1, y + 1, s - 1)) { n           cout <<  ) ; n           go(x + 1, y + 1, s - 1); n           return; n         } n       } else { n         if (ret == 2 + call(x + 1, y + 1, s)) { n           cout <<  () ; n           go(x + 1, y + 1, s); n           return; n         } n       } n     } n   } else { n     if (x < a.size()) { n       if (a[x] ==  ( ) { n         if (ret == 1 + call(x + 1, y, s + 1)) { n           cout <<  ( ; n           go(x + 1, y, s + 1); n           return; n         } n       } else { n         if (s) { n           if (ret == 1 + call(x + 1, y, s - 1)) { n             cout <<  ) ; n             go(x + 1, y, s - 1); n             return; n           } n         } else { n           if (ret == 2 + call(x + 1, y, s)) { n             cout <<  () ; n             go(x + 1, y, s); n             return; n           } n         } n       } n     } n     if (y < b.size()) { n       if (b[y] ==  ( ) { n         if (ret == 1 + call(x, y + 1, s + 1)) { n           cout <<  ( ; n           go(x, y + 1, s + 1); n           return; n         } n       } else { n         if (s) { n           if (ret == 1 + call(x, y + 1, s - 1)) { n             cout <<  ) ; n             go(x, y + 1, s - 1); n             return; n           } n         } else { n           if (ret == 2 + call(x, y + 1, s)) { n             cout <<  () ; n             go(x, y + 1, s); n             return; n           } n         } n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> a >> b; n   memset(dp, -1, sizeof(dp)); n   go(0, 0, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e8; n long long inf = 1e15; n long long n, m, k, ans, x; n long long t[2003], c[2003]; n long long dp[2003][4003]; n long long f(long long x, long long y) { n   long long &ret = dp[x][y]; n   if (ret != -1) return ret; n   if (x == n + 1 && y < 2000) return inf; n   if (x == n + 1) return 0; n   long long yy = y + t[x]; n   yy = min(yy, 4000LL); n   ret = c[x] + f(x + 1, yy); n   ret = min(ret, f(x + 1, y - 1)); n   return ret; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   memset(dp, -1, sizeof dp); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> t[i] >> c[i]; n   } n   cout << f(1, 2000); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class L> n bool smax(T &x, L y) { n   return x < y ? (x = y, 1) : 0; n } n template <class T, class L> n bool smin(T &x, L y) { n   return x > y ? (x = y, 1) : 0; n } n const int maxn = 15e3 + 17, maxk = 1e3 + 17, mod = 1e9 + 7, base = 1e7 + 19, n           lg = 17, maxq = 3e4 + 17; n vector<int> iman[maxq << 2]; n int n, k, nq, q, dp[lg][maxk], w[maxn], v[maxn], ans[maxq], po[maxk]; n void majid(int st, int en, int idx, int l = 0, int r = q, int id = 1) { n   if (st <= l && r <= en) { n     iman[id].push_back(idx); n     return; n   } n   if (en <= l || r <= st) return; n   int mid = l + r >> 1; n   majid(st, en, idx, l, mid, id << 1), majid(st, en, idx, mid, r, id << 1 | 1); n } n void hamid(int l = 0, int r = q, int id = 1, int h = 1) { n   memcpy(dp[h], dp[h - 1], sizeof dp[h]); n   for (auto idx : iman[id]) n     for (int wi = k - w[idx]; wi >= 0; wi--) n       smax(dp[h][wi + w[idx]], dp[h][wi] + v[idx]); n   partial_sum(dp[h], dp[h] + k + 1, dp[h], n               [](int &x, int &y) { return max(x, y); }); n   if (r - l < 2) { n     for (int i = 1; i <= k; i++) n       ans[l] = (ans[l] + (long long)po[i - 1] * dp[h][i]) % mod; n     return; n   } n   int mid = l + r >> 1; n   hamid(l, mid, id << 1, h + 1), hamid(mid, r, id << 1 | 1, h + 1); n } n int st[maxn], en[maxn]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> v[i] >> w[i]; n   po[0] = 1; n   for (int i = 1; i <= k; i++) po[i] = (long long)po[i - 1] * base % mod; n   memset(en, 63, sizeof en); n   cin >> nq; n   for (int t, x; nq--;) { n     cin >> t; n     if (t == 1) n       cin >> v[n] >> w[n], st[n++] = q; n     else if (t == 2) n       cin >> x, en[x - 1] = q; n     else n       q++; n   } n   for (int i = 0; i < n; i++) majid(st[i], min(q, en[i]), i); n   hamid(); n   for (int i = 0; i < q; i++) cout << ans[i] <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s; n   cin >> s; n   int n = s.size(); n   if (s[0] ==  b  && s[n - 1] ==  a ) { n     s[0] =  a ; n   } else if (s[0] ==  a  && s[n - 1] ==  b ) { n     s[n - 1] =  a ; n   } n   cout << s <<   n ; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<char> c(n); n   vector<int> id(n); n   map<int, bool> state, exist; n   for (int i = 0; i < n; i++) cin >> c[i] >> id[i]; n   for (int i = 0; i < n; i++) { n     if (c[i] ==  + ) n       exist[id[i]] = true; n     else { n       if (exist[id[i]]) n         exist[id[i]] = false; n       else n         state[id[i]] = true; n     } n   } n   int ans = state.size(); n   for (int i = 0; i < n; i++) { n     if (c[i] ==  + ) n       state[id[i]] = true; n     else { n       state.erase(id[i]); n     } n     ans = max(ans, int(state.size())); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n const int MAX = 3000; n const long long MOD = 1e9 + 7; n const int INF = 0x3f3f3f3f; n const long long LLINF = 0x3f3f3f3f3f3f3f3f; n const long double EPS = 1e-9; n const long double PI = acos(-1.0); n using namespace std; n bool eq(long double a, long double b) { return fabsl(a - b) <= EPS; } n struct point { n   long double x, y; n   int id; n   point(long double x = 0, long double y = 0) : x(x), y(y) {} n   point operator+(const point &o) const { return {x + o.x, y + o.y}; } n   point operator-(const point &o) const { return {x - o.x, y - o.y}; } n   point operator*(long double t) const { return {x * t, y * t}; } n   point operator/(long double t) const { return {x / t, y / t}; } n   long double operator*(const point &o) const { return x * o.x + y * o.y; } n   long double operator^(const point &o) const { return x * o.y - y * o.x; } n   bool operator<(const point &o) const { n     if (!eq(x, o.x)) return x < o.x; n     return y < o.y; n   } n   bool operator==(const point &o) const { return eq(x, o.x) and eq(y, o.y); } n }; n long double norm(point a) { return sqrt(a * a); } n long double proj(point a, point b) { return a * b / norm(b); } n long double max(long double a, long double b) { return (a > b ? a : b); } n long double min(long double a, long double b) { return (a < b ? a : b); } n long double angle(point a, point b) { n   long double ang = a * b / norm(a) / norm(b); n   return acos(max(min(ang, 1), -1)); n } n int ccw(point a, point b, point e) { n   long double tmp = (b - a) ^ (e - a); n   return (tmp > EPS) - (tmp < -EPS); n } n struct line { n   point p1, p2; n   line(point p1 = 0, point p2 = 0) : p1(p1), p2(p2) {} n   long double a = p1.y - p2.y; n   long double b = p2.x - p1.x; n   long double c = -(a * p1.x + b * p1.y); n   bool inside(point p) { n     return eq(norm(p1 - p) + norm(p2 - p) - norm(p2 - p1), 0); n   } n }; n point intersect(line l1, line l2) { n   long long det = l1.a * l2.b - l1.b * l2.a; n   if (det == 0) return point(INF, INF); n   long double x = (l1.b * l2.c - l1.c * l2.b) / det; n   long double y = (l1.c * l2.a - l1.a * l2.c) / det; n   return point(x, y); n } n bool check(point a[2], point b[2], point m[2]) { n   if (a[0] == b[1]) n     swap(b[0], b[1]); n   else if (a[1] == b[0]) n     swap(a[0], a[1]); n   else if (a[1] == b[1]) { n     swap(a[0], a[1]); n     swap(b[0], b[1]); n   } else if (!(a[0] == b[0])) n     return false; n   assert(a[0] == b[0]); n   long double ang = angle(a[1] - a[0], b[1] - b[0]); n   if (ang > PI / 2 + EPS or ang < EPS) return false; n   line A = line(a[0], a[1]); n   line B = line(b[0], b[1]); n   line M = line(m[0], m[1]); n   if (A.inside(m[0]) and B.inside(m[1])) { n     if (norm(a[1] - m[0]) > 4 * norm((m[0] - a[0])) or n         norm(a[0] - m[0]) > 4 * norm((m[0] - a[1]))) n       return false; n     if (norm(b[1] - m[1]) > 4 * norm((m[1] - b[0])) or n         norm(b[0] - m[1]) > 4 * norm((m[1] - b[1]))) n       return false; n     else n       return true; n   } else if (A.inside(m[1]) and B.inside(m[0])) { n     if (norm(a[1] - m[1]) > 4 * norm((m[1] - a[0])) or n         norm(a[0] - m[1]) > 4 * norm((m[1] - a[1]))) n       return false; n     if (norm(b[1] - m[0]) > 4 * norm((m[0] - b[0])) or n         norm(b[0] - m[0]) > 4 * norm((m[0] - b[1]))) n       return false; n     else n       return true; n   } else n     return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t, x, y; n   cin >> t; n   point vet[3][2]; n   while (t--) { n     for (int i = 0; i < 3; i++) n       for (int j = 0; j < 2; j++) { n         cin >> x >> y; n         vet[i][j] = point(x, y); n       } n     if (check(vet[0], vet[1], vet[2]) or check(vet[0], vet[2], vet[1]) or n         check(vet[1], vet[2], vet[0])) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, k, l, m, n, s, t, p, q, a, b, aa, bb, d; n char c[2222222]; n int main() { n   t = 1; n   while (gets(c) != 0) { n     for (i = 0; c[i] != 0; i++) n       if (c[i] !=    ) break; n     if (c[i] ==  # ) { n       if (t == 0) puts(  ); n       puts(c); n       t = 1; n     } else { n       for (i = 0; c[i] != 0; i++) n         if (c[i] !=    ) cout << c[i]; n       t = 0; n     } n   } n   if (t == 0) puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   long long inf = 1000000005; n   cin >> n >> m; n   long long i, j, k; n   bool a[n + 1][n + 1]; n   long long deg[n + 1]; n   for (i = 0; i < n + 1; i++) { n     deg[i] = 0; n     for (j = 0; j < n + 1; j++) a[i][j] = false; n   } n   long long res = inf; n   for (i = 0; i < m; i++) { n     cin >> j >> k; n     a[j][k] = true; n     a[k][j] = true; n     deg[j]++; n     deg[k]++; n   } n   for (i = 1; i < n; i++) { n     for (j = i + 1; j <= n; j++) { n       if (a[i][j]) { n         for (k = j + 1; k <= n; k++) { n           if (a[i][k] && a[j][k]) res = min(res, deg[i] + deg[j] + deg[k]); n         } n       } n     } n   } n   if (res == inf) n     cout << -1 << endl; n   else n     cout << res - 6 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int t; n   cin >> t; n   for (int i = 0; i < n; ++i) { n     int a, b; n     cin >> a >> b; n     int d[7] = {0}; n     d[a] = 1; n     d[7 - a] = 1, d[b] = 1, d[7 - b] = 1; n     d[t] = 1; n     int cn = 0; n     for (int j = 1; j < 7; ++j) { n       if (d[j] == 0) { n         t = 7 - j; n         cn++; n       } n     } n     if (cn == 2) return (cout <<  NO , 0); n   } n   cout <<  YES ; n   return 0; n } n