malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v);   template <typename A, typename B>   ostream &operator<<(ostream &cout, pair<A, B> const &p) {     return cout <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v) {     cout <<  [ ;     for (int i = 0; i < v.size(); i++) {       if (i) cout <<  ,  ;       cout << v[i];     }     return cout <<  ] ;   }   template <typename A, typename B>   istream &operator>>(istream &cin, pair<A, B> &p) {     cin >> p.first;     return cin >> p.second;   }   mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());   void usaco(string filename) {     freopen((filename +  .in ).c_str(),  r , stdin);     freopen((filename +  .out ).c_str(),  w , stdout);   }   const long double pi = 3.14159265358979323846;   long long n, m, q, k, l, r, x, y, z;   const long long template_array_size = 1e6 + 16318;   long long a[template_array_size];   long long b[template_array_size];   long long c[template_array_size];   string second, t;   long long ans = 0;   string op[5005];   string val[5005];   pair<long long, long long> dep[5005];   map<string, long long> rev;   long long dp[5005];   long long check(long long x, long long p, long long t) {     if (x == -1) return t;     if (dp[x] != -1) return dp[x];     if (op[x] ==  = )       return dp[x] = val[x][p] -  0 ;     else if (op[x] ==  OR )       return dp[x] = check(dep[x].first, p, t) | check(dep[x].second, p, t);     else if (op[x] ==  AND )       return dp[x] = check(dep[x].first, p, t) & check(dep[x].second, p, t);     else       return dp[x] = check(dep[x].first, p, t) ^ check(dep[x].second, p, t);   }   void solve(int tc = 0) {     cin >> n >> m;     string v;     getline(cin, v);     rev[ ? ] = -1;     for (long long i = 0; i < n; i++) {       vector<string> x;       getline(cin, v);       istringstream is(v);       while (is >> v) {         x.push_back(v);       }       rev[x[0]] = i;       if (x.size() == 3) {         op[i] =  = ;         val[i] = x[2];       } else {         op[i] = x[3];         dep[i] = {rev[x[2]], rev[x[4]]};       }     }     string rmin =   , rmax =   ;     for (long long i = 0; i < m; i++) {       long long c0 = 0, c1 = 0;       memset(dp, -1, sizeof(dp));       for (long long j = 0; j < n; j++) {         c0 += check(j, i, 0);       }       memset(dp, -1, sizeof(dp));       for (long long j = 0; j < n; j++) {         c1 += check(j, i, 1);       }       if (c0 == c1) {         rmin +=  0 ;         rmax +=  0 ;       } else if (c0 < c1) {         rmin +=  0 ;         rmax +=  1 ;       } else {         rmin +=  1 ;         rmax +=  0 ;       }     }     cout << rmin <<   n  << rmax <<   n ;   }   int main() {     { ios_base::sync_with_stdio(false); }     { cin.tie(NULL); }     cout << setprecision(15) << fixed;     int tc = 1;     for (int t = 0; t < tc; t++) solve(t);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int i, pos, n, cnt, sz, dummy;     vector<int> ans;     long long k, tot = 0, examine[100005], temp;     scanf( %d %I64d , &n, &k);     for (i = 0; i < n; ++i) {       scanf( %I64d , &examine[i]);       tot += examine[i];     }     if (tot < k)       printf( -1 n );     else if (tot == k)       printf(  n );     else {       dummy = n;       while (k > (long long)n) {         temp = k / (long long)n;         k = k % (long long)n;         cnt = 0;         for (i = 0; i < dummy; ++i) {           if (examine[i] != 0) {             if ((long long)examine[i] <= temp) {               k += (long long)(temp - (long long)examine[i]);               examine[i] = 0;               ++cnt;             } else               examine[i] -= temp;           }         }         n -= cnt;       }       pos = 0;       if (k != 0) {         while (k != 0) {           if (examine[pos] > 0) {             --examine[pos];             --k;           }           ++pos;         }       }       for (i = pos; i < dummy; ++i)         if (examine[i] > 0) ans.push_back(i + 1);       for (i = 0; i < pos; ++i)         if (examine[i] > 0) ans.push_back(i + 1);       sz = ans.size();       for (i = 0; i < (sz - 1); ++i) printf( %d  , ans[i]);       printf( %d n , ans[sz - 1]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     string s;     cin >> s;     long long l = s.length();     bool visit[256];     memset(visit, false, sizeof(visit));     int dis = 0, i;     for (i = 0; i < l; i++) {       if (!visit[s[i]]) {         visit[s[i]] = true;         dis++;       }     }     if (dis == 1) {       cout << ((l * (l + 1)) / 2) % 998244353;       return 0;     }     int s_cnt = 1, e_cnt = 1;     i = 1;     int j = l - 2;     for (; i < l; i++) {       if (s[i] == s[0])         s_cnt++;       else         break;     }     for (; j >= 0; j--) {       if (s[j] == s[n - 1])         e_cnt++;       else         break;     }     if (s[0] != s[l - 1]) {       cout << (e_cnt + s_cnt + 1) % 998244353;     } else {       cout << (long long)((long long)(s_cnt + 1) % 998244353 *                           (long long)(e_cnt + 1) % 998244353) %                   998244353;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, a[1000009], m, ans, mx, b[1000009];   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> b[i];       if (n % 2 == 0) {         if (i <= n / 2)           a[b[i] + (n / 2 - i)]++;         else           a[b[i] + (i - (n / 2) - 1)]++;       } else {         if (i <= n / 2 + 1)           a[b[i] + (n / 2 + 1 - i)]++;         else           a[b[i] + (i - (n / 2 + 1))]++;       }     }     for (int i = 1; i < 1000009; i++) {       if (n % 2 == 0 && i - n / 2 >= 0)         mx = max(mx, a[i]);       else if (n % 2 == 1 && i - (n / 2 + 1) >= 0)         mx = max(mx, a[i]);     }     cout << n - mx;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const int Nmax = 100005;   int count_one(vector<int> s) {     int ct = 0;     int i = 0;     while (i < s.size()) {       if (s[i] == 1) ++ct;       ++i;     }     return ct;   }   int count_zero(string s, int index) {     int i = 0;     int ct = 0;     while (s[i] !=  1  && i < s.length()) {       ++ct;       ++i;     }   }   int gcd(int a, int b) {     if (a == 0) {       return b;     }     return gcd(b % a, a);   }   bool isPrime(int n) {     for (int i = 2; i <= sqrt(n); i++)       if (n % i == 0) return 0;     return 1;   }   void binaryRepresentation(int x) {     for (int i = 31; i >= 0; i--) {       if (x & (1 << i))         cout <<  1 ;       else         cout <<  0 ;     }   }   void DFS(vector<vector<int> > &adj, int u, vector<bool> &visit,            vector<int> &dist, vector<int> &freq) {     visit[u] = true;     for (int i = 0; i < adj[u].size(); i++) {       int x = adj[u][i];       if (!visit[x]) {         dist[x] = dist[u] + 1;         freq[u]++;         DFS(adj, adj[u][i], visit, dist, freq);       }     }   }   bool check(string a, string b) {     vector<int> first(26, 0);     vector<int> second(26, 0);     for (int i = 0; i < a.length(); i++) {       first[a[i] -  a ]++;       second[b[i] -  a ]++;     }     if (first != second)       return false;     else       return true;   }   long long combination(long long n, long long r) {     if (r == n || r == 0) {       return 1;     }     return combination(n - 1, r - 1) + combination(n - 1, r);   }   bool cmp(const pair<int, int> &a, const pair<int, int> &b) {     if (a.second == b.second) return a.first < b.first;     return a.second > b.second;   }   int main() {     ios_base::sync_with_stdio(false);     long long n, k;     cin >> n >> k;     vector<int> ar(n);     map<long long, long long> right;     map<long long, long long> left;     for (long long i = 0; i < n; i++) {       cin >> ar[i];     }     long long ans = 0;     for (long long i = n - 1; i >= 0; i--) {       ans += right[ar[i] * k];       right[ar[i]] += left[ar[i] * k];       left[ar[i]]++;     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const unsigned long long base = 133;   unsigned long long hash1[200005][26], hash2[200005][26], Pow[200005];   int n, m;   char str1[200005], str2[200005];   int main() {     scanf( %d%d , &n, &m);     scanf( %s%s , str1 + 1, str2 + 1);     Pow[0] = 1;     for (int i = 1; i <= n; i++) {       Pow[i] = Pow[i - 1] * base;     }     for (int i = 0; i < 26; i++) {       for (int j = 1; j <= n; j++) {         hash1[j][i] = hash1[j - 1][i] * base + (str1[j] -  a  == i);       }     }     for (int i = 0; i < 26; i++) {       for (int j = 1; j <= m; j++) {         hash2[j][i] = hash2[j - 1][i] * base + (str2[j] -  a  == i);       }     }     vector<int> vec;     for (int i = 1; i <= n - m + 1; i++) {       int cnt = 0;       for (int x = 0; x < 26; x++) {         if (cnt != x) break;         for (int y = 0; y < 26; y++) {           if (hash2[m][x] == hash1[i + m - 1][y] - hash1[i - 1][y] * Pow[m]) {             if (hash2[m][y] == hash1[i + m - 1][x] - hash1[i - 1][x] * Pow[m]) {               cnt++;               break;             }           }         }       }       if (cnt == 26) vec.push_back(i);     }     int sz = vec.size();     printf( %d n , sz);     for (int i = 0; i < sz; i++) {       printf( %d  , vec[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, i, j, y, s, ans;   char a[2222][2222];   int l[2222][2222], r[2222][2222], u[2222][2222], d[2222][2222];   pair<pair<int, int>, int> st[2444444];   bool f[2222][2222];   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m;     for (i = 1; i <= n; i++)       for (j = 1; j <= m; j++) cin >> a[i][j];     for (i = 1; i <= n; i++)       for (j = 1; j <= m; j++)         if (a[i][j] ==  * )           l[i][j] = l[i][j - 1] + 1;         else           l[i][j] = 0;     for (i = 1; i <= n; i++)       for (j = m; j >= 1; j--)         if (a[i][j] ==  * )           r[i][j] = r[i][j + 1] + 1;         else           r[i][j] = 0;     for (j = 1; j <= m; j++)       for (i = 1; i <= n; i++)         if (a[i][j] ==  * )           u[i][j] = u[i - 1][j] + 1;         else           u[i][j] = 0;     for (j = m; j >= 1; j--)       for (i = n; i >= 1; i--)         if (a[i][j] ==  * )           d[i][j] = d[i + 1][j] + 1;         else           d[i][j] = 0;     for (i = 1; i <= n; i++)       for (j = 1; j <= m; j++)         if (a[i][j] ==  * ) {           s = min(min(r[i][j + 1], l[i][j - 1]), min(u[i - 1][j], d[i + 1][j]));           if (s > 0) {             st[++ans] = {{i, j}, s};             for (y = j - s; y <= j + s; y++) f[i][y] = 1;             for (y = i - s; y <= i + s; y++) f[y][j] = 1;           }         }     for (i = 1; i <= n; i++)       for (j = 1; j <= m; j++)         if (a[i][j] ==  *  && f[i][j] == 0) {           cout << -1 <<   n ;           return 0;         }     cout << ans <<   n ;     for (i = 1; i <= ans; i++)       cout << st[i].first.first <<     << st[i].first.second <<                << st[i].second <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 5, INF = -1e9;   long long down[N], mx[N], up[N], dp[N], sum[N];   unordered_map<long long, vector<long long>> mp[N];   long long n, m, pre[N], suf[N], ans[N];   vector<pair<long long, long long>> adj[N];   bool mark[N];   void dfs_down(long long u, long long par) {     down[u] = up[u] = mark[u] ? 0 : INF;     for (auto x : adj[u]) {       long long v = x.first, w = x.second;       if (v == par) continue;       dfs_down(v, u);       mp[u][down[v] + w].push_back(v);       down[u] = max(down[u], down[v] + w);     }     pre[0] = suf[adj[u].size() - 1] = INF;     for (long long i = 0; i < adj[u].size(); i++) {       long long v = adj[u][i].first, w = adj[u][i].second;       pre[i + 1] = v ^ par ? max(pre[i], down[v] + w) : pre[i];     }     for (long long i = adj[u].size() - 1; i; i--) {       long long v = adj[u][i].first, w = adj[u][i].second;       suf[i - 1] = v ^ par ? max(suf[i], down[v] + w) : suf[i];     }     for (long long i = 0; i < adj[u].size(); i++)       if (adj[u][i].first ^ par) mx[adj[u][i].first] = max(suf[i], pre[i]);   }   void dfs_up(long long u, long long par) {     for (auto x : adj[u]) {       long long v = x.first, w = x.second;       if (v == par) continue;       up[v] = max(up[v], max(up[u], mx[v]) + w);       dfs_up(v, u);     }   }   void dfs1(long long u, long long par) {     if (mark[u] && up[u] > down[u]) dp[u]++;     for (auto x : adj[u]) {       long long v = x.first, w = x.second;       if (v == par) continue;       dfs1(v, u);       ans[u] += dp[v];       if (up[u] > mx[v]) dp[u] += dp[v];       if (up[u] < mx[v]) {         long long sz = mp[u][mx[v]].size();         if (down[v] + w == mx[v]) sz--;         if (sz ^ 1) continue;         for (long long s : mp[u][mx[v]])           if (s ^ v) sum[s] += dp[v];       }     }   }   void dfs2(long long u, long long par) {     if (mark[u] && down[u] > up[u]) sum[u]++;     if (mp[u][down[u]].size() == 1) sum[mp[u][down[u]][0]] += sum[u];     for (auto x : adj[u])       if (x.first ^ par) dfs2(x.first, u);     ans[u] += sum[u];   }   inline void read_input() {     cin >> n >> m;     for (long long i = 0; i < m; i++) {       long long v;       cin >> v;       mark[--v] = true;     }     for (long long i = 1; i < n; i++) {       long long u, v, w;       cin >> u >> v >> w;       adj[--u].push_back({--v, w});       adj[v].push_back({u, w});     }   }   inline void solve() {     dfs_down(0, 0);     dfs_up(0, 0);     dfs1(0, 0);     dfs2(0, 0);   }   inline void write_output() {     long long mx = 0, cnt = 0;     for (long long u = 0; u < n; u++) {       if (mark[u]) continue;       if (mx < ans[u]) {         mx = ans[u];         cnt = 0;       }       if (mx == ans[u]) cnt++;     }     cout << mx <<     << cnt << endl;   }   int32_t main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     read_input(), solve(), write_output();     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( -O2 )   using namespace std;   void err(istream_iterator<string> it) { cerr << endl; }   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a <<   t ;     err(++it, args...);   }   template <typename T1, typename T2>   ostream& operator<<(ostream& c, pair<T1, T2>& v) {     c <<  (  << v.first <<  ,  << v.second <<  ) ;     return c;   }   template <template <class...> class TT, class... T>   ostream& operator<<(ostream& out, TT<T...>& c) {     out <<  {  ;     for (auto& x : c) out << x <<    ;     out <<  } ;     return out;   }   const int LIM = 1e5 + 5, MOD = 1e9 + 7;   int t, n, m, k, x, y;   struct Gaussbase2 {     int numofbits = 31;     int rk = 0;     vector<int> Base;     Gaussbase2() { Base.assign(numofbits, 0); }     Gaussbase2& operator=(Gaussbase2& g) {       for (int i = 0; i < numofbits; i++) Base[i] = g.Base[i];       rk = g.rk;     }     bool canbemade(int x) {       for (int i = numofbits - 1; i >= 0; i--) x = min(x, x ^ Base[i]);       return x == 0;     }     void Add(int x) {       for (int i = numofbits; i >= 0; i--) {         if ((x >> i) & 1) {           if (!Base[i]) {             Base[i] = x;             rk++;             return;           } else             x ^= Base[i];         }       }     }   } g;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     int tot = 0;     for (int i = 0; i < n; i++) {       cin >> x;       tot ^= x;       g.Add(x);     }     if (tot == 0)       cout << -1 <<   n ;     else       cout << g.rk <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[100010];   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       for (int i = 0; i < n; i++) cin >> a[i];       sort(a, a + n);       int i = (n - 1) / 2, j = i + 1;       while (i >= 0 || j <= n - 1) {         if (i >= 0) cout << a[i--] <<    ;         if (j <= n - 1) cout << a[j++] <<    ;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     int aray[100007];     while (scanf( %d %d , &n, &m) != EOF) {       for (int i = 1; i < n; i++) {         scanf( %d , &aray[i]);       }       int sum = 0, i = 1;       bool flag = false;       while (true) {         sum = (i + aray[i]);         i = sum;         if (sum == m) {           flag = true;           break;         }         if (sum > m) {           break;         }       }       (flag) ? printf( YES n ) : printf( NO n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 333;   const long long linf = 1e18 + inf;   const int LEN = 6e5 + 5;   int n, m, q, len;   char str[LEN], s[LEN];   int a[LEN], st[LEN], nd[LEN], suf[LEN], ord[LEN], lcp[LEN], w[LEN], root[LEN],       L[LEN], R[LEN];   pair<pair<int, int>, int> C[LEN];   long long cnt[LEN];   void add(int x, char str[]) {     int m = strlen(str);     s[++len] =  # ;     st[x] = len + 1;     for (int i = 0; i < m; i++) s[++len] = str[i];     nd[x] = len;   }   int f(int x) {     if (x == root[x]) return x;     return root[x] = f(root[x]);   }   void merge(int x, int y) {     x = f(x);     y = f(y);     if (x == y) return;     root[y] = x;     cnt[x] += cnt[y];     L[x] = min(L[x], L[y]);     R[x] = max(R[x], R[y]);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %s , str);       add(i, str);     }     for (int i = 1; i <= n; i++) scanf( %d , a + i);     for (int i = 1; i <= len; i++) suf[i] = s[i];     for (int it = 1; it < len; it *= 2) {       for (int j = 1; j <= len; j++) {         C[j] = make_pair(pair<int, int>(suf[j], suf[min(j + it, len + 1)]), j);       }       sort(C + 1, C + len + 1);       for (int j = 1; j <= len; j++) {         suf[C[j].second] = suf[C[j - 1].second] + (C[j].first != C[j - 1].first);       }     }     for (int i = 1; i <= len; i++) ord[suf[i]] = i;     for (int i = 1; i <= len; i++) {     }     int j = 0;     for (int i = 1; i <= len; i++) {       if (suf[i] == 1) continue;       while (i + j <= len and ord[suf[i] - 1] + j <= len and              s[i + j] == s[ord[suf[i] - 1] + j] and s[i + j] !=  # )         j++;       lcp[suf[i]] = j;       if (j) j--;     }     for (int i = 1; i <= len; i++) {       assert(s[ord[i] + lcp[i]] != s[ord[i - 1] + lcp[i]] or              s[ord[i] + lcp[i]] ==  # );     }     for (int i = 1; i <= len; i++) {     }     memset(w, -1, sizeof(w));     for (int i = 1; i <= n; i++) {       for (int j = st[i]; j <= nd[i]; j++) {         w[suf[j]] = i;       }     }     for (int i = 1; i <= len; i++) {       if (w[i] != -1) cnt[i] = a[w[i]];       L[i] = R[i] = root[i] = i;     }     vector<pair<int, int> > vs;     for (int i = 1; i <= len; i++) {       vs.push_back(pair<int, int>(lcp[i], i));     }     sort(vs.begin(), vs.end());     long long ans = 0;     for (int i = 1; i <= n; i++) {       int x = suf[st[i]];       if (nd[i] - st[i] + 1 > lcp[x] and nd[i] - st[i] + 1 > lcp[x + 1]) {         ans = max(ans, (long long)a[i] * (nd[i] - st[i] + 1));       }     }     for (int it = len - 1; it >= 0; it--) {       int l = vs[it].first, x = vs[it].second;       merge(x, x - 1);       x = f(x);       if (lcp[L[x]] < l and lcp[R[x] + 1] < l) ans = max(ans, cnt[f(x)] * l);     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, s, ll, v[100011];   int dp[100011];   const int oo = 1 << 28;   int seg[400001];   int at, l, r;   void add(int n, int s, int e) {     if (at < s || at > e || s > e) return;     if (s == e) {       seg[n] = dp[s];       return;     }     add(n * 2, s, (s + e) / 2);     add(n * 2 + 1, (s + e) / 2 + 1, e);     seg[n] = min(seg[n * 2], seg[n * 2 + 1]);   }   int get(int n, int s, int e) {     if (l > e || r < s || l > r || s > e) return oo;     if (s >= l && e <= r) return seg[n];     return min(get(n * 2, s, (s + e) / 2), get(n * 2 + 1, (s + e) / 2 + 1, e));   }   int main() {     scanf( %d%d%d , &n, &s, &ll);     for (int i = 1; i <= n; ++i) scanf( %d , v + i);     set<pair<int, int> > q;     int E = n;     for (int i = n; i > 0; --i) {       dp[i] = oo;       q.insert(make_pair(v[i], i));       while ((--q.end())->first - q.begin()->first > s) {         q.erase(make_pair(v[E], E));         --E;       }       l = i + ll;       r = E + 1;       if (r >= l) {         if (r == n + 1)           dp[i] = 1;         else           dp[i] = min(dp[i], 1 + get(1, 1, n));       }       at = i;       add(1, 1, n);     }     if (dp[1] > 500000) dp[1] = -1;     printf( %d n , dp[1]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e3 + 10;   const int mod = 1e9 + 7;   int read() {     int x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) x = (x << 1) + (x << 3) + c -  0 , c = getchar();     return x * f;   }   int inc(int x, int y) { return (x + y >= mod) ? (x + y - mod) : (x + y); }   int del(int x, int y) { return (x - y < 0) ? (x - y + mod) : (x - y); }   int qpow(int a, int x, int mo) {     int res = 1;     while (x) {       if (x & 1) res = 1ll * res * a % mo;       a = 1ll * a * a % mod;       x >>= 1;     }     return res;   }   vector<pair<int, int> > g[N];   long long dp[N][N];   map<int, long long> mp;   int q[N];   int main() {     int n = read(), m = read(), Q = read();     for (int i = 0; i < m; i++) {       int u = read(), v = read(), w = read();       g[u].push_back(make_pair(v, w));       g[v].push_back(make_pair(u, w));     }     for (int i = 0; i <= m; i++)       for (int j = 1; j <= n; j++) dp[i][j] = -2e18;     dp[0][1] = 0;     int ans = 0;     for (int i = 1; i <= m; i++) {       long long res = 0;       for (int j = 1; j <= n; j++)         for (int t = 0; t < g[j].size(); t++) {           pair<int, int> now = g[j][t];           int u = now.first, v = j;           dp[i][u] = max(dp[i - 1][v] + now.second, dp[i][u]);           if (i == m)             mp[now.second] = max(dp[i - 1][v] + now.second, mp[now.second]);           res = max(res, dp[i][u]);         }       ans = inc(ans, res % mod);     }     vector<pair<int, int> > p;     for (auto it : mp) {       p.push_back(it);     }     int head = 1, tail = 0;     int Inv2 = qpow(2, mod - 2, mod);     for (int i = 0; i < p.size(); i++) {       while (head <= tail && p[q[tail]].second <= p[i].second) tail--;       q[++tail] = i;     }     Q -= m;     int cnt = 0;     int pre = 1;     for (int i = 2; i <= tail; i++) {       pair<int, int> now = p[q[pre]];       pair<int, int> nxt = p[q[i]];       bool ok = 1;       long long k = (now.second - nxt.second) / (nxt.first - now.first);       for (int j = i + 1; j <= tail; j++) {         pair<int, int> o = p[q[j]];         if ((now.second - o.second) / (o.first - now.first) <= k) {           ok = 0;           break;         }       }       k -= cnt;       if (k <= 0 || ok == 0) continue;       if (Q <= k) k = Q;       ans = inc(ans, inc(1ll * k * (1 + k) % mod * Inv2 % mod * now.first % mod,                          1ll * (now.second + 1ll * cnt * now.first % mod) % mod *                              k % mod));       Q -= k;       cnt += k;       pre = i;       if (Q == 0) break;     }     pair<int, int> now = p[q[tail]];     if (Q > 0)       ans = inc(ans, inc(1ll * Q * (1 + Q) % mod * Inv2 % mod * now.first % mod,                          1ll * (now.second + 1ll * cnt * now.first % mod) % mod *                              Q % mod));     printf( %d n , ans);   }