#include <bits/stdc++.h>   using namespace std;   int n, m;   int edge[1005];   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> edge[i];     }     int sum = 0;     int tmp1, tmp2;     for (int i = 0; i < m; i++) {       cin >> tmp1 >> tmp2;       sum += min(edge[tmp1], edge[tmp2]);     }     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k;     scanf( %lld%lld , &n, &k);     if (k > (long long)1e8) {       printf( -1 n );       return 0;     }     long long b = n / (k * (k + 1) / 2);     if (b == 0) {       printf( -1 n );       return 0;     }     long long r = 1;     for (long long x = 1; x * x <= n; x++) {       if (n % x != 0) continue;       if (x <= b && x > r) r = x;       if (n / x <= b && n / x > r) r = n / x;     }     for (int i = 1; i < k; i++) printf( %lld  , r * i);     n -= r * k * (k - 1) / 2;     printf( %lld n , n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100 * 1000 + 123;   int n, p[MAXN], s[MAXN], a[MAXN];   vector<int> c[MAXN];   void inp();   void dfs(int);   int main() {     inp();     a[0] = s[0];     dfs(0);     for (int i = 0; i < n; i++)       if (a[i] < 0) return cout << -1, 0;     cout << accumulate(a, a + n, 0ll);     return 0;   }   void inp() {     cin >> n;     p[0] = -1;     for (int i = 1; i < n; i++) cin >> p[i], c[--p[i]].push_back(i);     for (int i = 0; i < n; i++) cin >> s[i];   }   void dfs(int v) {     if (c[v].empty()) return;     if (~s[v])       for (int u : c[v]) dfs(u);     else {       int kam = INT_MAX;       for (int u : c[v]) kam = min(kam, s[u]);       a[v] = kam - s[p[v]];       s[v] = s[p[v]] + a[v];       for (int u : c[v]) {         a[u] = s[u] - s[v];         dfs(u);       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     double c = 0, l = 0, i, j;     string x;     cin >> x;     if (x.size() == 1) {       cout << x[0] << x[0];       return 0;     }     if (x.size() % 2 == 0)       c = x.size() / 2 + 1;     else       c = x.size() / 2;     string a, b, m;     for (i = x.size() / 2; i >= 0; i--) {       a.clear();       b.clear();       m.clear();       for (j = l + c; j < x.size(); j++) {         if (x[i] != x[j]) {           a = x;           m = x;           a.insert(x.size() - i, 1, x[i]);           m.insert(x.size() - j - 1, 1, x[j]);         }         break;       }       l++;       b = a;       reverse(b.begin(), b.end());       if (a == b and a.size() != 0 and b.size() != 0) {         cout << a;         return 0;       }       b.clear();       b = m;       reverse(b.begin(), b.end());       if (m == b and m.size() != 0 and b.size() != 0) {         cout << m;         return 0;       }     }     l = 0;     for (i = x.size() - 1 / 2; i >= 0; i--) {       a.clear();       b.clear();       m.clear();       for (j = l + c; j < x.size(); j++) {         if (x[i] != x[j]) {           a = x;           m = x;           a.insert(x.size() - i, 1, x[i]);           m.insert(x.size() - j - 1, 1, x[j]);         }         break;       }       l++;       b = a;       reverse(b.begin(), b.end());       if (a == b and a.size() != 0 and b.size() != 0) {         cout << a;         return 0;       }       b.clear();       b = m;       reverse(b.begin(), b.end());       if (m == b and m.size() != 0 and b.size() != 0) {         cout << m;         return 0;       }     }     a.clear();     b.clear();     if (x.size() % 2 == 0)       x += x[0];     else       x = x[x.size() - 1] + x;     a = x;     reverse(a.begin(), a.end());     if (a == x)       cout << a;     else       cout <<  NA ;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const double eps = 1e-10;   const int INF = 0x3f3f3f3f;   const long long INFLL = 0x3f3f3f3f3f3f3f3fll;   const int MAXN = 2e5 + 5;   const int mod = 1e9 + 7;   int a[MAXN], b[MAXN];   long long fastpow(long long a, int n) {     long long base = a, ret = 1;     while (n) {       if (n & 1) ret = ret * base % MOD;       base = base * base % MOD;       n >>= 1;     }     return ret;   }   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) scanf( %d , &b[i]);     long long prob = 1, ans = 0;     for (int i = 1; i <= n; i++) {       if (a[i] == 0) {         if (b[i] == 0)           ans = (ans + prob * (1 - fastpow(m, MOD - 2) + MOD) % MOD *                            fastpow(2, MOD - 2) % MOD) %                 MOD;         else           ans = (ans + prob * (m - b[i]) % MOD * fastpow(m, MOD - 2) % MOD) % MOD;         prob = prob * fastpow(m, MOD - 2) % MOD;       } else {         if (b[i] == 0) {           ans = (ans + prob * (a[i] - 1) % MOD * fastpow(m, MOD - 2) % MOD) % MOD;           prob = prob * fastpow(m, MOD - 2) % MOD;         } else {           if (b[i] > a[i])             break;           else if (b[i] < a[i]) {             ans = (ans + prob) % MOD;             break;           }         }       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int temp[100012], arr[100012], t[100012];     for (int i = 0; i < n; i++) {       cin >> arr[i];       t[i] = arr[i];     }     sort(t, t + n);     int v = 0;     for (int i = 0; i < n; i++) {       if (t[i] != arr[i]) {         v = 1;         break;       }     }     if (v == 0) {       cout << n << endl;       return 0;     }     int c = 0;     for (int i = 0; i < n; i++) {       if (arr[i] == i)         c++;       else {         temp[i] = arr[i];       }     }     int f = 0;     for (int i = 0; i < n; i++) {       if (arr[i] != i) {         if (i == temp[temp[i]]) {           c += 2;           f = 1;           break;         }       }     }     if (f == 0) c += 1;     if (c > n) c = n;     cout << c << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long arr[100005], tree[100005], used[100005], l[100005], r[100005],       d[100005], b[100005], query[100005], x, y, n, m, k;   template <class T>   inline T lowbit(T n) {     return (n & -n);   }   void update(long long s[], int index, long long val) {     while (index <= 100005) {       s[index] += val;       index += (index & (-index));     }   }   long long getS(long long s[], int l, int r) {     if (l > r)       return 0;     else if (l == 1) {       long long res = 0;       for (; r > 0; r -= lowbit(r)) res += s[r];       return res;     } else {       return getS(s, 1, r) - getS(s, 1, l - 1);     }   }   int main() {     cin >> n >> m >> k;     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     for (int i = 0; i < m; i++) {       cin >> l[i] >> r[i] >> d[i];     }     for (int i = 0; i < k; i++) {       cin >> x >> y;       update(tree, x, 1);       update(tree, y + 1, -1);     }     for (int i = 1; i <= m; i++) {       used[i] = getS(tree, 1, i);     }     for (int i = 0; i < m; i++) {       update(b, l[i], d[i] * used[i + 1]);       update(b, r[i] + 1, -d[i] * used[i + 1]);     }     for (int i = 0; i < n; i++) {       query[i + 1] = getS(b, 1, i + 1);     }     for (int i = 1; i <= n; i++) {       cout << arr[i - 1] + query[i];       if (i != n) {         cout <<    ;       } else {         cout << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, j;     char t, w;     cin >> n;     cin >> t >> w;     bool ok = true;     if (t == w) ok = false;     for (i = 0; i < n; i++)       for (j = 0; j < n; j++) {         if (i == 0 && j < 2) continue;         char x;         cin >> x;         if ((i == j || i + j == n - 1) && x != t) ok = false;         if (!(i == j || i + j == n - 1) && x != w) ok = false;       }     if (ok)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   int prime(int n) {     int i;     for (i = 3; i * i <= n; i += 2) {       if (n % i == 0) return 0;     }     return 1;   }   int m, i;   int main() {     scanf( %d , &m);     if (m < 9) {       printf( 1 n%d , m);       return 0;     }     printf( 3 n3  );     m -= 3;     if (m == 4) {       printf( 2 2 );       return 0;     }     for (i = 3;; i += 2) {       if (!prime(i)) continue;       if (!prime(m - i)) continue;       printf( %d %d , i, m - i);       return 0;     }   }


#include <bits/stdc++.h>   using namespace std;   struct Order {     char d;     int p;     int q;   };   int main() {     int n, s;     Order a[1001];     cin >> n >> s;     for (int i = 0; i < n; i++) {       cin >> a[i].d >> a[i].p >> a[i].q;     }     map<int, int> bmp, smp;     for (int i = 0; i < n; i++) {       if (a[i].d ==  B ) {         bmp[a[i].p] += a[i].q;       } else {         smp[a[i].p] += a[i].q;       }     }     vector<pair<int, int> > bv;     for (map<int, int>::iterator it = bmp.begin(); it != bmp.end(); it++) {       bv.push_back(make_pair(it->first, it->second));     }     vector<pair<int, int> > sv;     for (map<int, int>::iterator it = smp.begin(); it != smp.end(); it++) {       sv.push_back(make_pair(it->first, it->second));     }     sort(bv.begin(), bv.end());     sort(sv.begin(), sv.end());     reverse(bv.begin(), bv.end());     int n1 = min(s, (int)bv.size());     int n2 = min(s, (int)sv.size());     vector<pair<int, int> > resv;     for (int i = 0; i < n2; i++) {       resv.push_back(sv[i]);     }     reverse(resv.begin(), resv.end());     for (int i = 0; i < n2; i++) {       cout <<  S   << resv[i].first <<     << resv[i].second <<   n ;     }     for (int i = 0; i < n1; i++) {       cout <<  B   << bv[i].first <<     << bv[i].second <<   n ;     }     return 0;   }


