malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     double x1, y1, x2, y2, x0, y0;     while (scanf( %lf%lf%lf%lf%lf%lf , &x0, &y0, &x1, &y1, &x2, &y2) != EOF) {       double area = x0 * y1 + x2 * y0 + x1 * y2 - x2 * y1 - x0 * y2 - x1 * y0;       if (area == 0) {         printf( TOWARDS n );       } else if (area > 0) {         printf( LEFT n );       } else {         printf( RIGHT n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 200 * 1000 + 1;   struct Seg {     int s, e, Id;     friend bool operator<(Seg a, Seg b) { return a.s < b.s; }     friend bool operator>(Seg a, Seg b) {       return (a.e != b.e ? a.e > b.e : a.Id > b.Id);     }   } a[MAX + 10];   set<Seg, greater<Seg> > s;   bool mrk[MAX + 10];   int cnt = 0;   int n, m;   int main() {     ios::sync_with_stdio(false);     cin >> n >> m;     for (int i = 0; i < m; i++)       cin >> a[i].s >> a[i].e, a[i].Id = i, a[i].e += a[i].s;     sort(a, a + m);     int p = 0;     int next = 0;     for (int i = 1; i <= n; i++) {       while (p < m && a[p].s == i) s.insert(a[p++]);       if (next <= i && !s.empty() && s.begin()->e > i) {         mrk[s.begin()->Id] = true;         next = s.begin()->e;         s.erase(s.begin());         cnt++;       }     }     cout << m - cnt << endl;     for (int i = 0; i < m; i++)       if (!mrk[i]) cout << i + 1 <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, s;   const int N = 1e5 + 10;   int v[N];   int ans;   bool check(int x, int &res) {     int k[n];     for (int i = 0; i < n; i++) {       k[i] = v[i] + (i + 1) * x;     }     sort(k, k + n);     for (int i = 0; i < x; i++)       if (res <= s) res += k[i];     return res <= s;   }   int main() {     cin >> n >> s;     for (int i = 0; i < n; i++) {       cin >> v[i];     }     int hi = n + 1;     int lo = 0;     for (int i = 0; i < 100; i++) {       int mid = (hi + lo) >> 1;       int valor = 0;       if (check(mid, valor)) {         ans = valor;         lo = mid;       } else         hi = mid;     }     cout << lo <<     << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string sol;   const int kBufferSize = 10000;   int BufferInd = kBufferSize;   char Buffer[kBufferSize];   void cit(int &n) {     ((++BufferInd >= kBufferSize) ? (cin.read(Buffer, kBufferSize), BufferInd = 0)                                   : (0));     for (; not((((Buffer[BufferInd]) >=  0  and (Buffer[BufferInd]) <=  9 ) or                 (Buffer[BufferInd]) ==  - )                    ? (1)                    : (0));          ((++BufferInd >= kBufferSize)               ? (cin.read(Buffer, kBufferSize), BufferInd = 0)               : (0)))       ;     bool sign = false;     if ((Buffer[BufferInd]) ==  - ) {       ((++BufferInd >= kBufferSize)            ? (cin.read(Buffer, kBufferSize), BufferInd = 0)            : (0));       sign = true;     }     n = 0;     for (; ((((Buffer[BufferInd]) >=  0  and (Buffer[BufferInd]) <=  9 ) or              (Buffer[BufferInd]) ==  - )                 ? (1)                 : (0));          ((++BufferInd >= kBufferSize)               ? (cin.read(Buffer, kBufferSize), BufferInd = 0)               : (0))) {       n *= 10;       n += (Buffer[BufferInd]) -  0 ;     }     if (sign) n *= -1;   }   int main() {     std::ios_base::sync_with_stdio(false);     int n, sum = 0;     cin >> n;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) {         int x;         cin >> x;         if (i == j) sum ^= x;       }     int q;     cin >> q;     while (q--) {       int t;       cin >> t;       if (t == 1 or t == 2) {         int x;         cin >> x;         sum ^= 1;       } else {         sol +=  0  + sum;       }     }     cout << sol <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool func(pair<int, int> &a, pair<int, int> &b) { return a.first < b.first; }   int main() {     int n, q;     vector<pair<int, int>> arr;     int input1, input2;     int i, j;     int level[5001] = {         0,     };     priority_queue<int, vector<int>, greater<int>> exit;     int curlev = 1;     int cur, prev = 0;     int acc1[5001], acc2[5001];     pair<int, int> interv1, interv2, temp;     int over1, over2;     int last = 0, sum;     int cand;     int lmax = 0;     scanf( %d %d , &n, &q);     for (i = 0; i < q; i++) {       scanf( %d %d , &input1, &input2);       arr.push_back(pair<int, int>(input1, input2));       for (j = input1; j <= input2; j++) {         level[j]++;       }     }     sort(arr.begin(), arr.end(), func);     sum = n;     for (i = 0; i < q; i++) {       if (arr[i].first > prev + 1) {         sum -= arr[i].first - (prev + 1);       }       if (arr[i].second > prev) {         prev = arr[i].second;       }     }     sum -= n - prev;     acc1[0] = acc2[0] = 0;     for (i = 1; i <= n; i++) {       acc1[i] = acc1[i - 1] + (level[i] == 1);       acc2[i] = acc2[i - 1] + (level[i] == 2);     }     for (i = 0; i < q - 1; i++) {       for (j = i + 1; j < q; j++) {         interv1 = arr[i];         interv2 = arr[j];         over1 = 1;         over2 = 0;         if (interv2.first <= interv1.second) {           over1 = interv2.first;           if (interv2.second < interv1.second) {             over2 = interv2.second;           } else {             over2 = interv1.second;           }         }         cand = sum -                ((acc1[interv1.second] - acc1[interv1.first - 1]) +                 (acc1[interv2.second] - acc1[interv2.first - 1])) -                (acc2[over2] - acc2[over1 - 1]);         if (cand > lmax) {           lmax = cand;         }       }     }     printf( %d n , lmax);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using lli = long long int;   int main() {     int n;     cin >> n;     if (n == 2) {       cout << 1 << endl << 1 <<     << 1 << endl;       return 0;     } else if (n == 3) {       cout <<  0 n2 1 2 n ;     } else if (n == 4) {       cout <<  0 n2 1 4 n ;     } else {       vector<int> v;       vector<int> c;       lli sum_v = 0, sum_c = 0;       for (lli i = n; i >= 1; i--) {         if (abs(sum_v + i - sum_c) < abs(sum_c + i - sum_v)) {           sum_v += i;           v.push_back(i);         } else {           sum_c += i;           c.push_back(i);         }       }       cout << abs(sum_v - sum_c) << endl;       cout << c.size() <<    ;       int in = c.size();       for (lli i = 0; i < (in); i++) {         cout << c[i];         if (i == in - 1)           cout << endl;         else           cout <<    ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long int cnt;   long long int v[100005], lft[100005], rgt[100005], rt[100005];   std::map<long long int, bool> M;   void check(long long int i, long long int left, long long int right) {     if (v[i] <= left && v[i] >= right) {       M[v[i]] = true;     }     if (lft[i] != -1) {       long long int left1 = min(left, v[i]);       check(lft[i], left1, right);     }     if (rgt[i] != -1) {       long long int right1 = max(right, v[i]);       check(rgt[i], left, right1);     }   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     ;     long long int n, root;     cin >> n;     for (long long int i = 1; i <= n; i++) {       cin >> v[i] >> lft[i] >> rgt[i];     }     cnt = 0;     for (long long int i = 1; i <= n; i++) {       if (lft[i] != -1) rt[lft[i]] = 1;       if (rgt[i] != -1) rt[rgt[i]] = 1;     }     for (long long int i = 1; i <= n; i++) {       if (rt[i] == 0) {         root = i;         break;       }     }     long long int left = INT_MAX;     long long int right = INT_MIN;     check(root, left, right);     for (long long int i = 1; i <= n; i++) cnt += !M[v[i]];     cout << cnt << endl;     return 0;   }
#include <bits/stdc++.h>   template <typename T>   T pow(T a, T b, long long m) {     T ans = 1;     while (b > 0) {       if (b % 2 == 1) ans = (ans * a) % m;       b /= 2;       a = (a * a) % m;     }     return ans % m;   }   using namespace std;   const long long N = 2e5 + 77;   string s;   void sol() {     long long n;     cin >> n;     vector<long long> A(30, 0);     for (long long(i) = (0); (i) < (n); (i)++) {       cin >> s;       for (long long(j) = (0); (j) < ((long long)s.size()); (j)++)         A[s[j] -  a ]++;     }     for (long long(i) = (0); (i) < ((long long)A.size()); (i)++)       if (A[i] % n != 0) {         cout <<  NO  <<   n ;         return;       }     cout <<  YES  <<   n ;   }   int32_t main() {     long long t;     cin >> t;     while (t--) sol();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100050;   int item[MAXN];   int main() {     int dcnt;     while (scanf( %d , &dcnt) == 1) {       int discount = MAXN;       for (int i = 0; i < dcnt; i++) {         int tmp;         scanf( %d , &tmp);         discount = min(discount, tmp);       }       int icnt;       scanf( %d , &icnt);       for (int i = 1; i <= icnt; i++) scanf( %d , &item[i]);       sort(item + 1, item + 1 + icnt, greater<int>());       long long cost = 0;       long long dealed = 0, cur = 0;       while (dealed < icnt) {         if (cur == discount) {           cur = 0;           dealed += 2;         }         cost += item[++dealed];         cur++;       }       printf( %lld n , cost);     }   }
#include <bits/stdc++.h>   int main() {     int n, m;     scanf( %d%d , &n, &m);     int min_row = n, max_row = 0, min_col = m, max_col = 0;     char S[105][105];     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         scanf(  %c , &S[i][j]);         if (S[i][j] ==  * ) {           min_row = std::min(min_row, i), max_row = std::max(max_row, i),           min_col = std::min(min_col, j), max_col = std::max(max_col, j);         }       }     }     for (int i = min_row; i <= max_row; i++, putchar(  n )) {       for (int j = min_col; j <= max_col; j++) {         printf( %c , S[i][j]);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const char dig[10][8] = { 1110111 ,  0010010 ,  1011101 ,  1011011 ,  0111010 ,                             1101011 ,  1101111 ,  1010010 ,  1111111 ,  1111011 };   int n, m;   char s[2022][8];   int det[2022][10];   bool f[2022][2022];   int calc(int id1, int id2) {     int res = 0;     for (int i = 0; i < 7; i++) {       if (s[id1][i] ==  0  && dig[id2][i] ==  1 ) res++;       if (s[id1][i] ==  1  && dig[id2][i] ==  0 ) return -1;     }     return res;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %s , s[i]);     for (int i = 1; i <= n; i++)       for (int j = 0; j < 10; j++) det[i][j] = calc(i, j);     memset(f, false, sizeof f);     f[n + 1][0] = true;     for (int i = n; i > 0; i--)       for (int j = 0; j <= m; j++) {         if (!f[i + 1][j]) continue;         for (int k = 0; k < 10; k++)           if (det[i][k] >= 0) f[i][j + det[i][k]] = true;       }     if (!f[1][m])       puts( -1 );     else {       int tot = 0;       for (int i = 1; i <= n; i++)         for (int j = 9; j >= 0; j--) {           int x = det[i][j];           if (x >= 0 && tot + x <= m && f[i + 1][m - tot - x]) {             printf( %d , j);             tot += x;             break;           }         }       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   long long n, sum;   int a[maxn], ans[maxn];   int main() {     scanf( %I64d%I64d , &n, &sum);     long long tot = 0;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       tot += a[i];     }     for (int i = 1; i <= n; i++) {       if (a[i] > sum - n + 1) ans[i] += a[i] - (sum - n + 1);       if (sum - 1 > tot - a[i]) ans[i] += sum - 1 - (tot - a[i]);     }     for (int i = 1; i <= n; i++) printf( %d%c , ans[i], i == n ?   n  :    );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MX = 300005;   long long a[MX], b[MX];   int32_t main() {     long long n, p, k;     cin >> n >> p >> k;     for (long long i = 0; i < n; i++) {       cin >> a[i];       b[i] =           (((a[i] * a[i]) % p) * ((a[i] * a[i]) % p) - (k * a[i]) + (k * p)) % p;     }     cout << endl;     sort(b, b + n);     long long j = 0, sum = 0;     for (long long i = 0; i < n; i = j) {       while (b[i] == b[j] && j < n) {         j++;       }       long long num = j - i;       sum += (num * (num - 1)) / 2;     }     cout << sum;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long t, h, g, n;     scanf( %lld , &t);     while (t--) {       scanf( %lld %lld , &h, &g);       n = 1ll << h;       pair<long long, long long> p[n - 1];       for (long long i = 1; i < n; i++)         scanf( %lld , &p[i - 1].first), p[i - 1].second = i;       sort(p, p + n - 1);       bool f[n * 2], ans[n];       for (long long i = 1; i < n; i++) {         if (i < (1ll << g))           f[i] = true;         else           f[i] = false;         ans[i] = false;       }       long long s = 0;       for (auto e : p) {         long long c = e.second;         while (c > 0) {           if (f[c] && !f[c * 2] && !f[c * 2 + 1]) {             ans[e.second] = true, f[c] = false, s += e.first;             break;           }           c /= 2;         }       }       printf( %lld n , s);       for (long long i = n - 1; i > 0; i--)         if (!ans[i]) printf( %lld  , i);       printf(  n );     }   }
#include <cstdio>   #include <algorithm>   #define ll long long int   using namespace std;   const int MAX = 1e7+10;   int t, c;   int ans[MAX];   int prime[MAX], pcount;   bool vis[MAX];   ll d[MAX], f[MAX];   void eular() {       for (int i = 1; i <= MAX; i++) ans[i] = MAX;       ans[1] = 1;       for (int i = 2; i < MAX; i++) {           if (!vis[i]) {               vis[i] = 1;               prime[pcount++] = i;               d[i] = f[i] = i + 1;               if (ans[i + 1] > i) ans[i + 1] = i;           }           for (int j = 0; j < pcount && (ll)prime[j] * i < MAX; j++) {               vis[prime[j] * i] = 1;               if (i % prime[j] == 0) {                   f[prime[j] * i] = f[i] * prime[j] + 1;                   d[prime[j] * i] = d[i] / f[i] * f[prime[j] * i];               } else {                   f[prime[j] * i] = f[prime[j]];                   d[prime[j] * i] = d[i] * f[prime[j] * i];               }               if (d[prime[j] * i] < MAX && ans[d[prime[j] * i]] > prime[j] * i) ans[d[prime[j] * i]] = prime[j] * i;               if (i % prime[j] == 0) break;           }       }   }   int main() {       eular();       scanf( %d , &t);       while(t--) {           scanf( %d , &c);           if (ans[c] != MAX) printf( %d n , ans[c]);           else printf( -1 n );       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1000010;   int n, arr[N], arr2[N], seg[4 * N], lazy[4 * N];   inline void fix(int s, int e, int idx) {     seg[idx] += lazy[idx];     if (s != e) {       lazy[(idx << 1)] += lazy[idx];       lazy[(idx << 1) + 1] += lazy[idx];     }     lazy[idx] = 0;   }   int build(int s, int e, int idx) {     if (s == e) return seg[idx] = arr2[s];     return seg[idx] = max(build(s, ((s + e) >> 1), (idx << 1)),                           build(((s + e) >> 1) + 1, e, (idx << 1) + 1));   }   int l, r, val;   inline int update(int s, int e, int idx) {     fix(s, e, idx);     if (s > r || e < l) return seg[idx];     if (s >= l && e <= r) {       lazy[idx] += val;       fix(s, e, idx);       return seg[idx];     }     return seg[idx] = max(update(s, ((s + e) >> 1), (idx << 1)),                           update(((s + e) >> 1) + 1, e, (idx << 1) + 1));   }   int s, e, idx, mid;   inline int Lower_bound(int val) {     s = 0, e = n, idx = 1;     while (true) {       fix(s, e, idx);       if (s == e) return s;       mid = ((s + e) >> 1);       fix(s, mid, (idx << 1));       fix(mid + 1, e, (idx << 1) + 1);       if (seg[(idx << 1)] >= val) {         idx = (idx << 1);         e = mid;       } else {         idx = (idx << 1) + 1;         s = mid + 1;       }     }     assert(false);   }   int Idx;   inline int get() {     s = 0, e = n, idx = 1;     while (true) {       fix(s, e, idx);       if (s == e) return seg[idx];       mid = ((s + e) >> 1);       if (Idx <= mid) {         idx = (idx << 1);         e = mid;       } else {         idx = (idx << 1) + 1;         s = mid + 1;       }     }     assert(false);   }   int tmp1, tmp2, tmp3, tmp4, tmp5;   int tmp7 = 0, tmp8 = 0;   void check2(int cur) {     for (int i = 1; i <= 4 * (n + 1); i++) seg[i] = lazy[i] = 0;     arr[n] = cur;     for (int i = 0; i <= n; i++) {       arr2[i] = arr[i];     }     sort(arr2, arr2 + n + 1);     build(0, n, 1);     tmp7 = 0;     for (int i = n; i >= 0; i--) {       Idx = i;       tmp1 = get();       if (tmp1 <= 0) {         tmp7 -= tmp1;         continue;       }       if (i - tmp1 < 0) {         l = 0, r = i - 1, val = -1;         update(0, n, 1);         tmp7 -= (i - tmp1);         continue;       }       Idx = i - tmp1;       tmp2 = get();       tmp3 = Lower_bound(tmp2);       Idx = i - 1;       tmp4 = get();       if (tmp2 == tmp4) {         l = tmp3;         r = tmp3 + tmp1 - 1;         val = -1;         update(0, n, 1);       } else {         tmp5 = Lower_bound(tmp2 + 1);         l = tmp5;         r = i - 1;         val = -1;         update(0, n, 1);         l = tmp3;         r = l + (tmp5 - (i - tmp1)) - 1;         val - 1;         update(0, n, 1);       }     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &arr[i]);     }     check2(0);     tmp8 = tmp7;     check2(n);     tmp7 = n - tmp7;     if (tmp7 < tmp8) {       puts( -1 );       return 0;     }     for (int i = tmp8; i <= tmp7; i += 2) {       printf( %d  , i);     }     puts(  );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MX = 6;   int n, Arr[6];   int solve(int rem, int mask) {     if (mask == ((1 << MX) - 1)) return 0;     int Ans = 50;     for (int i = 0; i < MX; i++) {       if (!(mask & (1 << i))) {         if (rem < Arr[i])           Ans = min(Ans, 1 + solve(n - Arr[i], mask | (1 << i)));         else           Ans = min(Ans, solve(rem - Arr[i], mask | (1 << i)));       }     }     return Ans;   }   int main() {     scanf( %d %d %d , &n, &Arr[0], &Arr[2]);     Arr[1] = Arr[3] = Arr[4] = Arr[0];     Arr[5] = Arr[2];     printf( %d n , solve(0, 0));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int in() {     int x, y;     y = scanf( %d , &x);     return x;   }   const int MAXN = 2e6 + 10;   int n, p[MAXN], sec[MAXN], pt[MAXN];   string s;   int nxt[MAXN], prv[MAXN];   int d[MAXN][2];   bool ok(int mid, int sz) {     memset(d, 0, sizeof(d));     d[0][0] = d[0][1] = 1;     for (int i = 1; i <= n; i++) {       if (s[i - 1] ==  . )         d[i][0] = d[i - 1][0];       else if (s[i - 1] ==  P )         d[i][0] = d[i - 1][1];       else {         int xx = prv[i - 1];         if (~xx && xx + mid >= i - 1) {           d[i][0] |= d[xx][0];           int temp = prv[xx];           if (~temp && temp + mid >= i - 1) {             int t = max(0, xx - mid);             int temp2 = 0;             if (t && nxt[t - 1] < temp) temp2 = 1;             d[i][0] |= d[t][temp2];           }         }       }       int xx = nxt[i - 1];       if (xx < n) {         int temp = max(0, xx - mid);         temp = min(temp, i);         int temp2 = 0;         if (temp && nxt[temp - 1] < xx) temp2 = 1;         d[i][1] |= d[temp][temp2];       }     }     return d[n][0];   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> s;     int sz = 0, c = 0;     for (int i = 0; i < n; i++)       if (s[i] ==  P )         sec[sz++] = i;       else if (s[i] ==  * )         c++;     int mn = 1e9, mx = -1;     for (int i = 0; i < n; i++) {       p[i + 1] = p[i];       if (s[i] ==  * ) {         p[i + 1]++;         mn = min(mn, i);         mx = max(mx, i);       }     }     int last = n;     for (int i = n - 1; ~i; i--) {       nxt[i] = last;       if (s[i] ==  P ) last = i;     }     last = -1;     for (int i = 0; i < n; i++) {       prv[i] = last;       if (s[i] ==  P ) last = i;     }     memcpy(pt, p, sizeof(p));     if (sz == 1) {       if (mn > sec[0])         cout << c <<     << mx - sec[0] <<   n ;       else if (mx < sec[0])         cout << c <<     << sec[0] - mn <<   n ;       else {         if (p[sec[0]] > c - p[sec[0]])           cout << p[sec[0]] <<      << sec[0] - mn <<   n ;         else if (p[sec[0]] < c - p[sec[0]])           cout << c - p[sec[0]] <<     << mx - sec[0] <<   n ;         else           cout << p[sec[0]] <<     << min(sec[0] - mn, mx - sec[0]) <<   n ;       }     } else {       int lo = 0, hi = n - 1;       while (hi - lo > 1) {         int mid = hi + lo >> 1;         if (ok(mid, sz))           hi = mid;         else           lo = mid;       }       cout << c <<     << hi <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e15;   int n, B, c0[5010], c1[5010];   vector<int> e[5010];   vector<long long> f[5010], g[5010];   void combine(vector<long long>& a, const vector<long long>& b) {     int la = a.size(), lb = b.size(), lc = la + lb - 1;     vector<long long> c(lc);     for (int i = 0; i < lc; ++i) c[i] = inf;     for (int i = 0; i < la; ++i)       for (int j = 0; j < lb; ++j) c[i + j] = min(c[i + j], a[i] + b[j]);     a = c;   }   void dfs(int x) {     vector<long long> a, b;     a.push_back(0);     b.push_back(0);     for (int v : e[x]) {       dfs(v);       combine(a, f[v]);       combine(b, g[v]);     }     f[x].push_back(0);     for (int i = 0; i < a.size(); ++i) {       f[x].push_back(a[i] + c0[x]);     }     g[x] = b;     g[x].push_back(inf);     for (int i = 0; i < b.size(); ++i) {       g[x][i + 1] = min(g[x][i + 1], b[i] + c1[x]);     }     for (int i = 0; i < b.size(); ++i) {       f[x][i] = min(f[x][i], g[x][i]);     }   }   int main() {     scanf( %d%d , &n, &B);     for (int i = 1; i <= n; ++i) {       scanf( %d%d , &c1[i], &c0[i]);       c0[i] = c1[i] - c0[i];       int fa;       if (i != 1) scanf( %d , &fa), e[fa].push_back(i);     }     dfs(1);     int ans = 0;     for (int i = 0; i < f[1].size(); ++i)       if (f[1][i] <= B) ans = i;     for (int i = ans + 1; i <= (int)g[1].size() - 1; ++i)       if (g[1][i] <= B) ans = i;     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct keep {     int pos;     int apples;   };   keep inp[1000];   bool cmp(keep x, keep y) {     if (x.pos < y.pos) return 1;     return 0;   }   int main() {     int n;     cin >> n;     int l = 0, r = 0;     int sum = 0;     for (int i = 0; i < n; i++) {       cin >> inp[i].pos >> inp[i].apples;       inp[i].pos < 0 ? l++ : r++;       sum += inp[i].apples;     }     sort(inp, inp + n, cmp);     if (l == 0) {       cout << inp[0].apples;       return 0;     }     if (r == 0) {       cout << inp[n - 1].apples;       return 0;     }     if (l - r == 1 || r - l == 1 || l == r) {       cout << sum << endl;       return 0;     }     if (l > r) {       int ans = 0;       int c = 0;       int i;       for (i = n - 1; inp[i].pos > 0; i--) {         ans += inp[i].apples;         c++;       }       int cc = 1;       for (; cc <= c + 1; i--) {         ans += inp[i].apples;         cc++;       }       cout << ans;       return 0;     }     if (r > l) {       int ans = 0;       int c = 0;       int i;       for (i = 0; inp[i].pos < 0; i++) {         ans += inp[i].apples;         c++;       }       int cc = 1;       for (; cc <= c + 1; i++) {         ans += inp[i].apples;         cc++;       }       cout << ans;       return 0;     }     return 0;   }