malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = (int)5e5 + 50;   int n;   int ans[N];   vector<int> G[N];   int q[N];   int dep[N];   vector<pair<int, int> > hist[N];   void upd(int u, int val) { q[u] = max(q[u], val); }   void calc(int v, int p) {     dep[v] = 0;     for (int nxt : G[v]) {       if (nxt == p) continue;       calc(nxt, v);       dep[v] = max(dep[nxt] + 1, dep[v]);     }   }   void merge(vector<pair<int, int> > h1, vector<pair<int, int> > h2) {     int r = 0;     for (int i = 0; i < h1.size(); i++) {       while (r < h2.size() && (h2[r].first >= h1[i].first)) {         r++;       }       if (r - 1 >= 0 && h2[r - 1].second > 1)         upd(2 * h1[i].first, h2[r - 1].second + h1[i].second - 2);     }   }   void dfs(int v, int p = -1, int up_dep = -1) {     vector<int> deps;     if (up_dep != -1) deps.push_back(up_dep + 1);     for (int nxt : G[v]) {       if (nxt != p) deps.push_back(dep[nxt] + 1);     }     sort(deps.begin(), deps.end(),          [](const int a, const int b) { return a > b; });     for (int i = 0; i < deps.size(); i++) {       upd(2 * deps[i], i + 1);       upd(2 * deps[i] - 1, i + 1);       if (i > 0 && deps[i - 1] > deps[i]) upd(2 * deps[i] + 1, i + 1);     }     for (int i = 0; i < deps.size(); i++) {       if (hist[v].empty() || hist[v].back().first != deps[i])         hist[v].push_back({deps[i], 1});       else         hist[v].back().second++;     }     for (int i = 1; i < hist[v].size(); i++)       hist[v][i].second += hist[v][i - 1].second;     for (int nxt : G[v]) {       if (nxt == p) continue;       dfs(nxt, v,           (deps.size() == 1 ? 0 : (deps[0] == dep[nxt] + 1 ? deps[1] : deps[0])));       merge(hist[nxt], hist[v]);       merge(hist[v], hist[nxt]);     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       a--, b--;       G[a].push_back(b);       G[b].push_back(a);     }     for (int i = 0; i < n; i++) {       ans[1] = max(ans[1], (int)G[i].size() + 1);     }     calc(0, -1);     dfs(0);     int rmax = 0;     for (int i = (n - 1) / 2 * 2 + 1; i >= 2; i -= 2) {       rmax = max(rmax, q[i]);       ans[i] = rmax;     }     rmax = 0;     for (int i = n / 2 * 2; i >= 2; i -= 2) {       rmax = max(rmax, q[i]);       ans[i] = rmax;     }     for (int i = 1; i <= n; i++) cout << max(ans[i], 1) <<    ;   }
#include <cstdio>   #include <iostream>      #define ll long long      using namespace std;      int n , m , cnt = 0;      const int N = 5e5 + 5;   const int mod = 1e9 + 7;      ll pow2[N];      int num[N] , fa[N];      int find(int x){    if(fa[x] == x)     return x;    return fa[x] = find(fa[x]);   }      int main(){    pow2[0] = 1;    cin >> n >> m;    for(int i = 1; i < N; i++)     pow2[i] = 1ll * 2 * pow2[i - 1] % mod;    for(int i = 1; i <= m + 1; i++)     fa[i] = i;    for(int i = 1; i <= n; i++){     int k;     int x , y;     scanf( %d  , &k);     if(k == 1){      scanf( %d  , &x);      y = m + 1;     }     else      scanf( %d%d  , &x , &y);     int fx , fy;     fx = find(x);     fy = find(y);     if(fx == fy)      continue;     fa[fx] = fy;     num[++cnt] = i;    }    ll ans = pow2[cnt] % mod;    cout << ans <<     << cnt << endl;    for(int i = 1; i <= cnt; i++)     printf( %d   , num[i]);    return 0;   }
#include <bits/stdc++.h>   using namespace std;   unordered_map<long long, int> mpSt;   unordered_map<long long, int> mpEnd;   set<long long> AllPoints;   long long K[200005];   int main() {     int N;     scanf( %d , &N);     for (int i = 0; i < N; i++) {       long long st, end;       scanf(  %lld %lld , &st, &end);       AllPoints.insert(st);       AllPoints.insert(end);       if (mpSt.count(st) > 0)         mpSt[st]++;       else         mpSt[st] = 1;       if (mpEnd.count(end) > 0)         mpEnd[end]++;       else         mpEnd[end] = 1;     }     if (N == 1) {       if (AllPoints.size() == 1) {         printf( 1 );       } else {         long long seg = *next(AllPoints.begin()) - *AllPoints.begin() + 1;         printf( %lld , seg);       }       return 0;     }     auto nextIt = next(AllPoints.begin());     long long cumulative = 0;     for (auto it = AllPoints.begin(); nextIt != AllPoints.end(); it++) {       long long cur = *it;       long long nxt = *nextIt;       cumulative += mpSt[cur];       K[cumulative] += 1;       cumulative -= mpEnd[cur];       long long middle = nxt - cur - 1;       K[cumulative] += middle;       nextIt = next(nextIt);     }     cumulative += mpSt[*prev(AllPoints.end())];     K[cumulative]++;     for (int i = 1; i <= N; i++) {       printf( %lld  , K[i]);     }     return 0;   }
#include <bits/stdc++.h>   int main() {     int n, temp, count, i, len, len1, j;     char c, a[200], b[40];     strcpy(b,  qwertyuiopasdfghjkl;zxcvbnm,./ );     while (~scanf( %c , &c)) {       getchar();       gets(a);       len = strlen(a);       len1 = strlen(b);       if (c ==  R )         temp = -1;       else         temp = 1;       for (i = 0; i < len; i++) {         for (j = 0; j < len1; j++) {           if (a[i] == b[j]) printf( %c , b[j + temp]);         }       }       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   int n, i, j, l, h, k[5500], f[5500], t[5500];   int main() {     scanf( %d n , &n);     for (i = 1; i <= n; i++) {       scanf( %d , &h);       k[i] = k[i - 1] + h;       j = i - 1;       while (t[j] > k[i] - k[j]) j--;       f[i] = f[j] + i - 1 - j;       t[i] = k[i] - k[j];     }     printf( %d , f[n]);     return (0);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, j, x = 0;     char c[30][30] = {0};     scanf( %d n , &n);     for (i = 1; i <= n; i++) {       char temp[605] =   ;       cin.getline(temp, 605);       int length = strlen(temp);       for (j = 0; j < length; j++) {         c[0][temp[j] - 96] = 1;         if (j != length - 1) c[temp[j] - 96][temp[j + 1] - 96] = 1;       }     }     for (i = 0; i <= 26; i++) {       for (j = 1; j <= 26; j++) {         if (c[i][j] == 0 && i == 0) {           printf( %c n , j + 96);           x = 1;         } else if (c[i][j] == 0) {           printf( %c%c n , i + 96, j + 96);           x = 1;         }         if (x == 1) return 0;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     t = 1;     while (t--) {       double n;       cin >> n;       double a[200001], sum = 0;       set<double> s;       for (int i = 0; i < n; i++) {         cin >> a[i];         sum += a[i];       }       for (int i = 0; i < n; i++) {         double z = sum - a[i];         double y = (z / (n - 1));         if (y == a[i]) s.insert(i + 1);       }       cout << s.size() << endl;       for (auto i : s) cout << i <<    ;       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-6;   const int N = 101;   const int INF = 0x3f3f3f3f;   const int mod = 1000000007;   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   int a[N], maxf, ans;   int d[N];   bool mul;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     for (int i = 0; i < m; i++) {       int k, f;       cin >> k >> f;       a[k] = f;       maxf = max(maxf, f);     }     int now = 1;     for (; now <= 100; now++) {       int line = 1;       bool ok = 1;       for (int i = 1; i <= 100; i++) {         if (a[i] && a[i] != line) {           ok = 0;           break;         }         if (i == n && ans && d[n] != line) {           mul = 1;         }         if (i % now == 0) {           line++;         }       }       if (mul && ok) {         ans = -1;         break;       }       if (ok) {         line = 1;         for (int i = 1; i <= 100; i++) {           d[i] = line;           if (i % now == 0) {             line++;           }         }         ans = d[n];       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxl = 60, w = 10000;   struct Big {     int s[maxl], l;     Big() {       memset(s, 0, sizeof(s));       l = 1;     }     inline void fix() {       for (int i = 1; i < l; i++) {         if (s[i] >= w) {           s[i + 1] += s[i] / w;           s[i] %= w;         }       }       while (s[l] >= w) {         s[l + 1] += s[l] / w;         s[l] %= w;         l++;       }     }     void fuzhi(int x) {       s[1] = x;       l = 1;       fix();     }     inline void out() {       if (s[l] >= 1000) cout << s[l] / 1000 % 10;       if (s[l] >= 100) cout << s[l] / 100 % 10;       if (s[l] >= 10) cout << s[l] / 10 % 10;       cout << s[l] % 10;       for (int i = l - 1; i >= 1; i--) {         if (s[i] < 1000) cout << 0;         if (s[i] < 100) cout << 0;         if (s[i] < 10) cout << 0;         cout << s[i];       }       cout << endl;     }   };   inline Big operator*(const Big &x, const Big &y) {     Big ans;     ans.l = x.l + y.l - 1;     for (int i = 1; i <= x.l; i++) {       for (int j = 1; j <= y.l; j++) ans.s[i + j - 1] += x.s[i] * y.s[j];     }     ans.fix();     return ans;   }   inline bool operator<(const Big &x, const Big &y) {     if (x.l != y.l) return x.l < y.l;     for (int i = x.l; i >= 1; i--)       if (x.s[i] != y.s[i]) return x.s[i] < y.s[i];     return false;   }   inline Big tran(long long x) {     Big ans;     ans.fuzhi(x);     return ans;   }   const int maxn = 770;   struct E {     int from, to, next;   } e[maxn * 2];   int te = 1, n, point[maxn];   void add(int x, int y) {     te++;     e[te].from = x;     e[te].to = y;     e[te].next = point[x];     point[x] = te;   }   int size[maxn];   Big f[maxn][maxn];   void dfs(int p, int fa) {     size[p] = 1;     for (int i = 0; i <= n; i++) f[p][i].fuzhi(1);     for (int i = point[p]; i; i = e[i].next) {       int to = e[i].to;       if (to == fa) continue;       dfs(to, p);       for (int j = size[p]; j >= 0; j--)         for (int k = size[to]; k >= 0; k--)           f[p][j + k] = max(f[p][j + k], f[p][j] * f[to][k]);       size[p] += size[to];     }     for (int i = 1; i <= size[p]; i++) f[p][0] = max(f[p][0], f[p][i] * tran(i));   }   void ceshi() {     Big a, b;     int x, y;     cin >> x >> y;     a.fuzhi(x);     b.fuzhi(y);     a.out();     b.out();     a = a * b;     a.out();     exit(0);   }   int main() {     std::ios::sync_with_stdio(false);     cin >> n;     for (int i = 1; i < n; i++) {       int x, y;       cin >> x >> y;       add(x, y);       add(y, x);     }     dfs(1, 0);     f[1][0].out();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 105;   template <typename T>   inline void read(T &AKNOI) {     T x = 0, flag = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) flag = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     AKNOI = flag * x;   }   template <typename T>   inline void cmax(T &x, T y) {     if (x < y) x = y;   }   int n, m, lb[N][N], rb[N][N], dp[N][N];   void init() {     read(n);     read(m);     for (int i = 1; i <= n; ++i) {       int k;       read(k);       while (k--) {         int l, r;         read(l);         read(r);         for (int j = l; j <= r; ++j) {           lb[i][j] = l;           rb[i][j] = r;         }       }     }   }   void solve() {     for (int l = 1; l <= m; ++l) {       for (int i = 1, j = i + l - 1; j <= m; ++i, ++j) {         for (int k = i; k <= j; ++k) {           int cnt = 0;           for (int p = 1; p <= n; ++p) {             cnt += (i <= lb[p][k] && rb[p][k] <= j);           }           cmax(dp[i][j], dp[i][k - 1] + cnt * cnt + dp[k + 1][j]);         }       }     }     printf( %d n , dp[1][m]);   }   int main() {     init();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e5 + 123;   const long long INF = (long long)1e18 + 123;   const int inf = (int)1e9 + 123;   const int MOD = (int)1e9 + 7;   int n;   int main() {     cin >> n;     if (n % 2 == 0) {       cout <<  white n ;       cout <<  1 2 ;     } else       cout <<  black ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[300000 + 10], ans = 2e9;   vector<int> lj[300000 + 10];   multiset<int> s;   int main() {     int i;     scanf( %d , &n);     for (i = 1; i <= n; ++i) scanf( %d , &a[i]), s.insert(a[i]);     for (i = 1; i < n; ++i) {       int u, v;       scanf( %d%d , &u, &v);       lj[u].push_back(v);       lj[v].push_back(u);     }     for (i = 1; i <= n; ++i) {       int da = a[i];       s.erase(s.find(a[i]));       for (int j : lj[i]) da = max(da, a[j] + 1), s.erase(s.find(a[j]));       if (!s.empty()) da = max(da, *s.rbegin() + 2);       s.insert(a[i]);       for (int j : lj[i]) s.insert(a[j]);       ans = min(da, ans);     }     printf( %d , ans);   }
#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     cin >> n;     for (int i = 1; n; i++) {       if (i & 1)         printf( %d 2 n , i), n--;       else if (n == 1)         printf( %d 1 n , i), n = 0;       else         printf( %d 1 n%d 3 n , i, i), n -= 2;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e4 + 42;   int n, m, k;   int d[55][maxn], dp[55][maxn], s[55][maxn];   int val[maxn << 2], tag[maxn << 2];   int g;   void build(int day, int l, int r, int rt) {     if (l == r) {       val[rt] = dp[day][l] + s[day + 1][l + k - 1] - s[day + 1][l - 1];       return;     }     int mid = l + r >> 1;     build(day, l, mid, rt << 1);     build(day, mid + 1, r, rt << 1 | 1);     val[rt] = max(val[rt << 1], val[rt << 1 | 1]);   }   void pushd(int rt) {     if (!tag[rt]) return;     val[rt << 1] += tag[rt];     val[rt << 1 | 1] += tag[rt];     tag[rt << 1] += tag[rt];     tag[rt << 1 | 1] += tag[rt];     tag[rt] = 0;   }   void upd(int a, int b, int l, int r, int rt, int v) {     if (a <= l && r <= b) {       val[rt] += v, tag[rt] += v;       return;     }     pushd(rt);     int mid = l + r >> 1;     if (a <= mid) upd(a, b, l, mid, rt << 1, v);     if (b > mid) upd(a, b, mid + 1, r, rt << 1 | 1, v);     val[rt] = max(val[rt << 1], val[rt << 1 | 1]);   }   void upd(int day, int pos) {     if (pos) upd(max(1, pos - k + 1), min(g, pos), 1, g, 1, -d[day][pos]);   }   void del(int day, int pos) {     if (pos) upd(max(1, pos - k + 1), min(g, pos), 1, g, 1, d[day][pos]);   }   signed main() {     ios ::sync_with_stdio(false);     cin.tie(nullptr), cout.tie(nullptr);     cin >> n >> m >> k;     g = m - k + 1;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         cin >> d[i][j];         s[i][j] = s[i][j - 1] + d[i][j];       }     for (int i = 1; i <= n; i++) {       if (i > 1) {         for (int j = 1; j < k; j++) upd(i, j);       }       for (int j = 1; j + k - 1 <= m; j++) {         int l = j, r = j + k - 1;         if (i == 1)           dp[1][j] = s[1][r] - s[1][l - 1];         else {           del(i, l - 1);           upd(i, r);           dp[i][j] = s[i][r] - s[i][l - 1] + val[1];         }       }       memset(val, 0, sizeof(val));       memset(tag, 0, sizeof(tag));       if (i < n) build(i, 1, g, 1);     }     int ans = 0;     for (int i = 1; i <= g; i++) ans = max(ans, dp[n][i]);     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     long long cnt = 0;     for (long long i = 0; i < s.length(); i++) {       if ((s[i] -  0 ) % 2 == 0) cnt++;     }     if (cnt == 0)       cout << -1 << endl;     else {       vector<char> v;       for (long long i = 0; i < s.length(); i++) v.push_back(s[i]);       char c = s[s.length() - 1];       for (long long i = 0; i < s.length(); i++) {         if ((s[i] -  0 ) % 2 == 0) {           cnt--;           if (s[i] < c || cnt == 0) {             v[i] = c;             v[s.length() - 1] = s[i];             break;           }         }       }       cout << string(v.begin(), v.end()) << endl;     }   }
#include <bits/stdc++.h>   using namespace std::chrono;   using namespace std;   const long long int mod = 998244353;   const int inf = 1e9 + 7;   long long int f(long long int x, long long int n) {     long long int r = 1;     while (n > 0) {       if (n % 2 == 1) r = (r * x) % mod;       x = (x * x) % mod;       n /= 2;     }     return r;   }   long long int fac[400010], invf[400010];   void buildfactorial() {     fac[0] = 1;     fac[1] = 1;     for (int i = 2; i < 400010; i++)       fac[i] = ((long long int)i * fac[i - 1]) % mod;     for (int i = 0; i < 400010; ++i) {       invf[i] = f(fac[i], mod - 2);     }   }   long long int C(int n, int r) {     if (n < r) return 0;     return (((fac[n] * invf[r]) % mod) * invf[n - r]) % mod;   }   long double dist(long double x1, long double y1, long double x2,                    long double y2) {     return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));   }   struct node {     int v, d, p;   };   void solve() {     int n;     cin >> n;     string s[n];     for (int i = 0; i < n; ++i) cin >> s[i];     for (int i = n - 2; i >= 0; --i) {       string t =   ;       for (int j = 0; j < min(s[i].length(), s[i + 1].length()); ++j) {         if (s[i][j] > s[i + 1][j])           break;         else if (s[i][j] < s[i + 1][j]) {           t = s[i];           break;         }         t += s[i][j];       }       s[i] = t;     }     for (int i = 0; i < n; ++i) cout << s[i] << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     t = 1;     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   int main() {     int s1, s2, s3, s4;     int a[4];     cin >> a[0] >> a[1] >> a[2] >> a[3];     int i, j, k = 0;     sort(a, a + 4);     j = a[0];     for (i = 1; i < 4; ++i) {       if (j == a[i]) {         k++;       } else         j = a[i];     }     cout << k;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e8;   const int MN = 2e+7;   int n, m, nn, mm;   int i, j, k, x, y, z, p, u, v;   int d[2000], tt;   char ss[2000][2000];   void change() {     int n = nn, m = mm;     if (x == 1) {       int tmp = u;       u = v;       v = n - tmp + 1;       swap(n, m);     }     if (x == 2) {       u = n - u + 1;       v = m - v + 1;     }     if (x == 3) {       int tmp = v;       v = u;       u = m - tmp + 1;       swap(n, m);     }     if (y == 1) {       v = m - v + 1;     }     if (z == 3) {       int tmp = u;       u = v;       v = n - tmp + 1;     }     if (z == 2) {       u = n - u + 1;       v = m - v + 1;     }     if (z == 1) {       int tmp = v;       v = u;       u = m - tmp + 1;     }   }   int main() {     scanf( %d%d%d%d%d%d , &n, &m, &x, &y, &z, &p);     nn = n;     mm = m;     x = x % 4;     y = y % 2;     z = z % 4;     for (i = 1; i <= p; i++) {       scanf( %d%d , &u, &v);       change();       printf( %d %d n , u, v);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<long long> z;   const long long N = 5e3 + 7, M1 = 1e9 + 7, M2 = 1e9 + 9, b1 = 127, b2 = 239;   long long maxi1[N], maxi2[N], maxx[N];   pair<long long, long long> h1[N], h2[N];   pair<long long, long long> poww[N];   string best;   long long mod(long long x, long long m) {     x %= m;     if (x < 0) x += m;     return x;   }   pair<long long, long long> get_h(long long l, long long r) {     long long res1 =         mod(h1[r].first - h1[l - 1].first * poww[r - l + 1].first, M1);     long long res2 =         mod(h1[r].second - h1[l - 1].second * poww[r - l + 1].second, M2);     return {res1, res2};   }   pair<long long, long long> get_h2(long long l, long long r) {     long long res1 =         mod(h2[r].first - h2[l - 1].first * poww[r - l + 1].first, M1);     long long res2 =         mod(h2[r].second - h2[l - 1].second * poww[r - l + 1].second, M2);     return {res1, res2};   }   void solve(string s) {     long long n = s.length();     string t = s;     long long ser = (n + 1) / 2;     long long len = 0;     while (len != n && s[len] == s[n - len - 1]) {       len++;     }     if (len == n) {       best = s;       return;     }     if (2 * len > (long long)best.size()) {       best =   ;       for (long long i = 0; i < len; i++) best += s[i];       for (long long i = len - 1; i >= 0; i--) best += s[i];     }     for (long long i = 0; i < ser; i++) {       maxi1[i] = -1;       long long l = i, r = i;       while (l >= 0 && r < n && s[l] == s[r]) {         l--;         r++;         maxi1[i] += 2;         if (l < len) {           long long curlen = maxi1[i] + 2 * (l + 1);           if (curlen > (long long)best.size()) {             best =   ;             for (long long j = 0; j < r; j++) {               best += s[j];             }             for (long long j = l; j >= 0; j--) {               best += s[j];             }           }         }       }     }     if (n % 2 == 1) ser--;     for (long long i = 0; i < ser; i++) {       maxi2[i] = 0;       long long l = i, r = i + 1;       while (l >= 0 && r < n && s[l] == s[r]) {         l--;         r++;         maxi2[i] += 2;         if (l < len) {           long long curlen = maxi2[i] + 2 * (l + 1);           if (curlen > (long long)best.size()) {             best =   ;             for (long long j = 0; j < r; j++) {               best += s[j];             }             for (long long j = l; j >= 0; j--) {               best += s[j];             }           }         }       }     }   }   signed main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     for (long long i = 0; i < t; i++) {       string s;       cin >> s;       best = s[0];       solve(s);       reverse(s.begin(), s.end());       solve(s);       cout << best << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   void chkmax(T& x, T y) {     x = x > y ? x : y;   }   template <class T>   void chkmin(T& x, T y) {     x = x < y ? x : y;   }   int n, m;   vector<string> s;   int calc(int mask, int j) {     int res = 0;     for (int i = 0; i < n; ++i)       if ((mask >> i & 1) != (s[i][j] -  0 )) ++res;     return res;   }   bool check(int a, int b) {     for (int i = 0; i < n - 1; ++i)       if (((a >> i & 1) + (a >> i + 1 & 1) + (b >> i & 1) + (b >> i + 1 & 1)) %               2 ==           0)         return 0;     return 1;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cin >> n >> m;     if (n >= 4) {       cout << -1 <<   n ;       return 0;     }     s.resize(n);     for (int i = 0; i < n; ++i) cin >> s[i];     vector<vector<int>> dp(m, vector<int>(1 << n, 1e9));     for (int mask = 0; mask < 1 << n; ++mask) dp[0][mask] = calc(mask, 0);     for (int i = 0; i < m - 1; ++i)       for (int c_mask = 0; c_mask < 1 << n; ++c_mask)         for (int n_mask = 0; n_mask < 1 << n; ++n_mask)           if (check(c_mask, n_mask))             chkmin(dp[i + 1][n_mask], dp[i][c_mask] + calc(n_mask, i + 1));     int ans = 1e9;     for (int mask = 0; mask < 1 << n; ++mask) chkmin(ans, dp[m - 1][mask]);     cout << ans <<   n ;     return 0;   }