#include<bits/stdc++.h>   #define ll long long   #define pii pair<ll,ll>   #define debug(a) cout<<a<<  n    #define maxn 200009 /// I wanna be withma youlu   #define MOD 1000000007   #define F first   #define S second   #define rep(i, a, b) for(ll i = a; i < (b); ++i)   #define per(i, b, a) for(ll i = b-1; i>=a ; i--)   #define trav(a, x) for(auto& a : x)   #define allin(a , x) for(auto a : x)   #define all(x) begin(x), end(x)    #define sz(x) (ll)(x).size()   using namespace std;   const ll INF = 1e9 + 9;   int main(){       ios::sync_with_stdio(false);       cin.tie(0);       ll t;       cin>>t;       while(t--){           ll n;           cin>>n;           string s,s2;           cin>>s>>s2;           ll conta = 0,contb = 0;           rep(i,0,n){               if(s[i] -  0  > s2[i] -  0 )conta++;               else if(s[i] -  0  < s2[i] -  0 )contb++;           }           // cout<<conta<<   <<contb<<endl;           if(conta > contb) cout<< RED n ;           else if(conta == contb) cout<< EQUAL n ;           else cout<< BLUE n ;       }       return 0;     }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   static inline void amin(T &x, U y) {     if (y < x) x = y;   }   template <typename T, typename U>   static inline void amax(T &x, U y) {     if (y > x) x = y;   }   const long long inf = (long long)1e18 + 17;   const long long iinf = 1500000000;   const long long dx[] = {1, 0, -1, 0};   const long long dy[] = {0, 1, 0, -1};   const long long mod = 1e9 + 7;   bool test_case = 1;   void solve() {     string a, b;     cin >> a >> b;     long long next[a.size() + 10][26], n1 = a.size();     for (long long i = 0; i < 26; i++) next[n1][i] = inf;     for (long long i = n1 - 1; i >= 0; i--) {       for (long long j = 0; j < 26; j++) next[i][j] = next[i + 1][j];       next[i][a[i] -  a ] = i;     }     long long n = b.size();     long long dp[n + 10][n + 10];     dp[0][0] = 0;     for (long long ii = 0; ii < b.size(); ii++) {       long long l1 = ii + 1, l2 = b.size() - l1;       for (long long i = 0; i <= l1; i++) {         for (long long j = 0; j <= l2; j++) {           if (i == 0 && j == 0) continue;           dp[i][j] = inf;           if (j != 0) {             long long op1 = dp[i][j - 1];             if (op1 != inf && ii + j < b.size()) {               long long nex = b[ii + j] -  a ;               long long pos = next[op1][nex];               amin(dp[i][j], pos + 1);             }           }           if (i != 0) {             long long op1 = dp[i - 1][j];             if (op1 != inf) {               long long nex = b[i - 1] -  a ;               long long pos = next[op1][nex];               amin(dp[i][j], pos + 1);             }           }         }       }       if (dp[l1][l2] != inf) {         cout <<  YES n ;         return;       }     }     cout <<  NO n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     if (test_case == true) cin >> t;     for (long long i = 1; i <= t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 5e5;   long long pr(long long a, long long b, long long c) {     if (b == 0) return 1LL;     long long ans = 1;     while (b > 0) {       if (b % 2) ans = ((ans % c) * (a % c)) % c;       a = ((a % c) * (a % c)) % c;       b /= 2;       a %= c;       ans %= c;     }     return ans;   }   void solve() {     long long k, m, h, l, r, a, b, c, d, w, mini, maxi = INT_MAX, prev, res;     double n;     cin >> n;     n *= 2.0;     double y = (double)3.14159265358979323846 / n;     double ans = tan((3.14159265358979323846 / 2.0) - y);     cout << fixed << setprecision(10) << ans << endl;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   int main(void) {     int n, b = 0, p = 0, m, i = 0, matches = 0;     scanf( %d , &n);     scanf( %d , &b);     scanf( %d , &p);     m = n;     while (m != 1) {       matches = matches + m / 2;       m = m / 2 + m % 2;     }     printf( %d %d , b * (matches * 2) + matches, p * n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, arr[200002], cum[200002];     long long int res = 1000000000000LL, count = 0LL, i, j, m;     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %d , &arr[i]);     }     for (i = 1; i <= n; i++) {       cum[i] = arr[i];     }     for (i = 2; i <= n; i++) {       cum[i] += cum[i - 1];     }     for (i = 1, j = n; i < n; i++) {       if (arr[i] == 1) {         m = (cum[j] - cum[i + 1]);         count += j - i - m;       }     }     res = min(res, count);     count = 0;     for (i = n; i > 1; i--) {       if (arr[i] == 0) {         count += (cum[i - 1]);       }     }     res = min(res, count);     printf( %I64d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long MOD;   long long getFibb(long long n) {     long long a11 = 1, a12 = 1, a21 = 1, a22 = 0;     long long r11 = 1, r12 = 0;     long long q11 = 0, q12 = 0, q21 = 0, q22 = 0;     while (n > 0) {       if (n & 1) {         q11 = (r11 * a11 + r12 * a21) % MOD;         q12 = (r11 * a12 + r12 * a22) % MOD;         r11 = q11;         r12 = q12;       }       q11 = (a11 * a11 + a12 * a21) % MOD;       q12 = (a11 * a12 + a12 * a22) % MOD;       q21 = (a21 * a11 + a22 * a21) % MOD;       q22 = (a21 * a12 + a22 * a22) % MOD;       a11 = q11;       a12 = q12;       a21 = q21;       a22 = q22;       n >>= 1;     }     return r12;   }   long long m, l, r, k, ans;   long long cnt(long long L, long long R, long long X) {     return ((R / X) - ((L - 1) / X));   }   int main() {     cin >> m >> l >> r >> k;     MOD = m;     for (long long i = 1; i * i <= r; i++) {       if (cnt(l, r, i) >= k) ans = max(ans, i);       if (cnt(l, r, r / i) >= k) ans = max(ans, r / i);     }     cout << getFibb(ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, res, s, a, b;     cin >> n;     res = n - 1;     for (i = 1; i < n; i++) {       s = 0;       a = n;       b = i;       while (a != b) {         if (a > b)           a = a - b;         else           b = b - a;         s++;       }       if (a == 1 && b == 1)         if (s < res) res = s;     }     printf( %d n , res);   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 5;   const int INF = 1e9 + 7;   long long int arr[MAX];   signed main() {     cin.tie(NULL);     cout.tie(NULL);     ios::sync_with_stdio(0);     long long int n1, n2;     cin >> n1 >> n2;     string a, b;     cin >> a >> b;     long long int ans = INF;     long long int pos;     long long int cnt = 0;     for (long long int i = 0; i < n2 - n1 + 1; ++i) {       long long int cnt = 0;       for (long long int j = 0; j < n1; ++j) {         if (a[j] == b[i + j]) {           cnt++;         }       }       cnt = n1 - cnt;       if (cnt < ans) {         ans = cnt;         pos = i;       }     }     cout << ans << endl;     for (long long int i = pos; i < pos + n1; ++i) {       if (b[i] != a[i - pos]) {         cout << i - pos + 1 <<    ;       }     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int i, j, n, k, l, s = 0, m = 0, p = 0, r = 0;       cin >> n;       int a[n];       for (i = 0; i < n; i++) {         cin >> a[i];         s += a[i];         if (a[i] == 0) p++;         if (a[i] < -1) r++;         if (a[i] > 0) m++;       }       s += p;       if (s != 0)         cout << p << endl;       else {         cout << p + 1 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t;     scanf( %d%d , &n, &t);     string s;     cin >> s;     string s1 =   , s2 =   ;     bool ck = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  . )         ck = 1;       else if (ck)         s2 += s[i];       else         s1 += s[i];     }     if (ck == 0) {       cout << s << endl;       return 0;     }     int cnt = 0;     ck = 0;     for (int i = 0; i < s2.size(); i++) {       if (s2[i] >=  5 ) {         cnt = i;         ck = 1;         break;       }     }     int cp = 0;     if (ck) {       while (cnt >= 0) {         if (cp == t) break;         cp++;         cnt--;         int val = (s2[cnt] -  0 ) + 1;         s2[cnt] = (val +  0 );         if (val < 5) break;       }       if (cnt == -1) {         int len = s1.size();         string sl =   ;         for (int i = 0; i < len - 1; i++) sl +=  0 ;         sl +=  1 ;         int ca = 0;         string s9 =   ;         for (int i = len - 1; i >= 0; i--) {           int val = (s1[i] -  0 ) + (sl[i] -  0 ) + ca;           ca = val / 10;           s9 += (val % 10 +  0 );         }         if (ca) s9 += (ca +  0 );         reverse(s9.begin(), s9.end());         cout << s9 << endl;       } else {         cout << s1 <<  . ;         if (ck)           for (int i = 0; i <= cnt; i++) cout << s2[i];         else           cout << s2 << endl;         cout << endl;       }     } else {       cout << s << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1100000;   long long arr[N];   long long b[N];   long long n, m, w;   bool ok(long long x) {     memset(b, 0, sizeof(b));     long long remain = m;     long long c = 0;     for (int i = 0; i < n; i++) {       c += b[i];       if (c + arr[i] < x) {         long long need = x - (c + arr[i]);         b[i + w] -= need;         remain -= need;         c += need;       }     }     if (remain >= 0)       return true;     else       return false;   }   int main() {     while (cin >> n >> m >> w) {       memset(arr, 0, sizeof(arr));       for (int i = 0; i < n; i++) cin >> arr[i];       long long l = 1;       long long r = (long long)9e9;       while (l < r) {         long long mid = (l + r + 1) / 2;         if (ok(mid)) {           l = mid;         } else {           r = mid - 1;         }       }       cout << l << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3fll;   const int MAXN = 510;   char grid[MAXN][MAXN];   int n, k;   int component[MAXN][MAXN];   int size[MAXN * MAXN];   int orig_size[MAXN * MAXN];   int last_taken[MAXN * MAXN];   int dr[] = {1, 0, -1, 0};   int dc[] = {0, 1, 0, -1};   inline bool valid(int r, int c) { return 0 <= r && r < n && 0 <= c && c < n; }   void dfs(int r, int c, int cmp) {     component[r][c] = cmp;     size[cmp]++;     for (int i = 0; i < 4; i++) {       int nr = r + dr[i], nc = c + dc[i];       if (!valid(nr, nc)) continue;       if (component[nr][nc] != -1 || grid[nr][nc] ==  X ) continue;       dfs(nr, nc, cmp);     }   }   int main() {     scanf( %d%d , &n, &k);     for (int r = 0; r < n; r++)       for (int c = 0; c < n; c++) scanf(  %c , &grid[r][c]);     memset(component, -1, sizeof(component));     int cnt = 0;     for (int r = 0; r < n; r++) {       for (int c = 0; c < n; c++) {         if (grid[r][c] ==  .  && component[r][c] == -1) {           dfs(r, c, cnt);           orig_size[cnt] = size[cnt];           cnt++;         }       }     }     int best = 0;     int t = 1;     for (int r = 0; r + k - 1 < n; r++) {       for (int i = 0; i < cnt; i++) size[i] = orig_size[i];       for (int rr = 0; rr < k; rr++)         for (int cc = 0; cc < k; cc++)           if (grid[r + rr][cc] ==  . ) size[component[r + rr][cc]]--;       for (int c = 0; c + k - 1 < n; c++) {         int got = k * k;         for (int i = 0; i < k; i++) {           int rr, cc;           rr = r - 1, cc = c + i;           if (valid(rr, cc) && grid[rr][cc] ==  .  &&               last_taken[component[rr][cc]] != t) {             got += size[component[rr][cc]];             last_taken[component[rr][cc]] = t;           }           rr = r + i, cc = c + k;           if (valid(rr, cc) && grid[rr][cc] ==  .  &&               last_taken[component[rr][cc]] != t) {             got += size[component[rr][cc]];             last_taken[component[rr][cc]] = t;           }           rr = r + k, cc = c + i;           if (valid(rr, cc) && grid[rr][cc] ==  .  &&               last_taken[component[rr][cc]] != t) {             got += size[component[rr][cc]];             last_taken[component[rr][cc]] = t;           }           rr = r + i, cc = c - 1;           if (valid(rr, cc) && grid[rr][cc] ==  .  &&               last_taken[component[rr][cc]] != t) {             got += size[component[rr][cc]];             last_taken[component[rr][cc]] = t;           }         }         best = max(best, got);         for (int rr = r; rr < r + k; rr++) {           if (grid[rr][c] ==  . ) size[component[rr][c]]++;           if (c + k < n && grid[rr][c + k] ==  . ) size[component[rr][c + k]]--;         }         t++;       }     }     printf( %d n , best);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long par[200005], sz[200005];   long long root(long long x) {     return (x == par[x] ? x : (par[x] = root(par[x])));   }   long long ans = 0;   void join(long long x, long long y) {     ans -= sz[root(y)] * (sz[root(y)] - 1) / 2;     ans -= sz[root(x)] * (sz[root(x)] - 1) / 2;     sz[root(x)] += sz[root(y)];     par[root(y)] = root(x);     ans += sz[root(x)] * (sz[root(x)] - 1) / 2;   }   long long res[200005];   void solve() {     long long n, m;     cin >> n >> m;     vector<pair<long long, pair<long long, long long>>> sh;     for (long long i = 1; i < n; i++) {       long long x, y, z;       cin >> x >> y >> z;       sh.push_back({z, {x, y}});     }     for (long long i = 1; i <= n; i++) sz[i] = 1, par[i] = i;     sort(sh.begin(), sh.end());     long long l = 0;     vector<pair<long long, long long>> qr;     for (long long i = 0; i < m; i++) {       long long x;       cin >> x;       qr.push_back({x, i});     }     sort(qr.begin(), qr.end());     for (auto &cv : qr) {       while (l < sh.size() && sh[l].first <= cv.first) {         join(sh[l].second.first, sh[l].second.second);         l++;       }       res[cv.second] = ans;     }     for (long long i = 0; i < m; i++) cout << res[i] << (i == m - 1 ?   n  :    );   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000000007;   vector<int> rec[100008];   int dp[2][100008], a[100008];   int fun(int minn, int val) {     int ll = 0, rr, ss = rec[val].size();     if (ss == 0)       return maxn;     else if (rec[val][0] >= minn)       return rec[val][0] + 1;     rr = ss;     while (ll + 1 != rr) {       int mid = (ll + rr) / 2;       if (rec[val][mid] < minn)         ll = mid;       else         rr = mid;     }     if (rr == ss) return maxn;     return rec[val][rr] + 1;   }   int main() {     int n, m, s, e, i, cc, ans;     bool f;     scanf( %d%d%d%d , &n, &m, &s, &e);     for (i = 0; i < n; i++) scanf( %d , &a[i]);     for (i = 0; i <= 100000; i++) rec[i].clear();     for (i = 0; i < m; i++) {       int tmp;       scanf( %d , &tmp);       rec[tmp].push_back(i);     }     memset(dp, 0, sizeof(dp));     cc = 1;     ans = 0;     f = true;     while (f) {       f = false;       for (i = ans; i < n; i++) {         if (i != 0 && dp[1 - cc][i - 1] != maxn) {           dp[cc][i] = fun(dp[1 - cc][i - 1], a[i]);           if (i != ans) dp[cc][i] = min(dp[cc][i], dp[cc][i - 1]);           if (dp[cc][i] + i + 1 + (ans + 1) * e <= s) f = true;         } else if (i == 0) {           dp[cc][i] = fun(0, a[i]);           if (i != ans) dp[cc][i] = min(dp[cc][i], dp[cc][i - 1]);           if (dp[cc][i] + i + 1 + (ans + 1) * e <= s) f = true;         } else           dp[cc][i] = maxn;       }       if (f) ++ans;       cc = 1 - cc;     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = (long long)(2e5), MOD = 1e9 + 7;   const long long INF = 1e17;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     string s;     cin >> s;     int cnt = 0;     string copy;     copy += s[0];     for (int i = 1; i < s.size(); i++) {       if (s[i] == copy[copy.size() - 1]) {         copy.erase(copy.begin() + copy.size() - 1);         cnt++;       } else         copy += s[i];     }     if (cnt % 2 == 0)       cout <<  No ;     else       cout <<  Yes ;   }


