malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, len;     int i = 1;     cin >> n;     cin >> k;     vector<int> a;     while (n > 1) {       if (n % (i + 1) == 0) {         a.push_back(i + 1);         n = n / (i + 1);         i -= 1;       }       i += 1;     }     len = a.size() - k;     if (len >= 0) {       for (int i = 0; i < len; i++) {         a[len + k - i - 2] = a[len + k - i - 1] * a[len + k - i - 2];         a.pop_back();       }       for (int i = 0; i < a.size(); i++) {         cout << a[i] <<    ;       }     } else {       cout << -1;     }   }
#include <bits/stdc++.h>   using namespace std;   long long ar[100010];   unordered_map<long long, long long> um;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t, n, x, ans, y, prev;     scanf( %lld , &t);     long long sm = 0;     while (t--) {       scanf( %lld , &n);       scanf( %lld , &x);       for (int i = 0; i < n; i++) {         scanf( %lld , &ar[i]);       }       sm = 0;       prev = ans = -1;       for (long long i = 0; i < n; i++) {         sm = (sm + ar[i]) % x;         if (sm && prev == -1) prev = i;         if (sm)           ans = i + 1;         else if (prev != -1)           ans = max(ans, i - prev);       }       cout << ans <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   constexpr long long INF = 2e18;   constexpr long long MOD = 1e9 + 7;   constexpr long long MAXN = 2e5 + 3;   struct Matrix {     static constexpr long long MOD = 1e9 + 7;     static constexpr long long MAXN = 200, MAXM = 200;     array<array<long long, MAXM>, MAXN> mat = {};     long long n, m;     Matrix(const long long n, const long long m) : n(n), m(m) {}     static long long mod(long long n) {       n %= MOD;       if (n < 0) n += MOD;       return n;     }     Matrix identity() {       assert(n == m);       Matrix mat_identity(n, m);       for (long long i = 0; i < n; ++i) mat_identity.mat[i][i] = 1;       return mat_identity;     }     Matrix operator*(const Matrix &other) const {       assert(m == other.n);       Matrix ans(n, other.m);       for (long long i = 0; i < n; ++i)         for (long long j = 0; j < m; ++j)           for (long long k = 0; k < m; ++k)             ans.mat[i][j] = mod(ans.mat[i][j] + mat[i][k] * other.mat[k][j]);       return ans;     }     Matrix expo(long long p) {       assert(p >= 0);       Matrix ans = identity(), cur_power(n, m);       cur_power.mat = mat;       while (p) {         if (p & 1) ans = ans * cur_power;         cur_power = cur_power * cur_power;         p >>= 1;       }       return ans;     }   };   void solve() {     long long n;     cin >> n;     long long b, k, x;     cin >> b >> k >> x;     vector<long long> fq(10);     for (long long i = 0; i < n; ++i) {       long long x;       cin >> x;       ++fq[x];     }     Matrix mat(x, x);     for (long long j = 0; j < x; ++j)       for (long long d = 0; d <= 9; ++d) mat.mat[(10 * j + d) % x][j] += fq[d];     mat = mat.expo(b);     Matrix ini(x, 1);     ini.mat[0][0] = 1;     mat = mat * ini;     cout << mat.mat[k].front() <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   inline int Read() {     int x(0);     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     while (c >=  0  && c <=  9 ) x = (x << 3) + (x << 1) + c -  0 , c = getchar();     return x;   }   const int maxn = 1e5 + 9, mod = 998244353, maxm = 409;   int n, k, ans;   int a[maxn], cnt[maxn], lst[maxn], fir[maxn], bl[maxm], br[maxm], col[maxn],       v[maxn], lazy[maxm], f[maxn], sum[maxm][maxn];   inline int G(int x) {     while (x > mod) x -= mod;     return x;   }   inline void Fir() {     int size(sqrt(n)), pieces(ceil(1.0 * n / size));     for (int i = 1; i < pieces; ++i) {       bl[i] = (i - 1) * size + 1;       br[i] = i * size;       for (int j = bl[i]; j <= br[i]; ++j) col[j] = i;     }     bl[pieces] = (pieces - 1) * size + 1;     br[pieces] = n;     for (int j = bl[pieces]; j <= br[pieces]; ++j) col[j] = pieces;     for (int i = 1; i <= n; ++i) {       lst[i] = fir[a[i]];       fir[a[i]] = i;     }   }   inline void Modify(int l, int r, int val) {     int lt(col[l]), rt(col[r]);     if (lt == rt) {       for (int i = l; i <= r; ++i) {         if (val == 1) {           if (v[i] + lazy[lt] == k) ans = G(ans - f[i] + mod);         } else {           if (v[i] + lazy[lt] == k + 1) ans = G(ans + f[i]);         }         sum[lt][v[i]] = G(sum[lt][v[i]] - f[i] + mod), v[i] += val,         sum[lt][v[i]] = G(sum[lt][v[i]] + f[i]);       }     } else {       for (int i = l; i <= br[lt]; ++i) {         if (val == 1) {           if (v[i] + lazy[lt] == k) ans = G(ans - f[i] + mod);         } else {           if (v[i] + lazy[lt] == k + 1) ans = G(ans + f[i]);         }         sum[lt][v[i]] = G(sum[lt][v[i]] - f[i] + mod), v[i] += val,         sum[lt][v[i]] = G(sum[lt][v[i]] + f[i]);       }       for (int i = bl[rt]; i <= r; ++i) {         if (val == 1) {           if (v[i] + lazy[rt] == k) ans = G(ans - f[i] + mod);         } else {           if (v[i] + lazy[rt] == k + 1) ans = G(ans + f[i]);         }         sum[rt][v[i]] = G(sum[rt][v[i]] - f[i] + mod), v[i] += val,         sum[rt][v[i]] = G(sum[rt][v[i]] + f[i]);       }       for (int i = lt + 1; i <= rt - 1; ++i) {         if (val == 1)           ans = G(ans - sum[i][k - lazy[i]] + mod);         else           ans = G(ans + sum[i][k + 1 - lazy[i]]);         lazy[i] += val;       }     }   }   inline void Solve() {     ans = f[1] = sum[1][0] = 1;     for (int i = 1; i <= n; ++i) {       ++cnt[a[i]];       int p(lst[i]), q(lst[p]);       if (cnt[a[i]] == 1)         Modify(1, i, 1);       else if (cnt[a[i]] == 2) {         Modify(p + 1, i, 1);         Modify(1, p, -1);       } else {         Modify(p + 1, i, 1);         Modify(q + 1, p, -1);       }       f[i + 1] = ans;       sum[col[i + 1]][v[i + 1]] = G(sum[col[i + 1]][v[i + 1]] + f[i + 1]);       ans = G(ans + f[i + 1]);     }     printf( %d , f[n + 1]);   }   int main() {     n = Read();     k = Read();     for (int i = 1; i <= n; ++i) a[i] = Read();     Fir();     Solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   void REV(T *a, int n) {     reverse(a + 1, a + 1 + n);   }   template <class T>   void SRT(T *a, int n) {     sort(a + 1, a + 1 + n);   }   template <class T>   int UNI(T *a, int n) {     sort(a + 1, a + 1 + n);     return unique(a + 1, a + 1 + n) - (a + 1);   }   template <class T>   int POS(T *a, int n, T v) {     return lower_bound(a + 1, a + 1 + n, v) - a;   }   template <class T>   void _R(T &x) {     cin >> x;   }   void _R(int &x) { scanf( %d , &x); }   void _R(long long &x) { scanf( %lld , &x); }   void _R(double &x) { scanf( %lf , &x); }   void _R(char &x) { scanf(  %c , &x); }   void _R(char *x) { scanf( %s , x); }   void R() {}   template <class T, class... U>   void R(T &head, U &...tail) {     _R(head);     R(tail...);   }   template <class T>   void _W(const T &x) {     cout << x;   }   void _W(const int &x) { printf( %d , x); }   void _W(const long long &x) { printf( %lld , x); }   void _W(const double &x) { printf( %.12f , x); }   void _W(const char &x) { putchar(x); }   void _W(const char *x) { printf( %s , x); }   template <class T, class U>   void _W(const pair<T, U> &x) {     _W(x.F);     putchar(   );     _W(x.S);   }   template <class T>   void _W(const vector<T> &x) {     for (auto i = x.begin(); i != x.end(); _W(*i++))       if (i != x.cbegin()) putchar(   );   }   void W() {}   template <class T, class... U>   void W(const T &head, const U &...tail) {     _W(head);     putchar(sizeof...(tail) ?     :   n );     W(tail...);   }   template <typename T>   void cmin(T &x, T y) {     if (y < x) x = y;   }   template <typename T>   void cmax(T &x, T y) {     if (y > x) x = y;   }   long long sum;   int n;   int a[200005];   int cnt[64];   void purin_solve() {     R(sum, n);     memset((cnt), 0, sizeof((cnt)));     long long cursum = 0;     for (int i = (1); i <= (n); ++i) {       long long x;       R(x);       cursum += x;       int c = -1;       while (x) {         ++c;         x >>= 1;       }       ++cnt[c];     }     if (cursum < sum) do {         puts( -1 );         return;       } while (false);     ;     long long SUM = 0;     int ans = 0;     for (int i = 0; i < 64; ++i) {       if (SUM >= (1LL << i)) {         SUM -= (1LL << i);         ++cnt[i];       }       if (sum >> i & 1) {         int j;         for (j = i; j < 64; ++j) {           if (cnt[j]) break;         };         if (j == 64) do {             puts( -1 );             return;           } while (false);         while (j > i) {           --cnt[j];           --j;           cnt[j] += 2;           ++ans;         }         assert(cnt[i]);         --cnt[i];       }       while (cnt[i]) {         SUM += (1LL << i);         --cnt[i];       }     }     W(ans);   }   int main() {     cout << fixed << setprecision(12);     cerr << fixed << setprecision(12);     int t = 1;     scanf( %d , &t);     while (t--) purin_solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char N[(int)1e6 + 5], B[(int)1e6 + 5];   long long n, b, c;   inline long long euler(long long n) {     long long ans = n;     for (int i = 2; i * i <= n; i++) {       if (n % i == 0) {         ans = ans / i * (i - 1);         while (n % i == 0) n /= i;       }     }     if (n > 1) ans = ans / n * (n - 1);     return ans;   }   long long qpow(long long x, long long n, long long mod) {     if (n == 0) return 1;     long long ans = qpow(x, n >> 1, mod);     ans = ans * ans % mod;     if (n & 1) ans = ans * x % mod;     return ans % mod;   }   int main() {     cin >> B;     cin >> N;     int xx = strlen(N);     int yy = strlen(B);     cin >> c;     n = 0;     long long u = euler(c);     if (xx < 10) {       for (int i = 0; i < xx; i++) n = n * 10 + N[i] -  0 ;       n--;     } else {       for (int i = 0; i < xx; i++) {         n = n * 10 + N[i] -  0 ;         n = n % u;       }       n = ((n - 1) + u) % u;       n += u;     }     for (int i = 0; i < yy; i++) {       b = b * 10 + B[i] -  0 ;       b = b % c;     }     long long ans = (b - 1 + c) % c;     ans = ans * qpow(b, n, c) % c;     if (!ans)       cout << c << endl;     else       cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   double dp[100111], ans;   int sz[100111], ds[100111];   std::list<int> edge[100111];   int i, j, n;   void dfs(int now, int fa) {     ds[now] = ds[fa] + 1;     ans += 1 / (double)ds[now];     for (std::list<int>::const_iterator it(edge[now].begin());          it != edge[now].end(); ++it)       if (*it != fa) dfs(*it, now);   }   int main() {     scanf( %d , &n);     while (--n) {       scanf( %d %d , &i, &j);       edge[i].push_back(j);       edge[j].push_back(i);     }     dfs(1, 0);     printf( %.10lf n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, jj = 0;   long long a[22], b[22];   long long dp[10000][1000];   long long memo(int idx, int cnt) {     long long res = (long long)(1e18);     if (idx == 2 * n - 1) {       if (cnt == 1) res = b[((idx + jj) % n)];       return dp[idx][cnt] = res;     }     if (dp[idx][cnt] != -1) return dp[idx][cnt];     if (cnt > 0) res = b[((idx + jj) % n)] + memo(idx + 1, cnt - 1);     res = min(res, a[((idx + jj) % n)] + memo(idx + 1, cnt + 1));     return dp[idx][cnt] = res;   }   int main() {     memset(dp, -1, sizeof dp);     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     long long mn = memo(0, 0);     long long ans = mn;     for (jj = 1; jj < n; jj++) {       memset(dp, -1, sizeof dp);       mn = min(memo(0, 0), mn);     }     cout << (m / 2 - 1) * mn + ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> leftSub[1000005];   vector<int> rightSub[1000005];   int leftSon[1000005], rightSon[1000005];   int maxVal[1000005], minVal[1000005];   int leftMax[1000005];   bool vis[1000005];   bool possible;   int n, c, a, b;   string d;   int cnt;   void findDfs(int u) {     if (vis[u]) return;     vis[u] = true;     maxVal[u] = minVal[u] = u;     int lmax = 0, rmax = 0;     int lmin = n + 1, rmin = n + 1;     int Lmax = 0, Rmin = n + 1;     for (int i = 0; i < leftSub[u].size(); ++i) {       int v = leftSub[u][i];       lmax = max(lmax, v);       lmin = min(lmin, v);       findDfs(v);       Lmax = max(Lmax, maxVal[v]);       maxVal[u] = max(maxVal[u], maxVal[v]);       minVal[u] = min(minVal[u], minVal[v]);     }     for (int i = 0; i < rightSub[u].size(); ++i) {       int v = rightSub[u][i];       rmax = max(rmax, v);       rmin = min(rmin, v);       findDfs(v);       Rmin = min(Rmin, minVal[v]);       maxVal[u] = max(maxVal[u], maxVal[v]);       minVal[u] = min(minVal[u], minVal[v]);     }     if (lmin <= u || lmax >= rmin) {       possible = false;       return;     }     if (Lmax >= Rmin) {       possible = false;       return;     }     leftMax[u] = Lmax;   }   void findBoundary() {     memset(vis, false, sizeof(vis));     for (int i = n; i >= 1; --i) {       if (!vis[i]) {         findDfs(i);       }     }   }   void dfs(int l, int r) {     ++cnt;     int lmin = n + 1, lmax = 0;     int rmin = n + 1, rmax = 0;     if (leftSub[l].size()) {       leftSon[l] = l + 1;       for (int i = 0; i < leftSub[l].size(); ++i) {         int v = leftSub[l][i];         lmin = min(lmin, v);         lmax = max(lmax, v);       }       if (lmin <= l) {         possible = false;         return;       }       dfs(l + 1, lmax);     }     if (rightSub[l].size()) {       for (int i = 0; i < rightSub[l].size(); ++i) {         int v = rightSub[l][i];         rmin = min(rmin, v);         rmax = max(rmax, v);       }       if (rmin <= cnt) {         possible = false;         return;       }       if (leftSub[l].size()) {         rightSon[l] = cnt + 1;         dfs(cnt + 1, max(r, rmax));       } else {         rightSon[l] = l + 1;         dfs(l + 1, max(r, rmax));       }     } else {       if (l == r) return;       int leftBound = max(l, cnt);       if (leftBound < r) {         rightSon[l] = leftBound + 1;         dfs(leftBound + 1, r);       }     }   }   void print(int u) {     if (leftSon[u]) {       print(leftSon[u]);     }     cout << u <<    ;     if (rightSon[u]) {       print(rightSon[u]);     }   }   void init() {     memset(leftSon, 0, sizeof(leftSon));     memset(rightSon, 0, sizeof(rightSon));     possible = true;     cnt = 0;     for (int i = 1; i <= n; ++i) {       leftMax[i] = 0;     }   }   int main() {     while (cin >> n >> c) {       for (int i = 0; i < c; ++i) {         cin >> a >> b >> d;         if (d[0] ==  L ) {           leftSub[a].push_back(b);         } else {           rightSub[a].push_back(b);         }       }       init();       dfs(1, n);       if (!possible) {         puts( IMPOSSIBLE );       } else {         print(1);         puts(  );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int MOD = 998244353;   void add(int& a, int b) {     a += b;     if (a >= MOD) a -= MOD;   }   int mul(int a, int b) { return ll(a) * b % MOD; }   int modinv(int a, int m = MOD) {     return a == 1 ? a : m - modinv(m % a, a) * ll(m) / a;   }   const int MAXV = 4242;   int p2[MAXV], invp2[MAXV];   int p2m1[MAXV], invp2m1[MAXV];   void precomp() {     p2[0] = 1;     for (int i = 1; i < MAXV; i++) {       p2[i] = mul(2, p2[i - 1]);     }     for (int i = 0; i < MAXV; i++) {       invp2[i] = modinv(p2[i]);     }     for (int i = 0; i < MAXV; i++) {       p2m1[i] = p2[i] - 1;     }     for (int i = 1; i < MAXV; i++) {       invp2m1[i] = modinv(p2m1[i]);     }   }   const int MAXN = 3010;   int N, C;   int A[MAXN];   int main() {     precomp();     scanf( %d %d , &N, &C);     for (int i = 0; i < N; i++) {       scanf( %d , &A[i]);       A[i]--;     }     vector<vector<int>> dp(N + 1);     dp[0] = {{1}};     if (C > 10) {       for (int i = 0; i < N; i++) {         vector<int> freq(C);         int prod = 1;         int cnt_bad = C - 1;         for (int j = i; j >= 0; j--) {           if (A[i] != A[j]) {             if (!freq[A[j]]) {               cnt_bad--;             } else {               prod = mul(prod, invp2m1[freq[A[j]]]);             }             freq[A[j]]++;             prod = mul(prod, p2m1[freq[A[j]]]);           }           if (cnt_bad == 0) {             if (dp[i + 1].size() < dp[j].size() + 1) {               dp[i + 1].resize(dp[j].size() + 1);             }             for (int l = 0; l < int(dp[j].size()); l++) {               add(dp[i + 1][l + 1], mul(prod, dp[j][l]));             }           }         }       }     } else {       vector<vector<int>> dp2(1 << C);       dp2[0] = {{1}};       for (int i = 0; i < N; i++) {         for (int m = (1 << C) - 1; m >= 0; m--) {           if (dp2[m | (1 << A[i])].size() < dp2[m].size()) {             dp2[m | (1 << A[i])].resize(dp2[m].size());           }           for (int l = 0; l < int(dp2[m].size()); l++) {             add(dp2[m | (1 << A[i])][l], dp2[m][l]);           }         }         swap(dp[i + 1], dp2[(1 << C) - 1]);         dp[i + 1].insert(dp[i + 1].begin(), 0);         if (dp2[0].size() < dp[i + 1].size()) {           dp2[0].resize(dp[i + 1].size());         }         for (int l = 0; l < int(dp[i + 1].size()); l++) {           add(dp2[0][l], dp[i + 1][l]);         }       }     }     vector<int> ans(N + 1);     for (int i = 0; i <= N; i++) {       for (int l = 0; l < int(dp[i].size()); l++) {         add(ans[l], mul(p2[N - i], dp[i][l]));       }     }     ans[0]--;     for (int i = 0; i < N; i++) {       add(ans[i], MOD - ans[i + 1]);     }     for (int i = 0; i <= N; i++) {       printf( %d%c , ans[i],    n [i == N]);     }     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 );   using namespace std;   const int MAX_ARRAY = 100005;   string num2str(int i) {     stringstream ss;     ss << i;     return ss.str();   }   std::vector<std::string> split(std::string str, std::string pattern) {     std::string::size_type pos;     std::vector<std::string> result;     str += pattern;     int size = str.size();     for (int i = 0; i < size; i++) {       pos = str.find(pattern, i);       if (pos < size) {         std::string s = str.substr(i, pos - i);         result.push_back(s);         i = pos + pattern.size() - 1;       }     }     return result;   }   bool DesCmp(int a, int b) { return a > b; }   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         cout << (j - 1) * n + ((i + j - 1) % n) + 1 <<    ;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void start() {}   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     cin >> t;     while (t--) {       long long a, b;       cin >> a >> b;       if (a < b) swap(a, b);       b = 2 * b;       if (b >= a)         cout << b * b << endl;       else         cout << a * a << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string str;       cin >> str;       long long count = 0, count2 = 0;       for (int i = 0; i < n; i++) {         if (str[i] ==  < ) {           count++;         } else {           break;         }       }       for (int i = n - 1; i >= 0; i--) {         if (str[i] ==  > ) {           count2++;         } else {           break;         }       }       cout << min(count, count2) << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     char a[n][7];     for (int i = 0; i < n; i++) {       for (int j = 0; j < 7; j++) cin >> a[i][j];     }     int ans = 0, ct = 0;     for (int j = 0; j < 7; j++) {       ct = 0;       for (int i = 0; i < n; i++) {         if (a[i][j] ==  1 ) ct++;       }       ans = max(ans, ct);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   inline long long read() {     long long x = 0, w = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) w = -1;       c = getchar();     }     while (c <=  9  && c >=  0 ) {       x = (x << 1) + (x << 3) + c -  0 ;       c = getchar();     }     return w == 1 ? x : -x;   }   const int N = 1e5 + 10;   int a[N], n;   int main() {     int _ = read();     while (_--) {       long long x1 = read(), y1 = read();       long long x2 = read(), y2 = read();       if (x1 == x2 || y1 == y2) {         puts( 1 );         continue;       }       long long ans = (x2 - x1) * (y2 - y1) + 1;       printf( %lld n , ans);     }   }
#include <bits/stdc++.h>   using namespace std;   mt19937 rng(time(0));   uniform_int_distribution<int> uid(-1e9, 1e9);   vector<int> g[100002];   int d[100002], u[100002], k;   long long res, w[505], v[505];   int find(int x, int px, int sz) {     for (auto& i : g[x])       if (i != px && !u[i] && d[i] > sz / 2) return find(i, x, sz);     return x;   }   int dfs(int x, int px) {     d[x] = 1;     for (auto& i : g[x])       if (i != px && !u[i]) d[x] += dfs(i, x);     return d[x];   }   void dfs1(int x, int px, int dx) {     if (dx <= k) ++w[dx];     for (auto& i : g[x])       if (i != px && !u[i]) dfs1(i, x, dx + 1);   }   void solve(int x) {     int y = find(x, x, dfs(x, x));     u[y] = 1;     fill(v, v + k + 1, 0);     for (auto& i : g[y]) {       if (!u[i]) {         fill(w, w + k + 1, 0);         dfs1(i, i, 1);         res += w[k];         for (int i = 1; i < k; ++i) res += w[i] * v[k - i];         for (int i = 0; i < k; ++i) v[i] += w[i];       }     }     for (auto& i : g[y])       if (!u[i]) solve(i);   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, x, y;     cin >> n >> k;     for (int i = 0; i < n - 1; ++i) {       cin >> x >> y;       g[--x].push_back(--y);       g[y].push_back(x);     }     solve(0);     cout << res <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int n, len, dp[200][100][2][2];   char x[200];   const int unDEF = 10000, INF = 1000000000;   int solve(int idx, int left, int dir, int isM) {     if (idx < 0) {       if (left + dir == 0) {         return 0;       }       if (isM) {         return -INF;       }       return INF;     }     int &temp = dp[idx][left][dir][isM];     if (temp != unDEF) {       return temp;     }     if (isM) {       temp = -INF;     } else       temp = INF;     int sign = dir ? -1 : 1;     for (int i = 0; i <= left; i++) {       int newDir = (x[idx] !=  T ) ^ (i & 1);       if (newDir) {         if (isM) {           temp = max(temp, solve(idx - 1, left - i, dir, isM) + sign);         } else {           temp = min(temp, solve(idx - 1, left - i, dir, isM) + sign);         }       } else {         if (isM) {           temp = max(temp, solve(idx - 1, left - i, dir ^ 1, isM));         } else {           temp = min(temp, solve(idx - 1, left - i, dir ^ 1, isM));         }       }     }     return temp;   }   int main() {     cin >> x >> n;     len = strlen(x);     for (int i = 0; i < len; i++) {       for (int j = 0; j <= n; j++) {         for (int k = 0; k < 2; k++) {           for (int l = 0; l < 2; l++) {             dp[i][j][k][l] = unDEF;           }         }       }     }     int ans = 0, a = solve(len - 1, n, 0, 0), b = solve(len - 1, n, 0, 1),         c = solve(len - 1, n, 1, 0), d = solve(len - 1, n, 1, 1);     if (a < 1000) ans = max(ans, abs(a));     if (c < 1000) ans = max(ans, abs(c));     if (b > -1000) ans = max(ans, abs(b));     if (d > -1000) ans = max(ans, abs(d));     cout << ans <<   n ;     return 0;   }