malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   int a[maxn], b[maxn], ww[maxn], www[maxn];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       ww[a[i]] = 1;     }     for (int i = 1; i <= n; i++) {       scanf( %d , &b[i]);       www[b[i]] = i;     }     int t1 = 1;     while (t1 <= n && b[t1] == 0) t1++;     if (t1 == n + 1) {       printf( %d n , n);     } else {       int f = 1;       for (int i = t1; i <= n; i++) {         if (b[i] != i - t1 + 1) {           f = 0;           break;         }       }       if (f) {         printf( %d n , t1 - 1);       } else {         int x = b[n];         int fx = 0;         for (int i = n - 1; i >= 1; i--) {           if (b[i + 1] == 1) {             fx = 1;             break;           }           if (b[i] != b[i + 1] - 1) {             break;           }         }         int ans = 0;         int fx2 = 0;         if (fx) {           ans = n - b[n];           if (!ww[b[n] + 1]) {             fx2 = 1;           }           for (int i = b[n] + 2; i <= n; i++) {             if (www[i] > i - b[n] - 1) {               fx2 = 1;               break;             }           }         }         if (fx2 || fx == 0) {           for (int i = 1; i <= n; i++) {             if (ww[i]) continue;             ans = max(ans, max(0, www[i] + 1 - i) + n);           }         }         printf( %d n , ans);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int dp[110][110];   int main() {     int n, v;     cin >> n >> v;     for (int i = 1; i <= v; i++) dp[1][i] = i;     for (int i = 2; i <= n; i++) {       for (int j = 0; j < v; j++) {         dp[i][j] = min(dp[i - 1][j + 1], dp[i - 1][j] + i);       }       dp[i][v] = dp[i - 1][v] + i;     }     int ans = 1000000000;     for (int i = 0; i < v + 1; i++) ans = min(ans, dp[n][i]);     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int gi() {     int x = 0, w = 1;     char ch = getchar();     while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar();     if (ch ==  - ) w = 0, ch = getchar();     while (ch >=  0  && ch <=  9 )       x = (x << 3) + (x << 1) + ch -  0 , ch = getchar();     return w ? x : -x;   }   const int N = 3e5 + 5;   int n, m, to[N << 1], nxt[N << 1], head[N], cnt, ls[N], rs[N], dis[N], rt[N],       top[N], vis[N], key[N], tag[N];   long long ans;   void cover(int x, int v) {     if (!x) return;     key[x] += v;     tag[x] += v;   }   void down(int x) {     if (!tag[x]) return;     cover(ls[x], tag[x]);     cover(rs[x], tag[x]);     tag[x] = 0;   }   int merge(int x, int y) {     if (!x || !y) return x | y;     if (key[x] > key[y]) swap(x, y);     down(x);     rs[x] = merge(rs[x], y);     if (dis[ls[x]] < dis[rs[x]]) swap(ls[x], rs[x]);     dis[x] = dis[rs[x]] + 1;     return x;   }   void dfs(int u, int f) {     for (int e = head[u]; e; e = nxt[e])       if (to[e] ^ f) dfs(to[e], u), rt[u] = merge(rt[u], rt[to[e]]);     if (u ^ 1) {       vis[u] = 1;       while (vis[top[rt[u]]]) rt[u] = merge(ls[rt[u]], rs[rt[u]]);       if (!rt[u]) puts( -1 ), exit(0);       ans += key[rt[u]];       cover(rt[u], -key[rt[u]]);     }   }   int main() {     n = gi();     m = gi();     for (int i = 1; i < n; ++i) {       int u = gi(), v = gi();       to[++cnt] = v;       nxt[cnt] = head[u];       head[u] = cnt;       to[++cnt] = u;       nxt[cnt] = head[v];       head[v] = cnt;     }     for (int i = 1; i <= m; ++i) {       int u = gi();       top[i] = gi();       key[i] = gi();       rt[u] = merge(rt[u], i);     }     dfs(1, 0);     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 1;   int bit[N] = {0};   void update(int x, int val) {     for (; x <= N; x += x & (-x)) bit[x] += val;   }   int query(int x) {     int sum = 0;     for (; x >= 1; x -= x & (-x)) sum += bit[x];     return sum;   }   int a[N];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     int ans = 0;     for (int i = n - 1; i >= 0; i--) {       int ele = a[i];       ans += query(a[i]);       update(a[i], 1);     }     int q;     cin >> q;     while (q--) {       int a, b;       cin >> a >> b;       int r = b - a;       int x = r * (r + 1);       x /= 2;       ans += x;       if (ans % 2 == 0)         cout <<  even n ;       else         cout <<  odd n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 101;   int n, a[N][30];   long long nd[N][N], num[N];   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) cin >> nd[i][j];     memset(a, 0, sizeof(a));     for (int i = 1; i <= n; ++i)       for (int j = i + 1; j <= n; ++j) {         int run = 29;         while (run >= 0) {           if (nd[i][j] & (1 << (29 - run))) a[i][run] = a[j][run] = 1;           --run;         }       }     for (int i = 1; i <= n; ++i)       for (int k = 0; k <= 29; ++k) num[i] += (1 << k) * (a[i][29 - k]);     for (int i = 1; i <= n; ++i) cout << num[i] <<    ;     cout <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   inline int Getlen(int x) {     int res = 0;     while (x) {       res++;       x /= 10;     }     return res;   }   int PP[5];   int a[51], len[51], n, p;   int id[51];   int res[51], tot;   inline void dfs(int now, int Gmod, int Gxor) {     if (Gmod == 0 && Gxor == 0 && tot > 0) {       puts( Yes );       cout << tot << endl;       for (int i = 0; i < tot; i++) cout << res[i] <<    ;       exit(0);     }     if (now > n) return;     dfs(now + 1, Gmod, Gxor);     res[tot++] = id[now];     dfs(now + 1, (Gmod * PP[len[now]] + a[now]) % p, Gxor ^ a[now]);     tot--;   }   int main() {     rand();     rand();     rand();     rand();     rand();     rand();     int tmpp;     tmpp = 0;     tmpp++;     tmpp++;     tot = 0;     PP[0] = 1;     for (int i = 1; i <= 3; i++) PP[i] = PP[i - 1] * 10;     scanf( %d%d , &n, &p);     int now = 0, tmp;     for (int i = 1; i <= n; i++) {       scanf( %d , &tmp);       if (tmp <= 31) a[++now] = tmp, len[now] = Getlen(a[now]), id[now] = i;     }     n = now;     dfs(1, 0, 0);     puts( No );   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n + 5];     for (int i = 1; i <= n; i++) cin >> a[i];     int m = 1;     for (int i = 2; i < n; i++) {       if (abs(a[i] - a[i + 1]) < abs(a[m] - a[m + 1])) m = i;     }     if (abs(a[n] - a[1]) < abs(a[m] - a[m + 1])) {       cout << n <<     << 1;     } else       cout << m <<     << m + 1 << endl;   }
#include <bits/stdc++.h>   using namespace std;   constexpr long long maxN = 1e5 + 43;   long long n, d;   vector<long long> a;   long long suff[maxN];   long long had_max[maxN];   signed main() {     ios_base::sync_with_stdio(false);     cin >> n >> d;     a.resize(n + 1);     for (long long i = 1; i <= n; ++i) cin >> a[i];     long long ans = 0;     long long maxx = 0, minn = 0;     for (long long i = 1; i <= n; ++i) {       if (a[i] != 0) {         maxx += a[i];         minn += a[i];         maxx = min(maxx, d);         if (minn > d) {           cout << -1;           return 0;         }       } else {         if (maxx < 0) {           maxx = d;           minn = 0;           ++ans;         } else {           minn = max((long long)0, minn);         }       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void output() {     long long n;     cin >> n;     long long a[n], i;     for (i = 0; i < n; i++) cin >> a[i];     map<long long, bool> make_pair;     long long s = 0;     make_pair[0] = true;     long long c = 0;     for (i = 0; i < n; i++) {       s += a[i];       if (make_pair[s]) {         c++;         make_pair.clear();         s = a[i];         make_pair[s] = true;         make_pair[0] = true;       } else {         make_pair[s] = true;       }     }     cout << c << endl;   }   signed main() {     long long t;     t = 1;     while (t--) output();   }
#include <bits/stdc++.h>   const int INF = (int)1e9;   const double PI = acos(-1.);   using namespace std;   string s;   long long sumr, suml, need;   int main() {     cin >> s;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  ^ ) need = i;     }     for (int i = need - 1; i >= 0; i--) {       if (s[i] !=  = ) {         suml += (s[i] -  0 ) * (need - i);       }     }     for (int i = need + 1; i < s.length(); i++) {       if (s[i] !=  = ) {         sumr += (s[i] -  0 ) * (i - need);       }     }     if (sumr == suml)       cout <<  balance ;     else if (suml > sumr)       cout <<  left ;     else       cout <<  right ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s =  abcdefghijklmnopqrstuvwxyz ;     string s2 =   ;     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) s2 += s[i % k];     cout << s2 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int forbidInd;   int A[100000 + 10];   long long int memo[60][60][60];   long long int solve(int i, int k, int x) {     if (k < 0 || x < 0) return 0;     if (i == 0) {       if (x == 0 && k == 0) return 1;       if (k == 1 && x == A[i] && forbidInd != 0)         return 1;       else         return 0;     }     if (memo[i][k][x] != -1) return memo[i][k][x];     long long int ret = 0;     ret = solve(i - 1, k, x);     if (i != forbidInd) ret += solve(i - 1, k - 1, x - A[i]);     return memo[i][k][x] = ret;   }   long long int C[60][60];   int main() {     for (int i = 0; i < 51; i++)       for (int j = 0; j < 51; j++)         if (i == j || j == 0)           C[i][j] = 1;         else           C[i][j] = C[i - 1][j] + C[i - 1][j - 1];     int n, P, x;     while (cin >> n) {       int sum = 0;       for (int i = 0; i < n; i++) {         cin >> A[i];         sum += A[i];       }       cin >> P;       double ans = 0;       for (int k = 0; k < n; k++) {         forbidInd = k;         memset(memo, -1, sizeof memo);         for (int s = 1; s <= n - 1; s++)           for (int x = P - A[k] + 1; x <= P; x++) {             long long int ret = solve(n - 1, s, x);             long long int tmp = C[n][s] * (n - s);             ans = ans + ((ret * s * 1.) / (tmp * 1.0));           }       }       if (sum <= P) ans = n * 1.;       printf( %.10lf n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int nsz = 1e5, ksz = 8, msksz = (1 << ksz) - 1, inf = 0x3f3f3f3f;   bool vis[nsz + 5];   int n, m, k, full, p[nsz + 5], q[ksz + 5], dp[2][msksz + 5], ans;   struct event {     int pos, id, st;     event(int pos = 0, int id = 0, int st = 0) {       this->pos = pos, this->id = id, this->st = st;     }   };   vector<event> dat;   bool inline operator<(event a, event b) {     return a.pos != b.pos ? a.pos < b.pos : a.st < b.st;   }   void inline upd(int &a, int b) { a < b && (a = b); }   string inline bin(int S, int len) {     string s;     for (int i = 0; i < len; ++i) {       s += (S >> i & 1) ^ 48;     }     return s;   }   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= n; ++i) {       int l, r;       scanf( %d%d , &l, &r);       dat.push_back(event(l, i, +1));       dat.push_back(event(r + 1, i, -1));     }     sort(dat.begin(), dat.end());     memset(dp, -inf, sizeof(dp));     int o = 0, x = 1, l = dat[0].pos, i = 0, k = 0;     for (; i < dat.size() && dat[i].pos == l; ++i) {       p[dat[i].id] = k;       q[k] = dat[i].id;       ++k;     }     full = (1 << k) - 1;     for (int S = 0; S <= full; ++S) {       dp[o][S] = 0;     }     for (; i < dat.size();) {       l = dat[i].pos;       int il = i, ir = i, len = dat[i].pos - dat[i - 1].pos, pk = k, dk = 0,           pfull = full, dfull;       memset(vis, 0, sizeof(vis));       for (; i < dat.size() && dat[i].pos == l; ++i, ++ir) {         if (dat[i].st == -1) {           --k;           vis[p[dat[i].id]] = 1;         }       }       for (int i = 0, j = 0; i < pk; ++i) {         if (vis[i]) continue;         p[q[i]] = j;         q[j] = q[i];         ++j;       }       for (int i = il; i <= ir - 1; ++i) {         if (dat[i].st == +1) {           p[dat[i].id] = k;           q[k] = dat[i].id;           ++k;           ++dk;         }       }       full = (1 << k) - 1;       dfull = (1 << dk) - 1;       for (int S = 0; S <= pfull; ++S) {         int val = dp[o][S] + (__builtin_popcount(S) & 1) * len, nS = 0, j = 0;         if (val < 0) continue;         for (int i = 0; i < pk; ++i) {           if (vis[i]) continue;           nS |= (S >> i & 1) << j;           ++j;         }         if (dk <= 0) {           upd(dp[x][nS], val);         } else {           for (int dS = 0; dS <= dfull; ++dS) {             int nnS = nS | (dS << j);             upd(dp[x][nnS], val);           }         }       }       swap(o, x);       memset(dp[x], -inf, sizeof(dp[x]));     }     for (int S = 0; S <= full; ++S) {       upd(ans, dp[o][S]);     }     printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 200000000;   struct Point {     int x, y;     Point(int _x, int _y) {       x = _x;       y = _y;     }   };   bool operator<(const Point a, const Point b) { return a.x < b.x; }   long long solve(vector<Point> v) {     int n = (int)v.size();     vector<int> MINI(n + 1);     vector<int> MAXI(n + 1);     MINI[n] = inf;     MAXI[n] = -inf;     for (int i = n - 1; i >= 0; i--) {       MINI[i] = min(v[i].y, MINI[i + 1]);       MAXI[i] = max(v[i].y, MAXI[i + 1]);     }     long long lo = 0, hi = 1LL * inf * inf, mi;     int mini2, maxi2;     while (lo <= hi) {       mi = (lo + hi) / 2;       bool ok = 0;       int mini = inf, maxi = -inf;       for (int i = 0, j = -1; i < n; i++) {         ok = 1;         while (j + 1 <= i) {           if (1LL * (v[j + 1].x - v[i].x) * (v[j + 1].x - v[i].x) > mi) {             mini = min(mini, v[j + 1].y);             maxi = max(maxi, v[j + 1].y);             j++;           } else {             break;           }         }         mini2 = min(mini, MINI[i + 1]), maxi2 = max(maxi, MAXI[i + 1]);         if (maxi2 < mini2) {           ok = 1;         } else {           if (1LL * (maxi2 - mini2) * (maxi2 - mini2) > mi) ok = 0;           if (1LL * mini2 * mini2 + 1LL * v[i].x * v[i].x > mi) {             ok = 0;           }           if (1LL * maxi2 * maxi2 + 1LL * v[i].x * v[i].x > mi) {             ok = 0;           }           if (1LL * mini2 * mini2 + 1LL * v[j + 1].x * v[j + 1].x > mi) {             ok = 0;           }           if (1LL * maxi2 * maxi2 + 1LL * v[j + 1].x * v[j + 1].x > mi) {             ok = 0;           }         }         if (!ok) {         } else {           ok = 1;           break;         }       }       if (ok)         hi = mi - 1;       else         lo = mi + 1;     }     return lo;   }   int main() {     int n;     vector<Point> v;     cin >> n;     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       v.push_back(Point(x, y));     }     sort(v.begin(), v.end());     long long ans = solve(v);     ans = min(1LL * (v[0].x - v[n - 1].x) * (v[0].x - v[n - 1].x), ans);     for (int i = 0; i < n; i++) {       swap(v[i].x, v[i].y);     }     sort(v.begin(), v.end());     ans = min(1LL * (v[0].x - v[n - 1].x) * (v[0].x - v[n - 1].x), ans);     ans = min(ans, solve(v));     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n;   long long int aib[15][100005];   inline void update(long long int aib[], int pos, long long int val) {     for (; pos <= n; pos += ((pos) & (-(pos)))) aib[pos] += val;   }   inline long long int query(long long int aib[], int pos) {     long long int ans = 0;     for (; pos; pos -= ((pos) & (-(pos)))) ans += aib[pos];     return ans;   }   long long int dp[15][100005];   int main() {     ios_base ::sync_with_stdio(false);     int k;     cin >> n >> k;     ++k;     long long int ans = 0;     int val;     for (int i = 1; i <= n; ++i) {       cin >> val;       dp[1][i] = val;       update(aib[1], val, 1);       for (int j = 2; j <= k; ++j) {         dp[j][i] = query(aib[j - 1], val - 1);         update(aib[j], val, dp[j][i]);       }       ans += dp[k][i];     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   #define Mid ((l + r) >> 1)   #define lson (rt << 1)   #define rson (rt << 1 | 1)   using namespace std;   int read(){    char c; int num, f = 1;    while(c = getchar(),!isdigit(c)) if(c ==  - ) f = -1; num = c -  0 ;    while(c = getchar(), isdigit(c)) num = num * 10 + c -  0 ;    return f * num;   }   const int N = 2e5 + 1009;   int a, b, k;   int x[N], y[N];   signed main()   {    a = read(); b = read(); k = read();    if(k == 0) {     printf( Yes n );     for(int i = 1; i <= b; i++) printf( 1 );     for(int i = 1; i <= a; i++) printf( 0 );printf(  n );     for(int i = 1; i <= b; i++) printf( 1 );     for(int i = 1; i <= a; i++) printf( 0 );printf(  n );     return 0;    }    if(a + b < k + 2 || (a > 0 && b == 1) || a == 0) {     printf( No n );     return 0;    }    if(a >= k) {     y[1] = 1;     x[k + 1] = 1;     for(int i = a + 2; i <= a + b; i++)      x[i] = y[i] = 1;    } else {     y[1] = 1;     x[k + 1] = 1;     for(int i = a + 1; i <= k; i++) {      x[i] = y[i] = 1;     }     for(int i = k + 2; i <= a + b; i++)      x[i] = y[i] = 1;    }    reverse(x + 1, x + 1 + a + b);    reverse(y + 1, y + 1 + a + b);    printf( Yes n );    for(int i = 1; i <= a + b; i++) printf( %d , x[i]); printf(  n );    for(int i = 1; i <= a + b; i++) printf( %d , y[i]); printf(  n );    return 0;   }   /*   两个位置都为1   表示这两个位置之间的数都变成1   也就是说只用两个1就可以构造出来了      可以把中间一对0换成1   可以减少0的数量   至少需要2+k   */
#include <bits/stdc++.h>   using namespace std;   int main() {     string s, r;     cin >> s;     int k, l, x, n;     cin >> k;     l = s.length();     if (l % k != 0) {       cout <<  NO n ;       return 0;     }     x = l / k;     for (int i = 0; i < l; i += x) {       r = s.substr(i, x);       n = r.length() - 1;       for (int j = 0; j <= n / 2; j++) {         if (r[j] != r[n - j]) {           cout <<  NO n ;           return 0;         }       }     }     cout <<  YES n ;     return 0;   }
#include <iostream>   #include <algorithm>   using namespace std;   inline int gcd(int a, int b) {       int Max = max(a, b);       int Min = min(a, b);       if (Max % Min == 0)           return Min;       else           return gcd(Min, Max % Min);   }   inline int lcm(int a, int b) {       return a * b / gcd(a, b);   }   int main()   {       int t; cin >> t;       int n, k;       while (t--) {           cin >> n >> k;           int temp1 = k - (n - k);           for (int i = 1; i < temp1; ++i)               cout << i <<    ;           for (int i = k; i >= temp1; --i)               cout << i <<    ;           cout <<   n ;       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a[3] = {0};     cin >> n;     while (n--) {       cin >> a[0] >> a[1] >> a[2];       sort(a, a + 3);       int ans = 0;       if (a[0] > 0) {         ans++;         a[0]--;       }       if (a[1] > 0) {         ans++;         a[1]--;       }       if (a[2] > 0) {         ans++;         a[2]--;       }       if (a[1] > 0 && a[2] > 0) {         ans++;         a[2]--;         a[1]--;       }       if (a[0] > 0 && a[2] > 0) {         ans++;         a[0]--;         a[2]--;       }       if (a[1] > 0 && a[0] > 0) {         ans++;         a[1]--;         a[0]--;       }       if (a[0] > 0 && a[1] > 0 && a[2] > 0) {         ans++;       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 300005;   int q, n;   int a[MAXN], lef[MAXN], rig[MAXN];   vector<int> v;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> q;     while (q--) {       cin >> n;       for (int i = 1; i <= n; i++) {         lef[i] = rig[i] = -1;       }       v.clear();       for (int i = 1; i <= n; i++) {         cin >> a[i];         v.push_back(a[i]);         if (lef[a[i]] == -1) lef[a[i]] = i;         rig[a[i]] = i;       }       sort(v.begin(), v.end());       v.erase(unique(v.begin(), v.end()), v.end());       int cnt = 1, mx = 0;       for (int i = 0; i < v.size(); i++) {         if (i == v.size() - 1 || lef[v[i + 1]] < rig[v[i]]) {           mx = max(mx, cnt);           cnt = 0;         }         cnt++;       }       cout << v.size() - mx <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int i, j, qan, n, m, k, l, p;   bool mark[102];   char s;   int main() {     cin >> n;     for (i = 1; i <= n; i++) {       cin >> s;       if (s ==  * ) {         mark[i] = true;       }     }     qan = (n - 5) / 4;     for (i = 0; i <= qan; i++) {       for (j = 1; j <= n - 4 - 4 * i; j++) {         if (mark[j] == true && mark[j + i + 1] == true &&             mark[j + 2 * i + 2] == true && mark[j + 3 * i + 3] == true &&             mark[4 * i + j + 4] == true) {           cout <<  yes  << endl;           return 0;         }       }     }     cout <<  no  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int test;     cin >> test;     while (test--) {       char str[10000], cpy[10000], st[10000];       cin >> str;       int ln = strlen(str), ck = 0;       for (int i = 0, j = ln - 1; i < ln; i++, j--) {         cpy[j] = str[i];       }       for (int i = 0; i < ln; i++) {         if (str[i] == cpy[i]) {           ck = 1;         } else {           ck = 0;           cout << str << endl;           break;         }       }       if (ck) {         strcpy(st, str);         sort(str, str + ln);         if (!strcmp(str, st))           printf( -1 n );         else           cout << str << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   int a[MAXN];   int cnt[101][101];   int main() {     int n, k;     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];       cnt[i % k][a[i]]++;     }     int ans = 0;     for (int i = 0; i < k; i++) {       ans += min(cnt[i][1], cnt[i][2]);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1001001001;   const long long INFLL = 1001001001001001001LL;   template <typename T>   void pv(T a, T b) {     for (T i = a; i != b; ++i) cout << *i <<    ;     cout << endl;   }   template <typename T>   void chmin(T& a, T b) {     if (a > b) a = b;   }   template <typename T>   void chmax(T& a, T b) {     if (a < b) a = b;   }   int in() {     int x;     scanf( %d , &x);     return x;   }   double fin() {     double x;     scanf( %lf , &x);     return x;   }   long long lin() {     long long x;     scanf( %lld , &x);     return x;   }   int N, K, P, A[500005], S[500005], nxt[500005][101];   vector<int> pos[101];   int dp[101][10001];   int main() {     N = in();     K = in();     P = in();     int s = 0;     for (int i = 0; i < N; ++i) {       A[i] = in() % P;       S[i] = ((s += A[i]) %= P);       pos[s].push_back(i);     }     vector<int> nx(P, INF);     for (int i = 0; i < P; ++i) {       pos[i].push_back(INF);       nx[i] = pos[i][0];     }     s = 0;     for (int i = 0; i < N; ++i) {       for (int j = 0; j < P; ++j) {         nxt[i][(j - s + P) % P] = nx[j];       }       (s += A[i]) %= P;       nx[s] = *upper_bound(pos[s].begin(), pos[s].end(), i);     }     for (int i = 0; i <= 100; ++i) {       fill(dp[i], dp[i] + 10001, INF);     }     dp[0][0] = 0;     for (int k = 0; k + 1 < K; ++k) {       for (int x = 0; x < K * P; ++x) {         if (dp[k][x] >= N) continue;         const int f = dp[k][x];         for (int p = 0; p < P; ++p) {           if (nxt[f][p] == INF) continue;           chmin(dp[k + 1][x + p], nxt[f][p] + 1);         }       }     }     int res = INF;     for (int x = 0; x < K * P; ++x) {       if (dp[K - 1][x] >= N) continue;       int v = dp[K - 1][x];       int t = (S[N - 1] - S[v - 1] + P) % P;       chmin(res, x + t);     }     printf( %d n , res);     return 0;   }