#include <bits/stdc++.h>   using namespace std;   long double PI = acos((long double)-1.0);   const long double eps = 1e-9;   namespace io_impl {   inline bool maybe_digit(char c) { return c >=  0  && c <=  9 ; }   struct io_s {    private:     bool negative;     bool ok = true;     char ch = next_char();     int precious;     long double epslion;     inline char next_char() const { return getchar(); }       public:     template <typename T>     inline bool rn(T& _v) {       negative = false;       _v = 0;       while (!maybe_digit(ch) && ch != EOF) {         negative = ch ==  - ;         ch = next_char();       };       if (ch == EOF) return ok = false;       do {         _v = (_v << 1) + (_v << 3) + ch -  0 ;       } while (maybe_digit(ch = next_char()));       if (negative) _v = -_v;       return true;     }     template <typename T>     inline void rn_unsafe(T& _v) {       negative = false;       _v = 0;       while (!maybe_digit(ch)) {         negative = ch ==  - ;         ch = next_char();       };       do {         _v = (_v << 1) + (_v << 3) + ch -  0 ;       } while (maybe_digit(ch = next_char()));       if (negative) _v = -_v;     }     template <typename T>     inline T rn() {       T v = T();       rn_unsafe(v);       return v;     }     template <typename T>     inline bool run(T& _v) {       _v = 0;       while (!maybe_digit(ch) && ch != EOF) ch = next_char();       if (ch == EOF) return ok = false;       do {         _v = (_v << 1) + (_v << 3) + ch -  0 ;       } while (maybe_digit(ch = next_char()));       return true;     }     template <typename T>     inline void run_unsafe(T& _v) {       _v = 0;       while (!maybe_digit(ch)) ch = next_char();       do {         _v = (_v << 1) + (_v << 3) + ch -  0 ;       } while (maybe_digit(ch = next_char()));     }     template <typename T>     inline T run() {       T v = T();       run_unsafe(v);       return v;     }     template <typename T>     inline bool rd(T& _v) {       negative = false;       _v = 0;       while (!maybe_digit(ch) && ch != EOF) {         negative = ch ==  - ;         ch = next_char();       };       if (ch == EOF) return ok = false;       do {         _v = (_v * 10) + (ch -  0 );       } while (maybe_digit(ch = next_char()));       static int stk[70], tp;       if (ch ==  . ) {         tp = 0;         T _v2 = 0;         while (maybe_digit(ch = next_char())) {           stk[tp++] = ch -  0 ;         }         while (tp--) {           _v2 = _v2 / 10 + stk[tp];         }         _v += _v2 / 10;       }       if (ch ==  e  || ch ==  E ) {         ch = next_char();         static bool _neg = false;         if (ch ==  + )           ch = next_char();         else if (ch ==  - )           _neg = true, ch = next_char();         if (maybe_digit(ch)) {           _v *= pow(10, run<long long>() * (_neg ? -1 : 1));         }       }       if (negative) _v = -_v;       return true;     }     template <typename T>     inline T rd() {       T v = T();       rd(v);       return v;     }     inline int gets(char* s) {       char* c = s;       while (ch ==   n  || ch ==   r  || ch ==    ) ch = next_char();       if (ch == EOF) return (ok = false), *c = 0;       do {         *(c++) = ch;         ch = next_char();       } while (ch !=   n  && ch !=   r  && ch !=     && ch != EOF);       *(c++) =   0 ;       return static_cast<int>(c - s - 1);     }     inline int getline(char* s) {       char* c = s;       while (ch ==   n  || ch ==   r ) ch = next_char();       if (ch == EOF) return (ok = false), *c = 0;       do {         *(c++) = ch;         ch = next_char();       } while (ch !=   n  && ch !=   r  && ch != EOF);       *(c++) =   0 ;       return static_cast<int>(c - s - 1);     }     inline char get_alpha() {       while (!isalpha(ch)) ch = next_char();       const char ret = ch;       ch = next_char();       return ret;     }     inline char get_nonblank() {       while (iswspace(ch)) ch = next_char();       const char ret = ch;       ch = next_char();       return ret;     }     inline char get_char() {       const char ret = ch;       ch = next_char();       return ret;     }     template <typename T>     inline void o(T p) {       static int stk[70], tp;       if (p == 0) {         putchar( 0 );         return;       }       if (p < 0) {         p = -p;         putchar( - );       }       while (p) stk[++tp] = p % 10, p /= 10;       while (tp) putchar(stk[tp--] +  0 );     }     template <typename T>     inline void od(T v) {       static int stk[70], tp;       tp = 0;       if (fabs(v) < epslion / 10) {         putchar( 0 );         if (precious > 0) {           putchar( . );           for (int i = 0; i < precious; ++i) putchar( 0 );         }         return;       } else {         if (v < 0) {           v = -v;           putchar( - );         }         v += epslion / 2;         T p = floor(v) + epslion / 10;         while (fabs(p) > 1 - epslion) {           stk[++tp] = fmod(p, 10), p = floor(p / 10) + epslion;         }         if (tp == 0) putchar( 0 );         while (tp) putchar(stk[tp--] +  0 );       }       if (precious == 0) return;       putchar( . );       v -= floor(v);       for (int i = 0; i < precious; ++i) {         v *= 10;         putchar((int)floor(v) +  0 );         v = fmod(v, 1);       }     }     typedef void io_operator(io_s& v);     typedef char* charptr;     template <typename T>     inline io_s& operator>>(T& x) {       if (numeric_limits<T>::is_signed)         rn(x);       else         run(x);       return *this;     }     template <typename T1, typename T2>     inline io_s& operator>>(pair<T1, T2> pr);     template <typename T>     inline io_s& operator<<(const T& x);     inline io_s& operator<<(io_operator* v) {       v(*this);       return *this;     }     operator bool() const { return ok; }     void set_precious(int x) {       precious = x;       epslion = pow(10, -x);     }     inline int ri() { return rn<int>(); }     inline long long rll() { return rn<long long>(); }     inline static void new_line(io_s& v) { putchar(  n ); }     inline static void flush(io_s& v) { fflush(stdout); }   };   template <>   inline io_s& io_s::operator>>(char*& x) {     gets(x);     return *this;   }   template <>   inline io_s& io_s::operator>>(float& x) {     rd(x);     return *this;   }   template <>   inline io_s& io_s::operator>>(double& x) {     rd(x);     return *this;   }   template <>   inline io_s& io_s::operator>>(long double& x) {     rd(x);     return *this;   }   template <typename T1, typename T2>   inline io_s& io_s::operator>>(pair<T1, T2> pr) {     rd(pr.frist);     rd(pr.second);     return *this;   }   template <>   inline void io_s::o(const char p) {     putchar(p);   }   template <>   inline void io_s::o(const char* p) {     printf(p);   }   template <>   inline void io_s::o(float p) {     od(p);   }   template <>   inline void io_s::o(double p) {     od(p);   }   template <>   inline void io_s::o(long double p) {     od(p);   }   template <typename T>   inline io_s& io_s::operator<<(const T& x) {     o(x);     return *this;   }   io_s::io_operator* nl = io_s::new_line;   io_s::io_operator* fl = io_s::flush;   }  // namespace io_impl   using namespace io_impl;   io_s io;   typedef void InitFunction();   typedef bool SolveFunction(int);   struct env_s {    private:     bool global_inited = false;     bool case_inited = false;       public:     bool once() { return global_inited ? false : (global_inited = true); }     bool every() { return case_inited ? false : (case_inited = true); }     void fileInput() { freopen( data.in ,  r , stdin); }     void fileOutput() { freopen( data.out ,  w , stdout); }     InitFunction *_initOnce, *_init;     template <InitFunction TInitOnce, InitFunction TInit>     void configure() {       _initOnce = TInitOnce;       _init = TInit;     }     void init(bool global) {       if (global)         if (_initOnce != NULL) _initOnce();       if (_init != NULL) _init();     }     void emptyInit() {}     template <SolveFunction TSolve>     void solveSingle() {       global_inited = false;       case_inited = false;       init(true);       TSolve(0);     }     template <SolveFunction TSolve>     void solveMany() {       global_inited = false;       int T = io.ri();       for (int cs = 1; T--; ++cs) {         case_inited = false;         init(cs == 1);         TSolve(cs);       }     }     template <SolveFunction TSolve>     void solveMuch() {       global_inited = false;       case_inited = false;       for (int cs = 1; init(cs == 1), TSolve(cs); ++cs) case_inited = false;     }   } env;   namespace solution {   void initOnce() { io.set_precious(12); }   void init() {}   bool solve(int _cas) {     int n;     io >> n;     int x = 1e9 + 7, a, aa, b, bb;     for (int i = 0; i < n; ++i) {       io >> a >> b;       aa = max(a, b);       bb = min(a, b);       if (aa <= x)         x = aa;       else if (bb <= x)         x = bb;       else {         puts( NO );         return true;       }     }     puts( YES );     return true;   }   }  // namespace solution   int main() {     env.configure<solution::initOnce, solution::init>();     env.solveSingle<solution::solve>();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n, q;     cin >> n >> q;     vector<int> a((size_t)n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     vector<pair<int, int>> b((size_t)q);     for (int i = 0; i < q; i++) {       cin >> b[i].first >> b[i].second;       b[i].first--, b[i].second--;     }     int m = 22;     vector<vector<int>> c((size_t)m, vector<int>((size_t)m, n));     vector<vector<int>> d((size_t)m, vector<int>((size_t)m, n));     vector<vector<int>> e((size_t)n, vector<int>((size_t)m, n));     for (int i = n - 1; i >= 0; i--) {       c = vector<vector<int>>((size_t)m, vector<int>((size_t)m, n));       vector<int> x((size_t)m, n);       for (int j = 0; j < m; j++) {         for (int k = 0; k < m; k++) {           if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) != 0) {             c[j][k] = i;           } else if ((a[i] & (1 << j)) != 0 || (a[i] & (1 << k)) != 0) {             if ((a[i] & (1 << j)) != 0) {               c[j][k] = d[j][k];             }             if ((a[i] & (1 << j)) != 0) {               x[k] = min(x[k], c[j][k]);             }           }         }       }       for (int j = 0; j < m; j++) {         for (int k = 0; k < m; k++) {           if ((a[i] & (1 << j)) != 0 && (a[i] & (1 << k)) == 0) {             c[j][k] = min(c[j][k], x[k]);           }           e[i][j] = min(e[i][j], c[j][k]);           e[i][k] = min(e[i][k], c[j][k]);           d[j][k] = min(d[j][k], c[j][k]);         }       }     }     for (int i = 0; i < q; i++) {       bool f = false;       for (int j = 0; j < m; j++) {         if ((a[b[i].second] & (1 << j)) != 0) {           if (e[b[i].first][j] <= b[i].second) {             f = true;             break;           }         }       }       cout << (f ?  Shi  :  Fou ) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, k;     while (cin >> n >> k) {       vector<long long> a(n);       long long total = 0;       for (auto &i : a) {         cin >> i;         total += i;       }       sort(a.begin(), a.end());       long long lo = 0, hi = (total / n);       while (lo < hi) {         long long mid = (lo + hi + 1) >> 1;         long long need = 0;         for (int i = 0; i < n; ++i) {           need += max(0LL, mid - a[i]);         }         if (need > k)           hi = mid - 1;         else           lo = mid;       }       long long tmp1 = lo;       lo = (total + n - 1) / n, hi = total;       while (lo < hi) {         long long mid = (lo + hi) >> 1;         long long need = 0;         for (int i = 0; i < n; ++i) {           need += max(0LL, a[i] - mid);         }         if (need > k)           lo = mid + 1;         else           hi = mid;       }       cout << llabs(lo - tmp1) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, h;     scanf( %d%d%d , &n, &m, &h);     h--;     n--;     int cnto = 0, cntr = 0, tot;     for (int i = 0; i < m; i++) {       int t;       scanf( %d , &t);       if (i == h)         cntr = t - 1;       else         cnto += t;     }     if (n > cnto + cntr) {       printf( -1.0 n );       return 0;     }     tot = cnto + cntr;     double prob = 1;     for (int i = n; i > 0; i--, cnto--, tot--) {       prob *= (double)cnto / tot;     }     printf( %.8lf n , 1 - prob);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int sum;   long long int cnt;   long long int maxs;   long long int a[100001];   map<long long int, long long int> jilu;   long long int maxa(long long int a, long long int b) { return a > b ? a : b; }   int main() {     int i, n;     while (scanf( %d , &n) != EOF) {       cnt = 0;       sum = 0;       maxs = 0;       jilu.clear();       for (i = 1; i < n + 1; i++) {         scanf( %lld , &a[i]);         sum += a[i];         maxs = maxa(maxs, ++jilu[sum]);       }       cnt = n - maxs;       printf( %lld n , cnt);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y, n;     scanf( %d%d%d , &x, &y, &n);     for (int i = 0; i < n; i++) y += x ^= y ^= x ^= y;     printf( %d n , x);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e3 + 15;   const int mod = 998244353;   char s[N];   int len;   int depth[N];   vector<int> adj[N];   int dp[N][2];   int add(int x, int y) { return ((x + y) % mod + mod) % mod; }   int mul(int x, int y) { return (long long)x * y % mod; }   int mypow(int x, int c) {     int ret = 1;     while (c > 0) {       if (c & 1) {         ret = mul(ret, x);       }       c /= 2;       x = mul(x, x);     }     return ret;   }   bool vis[N];   void dfs(int node, int par) {     vis[node] = true;     for (int ch : adj[node]) {       if (ch != par && !vis[ch]) {         depth[ch] = depth[node] + 1;         dfs(ch, node);       }     }   }   int solveDp(int node, int bit) {     int &ret = dp[node][bit];     if (ret != -1) {       return ret;     }     ret = 0;     bool isLeaf = true;     int cc = 0;     for (int ch : adj[node]) {       if (depth[ch] > depth[node]) {         isLeaf = false;       } else {         continue;       }       ++cc;       int diff = abs(ch - node);       if (diff >= len) {         int ind = min(ch, node);         if (s[ind] ==  ? ) {           ret = add(ret, solveDp(ch, 0));           ret = add(ret, solveDp(ch, 1));         } else {           int u = (s[ind] -  0 ) ^ bit;           ret = solveDp(ch, u);         }       } else {         ret = solveDp(ch, bit);       }     }     if (isLeaf) {       ret = 1;     }     return ret;   }   void solve() {     scanf(  %s  , s + 1);     len = strlen(s + 1);     int ret = 0;     for (int st = 2; st <= len; ++st) {       for (int i = 1; i <= len * 2; ++i) adj[i].clear();       memset(dp, -1, sizeof(dp));       memset(depth, 0, sizeof(depth));       memset(vis, 0, sizeof(vis));       for (int i = 1; i <= len; ++i) {         int j = len - i + 1;         if (i != j) adj[i].push_back(j);       }       int g = len - st + 1;       for (int j = st; j <= len; ++j) {         int ind = j - st + 1;         int nind = g - ind + 1 + (st - 1);         if (j + len != nind + len) adj[j + len].push_back(nind + len);       }       for (int i = 1; i < st; ++i) {         dp[i + len][1] = 0;       }       dp[1][0] = 0;       dp[len][0] = 0;       dp[st + len][0] = 0;       dp[len * 2][0] = 0;       for (int i = 1; i <= len; ++i) {         adj[i].push_back(i + len);         adj[i + len].push_back(i);       }       int tmp = 1;       for (int i = 1; i <= len * 2; ++i) {         assert(adj[i].size() <= 2);         if (adj[i].size() == 1 && !vis[i]) {           dfs(i, 0);         }       }       for (int i = 1; i <= len * 2; ++i) {         assert(vis[i]);         if (depth[i] == 0) {           assert(adj[i].size() == 1);           tmp = mul(tmp, add(solveDp(i, 0), solveDp(i, 1)));         }       }       ret = add(ret, tmp);     }     printf( %d n , ret);   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 50;   long long T, n, m;   char s[maxn];   int main() {     cin >> n;     cin >> s + 1;     int sign = 0, x = 0, y = 0, ans = 0;     for (int i = 1; i <= n; i++) {       if (s[i] ==  U )         y++;       else if (s[i] ==  R )         x++;       if (y > x) {         if (sign == -1) ans++;         sign = 1;       } else if (y < x) {         if (sign == 1) ans++;         sign = -1;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const long long llinf = 0x3f3f3f3f3f3f3f3f;   template <typename INint>   inline void IN(INint &first) {     first = 0;     int f = 1;     char c;     cin.get(c);     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       cin.get(c);     }     while (c >=  0  && c <=  9 ) {       first = first * 10 + c -  0 ;       cin.get(c);     }     first *= f;   }   template <typename INint>   inline void OUT(INint first) {     if (first > 9) OUT(first / 10);     cout.put(first % 10 +  0 );   }   const int maxn = int(4e5);   int mod = int(1e9 + 7);   int n;   int mn = -inf, mx = inf;   set<int> A;   int gtype(int first) {     if (first < mn) return 0;     if (first > mx) return 4;     if (first == mn) return 1;     if (first == mx) return 3;     return 2;   }   void mul(long long &a, long long b) {     a *= b;     if (a > mod) a %= mod;   }   int main() {     int T(1), times(0);     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     while (++times, T--) {       cin >> n;       A.insert(mx);       A.insert(mn);       long long res(1), ans(1);       for (int i = 1; i <= int(n); i++) {         int no;         string s;         cin >> s >> no;         int p = gtype(no);         if (s[1] ==  D ) {           A.insert(no);           if (p == 2) ++res;         } else {           if (p == 0 || p == 4) {             cout << 0 << endl;             return 0;           }           if (p == 2) mul(ans, 2);           A.erase(no);           res = 1;           auto pos = A.lower_bound(no);           mx = *pos;           mn = *(--pos);         }       }       ans *= res;       ans %= mod;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long n, m;   vector<long> clauses[100000 + 1];   vector<long> variables[100000 + 1];   long long ans[2] = {0};   bool check_clauses[100000 + 1] = {false};   long long dp_path[100000 + 1][2][2];   long long dp_cycle[100000 + 1][2][2][2];   void path_component(long start_clause) {     long cur_clause = 0, nxt_clause = 0, variable = 0;     long long temp[2];     if (clauses[start_clause].size() == 1) {       variable = abs(clauses[start_clause][0]);       if (variables[variable].size() == 1) {         if ((ans[0] == 0) && (ans[1] == 0)) {           ans[1] = 1;           ans[0] = 1;         } else {           temp[1] = (ans[1] + ans[0]) % 1000000007;           temp[0] = (ans[1] + ans[0]) % 1000000007;           ans[1] = temp[1];           ans[0] = temp[0];         }         return;       }       nxt_clause = (abs(variables[variable][0]) != start_clause)                        ? abs(variables[variable][0])                        : abs(variables[variable][1]);       if ((long long)variables[variable][0] * (long long)variables[variable][1] >           0) {         dp_path[nxt_clause][1][1] = 0;         dp_path[nxt_clause][1][0] = 1;         dp_path[nxt_clause][0][1] = 0;         dp_path[nxt_clause][0][0] = 1;       } else {         dp_path[nxt_clause][1][1] = 1;         dp_path[nxt_clause][1][0] = 0;         dp_path[nxt_clause][0][1] = 1;         dp_path[nxt_clause][0][0] = 0;       }       cur_clause = nxt_clause;     } else {       if ((variables[abs(clauses[start_clause][0])].size() == 2) &&           (variables[abs(clauses[start_clause][1])].size() == 2))         return;       variable = (variables[abs(clauses[start_clause][0])].size() == 1)                      ? abs(clauses[start_clause][0])                      : abs(clauses[start_clause][1]);       cur_clause = start_clause;       dp_path[start_clause][1][1] = 1;       dp_path[start_clause][1][0] = 0;       dp_path[start_clause][0][1] = 0;       dp_path[start_clause][0][0] = 1;     }     if (check_clauses[start_clause]) return;     check_clauses[start_clause] = true;     check_clauses[cur_clause] = true;     while (true) {       if (clauses[cur_clause].size() == 1) {         if ((ans[0] == 0) && (ans[1] == 0)) {           ans[0] = (dp_path[cur_clause][0][0] + dp_path[cur_clause][1][0]) %                    1000000007;           ans[1] = (dp_path[cur_clause][0][1] + dp_path[cur_clause][1][1]) %                    1000000007;         } else {           temp[0] = ((ans[1] *                       ((dp_path[cur_clause][0][1] + dp_path[cur_clause][1][1]) %                        1000000007)) %                          1000000007 +                      (ans[0] *                       ((dp_path[cur_clause][0][0] + dp_path[cur_clause][1][0]) %                        1000000007)) %                          1000000007) %                     1000000007;           temp[1] = ((ans[1] *                       ((dp_path[cur_clause][0][0] + dp_path[cur_clause][1][0]) %                        1000000007)) %                          1000000007 +                      (ans[0] *                       ((dp_path[cur_clause][0][1] + dp_path[cur_clause][1][1]) %                        1000000007)) %                          1000000007) %                     1000000007;           ans[0] = temp[0];           ans[1] = temp[1];         }         return;       }       variable = (abs(clauses[cur_clause][0]) != variable)                      ? abs(clauses[cur_clause][0])                      : abs(clauses[cur_clause][1]);       if (variables[variable].size() == 1) {         long long dp_temp[2][2];         dp_temp[0][1] = dp_path[cur_clause][0][1];         dp_temp[0][0] = dp_path[cur_clause][0][0];         dp_temp[1][1] =             (dp_path[cur_clause][0][0] + 2 * dp_path[cur_clause][1][1]) %             1000000007;         dp_temp[1][0] =             (dp_path[cur_clause][0][1] + 2 * dp_path[cur_clause][1][0]) %             1000000007;         if ((ans[0] == 0) && (ans[1] == 0)) {           ans[0] = (dp_temp[0][0] + dp_temp[1][0]) % 1000000007;           ans[1] = (dp_temp[0][1] + dp_temp[1][1]) % 1000000007;         } else {           temp[0] = ((ans[1] * ((dp_temp[0][1] + dp_temp[1][1]) % 1000000007)) %                          1000000007 +                      (ans[0] * ((dp_temp[0][0] + dp_temp[1][0]) % 1000000007)) %                          1000000007) %                     1000000007;           temp[1] = ((ans[1] * ((dp_temp[0][0] + dp_temp[1][0]) % 1000000007)) %                          1000000007 +                      (ans[0] * ((dp_temp[0][1] + dp_temp[1][1]) % 1000000007)) %                          1000000007) %                     1000000007;           ans[0] = temp[0];           ans[1] = temp[1];         }         return;       }       nxt_clause = (abs(variables[variable][0]) != cur_clause)                        ? abs(variables[variable][0])                        : abs(variables[variable][1]);       check_clauses[nxt_clause] = true;       if ((long long)variables[variable][0] * (long long)variables[variable][1] >           0) {         dp_path[nxt_clause][0][1] =             (dp_path[cur_clause][1][1] + dp_path[cur_clause][0][1]) % 1000000007;         dp_path[nxt_clause][0][0] =             (dp_path[cur_clause][1][0] + dp_path[cur_clause][0][0]) % 1000000007;         dp_path[nxt_clause][1][1] =             (dp_path[cur_clause][1][0] + dp_path[cur_clause][0][1]) % 1000000007;         dp_path[nxt_clause][1][0] =             (dp_path[cur_clause][1][1] + dp_path[cur_clause][0][0]) % 1000000007;       } else {         dp_path[nxt_clause][1][1] =             (dp_path[cur_clause][1][0] + dp_path[cur_clause][0][0]) % 1000000007;         dp_path[nxt_clause][1][0] =             (dp_path[cur_clause][1][1] + dp_path[cur_clause][0][1]) % 1000000007;         dp_path[nxt_clause][0][1] =             (dp_path[cur_clause][1][1] + dp_path[cur_clause][0][0]) % 1000000007;         dp_path[nxt_clause][0][0] =             (dp_path[cur_clause][1][0] + dp_path[cur_clause][0][1]) % 1000000007;       }       cur_clause = nxt_clause;     }   }   void cycle_component(long start_clause) {     long cur_clause = 0, nxt_clause = 0, variable = 0;     long long temp[2];     if (clauses[start_clause].size() == 1) return;     if ((variables[abs(clauses[start_clause][0])].size() == 1) ||         (variables[abs(clauses[start_clause][1])].size() == 1))       return;     if (check_clauses[start_clause]) return;     check_clauses[start_clause] = true;     variable = abs(clauses[start_clause][0]);     nxt_clause = (abs(variables[variable][0]) != start_clause)                      ? abs(variables[variable][0])                      : abs(variables[variable][1]);     if (nxt_clause == start_clause) {       if ((long long)variables[variable][0] * (long long)variables[variable][1] >           0) {         if ((ans[0] == 0) && (ans[1] == 0)) {           ans[1] = 1;           ans[0] = 1;         } else {           temp[1] = (ans[1] + ans[0]) % 1000000007;           temp[0] = (ans[1] + ans[0]) % 1000000007;           ans[1] = temp[1];           ans[0] = temp[0];         }       } else {         if ((ans[0] == 0) && (ans[1] == 0)) {           ans[1] = 2;           ans[0] = 0;         } else {           temp[1] = 2 * ans[0];           temp[0] = 2 * ans[1];           ans[1] = temp[1];           ans[0] = temp[0];         }       }       return;     }     if ((long long)variables[variable][0] * (long long)variables[variable][1] >         0) {       dp_cycle[nxt_clause][1][1][1] = 0;       dp_cycle[nxt_clause][1][1][0] = 1;       dp_cycle[nxt_clause][1][0][1] = 0;       dp_cycle[nxt_clause][1][0][0] = 0;       dp_cycle[nxt_clause][0][1][1] = 0;       dp_cycle[nxt_clause][0][1][0] = 0;       dp_cycle[nxt_clause][0][0][1] = 0;       dp_cycle[nxt_clause][0][0][0] = 1;     } else {       dp_cycle[nxt_clause][1][1][1] = 0;       dp_cycle[nxt_clause][1][1][0] = 0;       dp_cycle[nxt_clause][1][0][1] = 1;       dp_cycle[nxt_clause][1][0][0] = 0;       dp_cycle[nxt_clause][0][1][1] = 1;       dp_cycle[nxt_clause][0][1][0] = 0;       dp_cycle[nxt_clause][0][0][1] = 0;       dp_cycle[nxt_clause][0][0][0] = 0;     }     cur_clause = nxt_clause;     while (true) {       check_clauses[cur_clause] = true;       variable = (abs(clauses[cur_clause][0]) != variable)                      ? abs(clauses[cur_clause][0])                      : abs(clauses[cur_clause][1]);       nxt_clause = (abs(variables[variable][0]) != cur_clause)                        ? abs(variables[variable][0])                        : abs(variables[variable][1]);       if (nxt_clause == start_clause) {         long long dp_temp[2][2];         if ((long long)variables[variable][0] *                 (long long)variables[variable][1] >             0) {           dp_temp[0][1] =               (dp_cycle[cur_clause][1][1][1] + dp_cycle[cur_clause][1][0][1] +                dp_cycle[cur_clause][0][1][1] + dp_cycle[cur_clause][0][0][1]) %               1000000007;           dp_temp[0][0] =               (dp_cycle[cur_clause][1][1][0] + dp_cycle[cur_clause][1][0][0] +                dp_cycle[cur_clause][0][1][0] + dp_cycle[cur_clause][0][0][0]) %               1000000007;           dp_temp[1][1] =               (dp_cycle[cur_clause][1][1][1] + dp_cycle[cur_clause][1][0][0] +                dp_cycle[cur_clause][0][1][0] + dp_cycle[cur_clause][0][0][1]) %               1000000007;           dp_temp[1][0] =               (dp_cycle[cur_clause][1][1][0] + dp_cycle[cur_clause][1][0][1] +                dp_cycle[cur_clause][0][1][1] + dp_cycle[cur_clause][0][0][0]) %               1000000007;         } else {           dp_temp[1][1] =               (dp_cycle[cur_clause][1][1][1] + dp_cycle[cur_clause][1][0][0] +                dp_cycle[cur_clause][0][1][1] + dp_cycle[cur_clause][0][0][0]) %               1000000007;           dp_temp[1][0] =               (dp_cycle[cur_clause][1][1][0] + dp_cycle[cur_clause][1][0][1] +                dp_cycle[cur_clause][0][1][0] + dp_cycle[cur_clause][0][0][1]) %               1000000007;           dp_temp[0][1] =               (dp_cycle[cur_clause][1][1][1] + dp_cycle[cur_clause][1][0][1] +                dp_cycle[cur_clause][0][1][0] + dp_cycle[cur_clause][0][0][0]) %               1000000007;           dp_temp[0][0] =               (dp_cycle[cur_clause][1][1][0] + dp_cycle[cur_clause][1][0][0] +                dp_cycle[cur_clause][0][1][1] + dp_cycle[cur_clause][0][0][1]) %               1000000007;         }         if ((ans[0] == 0) && (ans[1] == 0)) {           ans[0] = (dp_temp[0][0] + dp_temp[1][0]) % 1000000007;           ans[1] = (dp_temp[0][1] + dp_temp[1][1]) % 1000000007;         } else {           temp[0] = ((ans[1] * ((dp_temp[0][1] + dp_temp[1][1]) % 1000000007)) %                          1000000007 +                      (ans[0] * ((dp_temp[0][0] + dp_temp[1][0]) % 1000000007)) %                          1000000007) %                     1000000007;           temp[1] = ((ans[1] * ((dp_temp[0][0] + dp_temp[1][0]) % 1000000007)) %                          1000000007 +                      (ans[0] * ((dp_temp[0][1] + dp_temp[1][1]) % 1000000007)) %                          1000000007) %                     1000000007;           ans[0] = temp[0];           ans[1] = temp[1];         }         return;       }       if ((long long)variables[variable][0] * (long long)variables[variable][1] >           0) {         for (long i = 0; i <= 1; i++) {           dp_cycle[nxt_clause][i][0][1] =               (dp_cycle[cur_clause][i][0][1] + dp_cycle[cur_clause][i][1][1]) %               1000000007;           dp_cycle[nxt_clause][i][0][0] =               (dp_cycle[cur_clause][i][0][0] + dp_cycle[cur_clause][i][1][0]) %               1000000007;           dp_cycle[nxt_clause][i][1][1] =               (dp_cycle[cur_clause][i][0][1] + dp_cycle[cur_clause][i][1][0]) %               1000000007;           dp_cycle[nxt_clause][i][1][0] =               (dp_cycle[cur_clause][i][0][0] + dp_cycle[cur_clause][i][1][1]) %               1000000007;         }       } else {         for (long i = 0; i <= 1; i++) {           dp_cycle[nxt_clause][i][1][1] =               (dp_cycle[cur_clause][i][0][0] + dp_cycle[cur_clause][i][1][0]) %               1000000007;           dp_cycle[nxt_clause][i][1][0] =               (dp_cycle[cur_clause][i][0][1] + dp_cycle[cur_clause][i][1][1]) %               1000000007;           dp_cycle[nxt_clause][i][0][1] =               (dp_cycle[cur_clause][i][0][0] + dp_cycle[cur_clause][i][1][1]) %               1000000007;           dp_cycle[nxt_clause][i][0][0] =               (dp_cycle[cur_clause][i][0][1] + dp_cycle[cur_clause][i][1][0]) %               1000000007;         }       }       cur_clause = nxt_clause;     }   }   int main() {     cin >> n >> m;     for (long i = 1; i <= n; i++) {       long k;       cin >> k;       clauses[i].resize(k);       for (long j = 0; j < k; j++) {         cin >> clauses[i][j];         if (clauses[i][j] < 0)           variables[-clauses[i][j]].push_back(-i);         else           variables[clauses[i][j]].push_back(i);       }     }     for (long i = 1; i <= n; i++) {       path_component(i);     }     for (long i = 1; i <= n; i++) {       cycle_component(i);     }     for (long i = 1; i <= m; i++) {       if (variables[i].size() == 0) {         ans[0] = (ans[0] * 2) % 1000000007;         ans[1] = (ans[1] * 2) % 1000000007;       }     }     ans[0] = ans[0] % 1000000007;     ans[1] = ans[1] % 1000000007;     cout << ans[1] << endl;     return 0;   }


