malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   const int N = 300010;   int n, m, ans, ansx, ansy, s1, s2, max1;   struct aa {     int x, y, w;   } a[N], b[N << 1];   multiset<int> q1, q2;   void Init(int &x) {     char y;     while ((y = getchar()) <  0  || y >  9 )       ;     x = y -  0 ;     while ((y = getchar()) >=  0  && y <=  9 ) x = (x << 3) + (x << 1) + y -  0 ;   }   bool cmp(aa x, aa y) { return x.x < y.x || x.x == y.x && x.y < y.y; }   int main() {     scanf( %d%d , &n, &m);     memset(a, 0, sizeof(a));     for (int i = 1; i <= n; ++i) scanf( %d%d , &a[i].x, &a[i].y);     for (int i = 1; i <= n; ++i)       b[(i << 1) - 1].x = a[i].x, b[(i << 1) - 1].y = 0, b[i << 1].x = a[i].y,                    b[i << 1].y = 1, b[i << 1].w = i;     n <<= 1;     sort(b + 1, b + n + 1, cmp);     ans = 0;     ansx = 0;     ansy = 0;     s1 = 0;     s2 = 0;     q1.clear();     q2.clear();     for (int i = 1; i <= n; ++i)       if (b[i].y) {         if (s1 == m && b[i].x - max1 + 1 > ans) {           ansx = max1;           ansy = b[i].x;           ans = ansy - ansx + 1;         }         if (a[b[i].w].x > max1)           q2.erase(q2.find(a[b[i].w].x)), --s2;         else {           q1.erase(q1.find(a[b[i].w].x));           --s1;           if (s2)             q1.insert(max1 = *q2.begin()), q2.erase(q2.begin()), ++s1, --s2;           else if (s1)             max1 = *q1.rbegin();         }       } else {         if (s1 < m)           q1.insert(b[i].x), ++s1, max1 = b[i].x;         else           q2.insert(b[i].x), ++s2;       }     printf( %d n , ans);     bool flag = 0;     if (ans == 0) {       for (int i = 1; i < m; ++i) printf( %d  , i);       printf( %d n , m);       return 0;     }     for (int i = 1; i <= n && m; ++i)       if (a[i].x <= ansx && a[i].y >= ansy) {         if (flag)           putchar(   );         else           flag = 1;         printf( %d , i);         --m;       }     putchar(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n = 0;     int nTemp = 0;     cin >> n;     nTemp = n;     int sum = 0;     int temp = 0;     while (nTemp--) {       cin >> temp;       sum = sum + temp;     }     int ways = 0;     for (int i = 1; i <= 5; i++) {       if ((sum + i) % (n + 1) != 1) {         ways++;       }     }     cout << ways << endl;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = (long long)5e18;   const long long N = 120000;   long long n, a[N], m, b[N];   long long pre[N], nxt[N];   long long q[N], top = 0;   long long _abs(long long x) { return x < 0 ? -x : x; }   void solve() {     scanf( %lld%lld , &n, &m);     for (long long i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       if (i > 1) b[i - 1] = _abs(a[i] - a[i - 1]);     }     b[0] = b[n] = inf;     q[++top] = 0;     for (long long i = 1; i <= n; i++) {       while (b[q[top]] < b[i]) nxt[q[top]] = i, top--;       pre[i] = q[top];       q[++top] = i;     }     while (m--) {       long long ans = 0;       long long l, r;       scanf( %lld%lld , &l, &r);       r--;       for (long long i = l; i <= r; i++) {         long long L = max(l, pre[i] + 1);         long long R = min(r, nxt[i] - 1);         ans += (R - i + 1) * (i - L + 1) * b[i];       }       printf( %lld n , ans);     }   }   signed main() {     long long TTT = 1;     while (TTT--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, q[10005];   long long W, B, X, f[10005], g[10005], cost[1005], c[1005];   int main() {     int i, j;     cin >> n >> W >> B >> X;     for (i = 1; i <= n; ++i) cin >> c[i];     for (i = 1; i <= n; ++i) cin >> cost[i];     for (i = 0; i <= 10000; ++i) f[i] = -1;     for (i = 0; i <= c[1] && i * cost[1] <= W; ++i) f[i] = W - i * cost[1];     for (i = 2; i <= n; ++i) {       int h = 0, t = -1;       for (j = 0; j <= 10000; ++j) {         if (f[j] < 0)           g[j] = -1;         else           g[j] = min(f[j] + X, W + j * B);         for (; h <= t && g[j] - g[q[t]] >= (q[t] - j) * cost[i]; --t)           ;         q[++t] = j;         for (; h <= t && q[h] < j - c[i]; ++h)           ;         if (g[q[h]] < 0)           f[j] = -1;         else           f[j] = g[q[h]] - (j - q[h]) * cost[i];       }     }     for (i = 10000; ~i; --i)       if (~f[i]) break;     cout << i;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   const int MAXN = 1000006;   char s[MAXN];   int qp(long long a, int b) {     long long ans = 1;     while (b) {       if (b % 2) ans = ans * a % mod;       b /= 2;       a = a * a % mod;     }     return ans;   }   void solve() {     int n, k;     scanf( %d%d , &n, &k);     scanf( %s , s);     string P(s);     P =  #  + P;     int pi[MAXN], m = P.size() - 1, q = 0;     pi[0] = pi[1] = 0;     for (int i = 2; i <= m; ++i) {       while (q && P[i] != P[q + 1]) q = pi[q];       if (P[i] == P[q + 1]) ++q;       pi[i] = q;     }     int ile = 0;     int prev = -m + 1;     bool ok = true;     set<int> S;     int i = m;     while (i) {       S.insert(pi[i]);       i = pi[i];     }     for (int i = 0; i < (k); ++i) {       int a;       scanf( %d , &a);       ile += max(0, a - (prev + m));       if (a - prev < m && prev > 0 && S.find(m - a + prev) == S.end()) ok = false;       prev = a;     }     ile += max(0, n - (prev + m) + 1);     if (!ok)       printf( 0 n );     else       printf( %d n , qp(26, ile));   }   int main() {     int t = 1;     for (int i = 0; i < (t); ++i) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long bigmod(long long n, long long pow, long long m) {     long long ret = 1;     while (pow) {       if (pow % 2) {         ret *= n;         ret %= m;       }       n *= n;       n %= m;       pow /= 2;     }     return ret;   }   long long arr[3 * 100009];   int main() {     long long n, a, sum = 0, sum1 = 0;     scanf( %I64d , &n);     for (long long i = 1; i <= n; i++) scanf( %I64d , &arr[i]);     sort(arr + 1, arr + n + 1);     for (long long i = 1; i <= n; i++) {       sum += (arr[i] * (bigmod((long long)2, i - 1, 1000000000 + 7)));       sum -= (arr[i] * (bigmod((long long)2, n - i, 1000000000 + 7)));       sum %= 1000000000 + 7;     }     printf( %I64d n , sum);   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1050;   long double pr[N][N];   int n, m, P[N];   inline long double gp(int i, int j) {     if (i < j)       return pr[i][j];     else       return 1 - pr[j][i];   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; ++i) cin >> P[i];     for (int i = 1; i <= n; ++i)       for (int j = i + 1; j <= n; ++j) pr[i][j] = P[i] > P[j];     for (int i = 1, a, b; i <= m; ++i) {       cin >> a >> b;       if (a > b) swap(a, b);       for (int i = 1; i <= n; ++i)         if (i != a && i != b) {           pr[i][b] = pr[i][a] = (gp(i, a) + gp(i, b)) / 2;           pr[b][i] = pr[a][i] = 1 - pr[i][a];         }       pr[a][b] = pr[b][a] = 0.5;     }     long double res = 0;     for (int i = 1; i <= n; ++i)       for (int j = i + 1; j <= n; ++j) res += pr[i][j];     cout << fixed << setprecision(10) << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int m;   vector<vector<char> > grid;   vector<vector<int> > mindist;   vector<int> first;   vector<int> last;   bool notpossible = false;   int main() {     scanf( %d %d n , &n, &m);     grid.assign(n, vector<char>(m));     first.assign(n, -1);     last.assign(n, -1);     mindist.assign(n, vector<int>(m));     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         scanf( %c , &grid[i][j]);         if (grid[i][j] ==  1 ) {           if (first[i] == -1) {             first[i] = j;           }           last[i] = j;         }       }       if (last[i] == -1) {         notpossible = true;       }       scanf(  n );     }     if (notpossible) {       printf( -1 n );       return 0;     }     for (int i = 0; i < n; i++) {       int current = m - last[i];       for (int j = 0; j < m; j++) {         if (grid[i][j] ==  1 ) {           current = 0;         }         mindist[i][j] = current;         current++;       }       current = first[i] + 1;       for (int j = m - 1; j >= 0; j--) {         if (grid[i][j] ==  1 ) {           current = 0;         }         mindist[i][j] = min(mindist[i][j], current);         current++;       }     }     int mincolumn = 0;     for (int i = 0; i < n; i++) {       mincolumn += mindist[i][0];     }     for (int i = 1; i < m; i++) {       int currentcolumn = 0;       for (int j = 0; j < n; j++) {         currentcolumn += mindist[j][i];       }       mincolumn = min(mincolumn, currentcolumn);     }     printf( %d n , mincolumn);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   struct point {     double x, y;     point(double _x = 0, double _y = 0) : x(_x), y(_y) {}     void read() { scanf( %lf%lf , &x, &y); }   };   point polygon[30];   int sign(double x) {     if (fabs(x) < eps) return 0;     return (x > 0) ? 1 : -1;   }   double cross(const point &a, const point &b, const point &c, const point &d) {     return (b.x - a.x) * (d.y - c.y) - (b.y - a.y) * (d.x - c.x);   }   double dist(const point &a, const point &b) {     return hypot(a.x - b.x, a.y - b.y);   }   pair<point, bool> getIntersection(const point &a, const point &b,                                     const point &c, const point &d) {     if (sign(cross(a, b, c, d)) == 0) return make_pair(point(0, 0), false);     double a1 = b.x - a.x, b1 = c.x - d.x, c1 = c.x - a.x;     double a2 = b.y - a.y, b2 = c.y - d.y, c2 = c.y - a.y;     double tmp = a1 * b2 - a2 * b1;     double x = (c1 * b2 - c2 * b1) / tmp;     double y = (c2 * a1 - c1 * a2) / tmp;     if (sign(x) >= 0 && sign(x - 1) < 0 && sign(y) >= 0 && sign(y - 1) < 0) {       return make_pair(point(a.x + x * (b.x - a.x), a.y + x * (b.y - a.y)), true);     } else {       return make_pair(point(0, 0), false);     }   }   int main() {     point start, end;     start.read();     end.read();     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) polygon[i].read();     int flag = 0;     point a, b;     int la, lb;     for (int i = 0; i < n; i++) {       pair<point, bool> tmp =           getIntersection(start, end, polygon[i], polygon[(i + 1) % n]);       if (tmp.second) {         if (flag == 0) {           a = tmp.first;           la = i;         } else {           b = tmp.first;           lb = i;         }         flag++;       }     }     if (flag < 2) {       printf( %.10lf n , dist(start, end));       return 0;     }     if (sign(dist(start, a) - dist(start, b)) > 0) {       swap(a, b);       swap(la, lb);     }     double ans = dist(start, a) + dist(a, b) * 2 + dist(b, end);     double s1 = dist(start, a) + dist(a, polygon[(la + 1) % n]);     for (int i = (la + 1) % n; i != lb; i = (i + 1) % n)       s1 += dist(polygon[i], polygon[(i + 1) % n]);     s1 += dist(polygon[lb], b) + dist(b, end);     ans = min(ans, s1);     double s2 = dist(start, a) + dist(a, polygon[la]);     for (int i = la; i != (lb + 1) % n; i = (i + n - 1) % n)       s2 += dist(polygon[i], polygon[(i + n - 1) % n]);     s2 += dist(polygon[(lb + 1) % n], b) + dist(b, end);     ans = min(ans, s2);     printf( %.10lf n , ans);     return 0;   }
#include <bits/stdc++.h>   int const MAXN = 300100;   char a[MAXN];   int n, x, y;   long long solve() {     if (n == 1) return a[0] ==  0  ? y : 0;     long long cc = 1;     for (int i = 1; i < n; ++i) {       if (a[i - 1] != a[i]) ++cc;     }     if (a[0] ==  1 ) --cc;     if (a[n - 1] ==  1 ) --cc;     if (cc <= 0) return 0;     long long price = ((cc + 1) / 2) * y;     long long rot = cc / 2;     long long ans = price;     while (rot > 0) {       price -= y;       price += x;       ans = std::min(ans, price);       --rot;     }     return ans;   }   int main() {     scanf( %d %d %d , &n, &x, &y);     scanf( %s , a);     long long ans = solve();     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     int hired = 0, untreated, counter = 0, counter2 = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] != -1) {         hired += a[i];       } else if (a[i] == -1 && hired != 0) {         counter++;         hired--;       }       if (a[i] == -1) {         counter2++;       }     }     untreated = counter2 - counter;     cout << untreated;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> sorted_pair(int x, int y) {     if (x > y) {       return pair<int, int>(y, x);     } else {       return pair<int, int>(x, y);     }   }   int main() {     int n;     cin >> n;     vector<vector<int>> graph(n + 1, vector<int>());     set<pair<int, int>> pairs;     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       graph[x].push_back(y);       graph[y].push_back(x);     }     for (int i = 1; i <= n; i++) {       queue<pair<int, int>> q;       q.push(pair<int, int>(i, 0));       vector<bool> visited(n + 1, false);       while (!q.empty()) {         int v = q.front().first;         int lvl = q.front().second;         visited[v] = true;         if (lvl < 2) {           for (int k = 0; k < graph[v].size(); k++) {             if (!visited[graph[v][k]]) {               q.push(pair<int, int>(graph[v][k], lvl + 1));             }           }         } else if (lvl == 2) {           pairs.insert(sorted_pair(i, v));         }         q.pop();       }     }     cout << pairs.size() << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct quad {     pair<int, int> points[4];     void read() {       for (int i = 0; i < 4; i++) {         cin >> points[i].first >> points[i].second;       }     }     int cross(int a, int b, int c, int d) { return a * d - b * c; }     int sgn(int x) {       if (x < 0) return -1;       if (x > 0) return 1;       return 0;     }     bool contains(int x, int y) {       int side = 0;       for (int i = 0; i < 4; i++) {         int j = (i + 1) % 4;         int a = points[j].first - points[i].first;         int b = points[j].second - points[i].second;         int c = x - points[i].first;         int d = y - points[i].second;         int v = sgn(cross(a, b, c, d));         if (v == 0) {           continue;         }         if (side == 0) {           side = v;         } else if (side != v) {           return false;         }       }       return true;     }   };   int main() {     quad A, B;     A.read();     B.read();     for (int x = -100; x <= 100; x++) {       for (int y = -100; y <= 100; y++) {         if (A.contains(x, y) && B.contains(x, y)) {           cout <<  YES n ;           return 0;         }       }     }     cout <<  NO n ;     return 0;   }
#include <bits/stdc++.h>   const int N = 2000010;   using namespace std;   char s[N], t[N];   int main() {     int n;     scanf( %d , &n);     scanf( %s%s , s, t);     int a[5] = {};     for (int i = 0; i < 2 * n; i++) {       a[((s[i] ==  1 ) << 1) | (t[i] ==  1 )]++;     }     int s0 = 0, s1 = 0;     int t = 0;     while (a[3]) {       if (t == 0)         s0++;       else         s1++;       a[3]--;       t ^= 1;     }     while (a[2]) {       if (t == 0)         s0++, a[2]--;       else         a[2]--;       t ^= 1;     }     while (a[1]) {       if (t == 1)         s1++, a[1]--;       else         a[1]--;       t ^= 1;     }     if (s0 > s1)       puts( First );     else if (s0 == s1)       puts( Draw );     else       puts( Second );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long m, n;     cin >> m >> n;     double sum;     sum = m;     for (double i = m - 1; i > 0; i--) {       sum -= pow(i / m, n);     }     cout << fixed;     cout << setprecision(12) << sum <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100007, dx[8] = {1, 1, 1, 0, 0, -1, -1, -1},             dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};   int xs, ys, xt, yt, n, r[maxn], a[maxn], b[maxn], d[maxn] = {}, cnt = 0;   bool vis[maxn] = {};   vector<int> row, col[maxn];   vector<pair<int, int> > seg[maxn], cells[maxn];   int dist(int x, int y) {     x = lower_bound(row.begin(), row.end(), x) - row.begin() + 1;     y = lower_bound(cells[x].begin(), cells[x].end(), make_pair(y, 0)) -         cells[x].begin();     return d[cells[x][y].second];   }   void set_dist(int x, int y, int val) {     x = lower_bound(row.begin(), row.end(), x) - row.begin() + 1;     y = lower_bound(cells[x].begin(), cells[x].end(), make_pair(y, 0)) -         cells[x].begin();     d[cells[x][y].second] = val;   }   bool valid(int x, int y) {     if (!binary_search(row.begin(), row.end(), x)) return 0;     x = lower_bound(row.begin(), row.end(), x) - row.begin() + 1;     if (!binary_search(col[x].begin(), col[x].end(), y)) return 0;     return 1;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> xs >> ys >> xt >> yt >> n;     for (int i = 1; i <= n; ++i) {       cin >> r[i] >> a[i] >> b[i];       row.push_back(r[i]);     }     sort(row.begin(), row.end());     for (int i = 1; i <= n; ++i)       r[i] = lower_bound(row.begin(), row.end(), r[i]) - row.begin() + 1;     for (int i = 1; i <= n; ++i) seg[r[i]].push_back({a[i], b[i]});     for (int i = 1; i <= n; ++i) {       if (vis[r[i]]) continue;       sort(seg[r[i]].begin(), seg[r[i]].end());       int cur = 1;       for (auto it : seg[r[i]]) {         cur = max(it.first, cur);         for (int j = cur; j <= it.second; ++j)           cells[r[i]].push_back({j, ++cnt}), col[r[i]].push_back(j);       }       vis[r[i]] = 1;     }     queue<pair<int, int> > q;     q.push({xs, ys});     set_dist(xs, ys, 1);     while (q.size()) {       int x = q.front().first, y = q.front().second;       q.pop();       for (int i = 0; i < 8; ++i) {         int xx = dx[i] + x, yy = dy[i] + y;         if (valid(xx, yy) && dist(xx, yy) == 0)           set_dist(xx, yy, dist(x, y) + 1), q.push({xx, yy});       }     }     cout << dist(xt, yt) - 1;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t, i, j, c = 0, d = 0, a, b, k = 0, sum = 0, sum1 = 0;     cin >> t;     string s;     stack<char> st;     cin >> s;     for (i = 0; i < s.size(); i++) {       if (s[i] ==  ( ) {         sum++;       } else {         sum1++;       }       if (s[i] ==  ( ) {         st.push(s[i]);       } else if (s[i] ==  ) ) {         if (st.size() > 0 && st.top() ==  ( ) {           st.pop();         } else {           c++;         }       }     }     if (sum != sum1) {       cout <<  NO  << endl;     } else if (st.size() <= 1 && c <= 1) {       cout <<  YES  << endl;     } else {       cout <<  NO  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int nxt() {     int x;     scanf( %d , &x);     return x;   }   int main() {     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());     int n = nxt();     vector<int> a(n);     for (int i = 0; i < n; ++i) {       a[i] = nxt();     }     int s = accumulate((a).begin(), (a).end(), 0);     int k = *max_element((a).begin(), (a).end());     int t = k * n;     int need = 2 * s + 1;     if (need > t) {       k += (need - t + n - 1) / n;     }     printf( %d n , k);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     size_t found;     vector<string> str;     string a, b, c, in, out;     long long i, j, k, m, n, l, t;     cin >> t;     while (t--) {       cin >> in;       for (i = 0, j = 0, k = 0; i < in.size(); i++) {         if (in[i] ==  :  && i == 0) {           i++;           j = 1;         }         if (in[i] ==  : ) {           b = in.substr(j, i - j);           if (b ==   ) k++;           str.push_back(b);           j = i + 1;         }       }       if (in[in.size() - 1] !=  : ) {         b = in.substr(j, in.size() - j);         str.push_back(b);       }       for (i = 0; i < str.size(); i++) {         if (i > 0) cout <<  : ;         if (str[i] ==   ) {           for (j = 0; j < 8 - str.size() + k; j++) {             if (j > 0) cout <<  : ;             cout <<  0000 ;           }         } else {           if (str[i].size() < 4) {             for (l = str[i].size(); l < 4; l++) cout <<  0 ;           }           cout << str[i];         }       }       cout << endl;       str.clear();       in.clear();       k = 0;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MaxN = 100000 + 5;   int N;   map<int, int> Numbers;   int main(void) {     scanf( %d , &N);     if (N % 2) {       printf( NO n );       return 0;     }     for (int i = 0; i < N; i++) {       int Number;       scanf( %d , &Number);       Numbers[Number]++;     }     while (!Numbers.empty()) {       while (!Numbers.begin()->second) Numbers.erase(Numbers.begin());       int Number = Numbers.begin()->first;       if (N == 2) {         if (Numbers[Number + 1] == 1)           printf( YES n );         else           printf( NO n );         return 0;       }       if (Numbers[Number + 1] < 2) {         printf( NO n );         return 0;       }       Numbers[Number]--;       Numbers[Number + 1]--;       N -= 2;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b, c, d, l = 1, v[1001];     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a >> b >> c >> d;       v[i] = a + b + c + d;       if (v[0] < v[i]) l++;     }     cout << l;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100 * 1000 + 5;   int n, m, cnt;   short int mark[N];   bool visited[N];   vector<int> in[N], out[N], comp[N];   void input() {     cin.tie(0);     cin >> n >> m;     int v, u;     for (int i = 0; i < m; i++) {       cin >> v >> u;       v--, u--;       out[v].push_back(u);       in[u].push_back(v);     }     return;   }   void getComp(int v) {     visited[v] = true;     for (int u : in[v])       if (!visited[u]) getComp(u);     for (int u : out[v])       if (!visited[u]) getComp(u);     comp[cnt].push_back(v);     return;   }   bool isDAG(int v) {     bool res = true;     mark[v]++;     for (int u : out[v])       if (!mark[u])         res &= isDAG(u);       else if (mark[u] == 1)         return false;     mark[v]++;     return res;   }   void solve() {     for (int i = 0; i < n; i++)       if (!visited[i]) {         getComp(i);         cnt++;       }     for (int i = 0; i < cnt; i++) {       bool check = true;       for (int v : comp[i])         if (!mark[v]) check &= isDAG(v);       if (check) n--;     }   }   int main() {     ios::sync_with_stdio(false);     input();     solve();     cout << n << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using namespace std;   long long n, m, f, t, d, val;   vector<long long> a, c;   set<pair<long long, long long> > s;   int main() {     cin.tie(0);     cout.tie(0);     ios::sync_with_stdio(0);     cin >> n >> m;     a.resize(n);     c.resize(n);     for (auto& i : a) cin >> i;     for (auto& i : c) cin >> i;     for (int i = 0; i < n; i++) s.insert(make_pair(c[i], i));     while (m--) {       cin >> t >> d;       t--;       long long bill = 0;       val = min(a[t], d);       bill += val * c[t];       a[t] -= val;       d -= val;       while (d) {         if (s.empty()) break;         val = min(a[s.begin()->second], d);         bill += val * (s.begin()->first);         a[s.begin()->second] -= val;         d -= val;         if (a[s.begin()->second] == 0) s.erase(s.begin());       }       if (d)         cout <<  0 n ;       else         cout << bill <<   n ;     }   }