malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   long long int M = 1000000007;   using namespace std;   vector<int> cnt(1e6 + 1);   void SieveOfEratosthenes(int n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (int p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (int i = p * p; i <= n; i += p) prime[i] = false;       }     }     int c = 0;     cnt[1] = c;     cnt[0] = c;     for (int p = 2; p <= n; p++)       if (prime[p])         cnt[p] = ++c;       else         cnt[p] = c;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout << setprecision(25);     ;     SieveOfEratosthenes(1e6 + 1);     int T;     cin >> T;     while (T--) {       int n;       cin >> n;       int r = floor(sqrt(n));       cout << 1 + cnt[n] - cnt[r] <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, k, m, n, boys[105], girls[105];     while (cin >> n >> m) {       memset(boys, 0, sizeof(boys));       memset(girls, 0, sizeof(girls));       cin >> k;       while (k--) {         cin >> j;         boys[j] = 1;       }       cin >> k;       while (k--) {         cin >> j;         girls[j] = 1;       }       int sqr = max(n, m);       sqr = sqr * sqr;       for (i = 0; i < sqr; i++) {         j = (i % n);         k = (i % m);         boys[j] = (boys[j] || girls[k]);         girls[k] = (boys[j] || girls[k]);       }       bool possible = true;       for (i = 0; i < n; i++) {         if (boys[i] == 0) possible = false;       }       for (i = 0; i < m; i++) {         if (girls[i] == 0) possible = false;       }       if (possible)         cout <<  Yes n ;       else         cout <<  No n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MN = 1e5 + 5;   int T, N, arr[MN];   void solve() {     scanf( %d , &N);     for (int i = 0; i < N; i++) scanf( %d , arr + i);     sort(arr, arr + N);     printf( %d n , min(min(arr[N - 1], arr[N - 2]) - 1, N - 2));   }   int main() {     scanf( %d , &T);     while (T--) solve();   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1210, inf = 0x3f3f3f3f;   int n, m, k, x, y, rd[N], s, t, cnt = 1, head[N], stk[N], col[N], tot, sum,                                   vis[N];   struct edge {     int to, nxt, c;   } e[N * 10];   void Adde(int x, int y, int c) {     e[++cnt].to = y;     e[cnt].nxt = head[x];     head[x] = cnt;     e[cnt].c = c;   }   void adde(int x, int y, int c) {     Adde(x, y, c);     Adde(y, x, 0);   }   void gg() {     for (int i = (1); i <= (m); i++) printf( 0  );     puts(  );   }   bool bfs() {     queue<int> q;     q.push(s);     for (int i = (1); i <= (t); i++) vis[i] = -1;     vis[s] = 1;     while (!q.empty()) {       int u = q.front();       q.pop();       for (int i = head[u], v; i; i = e[i].nxt)         if (v = e[i].to, e[i].c && vis[v] == -1) vis[v] = vis[u] + 1, q.push(v);     }     return vis[t] != -1;   }   int dfs(int u, int flow) {     if (u == t) return flow;     int used = 0, w;     for (int i = head[u], v; i; i = e[i].nxt)       if (v = e[i].to, e[i].c && vis[v] == vis[u] + 1) {         w = dfs(v, min(e[i].c, flow - used));         e[i].c -= w;         e[i ^ 1].c += w;         used += w;         if (used == flow) return flow;       }     if (!used) vis[u] = -1;     return used;   }   int dinic() {     int res = 0;     while (bfs()) res += dfs(s, inf);     return res;   }   int Main() {     scanf( %d%d%d , &n, &m, &k);     s = n + m + 1, t = s + 1;     tot = sum = 0;     for (int i = (1); i <= (m); i++) {       scanf( %d%d , &x, &y), rd[x]++, rd[y]++;       adde(i + n, x, 1), adde(i + n, y, 1), adde(s, i + n, 1);     }     for (int i = (1); i <= (n); i++)       if (rd[i] > k) adde(i, t, 2 * (rd[i] - k)), sum += 2 * (rd[i] - k);     if (dinic() == sum) {       memset(col, 0, m + 1 << 2);       for (int u = (1); u <= (n); u++) {         int top = 0;         for (int i = head[u]; i; i = e[i].nxt)           if (e[i].c) stk[++top] = e[i].to - n;         for (int i = 1; i <= top; i += 2) col[stk[i]] = col[stk[i + 1]] = ++tot;       }       for (int i = (1); i <= (m); i++)         if (!col[i]) col[i] = ++tot;       for (int i = (1); i <= (m); i++) printf( %d  , col[i]);       puts(  );     } else       gg();     cnt = 1;     memset(head, 0, t + 1 << 2);     memset(rd, 0, n + 1 << 2);     return 0;   }   int main() {     int T;     scanf( %d , &T);     while (T--) Main();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long double PI = 3.141592653589793238462643383;   struct Rcompare {     bool operator()(pair<int, int> lhs, pair<int, int> rhs) {       return lhs.first > rhs.first;     }   };   struct compare {     bool operator()(pair<int, int> lhs, pair<int, int> rhs) {       return lhs.first < rhs.first;     }   };   long long int gcd(long long int a, long long int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long int lcm(long long int a, long long int b) {     return a * (b / gcd(a, b));   }   long long int Abs(long long int a) {     if (a > 0) return a;     return -a;   }   long long int Ceil(long long int a, long long int b) {     if (a % b == 0)       return a / b;     else       return a / b + 1;   }   double Abs(double a) {     if (a > 0) return a;     return -a;   }   long long int pow(long long int x, long long int y) {     if (y == 0) return 1;     long long int tmp = pow(x, y / 2);     if (y % 2 == 1) return ((tmp * tmp) * x);     return (tmp * tmp);   }   const int MAX = 100009;   const int MOD = 1e9 + 7;   const int inf = 1e9 + 10;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int a, b, c, m;     cin >> c >> b >> a >> m;     if (2 * a < m || a > 2 * m) {       cout << -1 <<   n ;       return 0;     } else if (2 * m > 2 * b || 2 * m > 2 * c) {       cout << -1 <<   n ;       return 0;     } else {       for (int i = max(2 * m + 1, c); i <= (2 * c); ++i)         for (int j = max(2 * m + 1, b); j <= (2 * b); ++j)           for (int k = max(a, m); k <= (min(2 * a, 2 * m)); ++k) {             if (k < j && j < i) {               cout << i <<   n  << j <<   n  << k <<   n ;               return 0;             }           }     }     cout << -1 <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int arr[100005];   int main() {     long long int n, a, b, x, val, pos;     cin >> n >> a >> b;     for (long long int i = 0; i < n; i++) {       cin >> x;       long long int val = x * a;       long long int t = val / b;       long long int tb = t * b;       long long int bad = ceil((long double)tb / a);       cout << x - bad << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 99999997;   const int mmax = 200010;   const double eps = 1e-8;   const double pi = acos(-1.0);   const int inf = 0x3fffffff;   int a[200010], b[200010];   int main() {     int n, m, sa, sb;     int cnt1, cnt2, df;     while (cin >> n) {       for (int i = 0; i < n; i++) scanf( %d , &a[i]);       cin >> m;       for (int i = 0; i < m; i++) scanf( %d , &b[i]);       sort(a, a + n);       sort(b, b + m);       sa = 3 * n, sb = 3 * m;       df = sa - sb;       cnt1 = 0, cnt2 = 0;       while (cnt1 < n && cnt2 < m) {         if (a[cnt1] == b[cnt2])           cnt1++, cnt2++;         else if (a[cnt1] < b[cnt2]) {           df -= 1;           cnt1++;         } else {           df += 1;           if (df > sa - sb) {             sa = 2 * cnt1 + 3 * (n - cnt1);             sb = 2 * (cnt2 + 1) + 3 * (m - cnt2 - 1);           }           cnt2++;         }       }       if (2 * n - 2 * m > sa - sb) sa = 2 * n, sb = 2 * m;       cout << sa <<  :  << sb << endl;     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     vector<int> a(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     int lo = 0, hi = m;     while (lo < hi) {       int mid = (lo + hi) / 2;       int last = 0;       bool b = true;       for (int i = 0; i < n; ++i) {         int delta = (last - a[i] + m) % m;         if (delta <= mid) continue;         if (a[i] < last) {           b = false;           break;         }         last = a[i];       }       if (b) {         hi = mid;       } else {         lo = mid + 1;       }     }     cout << lo <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int sieve[1000003 + 5];   long long int num_primes[1000005] = {0};   void calc_sieve() {     for (long long int i = 0; i <= 1000003; i++) sieve[i] = 0;     sieve[0] = sieve[1] = 1;     for (long long int i = 2; i <= 1000003; i++) {       num_primes[i] = num_primes[i - 1];       if (sieve[i] == 0) {         num_primes[i]++;         for (long long int j = i + i; j <= 1000003; j = j + i) sieve[j]++;       }     }   }   long long int ans = -1;   bool check(long long int a, long long int b, long long int l, long long int k) {     for (long long int i = a; i <= b - l + 1; i++) {       if (num_primes[i + l - 1] - num_primes[i - 1] < k) return false;     }     ans = l;     return true;   }   void binary_search_primes(long long int a, long long int b, long long int k,                             long long int start, long long int end) {     if (end - start <= 1) {       if (!check(a, b, start, k)) check(a, b, end, k);       return;     }     long long int mid = (start + end) / 2;     if (check(a, b, mid, k)) {       binary_search_primes(a, b, k, start, mid - 1);     } else {       binary_search_primes(a, b, k, mid + 1, end);     }   }   int main() {     calc_sieve();     long long int a, b, k;     cin >> a >> b >> k;     binary_search_primes(a, b, k, 1, b - a + 1);     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int n1 = n;       int k1;       int k[n + 1];       int pri[n + 1];       for (int i = 1; i <= n; i++) {         k[i] = 0;         pri[i] = 0;       }       int f = 0;       std::vector<int> nhi;       for (int j = 1; j <= n; j++) {         f = 0;         cin >> k1;         int pr[k1 + 1];         for (int i = 1; i <= k1; i++) {           cin >> pr[i];         }         for (int i = 1; i <= k1; i++) {           if (k[pr[i]] == 0) {             k[pr[i]] = 1;             pri[j] = 1;             break;           }         }         if (pri[j] == 0) {           nhi.push_back(j);         }       }       if (nhi.empty())         cout <<  OPTIMAL               <<   n ;       else {         int g;         for (int i = 1; i <= n; i++) {           if (k[i] == 0) {             g = i;             break;           }         }         cout <<  IMPROVE               <<   n ;         cout << nhi[0] <<     << g <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char buf[25];   const int maxn = 1000010;   int a[maxn * 2], b[maxn * 2];   int n, m, x, y, t, mx;   int read() {     int x = 0, f = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = 1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     return f ? -x : x;   }   void write(int x) {     if (!x) {       putchar( 0 );       return;     }     if (x < 0) {       putchar( - );       x = -x;     }     int cnt = 0;     while (x) {       buf[++cnt] =  0  + x % 10;       x /= 10;     }     for (int i = cnt; i >= 1; --i) putchar(buf[i]);   }   int main() {     t = read();     for (int i = 1; i <= t; ++i) {       int v = read();       mx = max(mx, v);       ++a[v];     }     int x = 1;     for (int i = 1; i <= mx; ++i)       if (a[i] != i * 4) {         x = i;         break;       }     for (int i = x * 2 - 1; i <= t; ++i)       if (t % i == 0) {         n = i;         m = t / i;         y = n + m - x - mx;         if (y <= 0 || y * 2 - 1 > m) continue;         bool flag = true;         int t1 = 0, t2 = 0;         for (int j = 1; j <= n; ++j) {           bool f = true;           for (int k = 1; k <= m; ++k) {             t1 = j;             t2 = k;             int tmp = abs(j - x) + abs(k - y);             ++b[tmp];             if (b[tmp] > a[tmp]) {               flag = false;               f = false;               break;             }           }           if (!f) break;         }         for (int j = 1; j <= n; ++j) {           bool f = true;           for (int k = 1; k <= m; ++k) {             int tmp = abs(j - x) + abs(k - y);             --b[tmp];             if (j == t1 && k == t2) {               f = false;               break;             }           }           if (!f) break;         }         if (flag) {           write(n);           putchar(   );           write(m);           putchar(  n );           write(x);           putchar(   );           write(y);           putchar(  n );           return 0;         }       }     write(-1);     putchar(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     int a[n], i, j, k, cnt = 1e9, tmp = 0;     for (i = 0; i < n; i++) cin >> a[i];     for (i = 0; i < s.size() - 1; i++) {       if (s[i] ==  R  && s[i + 1] ==  L ) {         tmp = (a[i + 1] - a[i]) / 2;         if (tmp < cnt) cnt = tmp;       }     }     if (cnt == 1e9)       cout << -1 << endl;     else       cout << cnt << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, q;   const int max_w = 100;   vector<pair<int, int>> cnt_sort(vector<pair<int, int>>& a) {     vector<int> cnt(n * max_w + 5);     for (int i = 0; i < a.size(); ++i) {       ++cnt[a[i].first + 1];     }     for (int i = 1; i < cnt.size(); ++i) {       cnt[i] += cnt[i - 1];     }     vector<pair<int, int>> res(a.size());     for (int i = 0; i < a.size(); ++i) {       res[cnt[a[i].first]++] = a[i];     }     return res;   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m >> q;     vector<int> w(n);     for (int i = 0; i < n; ++i) {       cin >> w[i];     }     reverse(w.begin(), w.end());     vector<int> cnt(1 << n, 0);     string s;     for (int i = 0; i < m; ++i) {       cin >> s;       int res = 0;       for (int j = 0; j < n; ++j) {         res <<= 1;         res |= (s[j] ==  1 );       }       ++cnt[res];     }     vector<vector<pair<int, int>>> ans(1 << n, vector<pair<int, int>>());     vector<vector<int>> sum(1 << n, vector<int>(1 << n));     for (int i = 0; i < ans.size(); ++i) {       for (int j = 0; j < cnt.size(); ++j) {         if (cnt[j] == 0) continue;         int kok = i ^ j;         int res = 0;         for (int k = 0; k < n; ++k) {           if (!((kok >> k) & 1)) {             res += w[k];           }         }         ans[i].push_back({res, cnt[j]});       }       ans[i] = cnt_sort(ans[i]);       sum[i][0] = ans[i][0].second;       for (int j = 1; j < ans[i].size(); ++j) {         sum[i][j] = sum[i][j - 1] + ans[i][j].second;       }     }     int k;     while (q--) {       cin >> s >> k;       int res = 0;       for (int j = 0; j < n; ++j) {         res <<= 1;         res |= (s[j] ==  1 );       }       int j =           upper_bound(ans[res].begin(), ans[res].end(), make_pair(k, 100000000)) -           ans[res].begin();       --j;       if (j < 0) {         cout << 0;       } else {         cout << sum[res][j];       }       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int p[1000006], n, k;   int main() {     ios::sync_with_stdio(false);     cin >> n >> k;     iota(p + 1, p + n + 1, 1);     for (int i = 2; i <= n; i++) {       if (p[i] == i) {         p[i]--;         for (int j = i + i; j <= n; j += i) p[j] = p[j] / i * (i - 1);       }     }     sort(p + 1, p + n + 1);     long long ans = 0;     for (int i = 1; i <= k + 2; i++) {       ans += p[i];     }     if (k == 1) ans--;     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   const int N = 200005;   const int M = 500005;   using namespace std;   pair<int, int> a[N];   int p[20][M];   int main() {     int n, m;     scanf( %d%d , &n, &m);     int i, j;     for (i = 0; i < n; i++) {       scanf( %d%d , &a[i].first, &a[i].second);     }     sort(a, a + n);     memset(p, -1, sizeof(p));     int maxe = -1;     j = 0;     for (i = 0; i < M; i++) {       while (j < n && a[j].first <= i) {         if (a[j].second > maxe) maxe = a[j].second;         j++;       }       if (maxe >= i)         p[0][i] = maxe;       else         p[0][i] = -1;     }     for (j = 0; j < 19; j++)       for (i = 0; i < M; i++)         if (p[j][i] == -1)           p[j + 1][i] = -1;         else           p[j + 1][i] = p[j][p[j][i]];     int u, v;     for (i = 0; i < m; i++) {       scanf( %d%d , &u, &v);       int ans = 0;       for (j = 19; j >= 0; j--)         if (p[j][u] < v) {           u = p[j][u];           ans += 1 << j;         }       u = p[0][u];       ans++;       if (u >= v)         printf( %d n , ans);       else         printf( -1 n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, t;   int main() {     cin >> t;     while (t--) {       int s = 0, a;       cin >> n >> m;       for (int i = 1; i <= n; i++) {         cin >> a;         s += 2 * a;       }       if (m < n || n < 3) {         cout << -1 << endl;         continue;       }       cout << s << endl;       for (int i = 1; i < n; i++) cout << i <<     << i + 1 << endl;       cout << n <<     << 1 << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   void solve();   int main() {     ios::sync_with_stdio(0);     solve();     return 0;   }   list<pair<deque<int>, int> > basket;   int N, bN;   int p[100001];   void update(pair<deque<int>, int>& it) {     it.second = it.first[0];     for (int i = 0; i < (int)((it.first).size()); i++) {       it.second = max(it.second, it.first[i]);     }   }   void solve() {     cin >> N;     bN = 250;     basket.push_back(pair<deque<int>, int>(deque<int>(), 0));     for (int i = 0; i < N; i++) {       int a, c;       cin >> a >> c;       p[a] = i + 1;       int cc = c;       list<pair<deque<int>, int> >::iterator it = basket.begin();       for (; it != basket.end(); it++) {         if (it->second > a || (int)it->first.size() > cc) {           break;         }         cc -= it->first.size();       }       if (it == basket.end()) {         it--;         cc += it->first.size();       }       deque<int>& b = it->first;       b.push_front(a);       for (int j = 0; j < (int)((b).size()) - 1 && cc > 0; j++, cc--) {         if (b[j] < b[j + 1]) break;         swap(b[j], b[j + 1]);       }       if ((int)((b).size()) >= 2 * bN) {         pair<deque<int>, int> item(deque<int>(), 0);         while ((int)((b).size()) > bN) {           item.first.push_back(b.front());           b.pop_front();         }         update(*it);         update(item);         basket.insert(it, item);       } else {         update(*it);       }     }     vector<int> ans;     for (list<pair<deque<int>, int> >::iterator it = basket.begin();          it != basket.end(); it++) {       deque<int>& b = it->first;       for (int i = 0; i < (int)((b).size()); i++) {         ans.push_back(p[b[i]]);       }     }     reverse(ans.begin(), ans.end());     for (int i = 0; i < (int)((ans).size()); i++) cout << ans[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 5005;   const int maxm = maxn * 2;   const int maxe = 200005;   const int mod = 1e9 + 7;   int n, a, b;   string str;   int cc[26][maxn][26];   int exist[26];   int main() {     cin >> str;     n = str.length();     for (int i = 0; i < n; i++) {       int idx = str[i] -  a ;       exist[idx]++;       for (int j = 1; j < n; j++) {         int pos = (i + j) % n;         int idx2 = str[pos] -  a ;         cc[idx][j][idx2]++;       }     }     double ans = 0;     for (int i = 0; i < 26; i++) {       double cccc = 0;       for (int j = 1; j < n; j++) {         double tp = 0;         for (int ii = 0; ii <= n; ii++) {           if (str[ii] -  a  != i) continue;           int pos = (ii + j) % n;           int idx2 = str[pos] -  a ;           if (cc[i][j][idx2] == 1) tp++;         }         cccc = max(cccc, tp);       }       ans += cccc;     }     printf( %.15lf n , ans / n);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, k;     scanf( %lld%lld%lld , &n, &m, &k);     if (n % 2 == 0) {       printf( Marsel n );       return 0;     }     if (n % 2 == 1) {       if (k == 1 && m > 1) {         printf( Timur n );         return 0;       }       for (long long a = 2; a <= sqrt(m); a++)         if (m % a == 0)           if ((m / a) >= k) {             printf( Timur n );             return 0;           }     }     printf( Marsel n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:33554432 )   int N, Q, cnt[3][100005];   char A[100005];   int main() {     int i, j, k;     scanf( %s , A + 1);     N = strlen(A + 1);     for (i = 1; i <= N; i++) {       for (j = 0; j < 3; j++) cnt[j][i] += cnt[j][i - 1];       cnt[A[i] -  x ][i]++;     }     for (scanf( %d , &Q); Q--;) {       int c[3] =           {               0,           },           s, e;       scanf( %d%d , &s, &e);       for (i = 0; i < 3; i++) c[i] = cnt[i][e] - cnt[i][s - 1];       int mx = 0, mn = 1e9;       for (i = 0; i < 3; i++) mx = max(mx, c[i]), mn = min(mn, c[i]);       if (e - s + 1 < 3 || mx - mn < 2)         puts( YES );       else         puts( NO );     }   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, i, j, k, it, t;   long long r[101], rr[101], c[101], cc[101];   long long a[101][101];   long long R, C, x;   vector<long long> ans, ans2;   int main() {     cin >> n >> m;     int k = 1;     x = 3;     while (n != 0) {       if (n % 2 == 1) {         k = k * x % m;       }       x = x * x % m;       n = n / 2;     }     cout << (k - 1 + m) % m;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double pi = 3.1415926536;   int main() {     int n;     cin >> n;     int r[n];     for (int i = 0; i < n; i++) cin >> r[i];     double area = 0;     sort(r, r + n);     for (int i = n - 1; i >= 0; i -= 2) {       if (i > 0)         area += (r[i] * r[i] - r[i - 1] * r[i - 1]);       else         area += r[0] * r[0];     }     area *= pi;     printf( %0.10lf , area);     return 0;   }
#include <bits/stdc++.h>   const bool DEBUG = false;   using namespace std;   const long long maxn = 1e5 + 2;   const long long INF = 4e18;   const long long M = 1e9 + 7;   const int lg = 21;   const int z = 256 * 256;   int n, m, a[maxn], t[maxn], dp[maxn];   string s;   vector<int> g[2], v[2];   bool f(vector<int>(&v), int l, int r) {     if (lower_bound(v.begin(), v.end(), l) == v.end()) return 1;     int l1 = lower_bound(v.begin(), v.end(), l) - v.begin();     if (lower_bound(v.begin(), v.end(), r + 1) == v.end()) return 0;     int l2 = lower_bound(v.begin(), v.end(), r + 1) - v.begin();     if (l1 == l2) return 1;     return 0;   }   bool check(int l, int r) {     if (l % 2 == 0)       if (f(v[1], l, r) and f(g[0], l, r)) return 1;     if (l % 2 == 1)       if (f(v[0], l, r) and f(g[1], l, r)) return 1;     return 0;   }   int par(int l, int r) {     if (l == 0) return a[r];     return a[r] - a[l - 1];   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> s >> m;     a[0] = bool(s[0] ==  ? );     for (int i = 1; i < n; i++) a[i] = a[i - 1] + bool(s[i] ==  ? );     for (int i = 0; i < n; i++)       if (s[i] ==  a )         v[(i % 2)].push_back(i);       else if (s[i] ==  b )         g[(i % 2)].push_back(i);     t[n - m] = check(n - m, n - 1);     for (int i = n - m - 1; i >= 0; i--) {       if (check(i, i + m - 1))         t[i] = 1 + t[i + m];       else         t[i] = t[i + 1];     }     int maxx = t[0];     if (t[n - m] == 1) dp[n - m] = par(n - m, n - 1);     for (int i = n - m - 1; i >= 0; i--) {       if (check(i, i + m - 1)) {         dp[i] = par(i, i + m - 1) + dp[i + m];         if (t[i + 1] == t[i]) dp[i] = min(dp[i], dp[i + 1]);       } else         dp[i] = dp[i + 1];     }     cout << dp[0] << endl;     return 0;   }