#include <bits/stdc++.h>   int n, h;   int dynamics[1001][2][31][31][31];   int progress(int a) { return (a == 0 || a == h - 1) ? 0 : a + 1; }   inline void add(int& a, int b, int mod) { a = (a + b) % mod; }   int main(int argc, char* argv[]) {     std::cin >> n >> h;     if (h == 1)       dynamics[1][1][0][0][0] = 4;     else       dynamics[1][1][1][1][1] = 4;     for (int i = 1; i < n; i++) {       for (int alive = 0; alive < 2; alive++) {         for (int left = 0; left < h; left++) {           for (int right = 0; right < h; right++) {             for (int front = 0; front < h; front++) {               int current = dynamics[i][alive][left][right][front];               int next_left = progress(left);               int next_right = progress(right);               int next_front = progress(front);               add(dynamics[i + 1][alive][next_left][next_right][next_front],                   current, 1e9 + 9);               add(dynamics[i + 1][left > 0][alive][next_right][next_front],                   current, 1e9 + 9);               add(dynamics[i + 1][right > 0][alive][next_left][next_front],                   current, 1e9 + 9);               add(dynamics[i + 1][front > 0][alive][next_left][next_right],                   current, 1e9 + 9);             }           }         }       }     }     int result = 0;     for (int alive = 0; alive < 2; alive++) {       for (int left = 0; left < h; left++) {         for (int right = 0; right < h; right++) {           for (int front = 0; front < h; front++) {             if (alive > 0 || left > 0 || right > 0 || front > 0) {               add(result, dynamics[n][alive][left][right][front], 1e9 + 9);             }           }         }       }     }     std::cout << result << std::endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000 + 11;   int sdis(int x, int y, int xx, int yy) {     int dx = x - xx;     int dy = y - yy;     return dx * dx + dy * dy;   }   int main() {     int ax, ay, ar, bx, by, br;     while (cin >> ax >> ay >> ar >> bx >> by >> br) {       int ds = sdis(ax, ay, bx, by);       if ((ar + br) * (ar + br) <= ds) {         printf( %.10lf n , (sqrt(ds + 0.0) - ar - br) * 0.5);       } else if (ds <= (ar - br) * (ar - br)) {         printf( %.10lf n , (abs(ar - br) - sqrt(ds + 0.0)) * 0.5);       } else {         printf( %.10lf n , 0.0);       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long INFL = 1LL << 60;   const long long INF = INFL;   long long MOD = 1000000007;   vector<long long> dy = {0, -1, 0, 1, 1, 1, -1, -1, 0};   vector<long long> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0};   void pres(double A, long long x = 10) {     cout << fixed << setprecision(x) << A << endl;   }   void BinarySay(long long x, long long y = 60) {     for (long long i = 0; i < (y); i++) cout << (x >> (y - 1 - i) & 1);     cout << endl;   }   long long get_bit(long long x) { return __builtin_popcountll(x); }   long long pow_long(long long x, long long k) {     long long res = 1;     while (k > 0) {       if (k % 2) res *= x;       x *= x;       k /= 2;     }     return res;   }   long long pow_mod(long long x, long long k) {     long long res = 1;     while (k > 0) {       if (k % 2) {         res *= x;         res %= MOD;       }       x *= x;       x %= MOD;       k /= 2;     }     return res;   }   long long inverse(long long x) { return pow_mod(x, MOD - 2); };   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long x, long long y) { return x / gcd(x, y) * y; };   long long kai_mod(long long x) {     if (x == 0) return 1;     return x * kai_mod(x - 1) % MOD;   }   vector<long long> divisor(long long n) {     vector<long long> res(0);     for (long long i = 1; i * i <= n; i++) {       if (n % i == 0) {         res.push_back(i);         if (i != n / i) res.push_back(n / i);       }     }     sort(res.begin(), res.end());     return res;   }   vector<long long> MakePrimeList(long long x) {     vector<long long> res;     for (long long k = 2; k * k <= x; k++) {       if (x % k == 0) {         res.push_back(k);         while (x % k == 0) x /= k;       }     }     if (x > 1) res.push_back(x);     return res;   }   const int MAXcomb = 2000100;   long long fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];   void COMinit() {     fac[0] = fac[1] = 1;     finv[0] = finv[1] = 1;     inv[1] = 1;     for (int i = 2; i < MAXcomb; i++) {       fac[i] = fac[i - 1] * i % MOD;       inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;       finv[i] = finv[i - 1] * inv[i] % MOD;     }   }   long long comb(int n, int k) {     if (n < k) return 0;     if (n < 0 || k < 0) return 0;     return fac[n] * finv[k] % MOD * finv[n - k] % MOD;   }   long long disit(long long x, long long d = 10) {     long long res = 0;     while (x) {       res++;       x /= d;     }     return res;   }   void compress(vector<long long> &S) {     vector<long long> xs;     for (long long i = 0; i < (S.size()); i++) xs.push_back(S[i]);     sort(xs.begin(), xs.end());     xs.erase(unique(xs.begin(), xs.end()), xs.end());     for (long long i = 0; i < (S.size()); i++) {       S[i] = lower_bound(xs.begin(), xs.end(), S[i]) - xs.begin();     }   }   void solve() {     long long N;     cin >> N;     vector<long long> A(N);     for (long long i = 0; i < (N); i++) cin >> A[i];     compress(A);     long long M = A[0] + 1;     vector<long long> cnt(M, 0);     for (long long i = 0; i < (N); i++) cnt[A[i]]++;     long long g = cnt[M - 1];     long long second = 0;     long long at = M;     for (long long i = (M)-1; i >= 0; i--) {       if (i == M - 1) continue;       second += cnt[i];       at = i;       if (second > g) break;     }     long long B = 0;     long long b = 0;     for (long long i = at - 1; i >= 0; i--) {       b += cnt[i];       if (b <= g) continue;       if ((g + second + b) * 2 > N) break;       B = b;     }     if (B == 0) g = second = 0;     cout << g <<     << second <<     << B << endl;   }   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     long long T;     cin >> T;     for (long long i = 0; i < (T); i++) {       solve();     }   }


