malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   inline void chkmin(T1& a, T2 b) {     if (a > b) a = b;   }   template <typename T1, typename T2>   inline void chkmax(T1& a, T2 b) {     if (a < b) a = b;   }   using ll = long long;   using ld = long double;   const string FILENAME =  input ;   using ll = long long;   using ld = long double;   struct Point {     ld x, y;     Point() {}     Point(ld _x, ld _y) : x(_x), y(_y) {}     ld len() { return sqrt(x * x + y * y); }   };   Point operator+(const Point& a, const Point& b) {     return Point(a.x + b.x, a.y + b.y);   }   Point operator-(const Point& a, const Point& b) {     return Point(a.x - b.x, a.y - b.y);   }   ld operator*(const Point& a, const Point& b) { return a.x * b.y - a.y * b.x; }   ld operator^(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; }   const double Pi = acos(-1);   const int MAXN = 100228;   int n, k;   Point p[MAXN];   bool ok(double r) {     vector<pair<double, int> > st;     int add = 0;     for (int i = 0; i < n; i++) {       double f = p[i].len();       if (f > 2 * r + 1e-9) {         continue;       }       if (abs(p[i].x) < 1e-9 && abs(p[i].y) < 1e-9) {         add++;         continue;       }       Point q = p[i];       swap(q.x, q.y);       q.x *= -1;       double fm = sqrt(q.x * q.x + q.y * q.y);       q.x /= fm;       q.y /= fm;       double h = sqrt(r * r - (f / 2) * (f / 2));       q.x *= h;       q.y *= h;       Point s = p[i];       s.x /= 2;       s.y /= 2;       Point e = s + q;       Point e1 = s - q;       double l = atan2(e.y, e.x) + 2 * Pi;       double rr = atan2(e1.y, e1.x) + 2 * Pi;       while (l < 0) {         l += 2 * Pi;       }       while (rr < 0) {         rr += 2 * Pi;       }       while (l >= 2 * Pi) {         l -= 2 * Pi;       }       while (rr >= 2 * Pi) {         rr -= 2 * Pi;       }       if (l > rr) {         swap(l, rr);       }       if (rr - l <= Pi + 1e-9) {         st.push_back(make_pair(l, 1));         st.push_back(make_pair(rr, -1));       } else {         st.push_back(make_pair(0, 1));         st.push_back(make_pair(l, -1));         st.push_back(make_pair(rr, 1));         st.push_back(make_pair(2 * Pi, -1));       }     }     sort((st).begin(), (st).end());     int cur = 0;     int tm = 0;     for (auto x : st) {       cur += x.second;       chkmax(tm, cur);     }     return tm + add >= k;   }   void solve() {     cin >> n >> k;     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       p[i].x = x;       p[i].y = y;     }     double l = 0.0;     double r = 200228;     for (int it = 0; it < 36; it++) {       double mid = (l + r) / 2.0;       if (ok(mid)) {         r = mid;       } else {         l = mid;       }     }     cout << fixed << l <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1005;   long long sum[N][N];   int spt[11][N][N], mn[N][N], arr[N][N];   bool visit[N][N];   int n, m, a, b, x;   void buildSparse() {     int LN = log2(n) + 1, LM = log2(m) + 1;     for (int lg = 1; lg <= LM; lg++)       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++)           spt[lg][i][j] =               min(spt[lg - 1][i][j], spt[lg - 1][i][j + (1 << (lg - 1))]);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         sum[i][j] += sum[i - 1][j];         if (j >= b) {           int lg = log2(b), l = j - b + 1, r = j - (1 << lg) + 1;           mn[i][j] = min(spt[lg][i][l], spt[lg][i][r]);         }       }     for (int i = 1; i <= n; i++)       for (int j = b; j <= m; j++) spt[0][i][j] = mn[i][j];     for (int lg = 1; lg <= LN; lg++)       for (int i = 1; i <= n; i++)         for (int j = b; j <= m; j++)           spt[lg][i][j] =               min(spt[lg - 1][i][j], spt[lg - 1][i + (1 << (lg - 1))][j]);   }   long long getRemoved(int i, int j) {     int lg = log2(a), l = i - a + 1;     int r = i - (1 << lg) + 1;     int minn = min(spt[lg][l][j], spt[lg][r][j]);     long long res = sum[i][j] - sum[i - a][j] - sum[i][j - b] +                     sum[i - a][j - b] - 1LL * a * b * minn;     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m >> a >> b;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> arr[i][j];         spt[0][i][j] = arr[i][j];         sum[i][j] += sum[i][j - 1] + arr[i][j];       }     }     buildSparse();     vector<pair<long long, pair<int, int>>> v, ans;     for (int i = a; i <= n; i++)       for (int j = b; j <= m; j++) v.push_back({getRemoved(i, j), {i, j}});     sort(v.begin(), v.end());     for (int i = 0; i < v.size(); i++) {       int x = v[i].second.first - a + 1, y = v[i].second.second - b + 1;       if (!visit[x][y] && !visit[x + a - 1][y] && !visit[x][y + b - 1] &&           !visit[x + a - 1][y + b - 1]) {         ans.push_back({v[i].first, {x, y}});         for (int j = 0; j < a; j++)           for (int k = 0; k < b; k++) visit[x + j][y + k] = true;       }     }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++)       cout << ans[i].second.first <<     << ans[i].second.second <<                << ans[i].first <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100000 + 10;   struct Edge {     int from, to, w;   } edges[maxn];   int val[maxn];   long long ans;   int size[maxn], fa[maxn];   bool cmp(Edge a, Edge b) { return a.w > b.w; }   int Getfa(int x) {     if (fa[x] == x)       return x;     else       return fa[x] = Getfa(fa[x]);   }   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; ++i) scanf( %d , &val[i]);     for (int i = 1; i <= m; ++i) {       int u, v;       scanf( %d %d , &u, &v);       edges[i].from = u, edges[i].to = v;       edges[i].w = min(val[u], val[v]);     }     sort(edges + 1, edges + 1 + m, cmp);     for (int i = 1; i <= n; ++i) size[i] = 1, fa[i] = i;     ans = 0;     for (int i = 1; i <= m; ++i) {       int u = edges[i].from, v = edges[i].to;       int fx = Getfa(u), fy = Getfa(v);       if (fx == fy) continue;       ans += (long long)size[fx] * size[fy] * edges[i].w;       fa[fx] = fy;       size[fy] += size[fx];     }     printf( %.6lf n , (double)2.0 * ans / n / (n - 1));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int N, M;     char eat;     cin >> N >> M;     scanf( %c , &eat);     char temp[5000];     vector<vector<bool>> grid(N, vector<bool>(M));     for (int r = 0; r < N; r++) {       scanf( %s , temp);       for (int c = 0; c < M; c++) grid[r][c] = (temp[c] ==  1  ? 1 : 0);     }     int result = 0;     vector<vector<int>> memo(N, vector<int>(M));     for (int r = 0; r < N; r++) {       for (int c = M - 1; c >= 0; c--) {         if (grid[r][c] == 1) memo[r][c] = 1 + (c != M - 1 ? memo[r][c + 1] : 0);       }     }     for (int c = 0; c < M; c++) {       vector<int> width;       for (int r = 0; r < N; r++) {         if (memo[r][c] != 0) width.push_back(memo[r][c]);       }       sort(width.rbegin(), width.rend());       for (int i = 0; i < width.size(); i++) {         int area = (i + 1) * width[i];         result = max(result, area);       }     }     cout << result << endl;   }
#include <bits/stdc++.h>   using namespace std;          #define pb push_back   #define MP make_pair   #define ll long long   #define lb lower_bound   #define ub upper_bound   #define bs binary_search      #define fo(i,a,b) for(int i=a;i<b;i++)   #define rfo(i,b,a) for(int i=b;i>=a;i--)      typedef vector<ll> vi;   typedef vector<vi> vvi;       string solve(){      ll n;      cin>>n;      vi a(n);      map<int,int> m;            fo(i,0,n){          cin>>a[i];          m[a[i]] = m[a[i]]  + (i%2);      }      sort(a.begin(), a.end());      fo(i,0,n){          m[a[i]] = m[a[i]]-(i%2);      }      map<int,int> :: iterator itr;      for(itr = m.begin() ; itr!= m.end() ; itr++){          if(itr->second != 0) return  NO ;      }      return  YES ;          }         int main()   {    ios_base::sync_with_stdio(false);       cin.tie(NULL);       int t;    cin>>t;    while(t--)    {     cout<<solve()<<endl;    }    return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct t {     char c;     int a, b;     t(char c, int a, int b) : c(c), a(a), b(b) {}     t() {}   };   int n;   vector<t> e;   double temp;   double t0 = 1000;   double gen_rand() {     int x = rand() + 1, y = rand() + 1;     x %= y;     return x / (double)y;   }   int get_ans() {     int r = 0, b = 0;     int R = 0, B = 0;     int ans = 0;     for (int i = 0; i < n; i++) {       int need =           max(max(0, max(0, e[i].a - R) - r), max(0, max(0, e[i].b - B) - b));       ans += need;       r -= max(0, max(0, e[i].a - R));       b -= max(0, max(0, e[i].b - B));       r += need;       b += need;       ans++;       if (e[i].c ==  R ) {         R++;       } else {         B++;       }     }     return ans;   }   int main() {     srand(time(NULL));     cin >> n;     for (int i = 0; i < n; i++) {       char c;       int r, b;       cin >> c >> r >> b;       e.push_back({c, r, b});     }     random_shuffle(e.begin(), e.end());     temp = t0;     int ans = INT_MAX;     for (int qwe = 0; qwe < 70000; ++qwe) {       int cur = get_ans();       ans = min(ans, cur);       vector<t> was = e;       for (int i = 0; i < n * temp / t0; i++) {         swap(e[rand() % n], e[rand() % n]);       }       int now = get_ans();       ans = min(ans, now);       if (now < cur) {         cur = now;       } else {         double go = exp(-(now - cur) / temp);         if (gen_rand() <= go) {           cur = now;         } else {           e = was;         }       }       temp *= 0.999;     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     return (b ? __gcd(a, b) : a);   }   template <typename T>   T lcm(T a, T b) {     return (a * (b / gcd(a, b)));   }   template <class T>   ostream &operator<<(ostream &os, vector<T> V) {     os <<  [  ;     for (auto v : V) os << v <<    ;     return os <<  ] ;   }   template <class T>   ostream &operator<<(ostream &os, set<T> S) {     os <<  {  ;     for (auto s : S) os << s <<    ;     return os <<  } ;   }   template <class L, class R>   ostream &operator<<(ostream &os, pair<L, R> P) {     return os <<  (  << P.first <<  ,  << P.second <<  ) ;   }   template <class L, class R>   ostream &operator<<(ostream &os, map<L, R> M) {     os <<  {  ;     for (auto m : M) os <<  (  << m.first <<  :  << m.second <<  )  ;     return os <<  } ;   }   const int N = 5e5 + 5;   long long ar[N];   long long l[N], r[N], ans[N];   int main() {     cin.sync_with_stdio(0);     cin.tie(0);     cin.exceptions(cin.failbit);     int n;     cin >> n;     for (int i = 1; i <= n; ++i) cin >> ar[i];     vector<long long> lft(1, 0), rt(1, n + 1);     for (int i = 1; i <= n; ++i) {       while (ar[lft.back()] > ar[i]) {         lft.pop_back();       }       l[i] = l[lft.back()] + ar[i] * (i - lft.back());       lft.push_back(i);     }     for (int i = n; i; --i) {       while (ar[rt.back()] > ar[i]) rt.pop_back();       r[i] = r[rt.back()] + ar[i] * (rt.back() - i);       rt.push_back(i);     }     for (int i = 1; i <= n; ++i) {       ans[i] = l[i] + r[i] - ar[i];     }     long long maxa = max_element(ans + 1, ans + n + 1) - ans;     for (int i = maxa - 1; i; --i) {       if (ar[i] > ar[i + 1]) ar[i] = ar[i + 1];     }     for (int i = maxa + 1; i <= n; ++i)       if (ar[i - 1] < ar[i]) ar[i] = ar[i - 1];     for (int i = 1; i <= n; ++i) cout << ar[i] <<    ;     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string getbiner(long long n) {     string ret =   ;     while (n) {       if (n % 2)         ret +=  1 ;       else         ret +=  0 ;       n /= 2;     }     return ret;   }   long long getnum(string s) {     long long ret = 0;     long long now = 1;     cout <<   t  << ( s ) <<   =   << (s) <<   n ;     for (long long(i) = (0); (i) < (s.length()); (i)++) {       ret += ((s[i] -  0 ) * now);       now *= 2;     }     return ret;   }   int main() {     ios::sync_with_stdio(NULL);     cin.tie(NULL);     cout.tie(NULL);     long long n, m;     cin >> n >> m;     if (n < m) {       cout << 0 <<   n ;       return 0;     }     if ((n - m) % 2) {       cout << 0 <<   n ;       return 0;     }     long long k = (n - m) / 2;     string a = getbiner(m);     string b = getbiner(k);     long long mx = max(a.length(), b.length());     long long diffa = mx - a.length();     long long diffb = mx - b.length();     for (long long(i) = (0); (i) < (diffa); (i)++) a +=  0 ;     for (long long(i) = (0); (i) < (diffb); (i)++) b +=  0 ;     reverse(a.begin(), a.end());     reverse(b.begin(), b.end());     long long counter = 0;     int flag = 0;     for (long long(i) = (0); (i) < (a.length()); (i)++) {       if (a[i] ==  1  && b[i] ==  0 ) {         counter++;       } else if (a[i] ==  1  && b[i] ==  1 ) {         flag = 1;       }     }     if (flag) {       cout << 0 <<   n ;       return 0;     }     long long ans = pow(2, counter);     if (n == m) ans -= 2;     cout << ans <<   n ;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int arr[n + 1];     int dp[n + 1][3];     for (int i = 1; i <= n; i++) cin >> arr[i];     dp[0][0] = dp[0][1] = dp[0][2] = 0;     for (int i = 1; i <= n; i++) {       dp[i][0] = max(max(dp[i - 1][1], dp[i - 1][2]), dp[i - 1][0]);       if (arr[i] == 0) {         dp[i][1] = dp[i - 1][1];         dp[i][2] = dp[i - 1][2];       }       if (arr[i] == 1) {         dp[i][1] = max(dp[i - 1][0] + 1, dp[i - 1][2] + 1);         dp[i][2] = max(dp[i - 1][2], dp[i - 1][0]);       }       if (arr[i] == 2) {         dp[i][2] = max(dp[i - 1][0] + 1, dp[i - 1][1] + 1);         dp[i][1] = max(dp[i - 1][1], dp[i - 1][0]);       }       if (arr[i] == 3) {         dp[i][1] = max(dp[i - 1][0] + 1, dp[i - 1][2] + 1);         dp[i][2] = max(dp[i - 1][0] + 1, dp[i - 1][1] + 1);       }     }     cout << n - max(dp[n][0], max(dp[n][1], dp[n][2]));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int a[200005];   string ans =   ;   int main() {     ios_base::sync_with_stdio(0);     ;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     int l = 0, r = n - 1;     int mini = 0;     while (l <= r) {       if (l == r) {         if (a[l] > mini) {           ans +=  L ;         }         break;       }       if (a[l] > mini || a[r] > mini) {         if (a[l] > mini && a[r] > mini) {           if (a[l] < a[r]) {             ans +=  L ;             mini = a[l];             l++;           } else {             ans +=  R ;             mini = a[r];             r--;           }         } else if (a[l] > mini) {           ans +=  L ;           mini = a[l];           l++;         } else {           ans +=  R ;           mini = a[r];           r--;         }       } else {         break;       }     }     cout << ans.size() << endl;     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   int n, a[100100], m, x, rs = 1e9;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= m; i++) cin >> x, a[x]++;     for (int i = 1; i <= n; i++) rs = min(rs, a[i]);     cout << rs;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> a, b;   int x[50];   int main() {     int n, l;     scanf( %d%d , &n, &l);     for (int i = 0; i < (int)n; ++i) scanf( %d , x + i);     for (int i = 1; i < (int)n; ++i) a.push_back(x[i] - x[i - 1]);     a.push_back(x[0] + l - x[n - 1]);     for (int i = 0; i < (int)n; ++i) scanf( %d , x + i);     for (int i = 1; i < (int)n; ++i) b.push_back(x[i] - x[i - 1]);     b.push_back(x[0] + l - x[n - 1]);     for (int i = 0; i < (int)n; ++i)       if (a == b)         printf( YES n ), exit(0);       else {         int t = a[0];         for (int j = 1; j < (int)n; ++j) a[j - 1] = a[j];         a[n - 1] = t;       }     printf( NO n );   }
#include <bits/stdc++.h>   using namespace std;   complex<double> r() {     double a, b;     cin >> a >> b;     return {a, b};   }   double cross(complex<double> a, complex<double> b) {     return (conj(a) * b).imag();   }   double orient(complex<double> a, complex<double> b, complex<double> c) {     return cross(b - a, c - a);   }   void up(vector<complex<double> > &st, complex<double> p) {     while ((long long)(st).size() >= 2 && orient(st.end()[-2], st.back(), p) >= 0)       st.pop_back();     st.push_back(p);   }   struct line {     complex<double> d;     double c;     line(complex<double> a, complex<double> b) : d(b - a), c(cross(d, a)) {}   };   complex<double> inter(line l1, line l2) {     return (l1.c * l2.d - l2.c * l1.d) / cross(l1.d, l2.d);   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     long long n;     complex<double> obj;     cin >> n;     obj = r();     vector<complex<double> > ps(n);     double mx = 0, my = 0;     for (long long i = 0; i < (n); i++) {       ps[i] = r();       mx = max(mx, (ps[i].real()));       my = max(my, (ps[i].imag()));     }     ps.push_back({0.0, my});     sort(ps.begin(), ps.end(), [](complex<double> a, complex<double> b) {       return make_tuple(a.real(), -a.imag()) < make_tuple(b.real(), -b.imag());     });     vector<complex<double> > hull;     for (complex<double> p : ps) up(hull, p);     hull.push_back({mx, 0.0});     long long i = upper_bound(hull.begin(), hull.end(), obj,                               [](complex<double> a, complex<double> b) {                                 return cross(a, b) < 0;                               }) -                   hull.begin() - 1;     complex<double> a = inter(line(0, obj), line(hull[i], hull[i + 1]));     cout << fixed << setprecision(8) << obj.real() / a.real() <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   void USACO(string s) {     ios_base::sync_with_stdio(0);     cin.tie(0);     freopen((s +  .in ).c_str(),  r , stdin);     freopen((s +  .out ).c_str(),  w , stdout);   }   const long long MAXN = 5010;   void solve() {     long long n;     string s;     cin >> n;     cin >> s;     long long a[n];     for (long long i = 0; i < n; i++) cin >> a[i];     long long dp[n][4];     memset(dp, 0, sizeof(dp));     dp[0][0] = (s[0] !=  h  ? 0 : a[0]);     dp[0][1] = (s[0] ==  h  ? 0 : 1e15);     dp[0][2] = dp[0][3] = 1e15;     for (long long i = 1; i < n; i++) {       dp[i][0] = dp[i][1] = dp[i][2] = dp[i][3] = 1e15;       dp[i][0] = dp[i - 1][0] + (s[i] ==  h  ? a[i] : 0);       if (s[i] ==  h ) dp[i][1] = dp[i - 1][0];       dp[i][1] = min(dp[i][1], dp[i - 1][1] + (s[i] ==  a  ? a[i] : 0));       if (s[i] ==  a ) dp[i][2] = dp[i - 1][1];       dp[i][2] = min(dp[i][2], dp[i - 1][2] + (s[i] ==  r  ? a[i] : 0));       if (s[i] ==  r ) dp[i][3] = dp[i - 1][2];       dp[i][3] = min(dp[i][3], dp[i - 1][3] + (s[i] ==  d  ? a[i] : 0));     }     cout << min(min(dp[n - 1][0], dp[n - 1][1]), min(dp[n - 1][2], dp[n - 1][3]))          <<   n ;   }   int main() { solve(); }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 110;   const int dx[4] = {0, 0, 1, -1};   const int dy[4] = {1, -1, 0, 0};   int n, m, ret;   char G[maxn][maxn];   bool ok(int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; }   void dfs(int x, int y) {     if (G[x][y] ==  . ) return;     G[x][y] =  . ;     for (int i = 0; i < (4); i++) {       int tx = x + dx[i];       int ty = y + dy[i];       if (ok(tx, ty) && G[tx][ty] ==  B ) {         dfs(tx, ty);       }     }   }   signed main() {     while (~scanf( %d , &n) && ~scanf( %d , &m)) {       memset((G), 0, sizeof(G));       ret = 0;       for (int i = 0; i < (n); i++) scanf( %s , G[i]);       for (int i = 0; i < (n); i++) {         for (int j = 0; j < (m); j++) {           if (G[i][j] ==  B ) {             dfs(i, j);             ret++;           }         }       }       printf( %d n , ret);     }     return 0;   }
#include <bits/stdc++.h>   const int MOD = int(1E9) + 7;   const int MAXN = 100000;   int pw[MAXN + 5], cnt[MAXN + 5];   void init() {     pw[0] = 1;     for (int i = 1; i <= MAXN; i++) pw[i] = 2LL * pw[i - 1] % MOD;   }   int main() {     init();     int n, x;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       if (x != -1) cnt[x]++;     }     int ans = pw[n - 1] - 1;     for (int i = 1; i <= n; i++) ans = (ans - 1LL * (pw[cnt[i]] - 1) % MOD) % MOD;     printf( %d n , (ans + MOD) % MOD);   }
#include <bits/stdc++.h>   using namespace std;   void fre() {     freopen( c://test//input.in ,  r , stdin);     freopen( c://test//output.out ,  w , stdout);   }   template <class T1, class T2>   inline void gmax(T1 &a, T2 b) {     if (b > a) a = b;   }   template <class T1, class T2>   inline void gmin(T1 &a, T2 b) {     if (b < a) a = b;   }   const int N = 5005, M = 0, Z = 1e9 + 7;   const int inf = 0x3f3f3f3f;   template <class T1, class T2>   inline void gadd(T1 &a, T2 b) {     a = (a + b) % Z;   }   int casenum, casei;   int n, m;   int f[N][N];   int d[N][N];   int v[N];   int c[N];   vector<int> a[N];   int sz[N];   void dfs(int x) {     f[x][0] = d[x][0] = sz[x] = 0;     for (auto y : a[x]) {       dfs(y);       for (int i = sz[x]; ~i; --i) {         for (int j = 1; j <= sz[y]; ++j) {           gmin(f[x][i + j], f[x][i] + f[y][j]);           gmin(f[x][i + j], f[x][i] + d[y][j]);           gmin(d[x][i + j], d[x][i] + d[y][j]);         }       }       sz[x] += sz[y];     }     ++sz[x];     for (int i = sz[x]; i; --i) {       f[x][i] = min(f[x][i - 1] + v[x] - c[x], inf);       gmin(d[x][i], d[x][i - 1] + v[x]);     }   }   int main() {     while (~scanf( %d%d , &n, &m)) {       memset(f, 63, sizeof(f));       memset(d, 63, sizeof(d));       for (int i = 1; i <= n; ++i) {         a[i].clear();         scanf( %d%d , &v[i], &c[i]);         if (i > 1) {           int fa;           scanf( %d , &fa);           a[fa].push_back(i);         }       }       dfs(1);       int ans = 0;       for (int i = 1; i <= n; ++i) {         if (f[1][i] <= m || d[1][i] <= m) {           gmax(ans, i);         }       }       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, t, i, j, a[300006], q;   int main() {     scanf( %lld , &t);     while (t--) {       long long ans = 0;       memset(a, 0, sizeof a);       scanf( %lld%lld , &n, &q);       for (i = 1; i <= n; i++) {         scanf( %lld , &a[i]);       }       long long flag = 0;       for (i = 1; i <= n; i++) {         if (a[i] > a[i - 1] && a[i] > a[i + 1] && flag == 0) {           ans += a[i];           flag = 1;         } else if (a[i] < a[i - 1] && a[i] < a[i + 1] && flag == 1) {           ans -= a[i];           flag = 0;         }       }       printf( %lld n , ans);     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, k1, k2;       cin >> n >> k1 >> k2;       bool ya = 0;       while (k1--) {         int a;         cin >> a;         if (a == n) {           ya = 1;         }       }       while (k2--) {         int a;         cin >> a;       }       if (ya) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }   }