malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, m, aa[300005], bb[300005], v[1000005], q;   int x[4000005], y[4000005], dx;   void build(int l, int r, int i) {     if (l + 1 == r) {       y[i] = v[l];       return;     }     int mi = (l + r) / 2;     build(l, mi, i * 2);     build(mi, r, i * 2 + 1);     y[i] = max(y[i * 2], y[i * 2 + 1]);   }   void add(int a, int b, int l, int r, int i) {     if (a == l && b == r) {       x[i] += dx;       y[i] += dx;       return;     }     int mi = (l + r) / 2;     if (a < mi) add(a, min(mi, b), l, mi, i * 2);     if (mi < b) add(max(a, mi), b, mi, r, i * 2 + 1);     y[i] = max(y[i * 2], y[i * 2 + 1]) + x[i];   }   int find(int l, int r, int i, int vv) {     if (l + 1 == r) return l;     vv -= x[i];     int mi = (l + r) / 2;     if (y[i * 2 + 1] > vv) return find(mi, r, i * 2 + 1, vv);     return find(l, mi, i * 2, vv);   }   int main() {     scanf( %d%d , &n, &m);     v[0] = n + 1;     for (int i = 1; i <= n; i++) {       scanf( %d , aa + i);       v[aa[i]]++;     }     for (int i = 1; i <= m; i++) {       scanf( %d , bb + i);       v[bb[i]]--;     }     for (int i = 1000000; i >= 0; i--) v[i] += v[i + 1];     build(0, 1000001, 1);     scanf( %d , &q);     while (q--) {       int ta, tb, tc;       scanf( %d%d%d , &ta, &tb, &tc);       if (ta == 1) {         if (aa[tb] < tc) {           dx = 1;           add(aa[tb] + 1, tc + 1, 0, 1000001, 1);         } else if (aa[tb] > tc) {           dx = -1;           add(tc + 1, aa[tb] + 1, 0, 1000001, 1);         }         aa[tb] = tc;       } else {         if (bb[tb] < tc) {           dx = -1;           add(bb[tb] + 1, tc + 1, 0, 1000001, 1);         } else if (bb[tb] > tc) {           dx = 1;           add(tc + 1, bb[tb] + 1, 0, 1000001, 1);         }         bb[tb] = tc;       }       ta = find(0, 1000001, 1, 0);       if (ta == 0) ta = -1;       printf( %d n , ta);     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, maxi, mini;     cin >> n;     int z[n], o = 0;     for (i = 0; i < n; i++) {       cin >> z[i];       if (i == 0) {         maxi = z[i];         mini = z[i];       }       if (i > 0) {         if (z[i] > maxi) {           o++;           maxi = z[i];         }         if (z[i] < mini) {           o++;           mini = z[i];         }       }     }     cout << o;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const int INF = 1e9;   const long long LINF = 1LL * INF * INF;   const int MAXN = 100007;   const double EPS = 1e-7;   int A[MAXN];   int main() {     ios_base::sync_with_stdio(0);     int n, m;     cin >> n >> m;     if (n == 1 && m == 1) {       cout <<  YES n1 ;       return 0;     }     if (n == 1 && m <= 3 || m == 1 && n <= 3 || n == 2 && m <= 3 ||         m == 2 && n <= 3) {       cout <<  NO ;       return 0;     }     if (n == 1 && m == 4) {       cout <<  YES n2 4 1 3 ;       return 0;     }     if (n == 4 && m == 1) {       cout <<  YES n2 n4 n1 n3 ;       return 0;     }     if (n == 2 && m == 4) {       cout <<  YES n ;       cout <<  5 4 7 2  << endl;       cout <<  3 6 1 8 ;       return 0;     }     if (n == 4 && m == 2) {       cout <<  YES n ;       cout <<  6 1 n ;       cout <<  3 8 n ;       cout <<  2 5 n ;       cout <<  7 4 n ;       return 0;     }     if (n == 3 && m == 3) {       cout <<  YES n ;       cout <<  2 9 4 n ;       cout <<  7 5 3 n ;       cout <<  6 1 8 n ;       return 0;     }     if (n == 3 && m == 4) {       cout <<  YES n ;       cout <<  6 8 11 2 n ;       cout <<  9 3 1 10 n ;       cout <<  4 12 7 5 n ;       return 0;     }     if (n == 4 && m == 3) {       cout <<  YES n ;       cout <<  10 3 9 n ;       cout <<  5 1 7 n ;       cout <<  12 6 11 n ;       cout <<  4 8 2 n ;       return 0;     }     if (n == 4 && m == 4) {       cout <<  YES n ;       cout <<  2 11 14 9 n ;       cout <<  16 6 8 1 n ;       cout <<  13 3 10 4 n ;       cout <<  7 5 12 15 n ;       return 0;     }     if (n >= 5) {       cout <<  YES n ;       for (int i = (0); i < (2); ++i) {         for (int f = i * m + 1; f <= n * m; f += 2 * m) {           for (int j = (1); j < (m + 1); ++j) {             int cur = f + j - 1;             if ((j + 1) & 1) cur += m;             if (cur > n * m) cur -= n * m;             cout << cur <<    ;           }           cout << endl;         }       }       return 0;     }     if (m >= 5) {       cout <<  YES n ;       for (int i = (1); i < (n + 1); ++i) {         int cur = 1;         for (int j = (1); j < (m + 1); ++j) {           int c = cur;           cur += 2;           if (cur > m) cur = 2;           if ((i + 1) & 1) ++c;           if (c > m) c -= m;           cout << (i - 1) * m + c <<    ;         }         cout << endl;       }       return 0;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e5 + 5;   int n, k, a;   int vis_time[MAX], vis_cnt[MAX], ans;   struct data {     int size, op;     bool operator<(data b) const {       if (size == b.size)         return op > b.op;       else         return size < b.size;     }   };   priority_queue<data> line;   int main() {     scanf( %d%d , &n, &k);     for (register int i = 1; i <= n; ++i) {       scanf( %d , &a);       line.push((data){a, 0});     }     ans = 0x3f3f3f3f;     while (!line.empty()) {       data tmp = line.top();       line.pop();       int size = tmp.size, op = tmp.op;       vis_time[size]++;       vis_cnt[size] += op;       if (vis_time[size] == k) {         ans = min(ans, vis_cnt[size]);       }       if (!size)         continue;       else         line.push((data){size / 2, op + 1});     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long dp[100000 + 25];   struct node {     int iz, der;     long long tiempo;     node() {}     node(long long t, int i, int d) {       tiempo = t;       iz = i;       der = d;     }   };   struct otro {     int proct, pos;     long long tiemp;     otro() {}     otro(int ps, long long time, int timprt) {       pos = ps;       tiemp = time;       proct = timprt;     }     bool operator<(const otro& algo) const { return algo.tiemp < tiemp; }   };   vector<node> v;   int n, m, ini, fin;   int valoreleg;   char eleg;   char ch[500000];   map<long long, int> mp;   void f() {     long long tiemp = 1;     int c = 0;     int pos = ini;     while (true) {       if (v[mp[tiemp]].iz <= pos && v[mp[tiemp]].der >= pos) {         ch[c++] =  X ;         tiemp++;         continue;       }       pos += valoreleg;       ch[c] = eleg;       if (v[mp[tiemp]].iz <= pos && v[mp[tiemp]].der >= pos) {         pos -= valoreleg;         ch[c] =  X ;       }       c++;       tiemp++;       if (pos == fin) {         for (int a = 0; a < c; a++) cout << ch[a];         cout << endl;         return;       }     }   }   int main() {     cin >> n >> m >> ini >> fin;     int li, ri;     long long tim;     v.push_back(node(0, 0, 0));     for (int a = 0; a < m; a++) {       cin >> tim >> li >> ri;       mp[tim] = a + 1;       v.push_back(node(tim, li, ri));     }     if (ini < fin) {       valoreleg = 1;       eleg =  R ;     } else {       valoreleg = -1;       eleg =  L ;     }     f();   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int q;     cin >> q;     while (q--) {       int a, b, n, s;       cin >> a >> b >> n >> s;       if (s - n * min(a, s / n) <= b)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, cnt, ans;   int a[410][410], c[160010];   int now, f[2][410][410];   void Init() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) scanf( %d , &a[i][j]);   }   inline void insert(int x) {     if (c[x]) ++cnt;     ++c[x];   }   inline void erase(int x) {     --c[x];     if (c[x]) --cnt;   }   void Work() {     int p;     for (int i = 1; i <= n; ++i) {       p = 1;       for (int j = 1; j <= m; ++j) {         insert(a[i][j]);         while (cnt) {           f[0][i][p] = j - 1;           erase(a[i][p]);           ++p;         }       }       while (p <= m) {         f[0][i][p] = m;         erase(a[i][p]);         ++p;       }       for (int j = 1; j <= m; ++j) ans = max(ans, f[0][i][j] - j + 1);     }   }   void Solve() {     int p;     for (int s = 2; s <= n; ++s) {       now ^= 1;       for (int i = 1; i <= n - s + 1; ++i) {         int j = i + s - 1;         for (int k = 1; k <= m; ++k)           f[now][i][k] = min(f[now ^ 1][i][k], f[now ^ 1][i + 1][k]);         p = 1;         for (int k = 1; k <= m; ++k) {           insert(a[i][k]);           insert(a[j][k]);           while (cnt) {             f[now][i][p] = min(f[now][i][p], k - 1);             erase(a[i][p]);             erase(a[j][p]);             ++p;           }         }         while (p <= m) {           f[now][i][p] = min(f[now][i][p], m);           erase(a[i][p]);           erase(a[j][p]);           ++p;         }         for (int k = 1; k <= m; ++k) ans = max(ans, s * (f[now][i][k] - k + 1));       }     }     printf( %d n , ans);   }   int main() {     Init();     Work();     Solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 61;   int a[N], n;   long long dp[N][2][2], k;   long long Dfs(int pos, int x, int y) {     if (pos < n - pos + 1) return 1;     if (dp[pos][x][y] != -1) return dp[pos][x][y];     long long ans = 0;     for (int i = 0; i < 2; i++)       if (a[pos] == -1 || a[pos] == i)         for (int j = 0; j < 2; j++)           if (a[n - pos + 1] == -1 || a[n - pos + 1] == j) {             if (x && (i > j) || y && (i > !j) || pos == n - pos + 1 && (i != j))               continue;             ans += Dfs(pos - 1, x && (i == j), y && (i != j));           }     return dp[pos][x][y] = ans;   }   int main() {     scanf( %d%lld , &n, &k);     k++;     memset(dp, -1, sizeof(dp));     memset(a, -1, sizeof(a));     a[n] = 0;     if (Dfs(n, 1, 1) < k)       puts( -1 );     else {       for (int i = n - 1; i > 0; i--) {         a[i] = 0;         memset(dp, -1, sizeof(dp));         long long num = Dfs(n, 1, 1);         if (k > num) k -= num, a[i] = 1;       }       for (int i = n; i >= 1; i--) printf( %d , a[i]);       puts(  );     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, j;     string s1, s2;     cin >> n >> s1 >> s2;     if (s2 ==  week ) {       if (n == 5 || n == 6) {         cout <<  53  << endl;       } else {         cout <<  52  << endl;       }     } else {       if (n <= 29) {         cout <<  12  << endl;       } else if (n == 30) {         cout <<  11  << endl;       } else if (n == 31) {         cout <<  7  << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200002;   long long sum, ans, nd, T;   priority_queue<long long> q;   int n, k, b, c, t[N], i, j;   int main() {     scanf( %d%d%d%d , &n, &k, &b, &c), ans = 1e18;     b = min(b, c * 5);     for (i = 1; i <= n; i++) scanf( %d , &t[i]);     sort(t + 1, t + n + 1);     for (i = 0; i < 5; i++) {       T = 2e9 + i;       while (!q.empty()) q.pop();       sum = 0;       for (j = 1; j <= n; j++) {         nd = (T - t[j]) / 5 * b + (T - t[j]) % 5 * c;         sum += nd;         q.push(nd);         if (q.size() > k) sum -= q.top(), q.pop();         if (q.size() == k) ans = min(ans, sum - (T - t[j]) / 5 * b * k);       }     }     printf( %lld , ans);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long q = 0;     vector<long long> a;     a.push_back(0);     for (long long i = 0; i < 4; i++) {       long long te;       cin >> te;       a.push_back(te);     }     string s;     cin >> s;     for (long long i = 0; i < s.size(); i++) {       q += a[s[i] -  0 ];     }     cout << q;     return 0;     ;   }
#include <bits/stdc++.h>   using namespace std;   typedef int64_t g;   g n,p[269420],v=1e9+7,T,r,i,k,s;   map<g,g> l;   int main(){   cin>>T;   while(T--){   cin>>n;   l.clear();   for(i=0,s=0,p[0]=1,l[0]=1;++i<=n;){   cin>>k;   p[i]=(p[i-1]*2+v-p[l[s]-1])%v;   l[s]=i;   s+=k;   }cout<<p[n]<<endl;   }   }
#include <bits/stdc++.h>   using namespace std;   int n, m, p, dp[2][110][110][110], ans;   const int mod = 1e9 + 7;   int main() {     scanf( %d%d%d , &n, &m, &p);     for (int i = 1; i <= n; i++) dp[0][i][i][1] = 1;     for (int i = 0; i < m - 1; i++) {       memset(dp[!(i & 1)], 0, sizeof(dp[!(i & 1)]));       for (int j = 1; j <= n; j++)         for (int k = 1; k <= j; k++)           for (int s = 1; s <= p; s++)             if (dp[i & 1][j][k][s]) {               int ns = s;               for (int k2 = k; k2 <= n - j; k2++) {                 if (k2 > k) ns *= (k2 - 1), ns /= (k2 - k);                 if (ns > p) break;                 (dp[!(i & 1)][j + k2][k2 - k][ns] += dp[i & 1][j][k][s]) %= mod;               }             }       for (int j = 1; j <= n; j++)         for (int s = 1; s <= p; s++)           (ans += 1ll * (m - i - 1) * dp[!(i & 1)][j][0][s] % mod) %= mod;     }     return printf( %d , ans), 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;   const int n_ = 1e5 + 1000;   const long double PI = acos(-1.0);   long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }   long long power(long long a, long long n) {     long long p = 1;     while (n > 0) {       if (n % 2) {         p = p * a;       }       n >>= 1;       a *= a;     }     return p;   }   long long power(long long a, long long n, long long mod) {     long long p = 1;     while (n > 0) {       if (n % 2) {         p = p * a;         p %= mod;       }       n >>= 1;       a *= a;       a %= mod;     }     return p % mod;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, h;     cin >> n >> h;     int res = 0;     for (int(i) = 0; (i) < (n); (i)++) {       int a;       cin >> a;       if (a > h)         res += 2;       else         res++;     }     cout << res << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     double d, h, v, e;     cin >> d >> h >> v >> e;     d /= 2;     double vol = d * d * acos(-1) * h;     double incre = e * d * d * acos(-1);     if (incre >= v) {       cout <<  NO ;       return 0;     }     double ans = vol / (v - incre);     if (ans > 1e4) {       cout <<  NO ;       return 0;     }     cout <<  YES n ;     cout << setprecision(15) << ans;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n = 0, m = 0;     cin >> n >> m;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) {         if (i % 2 == 0)           cout <<  # ;         else if ((j == m - 1 && (i - 1) % 4 == 0) || (j == 0 && (i + 1) % 4 == 0))           cout <<  # ;         else           cout <<  . ;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int co;   bool query(int x, int y) {     assert(++co <= 60);     printf( 1 %d %d n , x, y);     fflush(stdout);     char st[5];     scanf( %s , st);     return st[0] ==  N ;   }   int main() {     int fst, scnd, n, k;     scanf( %d %d , &n, &k);     int l = 1, r = n;     while (l < r) {       int m = (l + r) / 2;       if (query(m, m + 1))         l = m + 1;       else         r = m;     }     fst = l;     l = 1, r = fst - 1;     while (l < r) {       int m = (l + r) / 2;       if (query(m, m + 1))         l = m + 1;       else         r = m;     }     if (l == r && !query(l, l + 1)) {       printf( 2 %d %d n , fst, l);       fflush(stdout);       return 0;     }     l = fst + 1, r = n;     while (l < r) {       int m = (l + r) / 2;       if (query(m, m + 1))         l = m + 1;       else         r = m;     }     printf( 2 %d %d n , fst, l);     fflush(stdout);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int dim = 52;   char a[dim][dim];   int s[dim][dim];   int dp[dim][dim][dim][dim];   void compute(int i1, int j1, int i2, int j2) {     if (dp[i1][j1][i2][j2] != 0x3f3f3f3f) return;     int ans = max(i2 - i1 + 1, j2 - j1 + 1);     if (s[i2][j2] - s[i1 - 1][j2] - s[i2][j1 - 1] + s[i1 - 1][j1 - 1] == 0) {       dp[i1][j1][i2][j2] = 0;       return;     }     if (s[i2][j2] - s[i1 - 1][j2] - s[i2][j1 - 1] + s[i1 - 1][j1 - 1] ==         (i2 - i1 + 1) * (j2 - j1 + 1)) {       dp[i1][j1][i2][j2] = ans;       return;     }     for (int i = i1; i < i2; ++i) {       compute(i1, j1, i, j2);       compute(i + 1, j1, i2, j2);       ans = min(ans, dp[i1][j1][i][j2] + dp[i + 1][j1][i2][j2]);     }     for (int j = j1; j < j2; ++j) {       compute(i1, j1, i2, j);       compute(i1, j + 1, i2, j2);       ans = min(ans, dp[i1][j1][i2][j] + dp[i1][j + 1][i2][j2]);     }     dp[i1][j1][i2][j2] = ans;   }   void solve() {     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i] + 1;       for (int j = 1; j <= n; ++j)         s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + (a[i][j] ==  # );     }     memset(dp, 0x3f, sizeof dp);     compute(1, 1, n, n);     cout << dp[1][1][n][n];   }   int main() {     cin.tie(0);     ios_base::sync_with_stdio(false);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     vector<int> arrNums;     int iTemp, nNeed, nNum, i;     long long iSum = 0;     cin >> nNum >> nNeed;     while (nNum--) {       cin >> iTemp;       arrNums.push_back(iTemp);     }     sort(arrNums.begin(), arrNums.end());     for (i = 0; i < nNeed; ++i) {       iSum += arrNums[i];     }     cout << iSum;   endapp:     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 7;   const int INF = 1e9 + 7;   int xx[8] = {1, 1, 1, 0, -1, -1, -1, 0}, yy[8] = {1, 0, -1, -1, -1, 0, 1, 1};   int n, m, hd = 0, ti = 0;   vector<int> a[N], p[N], dis[N];   char s[N];   int Qux[N * 5], Quy[N * 5];   void bfs() {     while (hd != ti) {       int tx = Qux[++hd], ty = Quy[hd];       for (int j = 0; j <= 8 - 1; j++) {         if (tx + xx[j] < 0 || tx + xx[j] > n + 1) continue;         if (ty + yy[j] < 0 || ty + yy[j] > m + 1) continue;         if (dis[tx + xx[j]][ty + yy[j]] > dis[tx][ty] + 1) {           dis[tx + xx[j]][ty + yy[j]] = dis[tx][ty] + 1;           Qux[++ti] = tx + xx[j];           Quy[ti] = ty + yy[j];         }       }     }   }   bool check(int x) {     for (int i = 0; i <= n + 1 - 1; i++)       for (int j = 0; j <= m + 1 - 1; j++) p[i][j] = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (dis[i][j] >= x) {           if (i - x + 1 <= 0) return 0;           if (i + x - 1 > n) return 0;           if (j - x + 1 <= 0) return 0;           if (j + x - 1 > m) return 0;           p[i - x + 1][j - x + 1]++;           p[i - x + 1][j + x]--;           p[i + x][j - x + 1]--;           p[i + x][j + x]++;         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         p[i][j] = p[i][j] + p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1];         if (!p[i][j] && a[i][j]) return 0;       }     }     return 1;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       Qux[++ti] = 0;       Quy[ti] = i;       Qux[++ti] = n + 1;       Quy[ti] = i;     }     for (int i = 0; i <= n + 2 - 1; i++)       for (int j = 0; j <= m + 2 - 1; j++) dis[i].push_back(0);     for (int i = 0; i <= n + 2 - 1; i++)       for (int j = 0; j <= m + 2 - 1; j++) p[i].push_back(0);     for (int i = 1; i <= n; i++) {       scanf( %s , s + 1);       a[i].push_back(0);       Qux[++ti] = i;       Quy[ti] = 0;       Qux[++ti] = i;       Quy[ti] = m + 1;       for (int j = 1; j <= m; j++) {         if (s[j] ==  X ) {           a[i].push_back(1);           dis[i][j] = INF;         } else {           a[i].push_back(0);           Qux[++ti] = i;           Quy[ti] = j;         }       }     }     bfs();     int l = 1, r = (min(n, m) + 1) / 2;     while (l < r - 1) {       int mid = (l + r) >> 1;       if (check(mid))         l = mid;       else         r = mid;     }     int ans;     if (check(r))       ans = r;     else       ans = l;     check(ans);     printf( %d n , ans - 1);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++)         if (dis[i][j] >= ans)           putchar( X );         else           putchar( . );       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 10010, M = 15010, inf = 1e9;   struct st1 {     int to, next, c;   } e[M << 1];   struct st2 {     int to, next, f, op;   } F[1000001];   int n, m, x, y, z, cnt, cnt2, ans, i, col, cyc, S, T, fi[N], c[N], l[N],       op[M << 1], d[N + M], f2[N + M], dep[N + M], num[N + M];   bool vis[N], u[M << 1];   void addl(int x, int y, int z) {     e[++cnt].to = y, e[cnt].next = fi[x], e[cnt].c = z, fi[x] = cnt;   }   void addf(int x, int y, int f) {     F[++cnt2].to = y, F[cnt2].next = f2[x], F[cnt2].f = f, F[cnt2].op = cnt2 + 1,     f2[x] = cnt2;     F[++cnt2].to = x, F[cnt2].next = f2[y], F[cnt2].f = 0, F[cnt2].op = cnt2 - 1,     f2[y] = cnt2;   }   void dfs(int x, int fa, int d) {     vis[x] = 1;     for (int i = fi[x]; i; i = e[i].next)       if (!u[i] && e[i].to != fa) {         int y = e[i].to;         if (vis[y]) {           cyc++, u[i] = u[op[i]] = 1;           addf(S, cyc, dep[x] - dep[y]);           for (int j = dep[y] + 1; j <= dep[x]; j++)             u[l[j]] = u[op[l[j]]] = 1, addf(cyc, c[j] + 1, 1);           addf(cyc, e[i].c + 1, 1);         } else {           c[dep[y] = d + 1] = e[i].c, l[d + 1] = i, dfs(y, x, d + 1);           if (!u[i]) addf(S, ++cyc, 1), addf(cyc, e[i].c + 1, 1);         }       }   }   int sap(int x, int del) {     if (x == T) return del;     int sum = 0, md = cyc + 1;     for (int i = f2[x]; i; i = F[i].next)       if (F[i].f) {         if (d[F[i].to] == d[x] - 1) {           int save = sap(F[i].to, min(F[i].f, del - sum));           sum += save, F[i].f -= save, F[F[i].op].f += save;           if (d[S] >= cyc + 1 || sum == del) return sum;         }         md = min(md, d[F[i].to]);       }     if (!sum) {       if (!(--num[d[x]]))         d[S] = cyc + 1;       else         num[d[x] = md + 1]++;     }     return sum;   }   int main() {     scanf( %d%d n , &n, &m);     for (i = 1; i <= m; i++) {       scanf( %d%d%d n , &x, &y, &z);       addl(x, y, z), addl(y, x, z);       col = max(col, z);     }     for (i = 1; i <= cnt; i += 2) op[op[i] = i + 1] = i;     S = 0, T = 1, cyc = 1 + col;     for (i = 1; i <= col; i++) addf(i + 1, T, 1);     dfs(1, 0, 0);     num[0] = cyc + 1;     for (; d[S] <= cyc;) ans += sap(S, inf);     printf( %d , ans);   }
#include <bits/stdc++.h>   using namespace std;   int N, M, M2, B;   const int MAXN = 100015;   const int MAXM = 20;   const int MAXM2 = (1 << 20);   char S[MAXN];   int bitc[MAXM2];   int bitl[MAXM2];   int dp[MAXM2];   int cnt[MAXM][MAXM];   int mcnt[MAXM2][MAXM];   int last;   int main() {     cin >> N >> M >> S;     M2 = (1 << M);     B = M2 - 1;     for (int i = 0; i < M; ++i)       for (int j = 0; j < M; ++j) cnt[i][j] = 0;     bitc[0] = 0;     bitl[0] = 0;     dp[0] = 0;     for (int j = 0; j < M; ++j) mcnt[0][j] = 0;     for (int i = 1; i < M2; ++i) {       int b = i & (i - 1);       bitc[i] = bitc[b] + 1;       for (int j = 0; j < M; ++j)         if ((1 << j) & i) {           bitl[i] = j;           break;         }       for (int j = 0; j < M; ++j) mcnt[i][j] = 0;       dp[i] = 1e9;     }     for (int i = 0; i < N - 1; ++i) {       if (S[i] == S[i + 1]) continue;       ++cnt[S[i] -  a ][S[i + 1] -  a ];       ++cnt[S[i + 1] -  a ][S[i] -  a ];     }     for (int i = 1; i < M2; ++i)       for (int j = 0; j < M; ++j)         mcnt[i][j] = mcnt[i & (i - 1)][j] + cnt[bitl[i]][j];     for (int i = 0; i < M2; ++i)       for (int j = 0; j < M; ++j) {         if (((1 << j) & i) == 0)           dp[i | (1 << j)] = min(dp[i | (1 << j)],                                  dp[i] + bitc[i] * (mcnt[i][j] - mcnt[B ^ i][j]));       }     cout << dp[B] << endl;     return 0;   }