#include <bits/stdc++.h>   int main(void) {     int t;     long long int a, b, k, p, s;     scanf( %d , &t);     for (int i = 0; i < t; i++) {       scanf( %lld  , &a);       scanf( %lld  , &b);       scanf( %lld  , &k);       s = k / 2;       if ((k % 2) == 0) {         p = (a - b) * (s);       }       if ((k % 2) == 1) {         p = ((a - b) * (s) + a);       }       printf( %lld  n , p);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   int a[maxn];   int t[maxn], l[maxn], r[maxn];   int n, m, ans;   void solve(int k) {     int tot = 0;     if (k == 1) {       for (int i = 2; i <= n; i++)         if (a[i] < a[i - 1]) tot++;     } else {       for (int i = 2; i <= n; i++) {         int p = i / k;         if (i % k > 1) p++;         if (a[i] < a[p]) tot++;       }     }     printf( %d  , tot);   }   void prepare() {     l[1] = 2, r[1] = m + 1;     for (int i = l[1]; i <= r[1]; i++)       if (a[i] < a[1]) ans++;     for (int i = l[1]; i <= r[1]; i++) {       l[i] = m * (i - 1) + 2;       if (l[i] > n) l[i] = n + 1;       r[i] = m * i + 1;       if (r[i] > n) r[i] = n;       for (int j = l[i]; j <= r[i]; j++)         if (a[j] < a[i]) ans++;     }     printf( %d  , ans);   }   void deal(int k) {     for (int i = 1; i <= m + 1; i++) {       if (l[i] > r[i]) break;       for (int j = 1; j < i; j++)         if (l[i] <= r[i] && a[l[i]++] < a[i]) ans--;       for (int j = 1; j <= i; j++)         if (r[i] < n && a[++r[i]] < a[i]) ans++;     }     printf( %d  , ans);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     m = sqrt(n) + 1;     for (int i = 1; i < m; i++) solve(i);     if (m > n - 1) return 0;     prepare();     for (int i = m + 1; i < n; i++) deal(i);     return 0;   }


