malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   const long long Inf = 0x3f3f3f3f3f3f3f3f;   int n, x, y, z, cnt, tot, head[maxn], vis[maxn];   long long res, r[maxn], l[maxn], l0[maxn], r0[maxn], sum[maxn], ans1,       ans2 = Inf, f[maxn], cir[maxn];   struct Edge {     int to, next, w;   } e[maxn << 1];   void add(int u, int v, int w) {     e[++cnt].to = v;     e[cnt].w = w;     e[cnt].next = head[u];     head[u] = cnt;   }   int fnd(int u, int fa = -1) {     if (vis[u]) return u;     vis[u] = -1;     for (int i = head[u]; i; i = e[i].next) {       int v = e[i].to;       if (v != fa) {         res = fnd(v, u);         if (res)           return cir[++tot] = u, sum[tot] = e[i].w, vis[u] = 1,                  res == u ? 0 : res;       }     }     return 0;   }   void dfs(int u, int fa = -1) {     for (int i = head[u]; i; i = e[i].next) {       int v = e[i].to;       if (vis[v] != 1 && v != fa) {         dfs(v, u);         ans1 = max(ans1, f[u] + f[v] + e[i].w);         f[u] = max(f[u], f[v] + e[i].w);       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d%d , &x, &y, &z);       add(x, y, z);       add(y, x, z);     }     fnd(1);     sum[0] = 0;     for (int i = 1; i <= tot; i++) {       dfs(cir[i]);       sum[i] += sum[i - 1];     }     res = l[0] = l0[0] = -Inf;     for (int i = 1; i <= tot; i++) {       l0[i] = max(l0[i - 1], f[cir[i]] + sum[i] + res);       l[i] = max(l[i - 1], f[cir[i]] + sum[i]);       res = max(res, f[cir[i]] - sum[i]);     }     res = r[tot + 1], r0[tot + 1] = -Inf;     for (int i = tot; i >= 1; i--) {       r0[i] = max(r0[i + 1], f[cir[i]] - sum[i] + res);       r[i] = max(r[i + 1], f[cir[i]] + sum[tot] - sum[i]);       res = max(res, f[cir[i]] + sum[i]);     }     for (int i = 1; i <= tot; i++) {       ans2 = min(ans2, max(l[i - 1] + r[i], max(l0[i - 1], r0[i])));     }     printf( %lld n , max(ans1, ans2));     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   bool Finish_read;   template <class T>   inline void read(T &x) {     Finish_read = 0;     x = 0;     int f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       if (ch == EOF) return;       ch = getchar();     }     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     x *= f;     Finish_read = 1;   }   template <class T>   inline void print(T x) {     if (x / 10 != 0) print(x / 10);     putchar(x % 10 +  0 );   }   template <class T>   inline void writeln(T x) {     if (x < 0) putchar( - );     x = abs(x);     print(x);     putchar(  n );   }   template <class T>   inline void write(T x) {     if (x < 0) putchar( - );     x = abs(x);     print(x);     putchar(   );   }   int main() {     int n;     scanf( %d , &n);     int t = sqrt(n);     if (n % t) {       for (int i = n / t * t + 1; i <= n; i++) printf( %d  , i);     }     for (int i = n / t; i >= 1; i--)       for (int j = (i - 1) * t + 1; j <= i * t; j++) printf( %d  , j);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200100;   int n, b;   queue<pair<int, int> > q;   long long ans[MAXN];   int main() {     scanf( %d%d , &n, &b);     long long available = 0;     memset(ans, 0xff, sizeof ans);     for (int i = 0, t, d; i < n; ++i) {       scanf( %d%d , &t, &d);       while (!q.empty() && available <= t) {         ans[q.front().first] = available += q.front().second;         q.pop();       }       available = max(available, (long long)t);       if ((int)q.size() < b) {         q.push(make_pair(i, d));       }     }     while (!q.empty()) {       ans[q.front().first] = available += q.front().second;       q.pop();     }     for (int i = 0; i < n; ++i) {       printf( %lld%c , ans[i],    n [i + 1 == n]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int x[3], l;     scanf( %d%d%d%d , x, x + 1, x + 2, &l);     long long ans = 1LL * (l + 3) * (l + 2) * (l + 1) / 6;     for (int i = 0; i < 3; i++)       for (int lx = 0; lx <= l; lx++) {         int xx = min(l - lx, x[i] + lx - x[(i + 1) % 3] - x[(i + 2) % 3]);         if (xx >= 0) ans -= 1LL * (2 + xx) * (1 + xx) / 2;       }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[100003], i;   int main() {     cin >> n;     for (i = 1; i <= n; i++) cin >> a[i];     long long p = clock();     while (clock() - p < 1400)       ;     sort(a + 1, a + 1 + n);     for (i = 1; i <= n; i++) cout << a[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 3010;   int a[MAXN], b[MAXN], c[MAXN];   int d[MAXN][2];   int main() {     int n;     while (~scanf( %d , &n)) {       for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);       for (int i = 1; i <= n; ++i) scanf( %d , &b[i]);       for (int i = 1; i <= n; ++i) scanf( %d , &c[i]);       d[1][0] = b[1];       d[1][1] = a[1];       for (int i = 2; i <= n; ++i) {         d[i][0] = max(d[i - 1][0] + b[i], d[i - 1][1] + c[i]);         d[i][1] = max(d[i - 1][0] + a[i], d[i - 1][1] + b[i]);       }       printf( %d n , d[n][1]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, l, a[110], s, x, len;   int main() {     cin >> n >> l;     for (int i = 1; i <= n; i++) {       cin >> a[i];       if (a[i] > s) s = a[i];     }     for (int i = l; i <= s; i++) {       for (int j = 1; j <= n; j++) x += a[j] / i;       if (len < i * x) len = i * x;       x = 0;     }     cout << len << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a, b;   int x;   int main() {     scanf( %d%d%d , &a, &b, &x);     if (a >= b)       for (int i = 0; i <= x - 2; i++) {         printf( %d , i & 1);         a -= !(i & 1);         b -= (i & 1);       }     else {       for (int i = 1; i <= x - 1; i++) {         printf( %d , i & 1);         a -= !(i & 1);         b -= (i & 1);       }       x++;     }     if (x & 1) {       for (int i = 0; i < a; i++) printf( 0 );       for (int i = 0; i < b; i++) printf( 1 );     } else {       for (int i = 0; i < b; i++) printf( 1 );       for (int i = 0; i < a; i++) printf( 0 );     }     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     long long n, m, p;     cin >> n >> m >> p;     long long a;     long long b;     long long u = 0, v = 0;     for (long long i = 1; i <= n; i++) {       cin >> a;       if (u == 0 && a % p != 0) {         u = i;       }     }     for (long long i = 1; i <= m; i++) {       cin >> b;       if (v == 0 && b % p != 0) {         v = i;       }     }     cout << u + v - 2 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     int n, k;     cin >> n >> k;     vector<int> *dist = new vector<int>[n];     int len = 0;     for (int i = 0; i < n; i++) {       int d;       cin >> d;       dist[d].push_back(i);       len = max(len, d);     }     if (dist[0].size() != 1) {       cout << -1;       return 0;     }     for (int i = 1; i <= len; i++) {       if (dist[i].empty() ||           dist[i].size() > (long long)dist[i - 1].size() * (k - (i != 1))) {         cout << -1;         return 0;       }     }     cout << n - 1 << endl;     for (int i = 1; i <= len; i++) {       int counter = 0;       int index = 0;       for (int child : dist[i]) {         if (counter == k - (i != 1)) {           counter = 0;           index++;         }         cout << dist[i - 1][index] + 1 <<     << child + 1 << endl;         counter++;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, n;     cin >> a >> b >> n;     bool aa = 0, bb = 1;     if (a < b) swap(a, b), swap(aa, bb);     for (int i = 0; i < a - (n + 1) / 2 - (n % 2 == 0); i++) cout << aa;     for (int i = 0; i < (n + 1) / 2; i++) cout << aa << bb;     for (int i = 0; i < b - (n + 1) / 2; i++) cout << bb;     if (n % 2 == 0) cout << aa;   }
#include <bits/stdc++.h>   char s[100005], stack[100005], ss;   int main() {     long long n, k, now, i, i2, l, plus = 0, kid, pos;     scanf( %lld %lld , &n, &k);     for (i = 1; i <= k; i++) {       scanf( %lld , &now);       plus = 0;       ss = 0;       kid = n;       while (kid / 2 + plus + 1 != now) {         if (kid / 2 + plus + 1 < now) {           stack[++ss] =  R ;           plus += kid / 2 + 1;           kid /= 2;         } else {           stack[++ss] =  L ;           kid /= 2;         }       }       now = kid;       scanf( %s , s);       l = strlen(s);       for (i2 = 0; i2 < l; i2++) {         if (s[i2] ==  R ) {           if (now == 1) continue;           plus += now / 2 + 1;           now /= 2;           stack[++ss] =  R ;         } else if (s[i2] ==  L ) {           if (now == 1) continue;           now /= 2;           stack[++ss] =  L ;         } else {           if (now == n) continue;           if (stack[ss] ==  R ) plus -= now + 1;           now = now * 2 + 1;           ss--;         }       }       printf( %lld n , plus + now / 2 + 1);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 10;   const int MAXM = 4e6 + 10;   const long long MOD = 1e9 + 7;   const long long INF = 0x3f3f3f3f;   const unsigned long long BASE = 233;   const int base = 5;   mt19937_64 mt(time(0));   char rubbish[110];   bool GETDATA;   inline long long read() {     long long x = 0, f = 1;     char ch;     do {       ch = getchar();       if (ch ==  - ) f = -1;     } while (ch <  0  || ch >  9 );     do {       x = x * 10 + ch - 48;       ch = getchar();     } while (ch >=  0  && ch <=  9 );     return x * f;   }   inline void write(long long x, char con =   n ) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) write(x / 10, 0);     putchar(x % 10 +  0 );     if (con) {       printf( %c , con);     }   }   long long N, M, K, Q;   long long a[MAXN];   char s[MAXN][110];   int sz[MAXN];   vector<int> es[30];   vector<pair<int, int> > tot;   vector<int> topsort;   bool occ[30];   bool vis[30];   int ord[30];   void dfs(int u) {     vis[u] = true;     for (auto v : es[u]) {       if (!vis[v]) {         dfs(v);       }     }     topsort.push_back(u);   }   void init() {     for (int i = 0; i < 30; i++) {       es[i].clear();       occ[i] = false;       vis[i] = false;     }     tot.clear();     topsort.clear();   }   void solve() {     long long ans = 0;     int now;     int id;     N = read();     M = read();     init();     for (int i = 0; i < N; i++) {       now = read();       for (int j = 0; j < M; j++) {         id = now * N + j;         scanf( %s , s[id]);         for (sz[id] = 0; s[id][sz[id]] !=   0 ; sz[id]++) {           occ[s[id][sz[id]] -  a ] = true;         }       }     }     for (int i = 1, j; i < N * M; i++) {       now = min(sz[i - 1], sz[i]);       for (j = 0; j < now; j++) {         if (s[i - 1][j] != s[i][j]) {           break;         }       }       if (j < now) {         es[s[i][j] -  a ].push_back(s[i - 1][j] -  a );         tot.push_back(pair<int, int>{s[i][j] -  a , s[i - 1][j] -  a });       } else if (sz[i - 1] > sz[i]) {         printf( IMPOSSIBLE n );         return;       }     }     for (int i = 0; i < 26; i++) {       if (occ[i] && !vis[i]) {         dfs(i);       }     }     for (int i = 0; i < topsort.size(); i++) {       ord[topsort[i]] = i;     }     for (auto x : tot) {       if (ord[x.first] < ord[x.second]) {         printf( IMPOSSIBLE n );         return;       }     }     for (auto ch : topsort) {       printf( %c , ch +  a );     }     printf(  n );   }   int main() {     int T;     T = 1;     for (int t = 1; t <= T; t++) {       GETDATA = false;       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int ts, kk = 1;   template <class T>   inline T _sq(T a) {     return a * a;   }   template <class T, class X>   inline T _pow(T a, X y) {     T z = 1;     for (int i = 1; i <= y; i++) {       z *= a;     }     return z;   }   template <class T>   inline T _gcd(T a, T b) {     a = _abs(a);     b = _abs(b);     if (!b) return a;     return _gcd(b, a % b);   }   template <class T>   inline T _lcm(T a, T b) {     a = _abs(a);     b = _abs(b);     return (a / _gcd(a, b)) * b;   }   template <class T>   inline T _extended(T a, T b, T &x, T &y) {     a = _abs(a);     b = _abs(b);     T g, x1, y1;     if (!b) {       x = 1;       y = 0;       g = a;       return g;     }     g = _extended(b, a % b, x1, y1);     x = y1;     y = x1 - (a / b) * y1;     return g;   }   template <class T, class X>   inline bool getbit(T a, X i) {     T t = 1;     return ((a & (t << i)) > 0);   }   template <class T, class X>   inline T setbit(T a, X i) {     T t = 1;     return (a | (t << i));   }   template <class T, class X>   inline T resetbit(T a, X i) {     T t = 1;     return (a & (~(t << i)));   }   template <class T, class X>   inline T togglebit(T a, X i) {     T t = 1;     return (a ^ (t << i));   }   template <class T, class X, class Y>   inline T _bigmod(T n, X m, Y mod) {     unsigned long long ret = 1, a = n % mod;     while (m) {       if (m & 1) ret = (ret * a) % mod;       m >>= 1;       a = (a * a) % mod;     }     ret %= mod;     return (T)ret;   }   template <class T, class Y>   inline T _modinv(T n, Y mod) {     return _bigmod(n, mod - 2, mod);   }   int main() {     int n, cnt = 0;     ;     double x, tot = 0.0, ans, add;     cin >> n;     for (int i = 1; i <= n + n; i++) {       cin >> x;       add = (x - (int)x);       tot += add;       if (add == 0.0) cnt++;     }     tot -= n;     while (cnt-- > 0 && tot < -.5) tot += 1.0;     tot = fabs(tot);     cout << setprecision(3) << fixed << tot << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const int maxn = 1007;   long long dp[maxn][maxn][4];   char s[maxn];   int T, k;   long long dfs(int pos, int dis, int ok, int limit) {     if (pos == -1) {       return ok;     }     if (!limit && dp[pos][dis][ok] != -1) return dp[pos][dis][ok];     int up = limit ? s[pos] -  0  : 9;     long long res = 0;     for (int i = 0; i <= up; i++) {       if (i != 4 && i != 7)         res = (res + dfs(pos - 1, max(0, dis - 1), ok, limit && (i == up))) % MOD;       else         res = (res + dfs(pos - 1, k, ok || dis, limit && (i == up))) % MOD;     }     if (!limit) dp[pos][dis][ok] = res;     return res;   }   int check(int len) {     int pre = -1;     for (int i = 0; i < len; i++) {       if (s[i] ==  4  || s[i] ==  7 ) {         if (pre != -1 && i - pre <= k) return 1;         pre = i;       }     }     return 0;   }   int main() {     memset(dp, -1, sizeof(dp));     scanf( %d%d , &T, &k);     while (T--) {       scanf( %s , s);       int len = strlen(s);       for (int i = 0; i < len / 2; i++) swap(s[i], s[len - 1 - i]);       long long ans = (dfs(len - 1, 0, 0, true) - check(len) + MOD) % MOD;       scanf( %s , s);       len = strlen(s);       for (int i = 0; i < len / 2; i++) swap(s[i], s[len - 1 - i]);       ans = (dfs(len - 1, 0, 0, true) - ans + MOD) % MOD;       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   clock_t time_p = clock();   void timer() {     time_p = clock() - time_p;     cerr <<  Execution Time:   << (double)(time_p) / CLOCKS_PER_SEC <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, k, r = 2, g = 5, b = 8, cnt = 0, tot = 0;     cin >> n >> k;     r *= n;     g *= n;     b *= n;     if (r % k)       cnt += (r / k + 1);     else       cnt += r / k;     if (g % k)       cnt += (g / k + 1);     else       cnt += g / k;     if (b % k)       cnt += (b / k + 1);     else       cnt += b / k;     cout << cnt;     timer();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e3 + 6.66;   const int MAXM = 2e3 + 6.66;   const long long MOD = 1e9 + 7;   long long dp[MAXN][MAXM];   long long dp1[MAXN][MAXM];   long long par[MAXN][MAXM];   long long par2[MAXN][MAXM];   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, m;     cin >> n >> m;     for (int sz = 0; sz < m - 1; sz++) dp[0][sz] = 1;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < m - 1; j++) {         if (i == 1) {           dp[i][j] = 1;           dp1[i][j] = 1;           par[i][j] = j + 1;           par2[i][j] = par2[i][j - 1] + j + 1;           continue;         }         dp1[i][j] = (1 + ((2 + j) * par[i - 1][j - 1]) % MOD * 1LL -                      par2[i - 1][j - 1] * 1LL + MOD) %                     MOD;         dp[i][j] =             (1 + ((2 + j) * par[i - 1][j]) % MOD - par2[i - 1][j] + MOD) % MOD;         par[i][j] = (par[i][j - 1] + dp[i][j]) % MOD;         par2[i][j] = (par2[i][j - 1] + (1LL * (j + 1LL) * dp[i][j]) % MOD) % MOD;       }     }     long long ans = 0LL;     for (int t = 0; t < n; t++)       for (int sz = 0; sz < m - 1; sz++) {         ans = (ans +                (((m - sz - 1LL) * 1LL * dp[t + 1][sz]) % MOD * dp1[n - t][sz]) %                    MOD) %               MOD;       }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long i, n, m, a[1000001], t1, t2, sum;   int main() {     cin >> n >> m;     for (i = 0; i < m; i++) {       scanf( %I64d %I64d , &t1, &t2);       a[t1]++;       a[t2]++;     }     for (i = 1; i <= n; i++) sum += a[i] * (n - 1LL - a[i]);     sum /= 2LL;     cout << n * (n - 1LL) * (n - 2LL) / 6LL - sum << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200005;   int N, M, K;   int A[MAXN];   int B[MAXN];   set<int> adj[MAXN];   int deg[MAXN];   set<pair<int, int> > st;   bool inGraph[MAXN];   int ans[MAXN];   void update(int x) {     if (!inGraph[x]) return;     set<pair<int, int> >::iterator it = st.find(make_pair(deg[x], x));     st.erase(it);     deg[x]--;     st.insert(make_pair(deg[x], x));   }   void fix() {     while (!st.empty() && st.begin()->first < K) {       int cur = st.begin()->second;       st.erase(st.begin());       inGraph[cur] = false;       for (auto nxt : adj[cur]) {         adj[nxt].erase(cur);         update(nxt);       }       adj[cur].clear();     }   }   int main() {     scanf( %d %d %d , &N, &M, &K);     for (int i = 0; i < M; i++) {       scanf( %d %d , &A[i], &B[i]);       deg[A[i]]++;       deg[B[i]]++;       adj[A[i]].insert(B[i]);       adj[B[i]].insert(A[i]);     }     for (int i = 1; i <= N; i++) {       st.insert(make_pair(deg[i], i));       inGraph[i] = true;     }     for (int i = M - 1; i >= 0; i--) {       fix();       ans[i] = st.size();       adj[A[i]].erase(B[i]);       adj[B[i]].erase(A[i]);       if (inGraph[B[i]]) update(A[i]);       if (inGraph[A[i]]) update(B[i]);     }     for (int i = 0; i < M; i++) printf( %d n , ans[i]);     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( avx2 )   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long k;     cin >> k;     long long pref = 0;     vector<long long> poww10(17);     poww10[0] = 1;     for (int i = 1; i < 17; i++) {       poww10[i] = poww10[i - 1] * 10;     }     for (int len = 1; len <= 15; len++) {       if (pref + len * (poww10[len] - poww10[len - 1]) >= k) {         k -= pref;         long long l = 0, r = poww10[len] - poww10[len - 1];         while (l + 1 < r) {           long long mid = (l + r) / 2;           if (len * mid < k) {             l = mid;           } else {             r = mid;           }         }         k -= len * l;         cout << to_string(poww10[len - 1] + l)[k - 1] <<   n ;         return 0;       }       pref += len * (poww10[len] - poww10[len - 1]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int l, r, x, y;   vector<int> primes;   int cnt(int idx, int a, int b) {     if (idx == primes.size()) return a >= l && a <= r && b >= l && b <= r;     return cnt(idx + 1, a * primes[idx], b) + cnt(idx + 1, a, b * primes[idx]);   }   int main() {     cin >> l >> r >> x >> y;     if (y % x) return puts( 0 );     y /= x;     for (int i = 2; i * i <= y; i++) {       int v = 1;       while (y % i == 0) y /= i, v *= i;       if (v > 1) primes.push_back(v);     }     if (y > 1) primes.push_back(y);     cout << cnt(0, x, x);   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());   const ld pi = acos(0) * 2;   template <typename T>   inline void read(T &x) {     char c;     bool nega = 0;     while ((!isdigit(c = getchar())) && (c !=  - ))       ;     if (c ==  - ) {       nega = 1;       c = getchar();     }     x = c - 48;     while (isdigit(c = getchar())) x = x * 10 + c - 48;     if (nega) x = -x;   }   template <typename T>   inline void writep(T x) {     if (x > 9) writep(x / 10);     putchar(x % 10 + 48);   }   template <typename T>   inline void write(T x) {     if (x < 0) {       putchar( - );       x = -x;     }     writep(x);   }   template <typename T>   inline void writeln(T x) {     write(x);     putchar(  n );   }   int n, m;   int r[500001];   int root(int u) {     if (r[u] < 0) return u;     return r[u] = root(r[u]);   }   bool unite(int u, int v) {     u = root(u);     v = root(v);     if (u == v) return 0;     if (r[u] > r[v]) swap(u, v);     r[u] += r[v];     r[v] = u;     return 1;   }   int main() {     read(n);     read(m);     for (int i = (1); i <= (n); i++) r[i] = -1;     for (int i = (1); i <= (m); i++) {       int k;       read(k);       int v = -1;       int u;       for (int i = (1); i <= (k); i++) {         read(u);         if (v != -1)           unite(u, v);         else           v = u;       }     }     for (int i = (1); i <= (n); i++) {       write(-r[root(i)]);       putchar(   n [i == n]);     }   }
#include <bits/stdc++.h>   using namespace std;   struct graph {     int n;     vector<vector<int>> adj;     graph(int n) : n(n), adj(n) {}     void add_edge(int u, int v) { adj[u].push_back(v); }     vector<int> &operator[](int u) { return adj[u]; }   };   vector<vector<int>> scc_gabow(graph &adj) {     int n = adj.n;     vector<vector<int>> scc;     vector<int> S, B, I(n);     function<void(int)> dfs = [&](int u) {       B.push_back(I[u] = S.size());       S.push_back(u);       for (int v : adj[u])         if (!I[v])           dfs(v);         else           while (I[v] < B.back()) B.pop_back();       if (I[u] == B.back()) {         scc.push_back({});         for (B.pop_back(); I[u] < S.size(); S.pop_back()) {           scc.back().push_back(S.back());           I[S.back()] = n + scc.size();         }       }     };     for (int u = 0; u < n; ++u)       if (!I[u]) dfs(u);     return scc;   }   const long long mod = 1000000007;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     vector<long long> a(n);     for (int i = 0; i < n; ++i) cin >> a[i];     graph adj(n);     int m;     cin >> m;     for (int i = 0; i < m; ++i) {       int u, v;       cin >> u >> v;       adj.add_edge(--u, --v);     }     auto scc = scc_gabow(adj);     long long mn = 0, ways = 1;     for (auto &comp : scc) {       long long best = a[comp[0]], total = 0;       for (int u : comp)         if (a[u] == best)           ++total;         else if (a[u] < best)           best = a[u], total = 1;       mn += best;       ways = ways * total % mod;     }     cout << mn <<     << ways <<   n ;     return 0;   }