malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, r, f[60005];   int dp[105][60005];   struct node {     int a, b, sign;   } pro[105];   bool cmp(node a, node b) {     if (a.sign == b.sign) {       if (a.sign == 1)         return a.a < b.a;       else         return a.a + a.b > b.a + b.b;     }     return a.sign < b.sign;   }   int main() {     scanf( %d%d , &n, &r);     for (register int i = 1; i <= n; i++) {       scanf( %d%d , &pro[i].a, &pro[i].b);       if (pro[i].b >= 0)         pro[i].sign = 1;       else         pro[i].sign = 2, pro[i].a = max(pro[i].a, abs(pro[i].b));     }     sort(pro + 1, pro + 1 + n, cmp);     int s = r, sum = 0;     vector<node> aa;     for (register int i = 1; i <= n; i++) {       if (pro[i].sign == 1 && s >= pro[i].a)         s += pro[i].b, sum++;       else         aa.push_back(pro[i]);     }     dp[0][s] = sum;     for (int i = 0; i < (int)(aa.size()); i++)       for (register int j = 0; j <= s; j++) {         if (j >= aa[i].a)           dp[i + 1][j + aa[i].b] = max(dp[i + 1][j + aa[i].b], dp[i][j] + 1);         dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);       }     sum = 0;     for (register int i = 0; i <= s; i++) sum = max(sum, dp[(int)(aa.size())][i]);     printf( %d n , sum);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1000000000;   struct sTree {     int pl, pr;     int val, cnt, debt;   } tree[4000009];   int treeSize;   int NewNode(int LL, int RR) {     int cur = ++treeSize;     tree[cur].cnt = RR - LL + 1;     return cur;   }   void CalcDebt(int LL, int RR, int cur);   void Add(int L, int R, int d, int LL, int RR, int cur) {     if (L <= LL && R >= RR) {       tree[cur].debt += d;       tree[cur].val += d;       return;     }     int M = LL + (RR - LL) / 2;     if (!tree[cur].pl) tree[cur].pl = NewNode(LL, M);     if (!tree[cur].pr) tree[cur].pr = NewNode(M + 1, RR);     CalcDebt(LL, RR, cur);     if (L <= M && LL <= R) {       Add(L, R, d, LL, M, tree[cur].pl);     }     if (L <= RR && M + 1 <= R) {       Add(L, R, d, M + 1, RR, tree[cur].pr);     }     if (tree[tree[cur].pl].val < tree[tree[cur].pr].val) {       tree[cur].val = tree[tree[cur].pl].val;       tree[cur].cnt = tree[tree[cur].pl].cnt;     } else if (tree[tree[cur].pl].val > tree[tree[cur].pr].val) {       tree[cur].val = tree[tree[cur].pr].val;       tree[cur].cnt = tree[tree[cur].pr].cnt;     } else {       tree[cur].val = tree[tree[cur].pl].val;       tree[cur].cnt = tree[tree[cur].pl].cnt + tree[tree[cur].pr].cnt;     }   }   int Get(int cur) {     if (tree[cur].val == 0) {       return 2 * inf + 1 - tree[cur].cnt;     }     return 2 * inf + 1;   }   void CalcDebt(int LL, int RR, int cur) {     int M = LL + (RR - LL) / 2;     Add(LL, M, tree[cur].debt, LL, M, tree[cur].pl);     Add(M + 1, RR, tree[cur].debt, M + 1, RR, tree[cur].pr);     tree[cur].debt = 0;   }   int n;   struct event {     char type;     int p, L, R;   } e[200009];   bool operator<(event a, event b) {     if (a.p != b.p) return a.p < b.p;     return a.type < b.type;   }   int main() {     ios::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < n; i++) {       int x1, y1, x2, y2;       cin >> x1 >> y1 >> x2 >> y2;       x1 += inf, y1 += inf, x2 += inf, y2 += inf;       e[2 * i].type =  + ;       e[2 * i].p = min(x1, x2) - 1;       e[2 * i].L = min(y1, y2);       e[2 * i].R = max(y1, y2);       e[2 * i + 1].type =  - ;       e[2 * i + 1].p = max(x1, x2);       e[2 * i + 1].L = min(y1, y2);       e[2 * i + 1].R = max(y1, y2);     }     sort(e, e + 2 * n);     int root = NewNode(0, 2 * inf);     long long ans = 0;     int prev = -1;     for (int i = 0; i < 2 * n; i++) {       int j = i;       while (j + 1 < 2 * n && e[j + 1].p == e[i].p) j++;       ans += 1LL * Get(root) * (e[i].p - prev);       for (int k = i; k <= j; k++) {         if (e[k].type ==  + ) {           Add(e[k].L, e[k].R, 1, 0, 2 * inf, root);         } else {           Add(e[k].L, e[k].R, -1, 0, 2 * inf, root);         }       }       i = j;       prev = e[i].p;     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, m;     cin >> n >> m;     map<int, int> a;     long long x;     for (long long i = 0; i < m; i++) {       cin >> x;       a[x]++;     }     long long mini = INT_MAX;     for (long long i = 1; i <= n; i++) {       if (mini > a[i]) {         mini = a[i];       }     }     cout << mini << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int x;     string s1, s2;     int cnt = 0;     cin >> x >> ws;     for (int i = x; i; i--) {       cin >> s1;       cin >> s2;       if (s2.compare( soft ) == 0) cnt++;     }     if (x - cnt > cnt) cnt = x - cnt;     for (int i = 0; i < 100; ++i) {       if (i * i / 2 + i % 2 >= cnt && i * i >= x) {         cout << i << endl;         return 0;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1, c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f ^= 1;     for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ;     return f ? x : -x;   }   const int N = 200005;   int n, m;   char s[N];   struct H {     int a, b;   };   H h[N][26], pw[N];   const int mul1 = 233, mod1 = 998244353;   const int mul2 = 19260817, mod2 = 1000000007;   vector<H> cal(int x, int l) {     vector<H> ans;     for (int i = 0; i < 26; ++i) {       H a = (H){};       a.a = (h[x + l][i].a - 1LL * h[x][i].a * pw[l].a % mod1 + mod1) % mod1;       a.b = (h[x + l][i].b - 1LL * h[x][i].b * pw[l].b % mod2 + mod2) % mod2;       ans.push_back(a);     }     sort(ans.begin(), ans.end(), [&](H a, H b) {       if (a.a != b.a) return a.a < b.a;       return a.b < b.b;     });     return ans;   }   int main() {     n = read(), m = read();     scanf( %s , s);     pw[0].a = pw[0].b = 1;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < 26; ++j) {         h[i + 1][j].a = 1LL * h[i][j].a * mul1 % mod1;         h[i + 1][j].b = 1LL * h[i][j].b * mul2 % mod2;       }       h[i + 1][s[i] -  a ].a = (h[i + 1][s[i] -  a ].a + 1) % mod1;       h[i + 1][s[i] -  a ].b = (h[i + 1][s[i] -  a ].b + 1) % mod2;       pw[i + 1].a = 1LL * pw[i].a * mul1 % mod1;       pw[i + 1].b = 1LL * pw[i].b * mul2 % mod2;     }     for (; m--;) {       int x = read() - 1, y = read() - 1, l = read();       vector<H> a = cal(x, l), b = cal(y, l);       bool ok = 1;       for (int i = 0; i < 26; ++i) {         if (a[i].a != b[i].a || a[i].b != b[i].b) ok = 0;       }       puts(ok ?  YES  :  NO );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     int cir = 0;     bool ans = true;     int now;     for (int i = 0; i < n; i++) {       if (!i) {         scanf( %d , &now);         continue;       }       int temp;       scanf( %d , &temp);       if (temp > now) {         if (cir != 0) {           ans = false;         }         now = temp;       } else if (temp == now) {         if (cir == 0) {           cir = 1;         } else if (cir == 1) {         } else {           ans = false;         }       } else {         if (cir == 0 || cir == 1) {           cir = 2;         }       }       now = temp;     }     if (ans)       printf( YES n );     else       printf( NO n );   }
#include <bits/stdc++.h>   using namespace std;   int c[3];   void solve() {     int m, n;     cin >> m >> n;     vector<vector<int>> matrix(m, vector<int>(n));     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         cin >> matrix[i][j];       }     }     for (int i = m - 1; i >= 0; --i) {       for (int j = n - 1; j >= 0; --j) {         if (matrix[i][j] != 0) {           continue;         } else {           int check;           check = matrix[i][j + 1] - 1;           if (check >= matrix[i + 1][j]) check = matrix[i + 1][j] - 1;           matrix[i][j] = check;         }       }     }     for (int i = m - 1; i >= 0; --i) {       for (int j = n - 1; j >= 0; --j) {         if (i != 0 && j != 0) {           if ((matrix[i][j] <= matrix[i][j - 1]) ||               (matrix[i][j] <= matrix[i - 1][j])) {             cout <<  -1  << endl;             return;           }         }       }     }     int sum = 0;     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         sum += matrix[i][j];       }     }     cout << sum << endl;     return;   }   int main() {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t, x, y, f1, f2, f3, f4, LIMH = 100000, LIML = -100000;     cin >> t;     while (t--) {       int q;       cin >> q;       int hiX, loX, hiY, loY;       hiX = LIMH;       hiY = LIMH;       loX = LIML;       loY = LIML;       for (int i = 0; i < q; i++) {         cin >> x >> y >> f1 >> f2 >> f3 >> f4;         if (!f1 && x > loX) loX = x;         if (!f2 && y < hiY) hiY = y;         if (!f3 && x < hiX) hiX = x;         if (!f4 && y > loY) loY = y;       }       if (loX > hiX || loY > hiY)         cout << 0 << endl;       else         cout << 1 <<     << loX <<     << loY << endl;     }     return 0;   }
#include <iostream>      using namespace std;   int c1=0,c2=0,cur1=1,cur2=1,ok1=0,ok2=0;   pair<int,int> f1[105*105],f2[150*105];   void fa1(int val){       cout<<val<<   ;       cout<<f1[cur1].first<<   <<f1[cur1].second<<  n ;       cur1++;       if(cur1==c1+1)           ok1=1;   }   void fa2(int val){       cout<<val<<   ;       cout<<f2[cur2].first<<   <<f2[cur2].second<<  n ;       cur2++;       if(cur2==c2+1){           ok2=1;       }   }   int main()   {       int n,a;       cin>>n;       int t=n*n;       for(int i=1;i<=n;i++){           for(int j=1;j<=n;j++){               if((i+j)%2==1){                   f1[++c1]=make_pair(i,j);               }               else{                   f2[++c2]=make_pair(i,j);               }           }       }       while(t--){           cin>>a;           if(a==1){               if(ok2==1){                   fa1(3);               }               else{                   fa2(2);               }           }           if(a==2){               if(ok1==1){                   fa2(3);               }               else{                   fa1(1);               }           }           if(a==3){               if(ok1==1){                   fa2(2);               }               else{                   fa1(1);               }           }           cout<<  n ;       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     string str;     for (int o = 0; o < t; o++) {       cin >> str;       int n = str.length();       int pre = 0, suf = 0, total = 0;       if (str[0] ==  1 ) {         pre = 0;       } else {         pre = 1;         for (int i = 1; i < n; i++) {           if (str[i] ==  0 ) {             pre = pre + 1;           } else {             break;           }         }       }       if (str[n - 1] ==  1 ) {         suf = 0;       } else {         suf = 1;         for (int j = n - 2; j > 0; j--) {           if (str[j] ==  0 ) {             suf = suf + 1;           } else {             break;           }         }       }       for (int k = 0; k < n; k++) {         if (str[k] ==  0 ) {           total = total + 1;         }       }       int ans = total - suf - pre;       if (pre == total) {         cout <<  0  << endl;       } else {         cout << ans << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100000 + 1;   int n, m, s;   int b[maxn];   int p[maxn];   int ansn, anst;   bool ok(int k) {     int i = n - 1;     int j = k - 1;     int tansn = 0, tanst = 0;     int ss = s;     while (j >= 0) {       if (i >= 0 && b[i] >= p[j]) {         tanst += p[j];         tansn++;         j--;         i--;       } else if (i >= 0 && p[j] > b[i] && ss >= p[j] - b[i]) {         tanst += b[i];         tansn++;         ss -= (p[j] - b[i]);         j--;         i--;       } else if (i >= 0)         i--;       else         j--;     }     if (ss > 0) tanst = max(0, tanst - ss);     if (tansn == k && tansn > ansn) {       ansn = tansn;       anst = tanst;     }     return tansn == k;   }   int main() {     scanf( %d %d %d , &n, &m, &s);     for (int i = 0; i < n; i++) scanf( %d , b + i);     for (int i = 0; i < m; i++) scanf( %d , p + i);     sort(b, b + n);     sort(p, p + m);     int l = 0;     int r = m + 1;     int mid;     while (l + 1 < r) {       mid = (l + r) / 2;       if (ok(mid))         l = mid;       else         r = mid;     }     printf( %d %d n , ansn, anst);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     char ch[6] =  aaaaa ;     int ara[4] = {0};     cin >> s;     map<char, int> ms, ms2;     for (int i = 0; i < s.size(); i += 4) {       for (int j = 0; j < 4 && (i + j) < s.size(); j++) {         if (s[i + j] ==  ! ) {           ara[j]++;         } else {           ch[j] = s[i + j];           if (!ms2[ch[j]]) ms2[ch[j]] = j;         }       }     }     int sz = s.size();     for (int i = 0; i < 4; i++) {       if (ch[i] ==  a ) {         if (!ms2[ R ]) {           ch[i] =  R ;           ms2[ R ] = 1;         } else if (!ms2[ G ]) {           ch[i] =  G ;           ms2[ G ] = 1;         } else if (!ms2[ B ]) {           ch[i] =  B ;           ms2[ B ] = 1;         } else if (!ms2[ Y ]) {           ch[i] =  Y ;           ms2[ Y ] = 1;         }       }       ms[ch[i]] = ara[i];     }     cout << ms[ R ] <<     << ms[ B ] <<     << ms[ Y ] <<     << ms[ G ];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int DBG = 0, INF = int(1e9);   string toString(long long k) {     stringstream ss;     ss << k;     string res;     ss >> res;     return res;   }   int toInt(string s) {     stringstream ss;     ss << s;     int res;     ss >> res;     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cout.setf(ios::fixed);     cout << setprecision(10);     int n, a, d;     cin >> n >> a >> d;     vector<pair<int, int> > V(n);     for (int _n = (n), i = 0; i < _n; ++i) cin >> V[i].first >> V[i].second;     double prev = -1;     for (int _n = (n), i = 0; i < _n; ++i) {       double accTime = double(V[i].second) / double(a);       double accDst = double(V[i].second) * accTime * 0.5;       if (accDst >= d) {         double t = sqrt((2. * double(d)) / double(a));         prev = max(t + double(V[i].first), prev);       } else {         double remDst = d - accDst;         double remTime = remDst / double(V[i].second);         prev = max(accTime + remTime + double(V[i].first), prev);       }       cout << prev << endl;     }     return 0;   }
#include <bits/stdc++.h>   const long long mod = 998244353;   const long long inf = 1e18 + 10;   using namespace std;   long long modpow(long long a, long long b) {     long long res = 1;     while (b) {       if (b % 2) {         res *= a;       }       a *= a;       res %= mod;       a %= mod;       b /= 2;     }     return res;   }   long long nCr(long long n, long long r) {     long long f = 1;     for (long long i = 2; i <= n; i++) {       f *= i;       f %= mod;     }     long long q = 1;     for (long long i = 2; i <= r; i++) {       q *= i;       q %= mod;     }     long long q2 = 1;     for (long long i = 2; i <= n - r; i++) {       q2 *= i;       q2 %= mod;     }     q = modpow(q, mod - 2);     q2 = modpow(q2, mod - 2);     f *= q;     f %= mod;     f *= q2;     f %= mod;     return f;   }   int32_t main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     long long a[2 * n];     for (long long i = 0; i < 2 * n; i++) cin >> a[i];     sort(a, a + 2 * n);     long long l = 0, r = 0;     for (long long i = 0; i < n; i++) l += a[i];     for (long long i = n; i < 2 * n; i++) r += a[i];     long long ans = r - l;     ans %= mod;     cout << ((ans * nCr(2 * n, n)) % mod) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const unsigned int N = 150;   unsigned int n, p, q, now[N], c[N], ans;   inline unsigned int read() {     unsigned int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 3) + (x << 1) + (ch ^ 48);       ch = getchar();     }     return x * f;   }   unsigned int gcd(unsigned int a, unsigned int b) {     return !b ? a : gcd(b, a % b);   }   signed main() {     n = read();     p = read();     q = read();     for (unsigned int i = 0; i <= min(p, n - 1); i++) {       for (unsigned int j = 1; j <= i; j++) now[j] = n - j + 1;       for (unsigned int j = 1; j <= i; j++) {         unsigned int x = j;         for (unsigned int k = i; k >= 1; k--) {           unsigned int g = gcd(now[k], x);           if (g != 1) {             now[k] /= g;             x /= g;             if (x == 1) break;           }         }       }       c[i] = 1;       for (unsigned int j = 1; j <= i; j++) c[i] = c[i] * now[j];     }     for (unsigned int i = 1; i <= q; i++) {       unsigned int res = 0, em = 1;       for (unsigned int j = 0; j <= min(p, n - 1); j++) {         res += em * c[j];         em = em * i;       }       ans ^= (res * i);     }     cout << ans << endl;     fclose(stdin);     fclose(stdout);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x;     cin >> n >> x;     int eq = 0;     int minPos = 1, maxPos;     for (int i(0); i < n; i++) {       int a;       cin >> a;       if (a < x)         minPos++;       else if (a == x)         eq++;     }     int c = 0;     if (eq == 0) {       c++;       maxPos = minPos;     } else       maxPos = minPos + eq - 1;     while ((minPos > (n + c + 1) / 2) || (maxPos < (n + c + 1) / 2)) {       if (maxPos < (n + c + 1) / 2) {         minPos++;         maxPos++;       };       c++;     }     cout << c << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 100100;   const long long oo = 1e18;   int N;   long long P[MAX], D[MAX];   int L[MAX], R[MAX];   void init_L() {     stack<int> S;     for (int i = 0; i <= N + 1; ++i) {       while (S.size() >= 2) {         int x = S.top();         S.pop();         if (D[x] >= P[i] - P[S.top()]) {           S.push(x);           break;         }       }       if (!S.empty()) L[i] = S.top();       S.push(i);     }   }   void init_R() {     stack<int> S;     for (int i = N + 1; i >= 0; --i) {       while (S.size() >= 2) {         int x = S.top();         S.pop();         if (D[x] >= P[S.top()] - P[i]) {           S.push(x);           break;         }       }       if (!S.empty()) R[i] = S.top();       S.push(i);     }   }   long long low(int x) {     if (x == N + 1) return -1e10;     long long ret = P[R[x]] - D[x];     return ret < P[x] ? ret : +oo;   }   long long high(int x) {     if (x == 0) return +1e10;     long long ret = P[L[x]] + D[x];     return ret > P[x] ? ret : -oo;   }   bool check(long long W) {     multiset<long long> S;     for (int a = 0, b = 0; a <= N; ++a) {       for (; b <= N + 1 && P[b] - P[a] <= W; ++b) S.insert(low(b));       S.erase(S.find(low(a)));       if (!S.empty() && high(a) >= *S.begin()) return true;     }     return false;   }   int main() {     scanf( %d , &N);     for (int i = 0; i <= N + 1; ++i) scanf( %lld , P + i);     for (int i = 1; i <= N; ++i) scanf( %lld , D + i), D[i] *= 2;     D[0] = D[N + 1] = oo;     init_L();     init_R();     if (R[0] != N + 1) {       puts( 0 );       return 0;     }     long long lo = 1, hi = 1e10;     while (lo < hi) {       long long mid = (lo + hi) / 2;       check(mid) ? hi = mid : lo = mid + 1;     }     printf( %.1lf n , lo / 2.0);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   const double C = 0.57721566490153286060651209;   const double pi = acos(-1.0);   const int mod = 1e9 + 7;   const int maxn = 1e5 + 10;   int a[maxn];   int b[maxn];   int used[maxn];   int main() {     int n;     scanf( %d , &n);     int k = 0;     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       b[a[i]]++;       if (b[a[i]] & 1)         k++;       else         k--;     }     if (k > 1) {       printf( 0 n );       return 0;     }     long long pos = 0;     for (int i = 1; i <= n / 2; ++i)       if (a[i] != a[n - i + 1]) {         pos = i;         break;       } else         b[a[i]] -= 2;     if (!pos) {       printf( %lld n , 1LL * n * (n + 1) / 2);       return 0;     }     int len = 0;     for (int i = pos; i <= n; ++i) {       used[a[i]]++;       if (used[a[i]] * 2 > b[a[i]]) {         if (i < n - i + 1 || (i == n - i + 1 && b[a[i]] % 2 == 0) ||             a[i] != a[n - i + 1])           break;       }       len++;     }     memset(used, 0, sizeof used);     for (int i = n - pos + 1; i >= 1; --i) {       used[a[i]]++;       if (used[a[i]] * 2 > b[a[i]])         if (i > n - i + 1 || (i == n - i + 1 && b[a[i]] % 2 == 0) ||             a[i] != a[n - i + 1])           break;       len++;     }     printf( %lld n , pos * (pos + len));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> v(n);     int ans = 0, cnt = 0;     for (int i = 0; i < n; i++) {       cin >> v[i];     }     for (int i = 0; i < n; i++) {       if (v[i] == i)         ans++;       else if (v[v[i]] == i) {         cnt = 1;       }     }     cout << min(ans + cnt + 1, n);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k;   long long arr[200001];   vector<long long> v;   string s;   int find(int i) {     int curr = v[i];     int num = i;     for (int j = i + 1; j < n; j++)       if (v[j] == curr)         num = j;       else         break;     return num;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> arr[i];     cin >> s;     for (int i = 0; i < n; i++) {       int a = s[i] -  a ;       v.push_back(a);     }     int count = 1;     long long d = 0;     int prev;     for (int i = 0; i < n; i++) {       int nx = find(i);       int dis = nx - i + 1;       if (dis <= k) {         for (int j = i; j <= nx; j++) d += arr[j];       } else {         sort(arr + i, arr + nx + 1);         for (int j = nx; j >= nx - k + 1; j--) d += arr[j];       }       i = nx;     }     cout << d;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> adj[500010], ar;   bool vis[500010];   int u, v, n, m, s, res = 0;   void dfs(int cur) {     vis[cur] = true;     for (auto i : adj[cur])       if (!vis[i]) dfs(i);     ar.push_back(cur);   }   int main() {     scanf( %d%d%d , &n, &m, &s);     for (int i = 0; i < m; ++i) {       scanf( %d%d , &u, &v);       adj[u].push_back(v);     }     dfs(s);     for (int i = 1; i <= n; ++i) {       if (!vis[i]) dfs(i);     }     memset(vis, false, sizeof vis);     for (int i = n - 1; i >= 0; --i) {       if (!vis[ar[i]]) {         if (s != ar[i]) res++;         dfs(ar[i]);       }     }     printf( %d n , res);     return 0;   }