#include <bits/stdc++.h>   int scan() {     int noRead = 0;     char p = getchar();     for (; p < 33;) {       p = getchar();     }     while (p > 32) {       noRead = (noRead << 3) + (noRead << 1) + (p -  0 );       p = getchar();     }     return noRead;   }   long long int lscan() {     long long int noRead = 0;     char p = getchar();     for (; p < 33;) {       p = getchar();     }     while (p > 32) {       noRead = (noRead << 3) + (noRead << 1) + (p -  0 );       p = getchar();     }     return noRead;   }   using namespace std;   int dp[501][501];   int vec[501];   int f(int l, int r) {     if (l > r) return 0;     if (l == r) return 1;     if (dp[l][r] != -1) return dp[l][r];     int &ans = dp[l][r];     ans = 100001;     ans = min(ans, 1 + f(l + 1, r));     for (int i = l + 2; i <= r; ++i) {       if (vec[l] == vec[i]) {         ans = min(ans, f(l + 1, i - 1) + f(i + 1, r));       }     }     if (vec[l] == vec[l + 1]) ans = min(ans, 1 + f(l + 2, r));     return ans;   }   int main(void) {     int n = scan();     for (int i = 0; i < n; ++i) vec[i] = scan();     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) dp[i][j] = -1;     printf( %d , f(0, n - 1));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = (int)2E5 + 5;   int n, x, y;   vector<int> Adj[maxN];   int deg[maxN];   bool avail[maxN];   bool marked[maxN];   int Count[maxN];   int DFS(int u) {     marked[u] = true;     Count[u] = 0;     int cnt = 0;     for (int i = 0, sz = Adj[u].size(); i < sz; ++i) {       int v = Adj[u][i];       if (marked[v]) continue;       DFS(v);       Count[u] += Count[v];       if (avail[v]) ++cnt;     }     if (cnt == 0)       avail[u] = true;     else if (cnt == 1)       avail[u] = true, ++Count[u];     else       avail[u] = false, Count[u] += 2;     return Count[u];   }   int main() {     scanf( %d %d %d , &n, &x, &y);     for (int u = 1; u <= n; ++u) Adj[u].clear();     memset(deg, 0, sizeof(deg));     for (int i = 0; i < n - 1; ++i) {       int u, v;       scanf( %d %d , &u, &v);       Adj[u].push_back(v);       Adj[v].push_back(u);       ++deg[u];       ++deg[v];     }     if (x >= y) {       for (int u = 1; u <= n; ++u)         if (deg[u] == n - 1) {           cout << (long long)(n - 2) * y + x << endl;           return 0;         }       cout << (long long)(n - 1) * y << endl;       return 0;     }     DFS(1);     memset(marked, false, sizeof(marked));     cout << (long long)Count[1] * x + (long long)(n - 1 - Count[1]) * y << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, l, r, deg[100005], num;   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i < n; i++) {       scanf( %d%d , &l, &r);       deg[l]++;       deg[r]++;     }     for (int i = 1; i <= n; i++)       if (deg[i] == 1) num++;     printf( %.7f n , (double)2.0 * m / num);   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 500005;   const long long inf = 0x3f3f3f3f3f3f3f3f;   const long long MOD = 100000007;   const double eps = 1e-10;   long long qpow(long long a, long long b) {     long long tmp = a % MOD, ans = 1;     while (b) {       if (b & 1) {         ans *= tmp, ans %= MOD;       }       tmp *= tmp, tmp %= MOD, b >>= 1;     }     return ans;   }   long long lowbit(long long x) { return x & -x; }   long long max(long long a, long long b) { return a > b ? a : b; }   long long min(long long a, long long b) { return a < b ? a : b; }   long long mmax(long long a, long long b, long long c) {     return max(a, max(b, c));   }   long long mmin(long long a, long long b, long long c) {     return min(a, min(b, c));   }   void mod(long long &a) {     a += MOD;     a %= MOD;   }   long long ll(long long p) { return p << 1; }   long long rr(long long p) { return p << 1 | 1; }   long long mm(long long l, long long r) { return (l + r) / 2; }   long long lg(long long x) {     if (x == 0) return 1;     return (long long)log2(x) + 1;   }   bool smleql(double a, double b) {     if (a < b || fabs(a - b) <= eps) return true;     return false;   }   double len(double a, double b, double c, double d) {     return sqrt((a - c) * (a - c) + (b - d) * (b - d));   }   bool isp(long long x) {     if (x == 1) return false;     if (x == 2) return true;     for (long long i = 2; i * i <= x; ++i)       if (x % i == 0) return false;     return true;   }   inline long long read() {     char ch = getchar();     long long s = 0, w = 1;     while (ch < 48 || ch > 57) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (ch >= 48 && ch <= 57) {       s = (s << 1) + (s << 3) + ch - 48;       ch = getchar();     }     return s * w;   }   inline void write(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) write(x / 10);     putchar(x % 10 + 48);   }   long long n, k, a[maxn], sum = 0;   deque<long long> q;   long long chk(long long now) {     long long res = 0;     for (long long i = 1; i <= n; ++i) res += min(now, a[i]);     return res;   }   long long half(long long l, long long r) {     while (l <= r) {       long long m = (l + r) / 2;       if (chk(m) <= k)         l = m + 1;       else         r = m - 1;     }     return r;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     cin >> n >> k;     long long mx = -inf;     for (long long i = 1; i <= n; ++i)       cin >> a[i], mx = max(mx, a[i]), sum += a[i];     if (sum < k)       cout << -1;     else if (sum == k)       ;     else {       long long t = half(1, mx);       k -= chk(t);       for (long long i = 1; i <= n; ++i) {         a[i] -= t;         if (a[i] > 0) q.push_back(i);       }       while (k--) {         long long f = q.front();         q.pop_front();         a[f]--;         if (a[f] > 0) q.push_back(f);       }       for (long long i = 0; i < q.size(); ++i) cout << q[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[100005], dp[100005][5];   bool solve(int i, int j) {     if (i == n - 1) return true;     if (dp[i][j] == -1) {       bool ans = false;       if (a[i] < a[i + 1])         for (int k = j + 1; k < 5; k++) ans |= solve(i + 1, k);       else if (a[i] > a[i + 1])         for (int k = 0; k < j; k++) ans |= solve(i + 1, k);       else         for (int k = 0; k < 5; k++)           if (j != k) ans |= solve(i + 1, k);       dp[i][j] = ans;     }     return dp[i][j];   }   void print(int i, int j) {     cout << j + 1 <<    ;     if (i == n - 1) {       cout << endl;       return;     }     if (a[i] < a[i + 1]) {       for (int k = j + 1; k < 5; k++) {         if (solve(i + 1, k)) {           print(i + 1, k);           return;         }       }     } else if (a[i] > a[i + 1]) {       for (int k = 0; k < j; k++) {         if (solve(i + 1, k)) {           print(i + 1, k);           return;         }       }     } else {       for (int k = 0; k < 5; k++) {         if (j != k && solve(i + 1, k)) {           print(i + 1, k);           return;         }       }     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     memset(dp, -1, sizeof(dp));     for (int j = 0; j < 5; j++) {       if (solve(0, j)) {         print(0, j);         return 0;       }     }     cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long k, b, n;     long long z = 1, t;     cin >> k >> b >> n >> t;     for (int i = 0; i < n; i++) {       if (z > t) {         cout << n - i + 1 << endl;         return 0;       }       z = z * k + b;     }     if (z <= t)       cout << 0 << endl;     else       cout << 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k, r, r2;     cin >> k;     if (k % 2 == 0)       cout << 4 <<     << k - 4;     else       cout << k - 9 <<     << 9;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = (long long)(1e9 + 17);   const long long mod = (long long)(1e9 + 7);   int n, k;   string s, t;   int dp[201][201][201];   int recurse(int idx, int tot, int ct) {     if (tot > k) {       return -inf;     }     if (idx == n) {       if (tot > k) return -inf;       return 0;     }     int &ans = dp[idx][tot][ct];     if (ans != -1) {       return ans;     }     ans = -inf;     if (s[idx] != t[1] or s[idx] != t[0])       ans = max(ans, recurse(idx + 1, tot, ct));     ans = max(ans, recurse(idx + 1, tot + (s[idx] != t[0]), ct + 1));     ans = max(ans,               ct + recurse(idx + 1, tot + (s[idx] != t[1]), ct + (t[0] == t[1])));     return ans;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> k;     cin >> s >> t;     memset(dp, -1, sizeof dp);     int ans = recurse(0, 0, 0);     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long limit = 1e5 + 1;   long long res = 0, ans = 1e17, sum = 0;   long long a[limit], cost[limit];   long long n;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (long long i = 1; i <= n; i++) cin >> a[i] >> cost[i];     for (long long i = 1; i <= n; i++) {       ans = min(cost[i], ans);       cost[i] = ans;     }     for (long long i = 1; i <= n; i++) res += a[i] * cost[i];     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   int get() {     char ch;     while (ch = getchar(), (ch <  0  || ch >  9 ) && ch !=  - )       ;     if (ch ==  - ) {       int s = 0;       while (ch = getchar(), ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 ;       return -s;     }     int s = ch -  0 ;     while (ch = getchar(), ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 ;     return s;   }   const int N = 3205;   const int INF = 1e9;   int n;   char rig[N], lef[N];   int lenr, lenl;   int f[2005][N];   pair<int, int> lst[2005][N];   int sa[N], y[N];   int rk[N];   int a[N], b[N], cnt[N];   int all[N];   int to[N][10];   int nxt[N];   int main() {     scanf( %s , lef + 1);     scanf( %s , rig + 1);     n = get();     lenl = strlen(lef + 1);     lenr = strlen(rig + 1);     int w = lenl;     for (; w && lef[lenl] ==  0 ;) lef[w--] =  9 ;     lef[w]--;     if (lef[1] ==  0 ) {       for (int i = 1; i <= lenl - 1; i++) lef[i] = lef[i + 1];       lenl--;     }     if (lenl == 0) lenl = 1, lef[1] =  0 ;     int m = 0;     for (int i = 1; i <= lenl; i++) all[y[++m] = i] = lef[i] -  0 ;     for (int i = 1; i <= lenr; i++) all[y[++m] = i + lenl + lenr] = rig[i] -  0 ;     for (int i = lenl + 1; i <= lenl + lenr; i++) all[i] = 10, a[i] = b[i] = INF;     for (int i = 1; i <= lenr; i++)       all[i + lenl + lenr * 2] = 10,                             a[i + lenl + lenr * 2] = b[i + lenl + lenr * 2] = INF;     for (int i = 1; i <= m; i++) cnt[all[y[i]]]++;     for (int i = 1; i <= 9; i++) cnt[i] += cnt[i - 1];     for (int i = 1; i <= m; i++) sa[cnt[all[y[i]]]--] = y[i];     int k = 1;     a[sa[1]] = 1;     for (int i = 2; i <= m; i++) {       if (all[sa[i]] > all[sa[i - 1]]) k++;       a[sa[i]] = k;     }     for (int j = 1; j <= max(lenl, lenr); j <<= 1) {       int u = 0;       for (int i = 1; i <= m; i++)         if (sa[i] > j && all[sa[i] - j] < 10) y[++u] = sa[i] - j;       for (int i = 1; i <= m; i++)         if (all[sa[i] + j] == 10) y[++u] = sa[i];       for (int i = 0; i <= m; i++) cnt[i] = 0;       for (int i = 1; i <= m; i++) cnt[a[y[i]]]++;       for (int i = 1; i <= m; i++) cnt[i] += cnt[i - 1];       for (int i = m; i >= 1; i--) sa[cnt[a[y[i]]]--] = y[i];       for (int i = 1; i <= m; i++) b[sa[i]] = a[sa[i]];       a[sa[1]] = k = 1;       for (int i = 2; i <= m; i++) {         if (b[sa[i]] > b[sa[i - 1]] ||             (b[sa[i]] == b[sa[i - 1]] && b[sa[i] + j] > b[sa[i - 1] + j]))           k++;         a[sa[i]] = k;       }     }     int wl, wr;     for (int i = 1; i <= m; i++) {       nxt[sa[i]] = sa[i] + 1;       if (sa[i] == 1) wl = i;       if (sa[i] == lenl + lenr + 1) wr = i;       if (sa[i] == lenl) nxt[sa[i]] = 0;       if (sa[i] == lenl + lenr + lenr) nxt[sa[i]] = 0;       rk[sa[i]] = i;     }     rk[0] = m + 1;     for (int i = 1; i <= m + 1; i++) {       int x = sa[i];       for (int c = 0; c <= 9; c++) {         int y = m + 1;         int l = 1, r = m;         for (; l <= r;) {           int mid = (l + r) / 2;           int u = sa[mid];           if (all[u] > c || (all[u] == c && rk[nxt[u]] >= i))             y = mid, r = mid - 1;           else             l = mid + 1;         }         to[i][c] = y;       }     }     for (int i = 0; i <= n + 1; i++)       for (int j = 1; j <= m + 1; j++) f[i][j] = -INF;     f[n + 1][m + 1] = 0;     for (int i = n + 1; i >= 2; i--)       for (int j = 1; j <= m + 1; j++)         if (f[i][j] != -INF) {           for (int c = 0; c <= 9; c++) {             int p = to[j][c];             int len = n - i + 2;             int ad = max(0, min(len + 1, lenr) - lenl - 1);             if (c != 0) {               if (lenl == lenr) {                 if (len >= lenl && p > wl && p <= wr) ad = 1;               } else {                 if (len >= lenl && p > wl) ad++;                 if (len >= lenr && p <= wr) ad++;               }             } else               ad = 0;             if (f[i][j] + ad > f[i - 1][p] ||                 (f[i][j] + ad == f[i - 1][p] &&                  (c < lst[i - 1][p].first ||                   (c == lst[i - 1][p].first && j < lst[i - 1][p].second)))) {               f[i - 1][p] = f[i][j] + ad;               lst[i - 1][p] = make_pair(c, j);             }           }         }     w = 1;     for (int i = 2; i <= m + 1; i++)       if (f[1][w] < f[1][i]) w = i;     printf( %d n , f[1][w]);     int pw = w;     w = pw;     for (int i = 1; i <= n; i++) {       putchar( 0  + lst[i][w].first);       w = lst[i][w].second;     }     putchar(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, s1, s2;   int x[100001];   bool C(int v) {     if (abs(s1 - s2) > v) return false;     set<int> st;     st.insert(s1);     int pos = s2;     for (int i = 0; i < n; i++) {       if (st.size()) {         st.insert(pos);       }       while (st.size() && abs(*st.begin() - x[i]) > v) {         st.erase(*st.begin());       }       while (st.size() && abs(*st.rbegin() - x[i]) > v) {         st.erase(*st.rbegin());       }       pos = x[i];     }     if (st.empty()) return false;     return true;   }   int main(void) {     scanf( %d%d%d , &n, &s1, &s2);     for (int i = 0; i < n; i++) {       scanf( %d , &x[i]);     }     int l = -1, r = 1000000007;     while (l + 1 < r) {       int mid = (l + r) / 2;       if (C(mid))         r = mid;       else         l = mid;     }     printf( %d n , r);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void chkmax(T& x, U y) {     if (x < y) x = y;   }   template <typename T, typename U>   inline void chkmin(T& x, U y) {     if (y < x) x = y;   }   const int MAXN = 110000;   int n, m, N;   int xn;   int ans[MAXN], c[MAXN], a[MAXN], b[MAXN], id[MAXN], x[MAXN];   int qid[MAXN], L[MAXN], R[MAXN], B[MAXN], ret[MAXN];   int dp[1 << 18];   void add(int u) {     for (; u <= xn; u += u & -u) c[u]++;   }   int get(int u) {     int ret = 0;     for (; u; u -= u & -u) ret += c[u];     return ret;   }   bool cmp(int u, int v) { return b[u] > b[v]; }   bool cmp1(int u, int v) { return B[u] > B[v]; }   int mx[MAXN << 2];   void pushup(int u) { mx[u] = max(mx[(u << 1)], mx[(u << 1 | 1)]); }   void update(int u, int l, int r, int pos, int x) {     if (l == r) {       mx[u] = max(mx[u], x);       return;     }     int mid = l + r >> 1;     if (pos <= mid)       update((u << 1), l, mid, pos, x);     else       update((u << 1 | 1), mid + 1, r, pos, x);     pushup(u);   }   int query(int u, int l, int r, int ql, int qr) {     if (ql > qr) return 0;     if (ql <= l && r <= qr) {       return mx[u];     }     int mid = l + r >> 1;     if (qr <= mid)       return query((u << 1), l, mid, ql, qr);     else if (ql > mid)       return query((u << 1 | 1), mid + 1, r, ql, qr);     else       return max(query((u << 1), l, mid, ql, qr),                  query((u << 1 | 1), mid + 1, r, ql, qr));   }   int tag[MAXN];   void work() {     xn = 0;     N = 0;     n = 0;     m = 0;     memset(ans, 0, sizeof(ans));     memset(c, 0, sizeof(c));     memset(a, 0, sizeof(a));     memset(b, 0, sizeof(b));     memset(id, 0, sizeof(id));     memset(x, 0, sizeof(x));     memset(qid, 0, sizeof(qid));     memset(L, 0, sizeof(L));     memset(R, 0, sizeof(R));     memset(B, 0, sizeof(B));     memset(ret, 0, sizeof(ret));     memset(dp, 0, sizeof(dp));     memset(mx, 0, sizeof(mx));     scanf( %d%d , &n, &m);     for (register int i = 1; i <= n; i++) scanf( %d , &b[i]);     for (register int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (register int i = 1; i <= n; i++) id[i] = i;     for (register int i = 1; i <= n; i++) x[xn++] = a[i];     sort(x, x + xn);     xn = unique(x, x + xn) - x;     for (register int i = 1; i <= n; i++) a[i] = lower_bound(x, x + xn, a[i]) - x;     sort(id + 1, id + n + 1, cmp);     for (register int i = n, j; i > 0; i = j) {       for (j = i; j > 0 && b[id[i]] == b[id[j]]; j--) add(a[id[j]] + 1);       for (register int k = i; k > j; k--) {         int u = id[k];         int r = upper_bound(x, x + xn, x[a[u]] + m) - x;         int l = lower_bound(x, x + xn, x[a[u]] - m) - x;         ans[u] = get(r) - get(l);       }     }     int q;     scanf( %d , &q);     for (register int i = 1; i <= q; i++) {       int u, v;       scanf( %d%d , &u, &v);       L[i] = lower_bound(x, x + xn, max(x[a[u]], x[a[v]]) - m) - x + 1;       R[i] = upper_bound(x, x + xn, min(x[a[u]], x[a[v]]) + m) - x;       B[i] = max(b[u], b[v]);       qid[i] = i;     }     sort(qid + 1, qid + q + 1, cmp1);     for (register int i = 1, j = 1; i <= q; i++) {       int u = qid[i];       for (; j <= n && b[id[j]] >= B[u]; j++)         update(1, 1, xn, a[id[j]] + 1, ans[id[j]]);       ret[u] = query(1, 1, xn, L[u], R[u]);     }     for (register int i = 1; i <= q; i++) {       if (!ret[i]) ret[i] = -1;       printf( %d n , ret[i]);     }     return;   }   int main() {     int T = 1;     while (T--) work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500010;   const int LOG = 20;   const long long INF = (1ll << 60);   int n;   int a[N];   vector<int> adj[N];   int par[LOG][N];   void dfs(int u, int p) {     par[0][u] = p;     for (int i = 1; i < LOG; i++) {       par[i][u] = par[i - 1][par[i - 1][u]];     }     for (int v : adj[u]) {       if (v != p) dfs(v, u);     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     int root = -1;     for (int i = 1; i <= n; i++) {       cin >> a[i];       if (root == -1 || a[root] > a[i]) root = i;     }     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     dfs(root, 0);     long long res = 0;     for (int i = 1; i <= n; i++) {       if (i == root) continue;       long long foo = INF;       for (int j = 0; j < LOG; j++) {         int u = par[j][i];         if (u == 0) u = root;         foo = min(foo, 1ll * a[u] * (j + 1));         if (u == root) break;       }       res += foo;       res += a[i];     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using uint = unsigned int;   using ull = unsigned long long;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   double startTime;   double gct() { return ((double)clock() - startTime) / CLOCKS_PER_SEC; }   void suraj() {     int n, m;     cin >> n >> m;     vector<int> ans;     if (m % 2 == 1) {       int ref = m / 2;       int ptr = 1;       ans.push_back(ref);       while (ptr <= ref) {         ans.push_back(ref - ptr);         ans.push_back(ref + ptr);         ptr++;       }     } else {       int ref = m / 2 - 1;       int ptr = 1;       ans.push_back(ref);       while (ptr <= ref) {         ans.push_back(ref + ptr);         ans.push_back(ref - ptr);         ptr++;       }       ans.push_back(ref + ptr);     }     for (int i = 0; i < n; i++) {       cout << ans[i % m] + 1 <<   n ;     }   }   void suraj2() {}   int main() {     startTime = (double)clock();     suraj();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 400, D = 8;   int vis[N][N];   int T[30];   bool used[N][N][D][6][31];   struct obj {     int x, y, d, t, i;     void move() {       if (d == 0 || d == 1 || d == 7) x++;       if (d == 5 || d == 4 || d == 3) x--;       if (d == 1 || d == 2 || d == 3) y++;       if (d == 5 || d == 6 || d == 7) y--;       t--;     }     obj splitL() { return {x, y, (d + 1) % D, T[i + 1], i + 1}; }     obj splitR() { return {x, y, (d + D - 1) % D, T[i + 1], i + 1}; }     bool check(bool set = false) {       if (set) used[x][y][d][t][i] = true;       return used[x][y][d][t][i];     }   };   queue<obj> q;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> T[i];     q.push({N / 2, N / 2, 0, T[0] - 1, 0});     while (not q.empty()) {       auto curr = q.front();       q.pop();       if (curr.check()) continue;       curr.check(true);       vis[curr.x][curr.y] = 1;       if (curr.t == 0) {         if (curr.i == n) continue;         q.push(curr.splitL());         q.push(curr.splitR());         continue;       }       curr.move();       q.push(curr);     }     int ans = 0;     for (int x = 0; x < N; x++) {       for (int y = 0; y < N; y++) {         ans += vis[x][y];       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long int;   using vi = vector<int>;   const ll INF = 1e9 + 7;   void solution() {     int n;     cin >> n;     if (n == 1) {       cout <<  -1 n ;       return;     } else {       cout <<  2 ;       n -= 1;       while (n--) {         cout <<  3 ;       }     }     cout << endl;   }   int main() {     int test;     cin >> test;     while (test--) {       solution();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005, logN = 18;   int n, q, l, r, tim;   struct vec {     int x, y, num;     bool operator<(const vec &A) const { return x < A.x || x == A.x && y > A.y; }   } a[N];   inline long long calc(vec *a) { return a->x + 1LL * tim * a->y; }   inline long long cross(vec &a, vec &b, vec &c) {     return (long long)(b.x - a.x) * (c.y - a.y) -            (long long)(b.y - a.y) * (c.x - a.x);   }   vec mem[N * logN * 2], *newmem = mem;   struct data {     vec *st, *en;   } t[N * 4];   void build(int i, int beg, int end) {     t[i].st = newmem;     if (end - beg == 1) {       scanf( %d%d , &t[i].st->x, &t[i].st->y);       t[i].st->num = end;       newmem++;       t[i].en = newmem;       return;     }     int mid = beg + end >> 1;     build(i << 1, beg, mid);     build(i << 1 | 1, mid, end);     t[i].st = t[i].en = newmem;     for (vec *l = t[i << 1].st, *r = t[i << 1 | 1].st;          l < t[i << 1].en || r < t[i << 1 | 1].en;)       if (l < t[i << 1].en && (r == t[i << 1 | 1].en || *l < *r)) {         while (t[i].en - t[i].st > 1 && cross(t[i].en[-2], t[i].en[-1], *l) >= 0)           t[i].en--;         *t[i].en++ = *l++;       } else {         while (t[i].en - t[i].st > 1 && cross(t[i].en[-2], t[i].en[-1], *r) >= 0)           t[i].en--;         *t[i].en++ = *r++;       }     newmem = t[i].en;   }   vec *query(int i, int beg, int end) {     if (r <= beg || l >= end) return a;     if (l <= beg && r >= end) {       vec *a = t[i].st, *b = t[i].en - 1;       long long fa = calc(a), fb = calc(b);       while (b - a + 1 >= 3) {         vec *mid1 = a + (b - a + 1) / 3, *mid2 = b - (b - a + 1) / 3;         long long f1 = calc(mid1), f2 = calc(mid2);         if (fa <= f1 && f1 <= f2)           a = mid1, fa = f1;         else if (fb <= f2 && f2 <= f1)           b = mid2, fb = f2;         else           puts( Error );       }       vec *res = b;       for (; a < b; a++)         if (calc(res) < calc(a)) res = a;       return res;     }     int mid = beg + end >> 1;     vec *a = query(i << 1, beg, mid), *b = query(i << 1 | 1, mid, end);     return calc(a) > calc(b) ? a : b;   }   int main() {     scanf( %d%d , &n, &q);     build(1, 0, n);     while (q--) {       scanf( %d%d%d , &l, &r, &tim);       l--;       printf( %d n , query(1, 0, n)->num);     }   }


