malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, m, f[1000010], a[1000010];   char s[1000010];   bool vis[1000010];   long long Pow(int x) {     if (x < 0) return 0;     if (x == 0) return 1;     long long ans = Pow(x >> 1);     ans = ans * ans % 1000000007ll;     if (x & 1) ans = (ans * 26ll) % 1000000007ll;     return ans;   }   long long ans;   int main() {     scanf( %d%d , &n, &m);     if (m == 0) {       printf( %lld n , Pow(n));       return 0;     }     scanf( %s , s);     int len = strlen(s);     for (int i = 1, j; i < len; i++) {       for (j = f[i]; j && s[i] != s[j]; j = f[j])         ;       f[i + 1] = s[i] == s[j] ? j + 1 : 0;     }     for (int i = f[len]; i; i = f[i]) vis[len - i] = true;     scanf( %d , &a[1]);     a[1]--;     ans = Pow(a[1]);     for (int i = 2; i <= m; i++) {       scanf( %d , &a[i]);       a[i]--;       if (a[i] - a[i - 1] < len) {         if (!vis[a[i] - a[i - 1]]) {           printf( 0 );           return 0;         }       } else         ans = (ans * Pow(a[i] - a[i - 1] - len)) % 1000000007ll;     }     printf( %lld n , ans * Pow(n - a[m] - len) % 1000000007ll);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 511111;   int num[maxn];   int q[maxn];   int main() {     int i, j, n, m, k;     scanf( %d%d , &n, &m);     memset(num, 0, sizeof(num));     num[0] = 1;     for (i = 1; i <= n; i++) {       scanf( %d , &k);       for (j = 500000; j >= k; j--) {         if (num[j - k]) num[j] = 1;       }     }     int tot = 0;     for (i = 0; i <= 500000; i++)       if (num[i]) q[++tot] = i;     q[0] = 0;     int tt = 0;     int ans = 0;     q[++tot] = 1111111111;     for (i = 1; i <= tot;) {       if (q[i] - ans <= m) {         i++;       } else {         i--;         if (ans == q[i]) break;         ans = q[i];         tt++;       }     }     printf( %d %d n , ans, tt);     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:134217728 )   using namespace std;   int main() {     int p[4], a, b;     for (int i(0); i <= (4 - 1); ++i) scanf( %d , &p[i]);     scanf( %d%d , &a, &b);     int res(0);     for (int i = a; i <= b; ++i) {       int perm[] = {0, 1, 2, 3};       int k(0);       do {         if (((((i % p[perm[0]]) % p[perm[1]]) % p[perm[2]]) % p[perm[3]]) == i)           ++k;       } while (next_permutation(perm, perm + 4));       if (k >= 7) ++res;     }     printf( %d n , res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 105;   const long long MOD = 1e9 + 7;   long long dp[MAXN][MAXN], tmp[MAXN][MAXN];   void fillTmp() {     for (int i = 0; i < MAXN; i++) fill(tmp[i], tmp[i] + MAXN, 0);     return;   }   void fillDp() {     for (int i = 0; i < MAXN; i++)       for (int t = 0; t < MAXN; t++) dp[i][t] = tmp[i][t];     return;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, k, d;     cin >> n >> k >> d;     dp[0][0] = 1;     long long res = 0;     for (int i = 0; i < MAXN; i++) {       fillTmp();       for (int w = 1; w <= k; w++)         for (int t = 0; t < MAXN; t++)           for (int j = 0; j < MAXN; j++)             if (dp[t][j] != 0 && t + w < MAXN)               tmp[t + w][max(w, j)] = (dp[t][j] + tmp[t + w][max(w, j)]) % MOD;       fillDp();       for (int i = d; i < MAXN; i++) res = (res + dp[n][i]) % MOD;     }     cout << res <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int N = 1e4 + 10;   int T, n;   int id[N], cnt[10], ans[10];   char s[N];   inline int check(int id) {     int mini = inf;     for (int i = 1; i <= 4; i++) mini = min(mini, cnt[i]);     if (cnt[id] == mini) return 1;     return 0;   }   inline void cal(char *s, int flag) {     memset(cnt, 0, sizeof(cnt));     int len = strlen(s + 1);     for (int i = 1; i <= len; i++) {       if (s[i] ==  G ) cnt[1]++;       if (s[i] ==  H ) cnt[2]++;       if (s[i] ==  R ) cnt[3]++;       if (s[i] ==  S ) cnt[4]++;       if (s[i] ==  ?  && flag == 0) {         int t;         while (1) {           int t = rand() % 4 + 1;           if (check(t)) {             cnt[t]++;             break;           }         }       }     }     int mini = inf;     for (int i = 1; i <= 4; i++) mini = min(mini, cnt[i]);     for (int i = 1; i <= 4; i++)       if (cnt[i] == mini) ans[i] = 1;   }   int main() {     srand(unsigned(time(0)));     scanf( %d , &n);     scanf( %s , s + 1);     for (int i = 1; i <= 4; i++) id[i] = i;     for (int i = 1; i <= 400; i++) cal(s, 0);     if (ans[1]) puts( Gryffindor );     if (ans[2]) puts( Hufflepuff );     if (ans[3]) puts( Ravenclaw );     if (ans[4]) puts( Slytherin );     memset(ans, 0, sizeof(ans));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, sum = 0, a = 101;   int main() {     cin >> n;     while (n--) {       cin >> m;       sum += m;       if (m & 1 && m < a) a = m;     }     if (sum & 1)       cout << sum << endl;     else {       if (a == 101)         cout << 0 << endl;       else         cout << sum - a << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const bool debug = false;   const int inf = 1000 * 1000 * 1000;   const int MAKSN = 1000 + 13;   const long long MOD = 123456789LL;   int n, m, k, l, x, y, a, b, p;   string s;   vector<int> v;   void readIn() {     cin >> n >> m >> p;     x = 0;     for (int i = 0; i < (int)n; i++) {       cin >> k;       if (k % p != 0 && x == 0) {         a = i;         x = 1;       }     }     for (int i = 0; i < (int)m; i++) {       cin >> k;       if (k % p != 0) {         cout << a + i <<   n ;         return;       }     }   }   void solve() {}   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     readIn();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char str[200], pat[20][200];   map<pair<int, int>, int> H[20];   set<pair<int, int> > st;   int a[20], b[20];   int N;   int main() {     int i, j, k, h, len, ans, cnt;     scanf( %s , str);     scanf( %d , &N);     for (i = 0; i < N; i++) scanf( %s%d%d , &pat[i], &a[i], &b[i]);     for (i = 0; i < N; i++) {       len = strlen(pat[i]);       for (j = 0; j < len; j++) {         h = 0;         for (k = j; k < len; k++) {           h = h * 129 + pat[i][k];           if (!H[i].count(pair<int, int>(h, k - j + 1)))             H[i][pair<int, int>(h, k - j + 1)] = 0;           H[i][pair<int, int>(h, k - j + 1)]++;         }       }     }     ans = 0;     len = strlen(str);     for (i = 0; i < len; i++) {       h = 0;       for (j = i; j < len; j++) {         h = h * 129 + str[j];         if (st.find(pair<int, int>(h, j - i + 1)) != st.end()) continue;         st.insert(pair<int, int>(h, j - i + 1));         for (k = 0; k < N; k++) {           if (!H[k].count(pair<int, int>(h, j - i + 1)))             cnt = 0;           else             cnt = H[k][pair<int, int>(h, j - i + 1)];           if (cnt < a[k] || cnt > b[k]) break;         }         if (k == N) ans++;       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int lim = 1e6 + 1;   int criba[lim];   void fun_criba(int n) {     for (int i = 2; i <= 2; i++) {       if (criba[i] == 0)         for (int j = i + i; j <= n; j += i) {           criba[j] = i;         }     }     for (int i = 3; i <= n; i += 2) {       if (criba[i] == 0)         for (int j = i + i; j <= n; j += i) {           criba[j] = i;         }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int x;     cin >> x;     memset(criba, 0, sizeof criba);     fun_criba(x);     int a = x;     int num = x;     for (int i = x - criba[x] + 1; i < x; i++) {       if (a >= (i - criba[i])) {         a = i - criba[i];         num = i;       }     }     x = num;     if (criba[x] != 0)       num = x - criba[x] + 1;     else       num = x - criba[x];     cout << num;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char c;   int n, m, cnt, f[3600][3];   bool a[60][60], flag, heng[51][51][51], shu[51][51][51];   bool go(int x1, int y1, int x2, int y2) {     if (heng[x1][min(y1, y2)][max(y1, y2)] && shu[y2][min(x1, x2)][max(x1, x2)])       return true;     if (heng[x2][min(y1, y2)][max(y1, y2)] && shu[y1][min(x1, x2)][max(x1, x2)])       return true;     return false;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         cin >> c;         if (c ==  B ) a[i][j] = 1, f[++cnt][1] = i, f[cnt][2] = j;       }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j]) {           heng[i][j][j] = true;           for (int k = j + 1; k <= m; k++) {             if (a[i][k])               heng[i][j][k] = true;             else               break;           }         }     for (int i = 1; i <= m; i++)       for (int j = 1; j <= n; j++)         if (a[j][i]) {           shu[i][j][j] = true;           for (int k = j + 1; k <= n; k++) {             if (a[k][i])               shu[i][j][k] = true;             else               break;           }         }     flag = true;     for (int i = 1; i < cnt; i++)       for (int j = i + 1; j <= cnt; j++)         if (!go(f[i][1], f[i][2], f[j][1], f[j][2])) {           flag = false;           break;         }     if (flag)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MX = (int)1e3 + 6;   int n, ax, ay, cx, cy, bx, by;   int dirx8[] = {0, 0, 1, -1, 1, -1, 1, -1};   int diry8[] = {1, -1, 0, 0, 1, -1, -1, 1};   bool vis[MX][MX];   void fast() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   bool vaild(int &x, int &y) {     if (x < 1 || x > n || y < 1 || y > n || vis[x][y] || x == ax || y == ay ||         x + y - 1 == ax + ay - 1 || x + (n - y + 1) - 1 == ax + (n - ay + 1) - 1)       return false;     return true;   }   int main() {     cin >> n;     cin >> ax >> ay;     cin >> bx >> by;     cin >> cx >> cy;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         vis[i][j] = false;       }     }     vis[bx][by] = true;     queue<pair<int, int>> q;     q.push({bx, by});     while (!q.empty()) {       pair<int, int> p = q.front();       q.pop();       if (p.first == cx && p.second == cy) {         cout <<  YES ;         return 0;       }       for (int k = 0; k < 8; k++) {         int px = p.first + dirx8[k], py = p.second + diry8[k];         if (vaild(px, py)) {           vis[px][py] = true;           q.push({px, py});         }       }     }     cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using pii = pair<int, int>;   using vi = vector<int>;   using ll = long long;   vi prefix_function(string &s) {     int n = int((s).size()), j = 0;     vi pre(n);     for (int i = int(1); i < int(n); i++) {       while (j > 0 && s[i] != s[j]) j = pre[j - 1];       pre[i] = s[i] == s[j] ? ++j : j;     }     return pre;   }   vector<vi> kmp_automaton(string &s) {     s +=  # ;     int n = int((s).size());     vi pre = prefix_function(s);     vector<vi> aut(n, vi(26));     for (int i = int(0); i < int(n); i++)       for (int c = int(0); c < int(26); c++) {         if (i > 0 &&  a  + c != s[i])           aut[i][c] = aut[pre[i - 1]][c];         else           aut[i][c] = i + ( a  + c == s[i]);       }     return aut;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     string t, s;     cin >> t >> s;     int m = int((t).size()), n = int((s).size());     vector<vi> aut = kmp_automaton(s), dp(m + 1, vi(n + 1));     for (int i = int(m - 1); i >= int(0); i--)       for (int j = int(0); j < int(n + 1); j++) {         int &x = dp[i][j];         if (t[i] ==  ? ) {           for (int c = int(0); c < int(26); c++) {             int v = aut[j][c];             x = max(x, dp[i + 1][v] + (v == n));           }         } else {           int v = aut[j][int(t[i] -  a )];           x = dp[i + 1][v] + (v == n);         }       }     cout << dp[0][0] <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 1;   int n, m, d;   pair<long long, long long> a[N];   deque<pair<long long, long long> > D;   int res;   int ans[N];   void Input() {     cin >> n >> m >> d;     for (int i = 1; i <= (int)n; i++) {       cin >> a[i].first;       a[i].second = i;     }   }   void Process() {     sort(a + 1, a + 1 + n);     res = 1;     int t = 2;     D.push_back(pair<long long, long long>(a[1].first, 1));     ans[a[1].second] = 1;     while (t <= n) {       if (a[t].first - D.front().first >= d + 1) {         D.push_back(pair<long long, long long>(a[t].first, D.front().second));         ans[a[t].second] = D.front().second;         D.pop_front();       } else {         ++res;         D.push_back(pair<long long, long long>(a[t].first, res));         ans[a[t].second] = res;       }       ++t;     }     cout << res << endl;     for (int i = 1; i <= (int)n; i++) cout << ans[i] <<    ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     Input();     Process();   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 10;   const int maxn5 = 3e5 + 10;   const int maxn3 = 1e3 + 10;   const long long mod = 1e9 + 7;   const long long inf = 2e18;   int a[maxn5], righ[24][maxn5], lef[24][maxn5];   int mn[18][maxn5], mx[18][maxn5], n, ans[maxn5], l[maxn5], r[maxn5];   void rmq(int k) {     for (int i = 0; i < 3 * n; i++) {       mn[0][i] = i - lef[k][i % n];       mx[0][i] = i + righ[k][i % n];     }     for (int i = 1; i < 18; i++) {       for (int j = 0; j < 3 * n and (j + (1 << i) - 1) < 3 * n; j++) {         mn[i][j] = min(mn[i - 1][j], mn[i - 1][j + (1 << (i - 1))]);         mx[i][j] = max(mx[i - 1][j], mx[i - 1][j + (1 << (i - 1))]);       }     }     return;   }   int get_left(int l, int r) {     int k = 31 - __builtin_clz(r - l + 1);     return min(mn[k][l], mn[k][r - (1 << k) + 1]);   }   int get_right(int l, int r) {     int k = 31 - __builtin_clz(r - l + 1);     return max(mx[k][l], mx[k][r - (1 << k) + 1]);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < 3 * n; i++) lef[0][i] = a[i % n], righ[0][i] = a[i % n];     for (int i = 1; i < 18; i++) {       rmq(i - 1);       for (int j = 0; j < n; j++) {         if (lef[i - 1][j] >= n or righ[i - 1][j] >= n) {           lef[i][j] = maxn;           righ[i][j] = maxn;           continue;         }         lef[i][j] =             n + j - get_left(n + j - lef[i - 1][j], n + j + righ[i - 1][j]);         righ[i][j] =             get_right(n + j - lef[i - 1][j], n + j + righ[i - 1][j]) - j - n;       }     }     for (int i = 0; i < n; i++) l[i] = i + n, r[i] = i + n;     for (int i = 17; i >= 0; i--) {       rmq(i);       for (int j = 0; j < n; j++)         if (get_right(l[j], r[j]) - get_left(l[j], r[j]) < n) {           int a = get_left(l[j], r[j]);           int b = get_right(l[j], r[j]);           l[j] = a, r[j] = b;           ans[j] += (1 << i);         }     }     for (int i = 0; i < n; i++)       cout << ans[i] + (r[i] - l[i] < n - 1 ? 1 : 0) <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > vvx;   vector<pair<int, int> > vvz;   bool contained(int a, int b, int c, int d) {     if (c <= b && d >= a)       return true;     else       return false;   }   int main() {     int p, q, l, r;     cin >> p >> q >> l >> r;     vvx.resize(p + 1);     vvz.resize(q + 1);     for (int i = 0; i < p; i++) {       cin >> vvx[i].first >> vvx[i].second;     }     for (int i = 0; i < q; i++) {       cin >> vvz[i].first >> vvz[i].second;     }     int res = 0;     bool ok = false;     for (int i = l; i <= r; i++) {       ok = false;       for (int j = 0; j < p; j++) {         if (ok) {           ok = false;           break;         }         for (int k = 0; k < q; k++) {           if (contained(vvx[j].first, vvx[j].second, vvz[k].first + i,                         vvz[k].second + i)) {             ok = true;             res++;             break;           }         }       }     }     cout << res;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using pint = pair<int, int>;   using pll = pair<ll, ll>;   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     ll n, m;     cin >> n >> m;     vector<ll> a(n);     for (ll i = 0; i < (ll)n; i++) cin >> a[i];     sort(a.begin(), a.end());     reverse(a.begin(), a.end());     ll sum = 0;     for (ll i = 0; i < (ll)n; i++) sum += a[i];     if (sum < m) {       cout << -1 << endl;       return 0;     }     ll l = 0, r = n;     while (r - l > 1) {       ll mid = (l + r) / 2;       ll power = 0;       ll cnt = 0, cost = 0;       for (ll i = 0; i < (ll)n; i++) {         if (a[i] < cost) break;         power += a[i] - cost;         cnt++;         if (cnt == mid) {           cnt = 0;           cost++;         }       }       if (power >= m)         r = mid;       else         l = mid;     }     cout << r << endl;     return 0;   }
#include <bits/stdc++.h>   using std::abs;   using std::array;   using std::cerr;   using std::cin;   using std::cout;   using std::generate;   using std::make_pair;   using std::map;   using std::max;   using std::max_element;   using std::min;   using std::min_element;   using std::pair;   using std::reverse;   using std::set;   using std::sort;   using std::string;   using std::unique;   using std::vector;   template <typename T>   T input() {     T res;     cin >> res;     return res;   }   template <typename IT>   void input_seq(IT b, IT e) {     std::generate(b, e,                   input<typename std::remove_reference<decltype(*b)>::type>);   }   int main() {     std::iostream::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     string st;     cin >> st;     int prev = -1;     int ans = -10;     int i = 0;     for (char ch : st) {       char tmp[6] = { A ,  E ,  I ,  O ,  U ,  Y };       if (std::find(tmp, tmp + 6, ch) != tmp + 6) {         ans = max(ans, i - prev);         prev = i;       }       ++i;     }     ans = max(ans, int((st).size()) - prev);     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, k;   long long phi(long long x) {     long long ret = 1;     for (long long i = 2; i * i <= x; i++) {       if (x % i == 0) {         ret *= i - 1;         x /= i;         while (x % i == 0) x /= i, ret *= i;       }     }     if (x > 1) ret *= x - 1;     return ret;   }   int main() {     scanf( %I64d%I64d , &n, &k);     if (k % 2 == 0) k--;     while (k != -1 && n != 1) {       n = phi(n);       k -= 2;     }     printf( %I64d n , n % 1000000007ll);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long spf[2];   long long fac[2];   void sieve() {     spf[1] = 1;     for (long long i = 2; i < 2; i++) spf[i] = i;     for (long long i = 4; i < 2; i += 2) spf[i] = 2;     for (long long i = 3; i * i < 2; i++) {       if (spf[i] == i) {         for (long long j = i * i; j < 2; j += i)           if (spf[j] == j) spf[j] = i;       }     }   }   map<long long, long long> getfactor(long long a) {     map<long long, long long> m;     while (a > 1) {       m[spf[a]]++;       a /= spf[a];     }     return m;   }   long long power(long long x, long long y, long long p) {     long long res = 1;     x = x;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x);       y = y >> 1;       x = (x * x);     }     return res;   }   long long gcd(long long a, long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long inverse(long long a, long long p) { return power(a, p - 2, p); }   long long ncr(long long n, long long r, long long p) {     if (r == 0) return 1;     return (fac[n] * inverse(fac[r], p) % p * inverse(fac[n - r], p) % p) % p;   }   void solve() {     long long n, m;     cin >> n >> m;     vector<pair<long long, long long> > v;     multiset<long long> s;     long long a[n + 1];     for (long long i = 1; i <= n; i++) {       cin >> a[i];       s.insert(a[i]);     }     long long flag = 0;     for (long long i = 0; i < m; i++) {       long long t, r;       cin >> t >> r;       if (v.size()) {         while (v[v.size() - 1].first <= r) {           v.pop_back();           if (v.size() == 0) break;         }       }       v.push_back(make_pair(r, t));     }     v.push_back(make_pair(0, 0));     long long j = 0, cur = 0;     for (long long i = n; i >= 1; i--) {       if (i == v[j].first) {         flag++;         cur = v[j].second;         j++;       }       if (flag) {         if (cur == 1) {           auto it = s.end();           it--;           a[i] = *it;           s.erase(it);         } else {           auto it = s.begin();           a[i] = *it;           s.erase(it);         }       } else         s.erase(s.find(a[i]));     }     for (long long i = 1; i <= n; i++) cout << a[i] <<    ;     cout <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   const int cap = 1e5 + 10;   int main() {     long long a[cap];     a[1] = 0;     long long n, m;     long long Max, Min;     cin >> n >> m;     if (m == 0)       Max = n;     else {       for (int i = 2; i <= n; i++) {         a[i] = a[i - 1] + i - 1;         if (a[i] >= m) {           Max = n - i;           break;         }       }     }     Min = n - m * 2;     if (Min < 0) Min = 0;     cout << Min <<     << Max << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 2e9 + 10;   const int MOD = 1e9 + 7;   const int N = 1e6 + 10;   int n, m, b;   vector<int> st;   int main() {     ios_base::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     cin >> n;     int cnt = 0;     int ans = 0;     for (int i = 1; i <= 2 * n; i++) {       string t;       int val;       cin >> t;       if (t ==  add ) {         cin >> val;         st.push_back(val);       } else {         cnt++;         if (st.empty())           ;         else if (st.back() == cnt)           st.pop_back();         else {           st.clear();           ans++;         }       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e6 + 3;   int toint(string s) {     int ans = 0;     for (char x : s) ans = ans * 10 + (x -  0 );     return ans;   }   int lcs(string a, string b) {     int n = a.size(), m = b.size();     int dp[2][m + 1];     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= m; j++) {         if (i == 0 || j == 0)           dp[i % 2][j] = 0;         else {           if (a[i - 1] == b[j - 1])             dp[i % 2][j] = 1 + dp[1 - i % 2][j - 1];           else             dp[i % 2][j] = max(dp[i % 2][j - 1], dp[1 - i % 2][j]);         }       }     }     return dp[n % 2][m];   }   int sum(int x) {     int s = 0;     while (x) {       s += x % 10;       x /= 10;     }     return s;   }   bool equal(string s) {     for (int i = 0, j = s.size() / 2; j < s.size(); i++, j++) {       if (s[i] != s[j]) return false;     }     return true;   }   long long fact(int n) {     long long ans = 1;     for (int i = 2; i < n + 1; i++) ans = (ans * i) % mod;     return ans;   }   unsigned long long f(long long a, long long x) {     if (a < 0) return 0;     return (a / x + 1);   }   bool comp(pair<int, int> a, pair<int, int> b) {     if (a.second == b.second) return a.first > b.first;     return a.second > b.second;   }   long long power(long long a, long long b) {     if (b == 0) return 1;     long long ans = power(a, b / 2);     ans = (ans * ans) % mod;     if (b % 2 == 1) ans = (ans * a) % mod;     return ans;   }   int main() {     int n, k;     cin >> n >> k;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     int ans = INT_MAX, ind = -1;     for (int i = 0; i < k; i++) {       int j = i;       int total = 0;       do {         total += a[j];         j = (j + k) % n;       } while (j != i);       if (total < ans) {         ans = total;         ind = i + 1;       }     }     cout << ind << endl;   }