malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   long long n;   long long len[1000005];   long long pai[1000005];   string s;   long long find(long long x) {     if (pai[x] == x) return x;     return pai[x] = find(pai[x]);   }   void merge(long long a, long long b) {     long long paiA = find(a), paiB = find(b);     pai[paiB] = paiA;   }   int32_t main() {     cin.tie(NULL);     cout.tie(NULL);     ios_base::sync_with_stdio(0);     cin >> n >> s;     long long p = 0, b = 0;     pai[0] = 0;     for (long long i = 1; i <= n; i++) {       pai[i] = i;       if (s[i - 1] ==  1 ) {         p++;         b = 0;       } else if (s[i - 1] ==  0 ) {         b++;         p = 0;       } else {         p++, b++;       }       len[i - 1] = max(p, b);     }     for (long long i = 1; i <= n; i++) {       long long res = 0, pos = 0;       while (pos < n) {         pos += i - 1;         while (pos < n and len[pos] < i) {           merge(pos + 1, pos);           pos = find(pos);         }         if (pos >= n) break;         pos++;         res++;       }       cout << res <<    ;     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int const bound = 2e6 + 2, N = 5e5 + 2;   long long OO = 1e18;   double eps = 1e-6;   int oo = 1e9, mod = oo + 7;   pair<int, int> can[101];   bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {     if (a.first.first != b.first.first) return a.first.first > b.first.first;     return a.first.second < b.first.second;   }   int main() {     int n, m, k, a;     cin >> n >> k >> m >> a;     for (int(i) = 0; (i) < (a); (i)++) {       int g;       cin >> g;       g--;       can[g].first++;       can[g].second = i;     }     for (int(i) = 0; (i) < (n); (i)++) {       pair<pair<int, int>, int> tmp[101];       for (int(j) = 0; (j) < (n); (j)++) tmp[j] = {can[j], j};       int sv = tmp[i].first.second;       tmp[i].first.first += m - a;       if (m - a) tmp[i].first.second = m;       sort(tmp, tmp + n, cmp);       bool no = 1;       for (int(j) = 0; (j) < (k); (j)++) {         if (tmp[j].first.first && tmp[j].second == i) {           no = 0;           break;         }       }       if (no) {         cout << 3 <<    ;         continue;       }       int to = -1, rem = m - a;       for (int(j) = 0; (j) < (n); (j)++)         if (tmp[j].second == i) {           tmp[j].first.first -= m - a;           tmp[j].first.second = sv;         }       sort(tmp, tmp + n, cmp);       for (int(j) = 0; (j) < (n); (j)++) {         if (to != -1) {           if (rem < to - tmp[j].first.first + 1) break;           rem -= to - tmp[j].first.first + 1;           tmp[j].first.first += to - tmp[j].first.first + 1;           tmp[j].first.second = m;         }         if (tmp[j].second == i) to = tmp[j].first.first;       }       sort(tmp, tmp + n, cmp);       no = 1;       for (int(j) = 0; (j) < (k); (j)++)         if (tmp[j].first.first && tmp[j].second == i) no = 0;       if (no)         cout << 2 <<    ;       else         cout << 1 <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int z[26][200001], w[26];   int main() {     int n, m, i, j, a, le;     char str[200001];     scanf( %d %d , &n, &m);     scanf( %s , str);     for (i = 0; str[i]; i++) {       z[str[i] -  a ][w[str[i] -  a ]] = i;       w[str[i] -  a ]++;     }     long long ans;     int mi;     while (n) {       n--;       ans = 0;       scanf( %s , str);       le = strlen(str);       for (i = 0; str[i]; i++) {         a = str[i] -  a ;         if (w[a] == 0)           ans += le;         else {           mi = abs(i - z[a][0]);           ;           for (j = 1; j < w[a]; j++) {             mi = min(mi, abs(i - z[a][j]));           }           ans += mi;         }       }       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9 + 100;   const long double eps = 1e-11;   const long double pi = acos(-1.0L);   int myrand() { return rand(); }   unsigned rdtsc() {     unsigned ans;     asm( rdtsc  :  =a (ans));     return ans;   }   int rnd(int x) { return myrand() % x; }   void precalc() {}   int n, k;   int a, b, c, d;   bool read() {     if (scanf( %d%d , &n, &k) < 2) {       return false;     }     scanf( %d%d%d%d , &a, &b, &c, &d);     return true;   }   void solve() {     if (n <= 4 || k < n + 1) {       printf( -1 n );       return;     }     vector<int> path;     for (int i = 1; i <= n; i++) {       if (i != a && i != b && i != c && i != d) {         path.push_back(i);       }     }     printf( %d %d  , a, c);     for (int i = 0; i < ((int)((path).size())); i++) {       printf( %d  , path[i]);     }     printf( %d %d n , d, b);     printf( %d %d  , c, a);     for (int i = 0; i < ((int)((path).size())); i++) {       printf( %d  , path[i]);     }     printf( %d %d n , b, d);   }   int main() {     srand(rdtsc());     precalc();     while (true) {       if (!read()) {         break;       }       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     map<int, int> m;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (i != j) {           ++m[a[i] + a[j]];         }       }     }     int Max = 0;     for (auto it = m.begin(); it != m.end(); it++) {       Max = max(it->second / 2, Max);     }     cout << Max;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t;     string In;     vector<char> Que;     cin >> n >> t >> In;     for (char x : In) {       Que.push_back(x);     }     for (int i = 0; i < t; i++) {       for (int x = 0; x < n; x++) {         if (Que[x] ==  B  && Que[x + 1] ==  G ) {           Que[x] =  G ;           Que[x + 1] =  B ;           x++;         }       }     }     for (char x : Que) {       cout << x;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int SIZE = 100011;   const int MAX = 1e9;   int mi[SIZE << 2], a[SIZE];   void init(int L, int R, int id) {     mi[id] = MAX;     if (L == R) return;     int mm = (L + R) >> 1;     init(L, mm, id << 1);     init(mm + 1, R, (id << 1) + 1);   }   void ins(int L, int R, int id, int x, int v) {     if (L == R) {       mi[id] = v;       return;     }     int mm = (L + R) >> 1;     if (x <= mm)       ins(L, mm, id << 1, x, v);     else       ins(mm + 1, R, (id << 1) + 1, x, v);     mi[id] = min(mi[id << 1], mi[(id << 1) | 1]);   }   int qq(int L, int R, int id, int l1, int rr) {     if (L >= l1 && R <= rr) return mi[id];     if (R < l1 || L > rr) return MAX;     int mm = (L + R) >> 1;     return min(qq(L, mm, id << 1, l1, rr), qq(mm + 1, R, (id << 1) | 1, l1, rr));   }   int main() {     int n, s, l;     scanf( %d%d%d , &n, &s, &l);     init(0, n, 1);     ins(0, n, 1, 0, 0);     multiset<int> H;     int lt = 1, an;     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       a[i] = x;       H.insert(x);       while (*H.rbegin() - *H.begin() > s) {         H.erase(H.find(a[lt++]));       }       if (i - lt + 1 >= l) {         ins(0, n, 1, i, an = qq(0, n, 1, lt - 1, i - l) + 1);       } else         an = MAX;     }     if (an >= MAX)       puts( -1 );     else       printf( %d n , an);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, start, x;   pair<int, int> ask(int v) {     printf( ? %d n , v);     fflush(stdout);     int value, nxt;     scanf( %d %d , &value, &nxt);     return pair<int, int>(value, nxt);   }   const int RANDOM_Q = 1000;   int main() {     srand(time(NULL));     scanf( %d %d %d , &n, &start, &x);     vector<int> v;     for (int i = int(1); i < int(n + 1); i++) {       v.push_back(i);     }     random_shuffle(v.begin(), v.end());     vector<int> randoms;     for (int i = int(0); i < int(min(RANDOM_Q, (int)v.size())); i++) {       randoms.push_back(v[i]);     }     int ans = (int)2e9;     int possible_start = start, start_val = -1;     for (auto &each : randoms) {       pair<int, int> p = ask(each);       if (p.first >= x && p.first < ans) {         ans = p.first;       } else if (p.first < x && p.first > start_val) {         possible_start = p.second;         start_val = p.first;       }     }     int cur = possible_start;     for (int i = 0; cur != -1 && i < 1999 - RANDOM_Q; i++) {       pair<int, int> p = ask(cur);       if (p.first >= x) {         ans = p.first;         break;       }       cur = p.second;     }     printf( ! %d n , ans == (int)2e9 ? -1 : ans);     fflush(stdout);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[4][4], kk;   bool ok;   int main() {     for (int i = 0; i < 3; ++i)       for (int j = 0; j < 3; ++j) scanf( %d , &a[i][j]);     kk = max(a[0][1] + a[0][2], max(a[1][0] + a[1][2], a[2][0] + a[2][1])) +          100000;     for (int i = a[0][1] + a[0][2] + 1; i <= kk; ++i) {       a[0][0] = i - a[0][1] - a[0][2];       a[1][1] = i - a[1][0] - a[1][2];       a[2][2] = i - a[2][0] - a[2][1];       ok = true;       for (int j = 0; j < 3 && ok; ++j) ok &= (a[0][j] + a[1][j] + a[2][j] == i);       ok &=           (a[0][0] + a[1][1] + a[2][2] == i && a[0][2] + a[1][1] + a[2][0] == i);       if (ok) break;     }     for (int i = 0; i < 3; ++i) {       for (int j = 0; j < 3; ++j) printf( %d  , a[i][j]);       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   #define ll long long   using namespace std;      int main()   {       ios_base::sync_with_stdio(0) ;       cin.tie(0) ;       cout.tie(0) ;       int t;       cin>>t;       while(t--)       {           ll n,x,s=0,a=1;           cin>>n>>x;           ll arr[n];           for(int i=0; i<n; i++)           {               cin>>arr[i];           }              int mn=100000000,gg=mn,ind=-1;              for(int i=0; i<n; i++)           {               int y=arr[i],cnt=0;               while(!(y%x))               {                   y/=x;                   cnt++;               }               if(cnt<mn)               {                   mn=cnt;                   ind=i;               }           }              for(int i=0; i<n; i++)           {               if(i<ind)               gg=mn+1;               else                   gg=mn;               s+=arr[i];               a=1;               while(((arr[i]%x)==0)&&(gg--))               {                   a*=x;                   arr[i]/=x;                   s+=a*arr[i];               }           }              cout<<s<<  n ;          }      }
#include <bits/stdc++.h>   using namespace std;   int main() {     string str;     cin >> str;     int c = 0;     for (int j = 0; j < str.length(); j++) {       if (str[j] ==  @ ) {         c = c + 1;       }     }     if (c == 0) {       cout <<  No solution  << endl;     } else if (c > 0) {       int c1 = 1;       int i = 0;       for (; i < str.length(); i++) {         char ch = str[i];         if (ch ==  @  and (i == 0 or i == (str.length() - 1))) {           break;         } else if (ch ==  @  and (i > 0 and i < (str.length() - 1))) {           char ch1 = str[i - 1];           char ch2 = str[i + 1];           if (ch1 ==  ,  or ch2 ==  , ) {             break;           }           if (ch1 !=  ,  and ch2 !=  ,  and c1 != c) {             string str1 = str.substr(0, i + 2);             string str2 = str.substr(i + 2);             str = str1 +  ,  + str2;             c1 = c1 + 1;           }           if (ch1 !=  ,  and ch2 !=  ,  and c1 == c) {             continue;           }         }       }       if (i < str.length()) {         cout <<  No solution  << endl;       }       if (i >= str.length()) {         cout << str << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> h(n);     for (int i = 0; i < n; i++) cin >> h[i];     vector<pair<int, int> > v(n);     for (int i = 0; i < n; i++) {       v[i] = make_pair(h[i], i);     }     sort(v.begin(), v.end());     sort(h.begin(), h.end());     h.erase(unique(h.begin(), h.end()), h.end());     if (n - h.size() < 2) {       cout <<  NO  << endl;       return 0;     } else {       cout <<  YES  << endl;     }     map<int, int> x;     for (int i = 0; i + 1 < n; i++) {       if (v[i].first == v[i + 1].first) {         x[v[i].first] += 1;       }     }     int type = x.size();     for (int i = 0; i < 3; i++) {       if (i == 0) {         for (int j = 0; j < n; j++) {           printf( %d  , v[j].second + 1);         }         printf(  n );       } else {         if (type == 1) {           vector<pair<int, int> >::iterator it =               lower_bound(v.begin(), v.end(), make_pair(x.begin()->first, -1));           swap(v[it - v.begin()], v[it - v.begin() + i]);           for (int j = 0; j < n; j++) {             printf( %d  , v[j].second + 1);           }           printf(  n );         } else {           vector<pair<int, int> >::iterator it =               lower_bound(v.begin(), v.end(), make_pair(x.begin()->first, -1));           x.erase(x.begin());           swap(v[it - v.begin()], v[it - v.begin() + 1]);           for (int j = 0; j < n; j++) {             printf( %d  , v[j].second + 1);           }           printf(  n );         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 1;   int a[N];   int main() {     long long ans = 0, ones = 0, k;     string s;     cin >> k >> s;     a[0] = -1;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  1 ) ones++, a[ones] = i;       if (ones >= k) {         if (k != 0)           ans += a[ones - k + 1] - a[ones - k];         else if (s[i] ==  0 )           ans += i - a[ones];       }     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10, p = 1e9 + 7;   int n, cnt[26], inv[N], fac[N], finv[N];   char a[N], b[N];   inline int solve(char *s) {     int ans = 0, noans = fac[n];     memset(cnt, 0, sizeof cnt);     for (int i = 1; i <= n; ++i) ++cnt[a[i] -  a ];     for (int i = 0; i < 26; ++i) noans = 1ll * noans * finv[cnt[i]] % p;     for (int i = 1; i <= n; ++i) {       int v = s[i] -  a ;       noans = 1ll * noans * inv[n - i + 1] % p;       for (int c = 0; c < v; ++c)         if (cnt[c]) {           noans = 1ll * noans * cnt[c] % p;           ans = (ans + noans) % p;           noans = 1ll * noans * inv[cnt[c]] % p;         }       if (!cnt[v]) return ans;       noans = 1ll * noans * cnt[v] % p;       --cnt[v];     }     return ans;   }   int main() {     scanf( %s%s , a + 1, b + 1);     n = strlen(a + 1);     fac[0] = 1;     for (int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % p;     inv[0] = inv[1] = 1;     for (int i = 2; i <= n; ++i) inv[i] = 1ll * inv[p % i] * (p - p / i) % p;     finv[0] = 1;     for (int i = 1; i <= n; ++i) finv[i] = 1ll * inv[i] * finv[i - 1] % p;     printf( %d , (solve(b) - solve(a) - 1 + p) % p);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) {       long long n, m;       cin >> n >> m;       vector<long long> vis(n);       for (int i = 0, ggdem = m; i < ggdem; ++i) {         long long a, b, c;         cin >> a >> b >> c;         a--;         b--;         c--;         vis[b] = 1;       }       long long ve = -1;       for (int i = 0, ggdem = n; i < ggdem; ++i) {         if (vis[i] == 0) ve = i;       }       for (int i = 0, ggdem = n; i < ggdem; ++i) {         if (i != ve) {           cout << i + 1 <<     << ve + 1 <<   n ;         }       }     }     return 0;   }
#include <bits/stdc++.h>   int main() {     unsigned long long int n, sum = 0, sum1 = 0, i, MAX = 0, temp;     scanf( %llu , &n);     int a[n];     for (i = 0; i < n; ++i) {       scanf( %llu , &a[i]);     }     for (i = 0; i < n; ++i) {       sum = sum + a[i];     }     for (i = 0; i < n; ++i) {       if (a[i] > MAX) {         MAX = a[i];       }     }     for (i = 0; i < n; ++i) {       if (a[i] == MAX) {         break;       }     }     temp = a[n - 1];     a[n - 1] = MAX;     a[i] = temp;     if (sum % 2 != 0) {       printf( NO );     } else {       for (i = 0; i < n - 1; ++i) {         sum1 = sum1 + a[i];       }       if (sum1 >= a[n - 1]) {         printf( YES );       } else {         printf( NO );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, x, y;     cin >> n >> x >> y;     int a[n];     int c = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] <= x) c++;     }     if (x > y)       cout << n << endl;     else {       if (c % 2 == 0)         cout << c / 2 << endl;       else         cout << (c / 2) + 1 << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int block[101];   int nuclea[101];   int inst[101][101];   int main() {     ios_base::sync_with_stdio(0);     int n, m, k;     cin >> n >> m >> k;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> inst[j][i];       }     }     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         if (nuclea[j] == 0) block[inst[i][j]]++;       }       block[0] = 0;       for (int j = 0; j < n; j++) {         if (block[inst[i][j]] > 1 && nuclea[j] == 0) {           nuclea[j] = i + 1;         }       }       for (int i = 1; i < 101; i++) {         if (block[i] < 2) block[i] = 0;       }     }     for (int i = 0; i < n; i++) cout << nuclea[i] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3 * 100 * 1000 + 1000;   const int N1 = 5100;   long long A[N], n, k;   unsigned long long dp[N1][N1];   void read() {     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> A[i];     sort(A, A + n);     for (int i = 0; i < N1; i++)       for (int j = 0; j < N1; j++) dp[i][j] = (1 << 31);   }   int cal(int p, int q) { return p * ((n + k - 1) / k) + q * (n / k) - 1; }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     read();     int p = n % k, q = k - n % k;     dp[0][0] = 0;     for (int i = 0; i <= p; i++)       for (int j = 0; j <= q; j++) {         int last = cal(i, j);         if (i - 1 >= 0)           dp[i][j] = min(dp[i][j],                          dp[i - 1][j] + A[last] - A[last - (n + k - 1) / k + 1]);         if (j - 1 >= 0)           dp[i][j] =               min(dp[i][j], dp[i][j - 1] + A[last] - A[last - (n / k) + 1]);       }     cout << dp[p][q] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   static const double EPS = 1e-10;   template <typename T, typename S>   vector<T>& operator<<(vector<T>& a, S b) {     a.push_back(b);     return a;   }   template <typename T>   void operator>>(vector<T>& a, int b) {     while (b--)       if (!a.empty()) a.pop_back();   }   bool isprime(int n) {     if (n < 2) return false;     for (int i = 2; i * i <= n; i++)       if (n % i == 0) return false;     return true;   }   long long b_pow(long long x, long long n) {     return n ? n == 1 ? x : b_pow(x, n >> 1) * b_pow(x, (n >> 1) + (n & 1)) : 1;   }   string itos(int n) {     stringstream ss;     ss << n;     return ss.str();   }   string table[119] = {        - ,   H ,   He ,  Li ,  Be ,  B ,    C ,    N ,    O ,    F ,    Ne ,  Na ,        Mg ,  Al ,  Si ,  P ,   S ,   Cl ,   Ar ,   K ,    Ca ,   Sc ,   Ti ,  V ,        Cr ,  Mn ,  Fe ,  Co ,  Ni ,  Cu ,   Zn ,   Ga ,   Ge ,   As ,   Se ,  Br ,        Kr ,  Rb ,  Sr ,  Y ,   Zr ,  Nb ,   Mo ,   Tc ,   Ru ,   Rh ,   Pd ,  Ag ,        Cd ,  In ,  Sn ,  Sb ,  Te ,  I ,    Xe ,   Cs ,   Ba ,   La ,   Ce ,  Pr ,        Nd ,  Pm ,  Sm ,  Eu ,  Gd ,  Tb ,   Dy ,   Ho ,   Er ,   Tm ,   Yb ,  Lu ,        Hf ,  Ta ,  W ,   Re ,  Os ,  Ir ,   Pt ,   Au ,   Hg ,   Tl ,   Pb ,  Bi ,        Po ,  At ,  Rn ,  Fr ,  Ra ,  Ac ,   Th ,   Pa ,   U ,    Np ,   Pu ,  Am ,        Cm ,  Bk ,  Cf ,  Es ,  Fm ,  Md ,   No ,   Lr ,   Rf ,   Db ,   Sg ,  Bh ,        Hs ,  Mt ,  Ds ,  Rg ,  Cn ,  Uut ,  Uuq ,  Uup ,  Uuh ,  Uus ,  Uuo };   vector<int> item, goal, able[17];   int n, m;   void output(int bit, int num) {     vector<string> ans;     for (int i = 0; i < n; i++)       if (bit & (1 << i)) ans.push_back(table[item[i]]);     for (int i = 0; i < ans.size(); i++) cout << (i ?  +  :   ) << ans[i];     cout <<  ->  << table[goal[num]] << endl;   }   vector<int> currentUsing;   bool done[1 << 17][17];   bool dfs(int pos, int bit) {     if (pos == m) {       cout <<  YES  << endl;       for (int i = 0; i < pos; i++) output(currentUsing[i], i);       return true;     }     if (done[bit][pos])       return false;     else       done[bit][pos] = 1;     for (int i = 0; i < able[pos].size(); i++) {       if (bit & able[pos][i]) continue;       currentUsing.push_back(able[pos][i]);       if (dfs(pos + 1, bit | able[pos][i])) return true;       currentUsing.pop_back();     }     return false;   }   int main() {     cin >> n >> m;     string tmp;     for (int i = 0; i < n; i++)       cin >> tmp, item.push_back(find(table, table + 119, tmp) - table);     for (int i = 0; i < m; i++)       cin >> tmp, goal.push_back(find(table, table + 119, tmp) - table);     for (int i = 0; i < m; i++) {       for (int bit = 0; bit < (1 << n); bit++) {         int sum = 0;         for (int j = 0; j < n; j++)           if (bit & (1 << j)) sum += item[j];         if (sum == goal[i]) able[i].push_back(bit);       }     }     if (!dfs(0, 0)) cout <<  NO  << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   const int mod = 998244353;   int a[N], s[N], f[N][105], b[N][105];   inline void add(int &x, int y) {     x += y;     if (x >= mod) x -= mod;     return;   }   template <typename T>   inline void chkmax(T &x, T y) {     x = max(x, y);   }   template <typename T>   inline void chkmin(T &x, T y) {     x = min(x, y);   }   template <typename T>   inline void read(T &x) {     T f = 1;     x = 0;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -f;     for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c -  0 ;     x *= f;   }   int main() {     int n, k, len;     read(n), read(k), read(len);     if (len == 1) {       printf( 0 n );       return 0;     }     s[0] = 1;     for (int i = 1; i <= n; ++i) read(a[i]);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= k; ++j)         if (a[i] == j || a[i] == -1) b[i][j] = 1;     for (int j = 1; j <= k; ++j)       for (int i = 1; i <= n; ++i) b[i][j] += b[i - 1][j];     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= k; ++j) {         if (~a[i] && a[i] != j) continue;         f[i][j] = s[i - 1];         if (i >= len && b[i][j] - b[i - len][j] == len)           add(f[i][j], (f[i - len][j] - s[i - len] + mod) % mod);         add(s[i], f[i][j]);       }     }     printf( %d n , s[n]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   int head[N], Next[N], ver[N], tot;   inline void add(int u, int v) {     ver[++tot] = v, Next[tot] = head[u], head[u] = tot;   }   int pa[N][25], top[N], son[N], dep[N], ans, n, L;   long long sum[N], w[N], S;   inline void init(int u) {     for (int i = 1; i <= 20; ++i) pa[u][i] = pa[pa[u][i - 1]][i - 1];     int dis = L, tmp = u;     for (int i = 20; i >= 0; --i) {       int fa = pa[tmp][i];       if (!fa || (1 << i) >= dis) continue;       if (sum[u] - sum[fa] + w[fa] > S) continue;       dis -= (1 << i), top[u] = fa, tmp = fa;     }   }   void dfs1(int u, int fa) {     sum[u] = sum[fa] + w[u], top[u] = u, dep[u] = dep[fa] + 1, init(u);     for (int i = head[u]; i; i = Next[i]) dfs1(ver[i], u);   }   void dfs2(int u) {     int mx = -1;     for (int i = head[u]; i; i = Next[i]) {       int v = ver[i];       dfs2(v);       if (son[v] == v) continue;       if (mx == -1 || dep[mx] > dep[son[v]]) mx = son[v];     }     if (mx == -1) ++ans, mx = top[u];     son[u] = mx;   }   int main() {     scanf( %d%d%lld , &n, &L, &S);     for (int i = 1; i <= n; ++i) {       scanf( %lld , &w[i]);       if (w[i] > S) return puts( -1 ), 0;     }     for (int i = 2, f; i <= n; ++i) scanf( %d , &f), add(f, i), pa[i][0] = f;     dfs1(1, 0), dfs2(1);     printf( %d n , ans);     return 0;   }