#include <bits/stdc++.h>   using namespace std;   int ans = 1000000005, rans = 0, bans = 0;   int numr[20], numb[20], color[20];   int dp[70005][160];   bool life[70005][160];   int nowr, nowb, tempr, tempb;   char s[6];   int nex[20];   void ok(int x) {     int now;     nowr = nowb = now = 0;     while (x) {       if (x % 2) {         if (color[now])           nowb++;         else           nowr++;       }       x /= 2;       now++;     }   }   int main() {     int n;     cin >> n;     int maxx = 1 << n, maxxx = n * (1 + n) / 2;     nex[0] = 1;     life[0][0] = 1;     for (int i = 1; i < n; i++) nex[i] = nex[i - 1] * 2;     for (int i = 0; i < n; i++) {       scanf( %s , s);       if (s[0] ==  R )         color[i] = 0;       else         color[i] = 1;       scanf( %d%d , numr + i, numb + i);       rans += numr[i];       bans += numb[i];     }     for (int i = 0; i < maxx; i++) {       ok(i);       for (int j = 0; j < n; j++) {         if ((nex[j] & i) == 0) {           tempr = min(nowr, numr[j]);           tempb = min(nowb, numb[j]);           for (int k = 0; k <= maxxx; k++)             if (life[i][k]) {               dp[i + nex[j]][k + tempr] =                   max(dp[i + nex[j]][k + tempr], dp[i][k] + tempb);               life[i + nex[j]][k + tempr] = 1;             }         }       }     }     for (int i = 0; i < maxxx; i++)       if (life[maxx - 1][i]) {         ans = min(ans, max(rans - i, bans - dp[maxx - 1][i]));       }     cout << ans + n << endl;     return 0;   }


#include<bits/stdc++.h>   using namespace std;      typedef long double ld;   typedef long long int ll;   typedef pair<int, int> pi;   typedef pair<long long, long long> pll;      #define endl   n    #define ff first   #define ss second   #define pb push_back   #define int long long   #define sz(v) (int)v.size()   #define inf 2147483647   #define llinf 9223372036854775807   #define all(v) v.begin(),v.end()   #define bp(n) __builtin_popcountll(n)   #define f(i,l,r) for(long long i=l;i<=r;i++)   #define rf(i,r,l) for(long long i=r;i>=l;i--)   #define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)      template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os <<  { ; string sep; for (const auto &x : v) os << sep << x, sep =  ,  ; return os <<  } ; }   template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os <<  { ; string sep; for (const auto &x : arr) os << sep << x, sep =  ,  ; return os <<  } ; }   template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os <<  (  << p.first <<  ,   << p.second <<  ) ; }      void dbg_out() { cout << endl; }   template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout <<     << H; dbg_out(T...); }         const int N = 3e5 + 5, mod = 1e9 + 7, bit = 61;      mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());      int getRand(int l, int r)   {    uniform_int_distribution<int> uid(l, r);    return uid(rng);   }            signed main()   {    fast;         #ifndef ONLINE_JUDGE    freopen( input.txt ,  r , stdin);    freopen( output.txt ,  w , stdout);   #endif       int t = 1;    cin >> t;    f(tc, 1, t)    {     int n, m, x;     cin >> n >> m >> x;     int r = (x % n);     if (r == 0)     {      r = n;     }     int c = ((x - r) / (n)) + 1;     cout << (r - 1) * m + c << endl;    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9;   const int N = 55;   int n, cnt[N];   vector<pair<int, int> > adj[N];   void dfs(int u, int p, int depth, vector<pair<int, int> > &leaves) {     bool leaf = 1;     for (auto e : adj[u]) {       int v, c;       tie(v, c) = e;       if (v == p) continue;       dfs(v, u, depth + c, leaves);       leaf = 0;     }     if (leaf) leaves.emplace_back(u, depth);   }   int dp[N][N], best[N][N];   int go(int root, int pre, int total, int put) {     vector<pair<int, int> > leaves;     dfs(root, pre, 0, leaves);     int sz = ((int)(leaves).size());     for (int from = (int)(sz + 1) - 1; from >= ((int)0); from--) {       for (int left = (int)(0); left < ((int)put + 1); left++) {         if (from == sz) {           best[from][left] = (left ? -INF : INF);         } else {           best[from][left] = best[from + 1][left];           int l = leaves[from].first, cost = leaves[from].second;           for (int pick = 1; pick <= left; pick++) {             best[from][left] =                 max(best[from][left],                     min(cost + dp[l][total - pick], best[from + 1][left - pick]));           }         }       }     }     return best[0][put];   }   void dfsSize(int u, int p) {     for (auto e : adj[u]) {       int v, c;       tie(v, c) = e;       if (v == p) continue;       dfsSize(v, u);       cnt[u] += cnt[v];     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int _ = 0; _ < (int)(n - 1); _++) {       int u, v, c;       cin >> u >> v >> c;       u--;       v--;       adj[u].emplace_back(v, c);       adj[v].emplace_back(u, c);     }     int s;     cin >> s;     s--;     int m;     cin >> m;     for (int _ = 0; _ < (int)(m); _++) {       int u;       cin >> u;       cnt[u - 1]++;     }     for (int total = 1; total <= m; total++) {       for (int u = 0; u < (int)(n); u++) {         dp[u][total] = go(u, u, total, total);       }     }     dfsSize(s, -1);     int ans = INF;     for (auto e : adj[s]) {       int v, c;       tie(v, c) = e;       ans = min(ans, c + go(v, s, m, cnt[v]));     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using i32 = int32_t;   using i64 = int64_t;   using pii = std::pair<i32, i32>;   using pll = std::pair<i64, i64>;   int dp[5000];   int iv[5000];   bool used[5000];   int div_up(int a, int b) { return (a + b - 1) / b; }   void log_pour(int n, int s, int d) {     if (n > 0) {       printf( %d %d %d n , n, s + 1, d + 1);     }   }   int main() {     int n, k, v;     scanf( %d%d%d , &n, &k, &v);     for (int i = 0; i < k; i++) {       dp[i] = -1;     }     int vmax = 0;     deque<int> q;     for (int i = 0; i < n; i++) {       scanf( %d , &iv[i]);       if (iv[i] == 0) continue;       auto v_mod = iv[i] % k;       if (dp[v_mod] < 0 || i < dp[v_mod]) {         dp[v_mod] = i;         q.push_back(v_mod);       }       vmax += iv[i];     }     if (vmax < v) {       printf( NO n );       return 0;     }     if (v == 0) {       printf( YES n );       log_pour(div_up(iv[0], k), 0, 1);       return 0;     }     if (v % k == 0) {       printf( YES n );       for (int i = 1; i < n; i++) {         log_pour(div_up(iv[i], k), i, 0);       }       log_pour(v / k, 0, 1);       return 0;     }     while (!q.empty()) {       auto v_mod = q.front();       q.pop_front();       assert(dp[v_mod] >= 0);       for (int i = dp[v_mod] + 1; i < n; i++) {         if (iv[i] == 0) continue;         auto v2 = (v_mod + iv[i]) % k;         if (dp[v2] < 0 || i < dp[v2]) {           dp[v2] = i;           q.push_back(v2);         }       }     }     if (dp[v % k] < 0) {       printf( NO n );       return 0;     }     printf( YES n );     int v0 = 0;     int r = v % k;     int j0 = dp[r];     do {       assert(dp[r] >= 0);       used[dp[r]] = true;       v0 += iv[dp[r]];       if (dp[r] != j0) {         log_pour(div_up(iv[dp[r]], k), dp[r], j0);       }       r = (k + r - iv[dp[r]] % k) % k;     } while (v0 % k != v % k);     for (int j = dp[j0]; v0 % k != v % k; j = dp[j]) {       used[j] = true;       v0 += iv[j];       log_pour(div_up(iv[j], k), dp[j], j0);     }     assert(v0 % k == v % k);     int j1 = -1;     int v1 = 0;     for (int i = 0; i < n; i++) {       if (used[i]) continue;       v1 += iv[i];       if (j1 < 0) {         j1 = i;       } else {         log_pour(div_up(iv[i], k), i, j1);       }     }     if (v0 > v) {       log_pour((v0 - v) / k, j0, (j0 + 1) % n);     } else if (v0 < v) {       assert(j1 >= 0);       log_pour((v - v0) / k, j1, j0);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int id[110001];   int ans[1000001];   int main() {     int n, m, i, number, t;     while (scanf( %d %d , &n, &m) != EOF) {       number = 0;       memset(id, 0, sizeof(id));       memset(ans, 0, sizeof(ans));       for (i = 0; i < n; i++) {         scanf( %d , &t);         if (!i)           id[number++] = t;         else if (id[number - 1] != t)           id[number++] = t;       }       for (i = 0; i < number; i++) {         if (i == 0 || i == number - 1)           ans[id[i]]++;         else if (id[i - 1] == id[i + 1])           ans[id[i]] += 2;         else           ans[id[i]] += 1;       }       int sum = 0, h = 0;       for (i = 1; i <= m; i++) {         if (ans[i] > sum) {           sum = ans[i];           h = i;         }       }       printf( %d n , h);     }     return 0;   }