malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, i, j, k, m, t, x, y, sum;     cin >> t;     while (t > 0) {       t = t - 1;       cin >> n >> k;       long long a[n], c[k];       for (i = 0; i < n; i++) {         cin >> a[i];       }       for (i = 0; i < k; i++) {         cin >> c[i];       }       sort(a, a + n);       sort(c, c + k);       sum = 0;       m = -1;       j = n - 1;       for (i = 0; i < k; i++) {         if (c[i] == 1) {           sum = sum + 2 * a[j];           m = i;         } else           sum = sum + a[j];         c[i] = c[i] - 1;         j = j - 1;       }       j = m + 1;       i = n - k - 1;       for (i = i; i >= 0; i = i - 1) {         while (c[j] > 1) {           c[j] = c[j] - 1;           i = i - 1;         }         sum = sum + a[i];         j = j + 1;       }       cout << sum <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double eps = 1e-9;   const int INF = 1e9 + 7;   const int MAXN = int(2e5 + 7);   int n, a[3], sum, g[10][10], d[10], ans = INF * 2;   string second[7] = { Anka ,    Chapay ,  Cleo ,        Troll ,                        Dracul ,  Snowy ,   Hexadecimal };   int get(string a) {     for (int i = 0; i < 7; i++)       if (second[i] == a) return i;   }   void check() {     vector<int> v[3];     for (int i = 0; i < 7; i++) v[d[i]].push_back(i);     int maxi = 0, mini = INF * 2;     for (int i = 0; i < 3; i++) {       if (v[i].size() == 0) return;       maxi = max(maxi, a[i] / (int)v[i].size());       mini = min(mini, a[i] / (int)v[i].size());     }     int res = 0;     for (int i = 0; i < 3; i++)       for (int j = 0; j < v[i].size(); j++)         for (int k = 0; k < v[i].size(); k++) res += g[v[i][j]][v[i][k]];     if (ans > maxi - mini)       ans = maxi - mini, sum = res;     else if (ans == maxi - mini && res > sum)       sum = res;   }   void go(int x) {     if (x == 7) {       check();       return;     }     for (int i = 0; i < 3; i++) {       d[x] = i;       go(x + 1);     }   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       string b[3];       cin >> b[0] >> b[1] >> b[2];       g[get(b[0])][get(b[2])] = 1;     }     cin >> a[0] >> a[1] >> a[2];     go(0);     cout << ans <<     << sum << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const unsigned long long inf = 1e18;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<unsigned long long> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     unsigned long long ans = 0;     for (int i = 0; i < n; i++) {       for (int j = i; j < n; j++) {         for (int k = j; k < n; k++) {           ans = max(ans, (a[i] | a[j] | a[k]));         }       }     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   const int MOD = 1e9 + 7;   const int N = 2e6 + 10;   int n, m, x, a[N], fi[N];   long long pos[N];   void prepare() {     scanf( %d%d , &n, &m);     for (auto i = 0; i < n; i++) scanf( %d , a + i), a[n + i] = a[i];     for (auto i = 1; i <= m; i++) scanf( %d , &x), fi[x] = i;   }   int solve() {     int ans = 0;     for (int r = 0, l = 0; r < 2 * n; r++) {       if (!fi[a[r]]) {         l = r + 1;         continue;       }       if (l < r)         pos[r] = fi[a[r]] + (pos[r - 1] - fi[a[r]] + m - 1) / m * m;       else         pos[r] = fi[a[r]];       while (r - l >= n || pos[r] - pos[l] >= m) l++;       ans = max(ans, r - l + 1);     }     return ans;   }   int main() {     prepare();     cout << solve();   }
#include <bits/stdc++.h>   using namespace std;   int n, m, d;   int a[(1 << 20) + 5];   int sum[(1 << 20) + 5];   int b[22][100005];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> d;     for (int i = 0; i < m; i++) {       int x, y;       cin >> x;       for (int j = 1; j <= x; j++) {         cin >> y;         b[i][max(1, y - d + 1)]++;         b[i][y + 1]--;       }     }     for (int j = 0; j < m; j++) {       int c = 0;       for (int i = 1; i <= n; i++) {         c += b[j][i];         if (c) a[i] += (1 << j);       }     }     int ans = 22;     for (int i = 1; i <= n - d + 1; i++) sum[a[i]]++;     for (int j = 0; j < m; j++)       for (int i = 0; i < (1 << m); i++)         if ((i & (1 << j))) sum[i] += sum[(i ^ (1 << j))];     for (int i = 0; i < (1 << m); i++)       if (sum[i] == 0) ans = min(ans, m - __builtin_popcount(i));     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void read(T &x) {     x = 0;     long long f = 0;     char ch = getchar();     while (!isdigit(ch)) f = ch ==  - , ch = getchar();     while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();     x = f ? -x : x;   }   const long long N = 15, M = 105;   long long mp[N][N], f[N][1 << N], n, m, q;   struct lca {     long long x, y, z;   } L[M];   struct xy {     long long x, y;   } B[M];   bool in(long long x, long long y) { return x & (1 << y); }   long long dfs(long long u, long long mask) {     if (~f[u][mask]) return f[u][mask];     long long &now = f[u][mask];     mask ^= (1 << u), now = 0;     long long pos;     for (long long i = 0; i < n; i++)       if (mask & (1 << i)) {         pos = i;         break;       }     for (long long sub = mask; sub; sub = (sub - 1) & mask) {       if (!(sub & (1 << pos))) continue;       long long flag = 0, v = -1;       for (long long i = 1; i <= q; i++)         if (in(sub, L[i].z)) {           if (!in(sub, L[i].x) || !in(sub, L[i].y)) {             flag = 1;             break;           }         } else if (u == L[i].z) {           if (in(sub, L[i].x) && in(sub, L[i].y)) {             flag = 1;             break;           }         }       if (flag) continue;       for (long long i = 1; i <= m; i++)         if (B[i].x != u && B[i].y != u && (in(sub, B[i].x) ^ in(sub, B[i].y))) {           flag = 1;           break;         }       if (flag) continue;       for (long long i = 0; i < n; i++)         if (mp[u][i] && in(sub, i))           if (!~v)             v = i;           else {             flag = 1;             break;           }       if (flag) continue;       if (~v)         now = (now + dfs(u, mask ^ (1 << u) ^ sub) * dfs(v, sub));       else         for (long long i = 0; i < n; i++)           if (in(sub, i))             now = (now + dfs(u, mask ^ (1 << u) ^ sub) * dfs(i, sub));     }     return now;   }   signed main() {     read(n), read(m), read(q);     for (long long i = 1, x, y; i <= m; i++)       read(B[i].x), read(B[i].y), --B[i].x, --B[i].y,           mp[B[i].x][B[i].y] = mp[B[i].y][B[i].x] = 1;     for (long long x, y, z, i = 1; i <= q; i++)       read(L[i].x), read(L[i].y), read(L[i].z), L[i].x--, L[i].y--, L[i].z--;     memset(f, -1, sizeof(f));     for (long long i = 0; i < n; i++) f[i][1 << i] = 1;     printf( %lld , dfs(0, (1 << n) - 1));   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100;   long long n, k;   int a[15];   long long rc[15], p[15], ans;   void solve() {     scanf( %d%d , &n, &k);     k++;     scanf( %d , &a[1]);     rc[1] = 1;     long long now = 1, tmp;     int ss = 0;     for (int i = 2; i <= n; i++) {       scanf( %d , &a[i]);       tmp = 1;       while (ss < a[i]) now *= 10, tmp *= 10, ss++;       p[i - 1] = tmp - 1;       rc[i] = now;     }     ans = 0;     for (int i = 1; i < n; i++) {       if (k >= p[i])         ans += p[i] * rc[i], k -= p[i];       else {         ans += rc[i] * k;         k = 0;       }     }     if (k) ans += rc[n] * k;     printf( %lld n , ans);   }   int main() {     int t;     scanf( %d , &t);     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mxN = 1e5;   int n;   string ans;   int kmp(string s) {     vector<int> pf(s.size());     for (int j = 1; j < s.size(); ++j) {       int k = pf[j - 1];       while (s[k] != s[j]) {         if (!k) {           k = -1;           break;         }         k = pf[k - 1];       }       pf[j] = k + 1;     }     return pf.back();   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < n; ++i) {       if (!i) {         cin >> ans;         continue;       }       string s;       cin >> s;       string t = s;       t +=  # ;       int a = min((int)ans.size(), (int)s.size());       t.insert(t.end(), ans.end() - a, ans.end());       int x = kmp(t);       while (x--) ans.pop_back();       ans.insert(ans.end(), s.begin(), s.end());     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   const long long int MOD = 1e18 + 7;   long long int modPow(long long int a, long long int b) {     long long int res = 1;     a %= mod;     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   long long int MODPow(long long int a, long long int b) {     long long int res = 1;     a %= MOD;     for (; b; b >>= 1) {       if (b & 1) res = res * a % MOD;       a = a * a % MOD;     }     return res;   }   long long int nCr(long long int n, long long int k) {     long long int res = 1;     if (k > n - k) k = n - k;     for (long long int i = 0; i < k; ++i) {       res = (res * (n - i)) % mod;       res = (res * modPow(i + 1, mod - 2)) % mod;     }     return res;   }   void Sieve(long long int n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (long long int p = 2; p * p <= n; p++)       if (prime[p] == true)         for (long long int i = p * 2; i <= n; i += p) prime[i] = false;   }   int main() {     int n, m, k;     cin >> n >> m >> k;     int a[n], b[n];     for (int i = 0; i < n; i++) cin >> a[i], b[i] = a[i];     sort(b, b + n, greater<int>());     map<int, int> mp;     for (int i = 0; i < m * k; i++) mp[b[i]]++;     long long int total = 0, sum = 0;     for (int i = 0; i < m * k; i++) total += b[i];     cout << total <<   n ;     int c = 0;     std::vector<int> v;     for (int i = 0; i < n; i++) {       if (mp[a[i]] > 0) c++, mp[a[i]]--;       if (c == m) v.push_back(i + 1), c = 0;     }     for (int i = 0; i < k - 1; i++) cout << v[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   template <typename tp>   inline void read(tp& x) {     x = 0;     char tmp;     bool key = 0;     for (tmp = getchar(); !isdigit(tmp); tmp = getchar()) key = (tmp ==  - );     for (; isdigit(tmp); tmp = getchar()) x = (x << 3) + (x << 1) + (tmp ^  0 );     if (key) x = -x;   }   template <typename tp>   inline void ckmn(tp& x, tp y) {     x = x < y ? x : y;   }   template <typename tp>   inline void ckmx(tp& x, tp y) {     x = x < y ? y : x;   }   const int N = 100010;   struct edge {     int la, b;   } con[N << 1];   int tot, first[N];   void add_edge(int from, int to) {     con[++tot] = (edge){first[from], to};     first[from] = tot;   }   int n, m;   vector<int> ans;   void clear() {     tot = 0;     for (int i = (1); i <= (n); ++i) first[i] = 0;   }   int key = 0;   int dep[N], vis[N], inp[N], num[N], mnv[N], dp[N];   void dfs(int pos) {     vis[pos] = 1;     inp[pos] = 1;     mnv[pos] = pos;     for (int i = first[pos]; i; i = con[i].la) {       if (vis[con[i].b]) {         if (inp[con[i].b]) {           if (dep[con[i].b] < dep[mnv[pos]]) mnv[pos] = con[i].b;           ++num[pos];           --num[con[i].b];         } else           key = 1;       } else {         dep[con[i].b] = dep[pos] + 1;         dfs(con[i].b);         num[pos] += num[con[i].b];         if (dep[mnv[con[i].b]] < dep[mnv[pos]]) mnv[pos] = mnv[con[i].b];       }     }     inp[pos] = 0;   }   void fsd(int pos) {     vis[pos] = 1;     if (dep[pos] > 0) {       if (num[pos] == 1) {         dp[pos] = dp[mnv[pos]];       }     }     for (int i = first[pos]; i; i = con[i].la)       if (!vis[con[i].b]) fsd(con[i].b);   }   bool doit(int rt) {     key = 0;     for (int i = (1); i <= (n); ++i) vis[i] = 0;     for (int i = (1); i <= (n); ++i) num[i] = 0;     dfs(rt);     if (key) return false;     for (int i = (1); i <= (n); ++i) dp[i] = 0;     dp[rt] = 1;     for (int i = (1); i <= (n); ++i) vis[i] = 0;     fsd(rt);     for (int i = (1); i <= (n); ++i)       if (dp[i]) ans.push_back(i);     return true;   }   void solve() {     int x, y;     int sed = 0;     read(n), read(m);     for (int i = (1); i <= (m); ++i) {       read(x), read(y);       add_edge(x, y);       sed = (sed + (long long)233 * x) % 998244353;     }     srand(sed);     ans.clear();     for (int _ = (1); _ <= (100); ++_) {       int rt = ((rand() << 16ll) | rand()) % n + 1;       if (doit(rt)) break;     }     if ((int)ans.size() * 5 < n) {       puts( -1 );     } else {       for (int i = (0); i <= ((int)ans.size() - 1); ++i) printf( %d  , ans[i]);       puts(  );     }     clear();   }   int main() {     int T;     read(T);     while (T--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     int a, b, c, d, i, j, n, flag = 0;     cin >> a >> b >> c >> d;     n = max(max(a, b), max(c, d));     flag = 1;     for (i = 0; i <= n; i++) {       for (j = 0; j <= n; j++) {         if (b + i * a == d + j * c) {           cout << b + i * a << endl;           flag = 0;           break;         }       }       if (!flag) break;     }     if (flag) cout << -1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     cin >> a >> b;     int m = 0, n = 0, c = 0;     m = a.length();     n = b.length();     int x = n - 1;     if (m != n) {       cout <<  NO ;       return 0;     }     for (int i = 0; i < m; i++) {       if (a[i] == b[x]) {         c++;       }       x--;     }     if (c == m) {       cout <<  YES ;     } else {       cout <<  NO ;     }   }
#include <bits/stdc++.h>   const long long int mod = 1e9 + 7;   using namespace std;   bool prime(long long int n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (long long int i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return false;     return true;   }   bool isPowerOfTwo(long long int n) {     if (n == 0) return false;     return (ceil(log2(n)) == floor(log2(n)));   }   void SieveOfEratosthenes(long long int n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (long long int p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (long long int i = p * p; i <= n; i += p) prime[i] = false;       }     }     for (long long int p = 2; p <= n; p++)       if (prime[p]) cout << p <<    ;   }   long long int modularinv(long long int x, long long int n) {     if (n == 0)       return 1;     else if (n % 2 == 0)       return modularinv((x * x) % mod, n / 2);     else       return (x * modularinv((x * x) % mod, (n - 1) / 2)) % mod;   }   class node {    public:     long long int first;     bool operator<(const node& rhs) const { return first > rhs.first; }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long int n, i;     cin >> n;     vector<long long int> v(n);     long long int sum = 0;     for (i = 0; i < n; i++) {       cin >> v[i];       sum += v[i];     }     long long int count = 0;     long long int d;     if (sum % 2 != 0)       sum = sum / 2 + 1;     else       sum = sum / 2;     for (i = 0; i < n; i++) {       count += v[i];       if (count >= sum) {         d = i + 1;         break;       }     }     cout << d;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     cin.get();     vector<string> v(n);     for (int i = 0; i < n; i++) getline(cin, v[i]);     vector<int> p(k);     for (int i = 0; i < k; i++) p[i] = i;     int minDiff = INT_MAX;     do {       int minNum, maxNum;       minNum = INT_MAX;       maxNum = INT_MIN;       for (int i = 0; i < n; i++) {         int num = 0;         for (int j = 0; j < k; j++) num = num * 10 + v[i][p[j]] -  0 ;         if (num < minNum) minNum = num;         if (num > maxNum) maxNum = num;       }       minDiff = min(minDiff, maxNum - minNum);     } while (next_permutation(p.begin(), p.end()));     cout << minDiff;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = 200009;   vector<int> a;   vector<int> b;   vector<int> bb;   int main() {     long long s, x;     cin >> s >> x;     int s1 = s, x1 = x;     int ans = 0;     for (int i = 0; i < 45; i++) {       if ((s & ((long long)1 << i)) && (x & ((long long)1 << i))) ans++;       if (!(s & ((long long)1 << i)) && (x & ((long long)1 << i))) {         cout << 0;         return 0;       }       if ((s & ((long long)1 << i)) && !(x & ((long long)1 << i))) {         cout << 0;         return 0;       }       if (!(s & ((long long)1 << i)) && !(x & ((long long)1 << i))) {         if ((s & ((long long)1 << (i + 1))) && !(x & ((long long)1 << (i + 1)))) {           s -= ((long long)1 << (i + 1));         }         if (!(s & ((long long)1 << (i + 1))) && (x & ((long long)1 << (i + 1)))) {           s -= ((long long)1 << (i + 1));         }       }     }     if (s1 != x1)       cout << ((long long)1 << ans);     else       cout << ((long long)1 << ans) - 2;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, p, q, d[110];   void nhap() {     memset(d, 0, sizeof(d));     int x;     cin >> n >> p;     for (int i = 1; i <= p; i++) {       cin >> x;       d[x] = 1;     }     cin >> q;     for (int i = 1; i <= q; i++) {       cin >> x;       d[x] = 1;     }   }   void cbi() {     for (int i = 1; i <= n; i++)       if (!d[i]) {         cout <<  Oh, my keyboard! ;         exit(0);       }     cout <<  I become the guy. ;   }   void xuli() {}   void ghikq() {}   int main() {     nhap();     cbi();     xuli();     ghikq();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 600010;   struct wl {     int k, l, pos;   };   wl w[MAXN];   wl temp[MAXN];   int n;   int p[MAXN];   int cnt, tcnt;   int *t;   int x[MAXN], y[MAXN];   int find(int p) {     int l = 0, r = tcnt;     while (l < r) {       int mid = (l + r) / 2;       if (p > t[mid])         l = mid + 1;       else         r = mid;     }     return l;   }   void init() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d%d , &x[i], &y[i]);       p[cnt++] = x[i];       p[cnt++] = y[i];     }     sort(p, p + cnt);     int new_cnt = 0;     new_cnt = unique(p, p + cnt) - p;     t = p;     cnt = new_cnt;     tcnt = cnt;     new_cnt = 0;     w[new_cnt].k = 0;     w[new_cnt++].l = t[0] - 1;     for (int i = 0; i < cnt - 1; i++) {       w[new_cnt].k = t[i];       w[new_cnt++].l = 1;       w[new_cnt].k = t[i] + 1;       w[new_cnt++].l = t[i + 1] - t[i] - 1;     }     w[new_cnt].k = t[cnt - 1];     w[new_cnt++].l = 1;     cnt = new_cnt;     for (int i = 0; i < n; i++) {       int xx = find(x[i]) + 1, yy = find(y[i]) + 1;       swap(w[(xx - 1) * 2 + 1], w[(yy - 1) * 2 + 1]);     }   }   long long ans = 0;   long long sum[MAXN];   void solve(int l, int r) {     if (l == r) return;     int p1 = l, p2, p = l;     int mid = (l + r) / 2;     p2 = mid + 1;     solve(l, mid);     solve(mid + 1, r);     sum[l] = w[l].l;     for (int i = l + 1; i <= mid; i++) sum[i] = sum[i - 1] + w[i].l;     while (p1 <= mid && p2 <= r) {       if (w[p1].k <= w[p2].k) {         temp[p++] = w[p1++];         continue;       }       if (w[p1].k > w[p2].k) {         temp[p] = w[p2];         ans += (sum[mid] - sum[p1] + (long long)w[p1].l) * (long long)(w[p2].l);         p2++;         p++;       }     }     for (int i = p2; i <= r; i++) temp[p++] = w[i];     for (int i = p1; i <= mid; i++) temp[p++] = w[i];     for (int i = l; i <= r; i++) w[i] = temp[i];     return;   }   int main() {     init();     solve(0, cnt - 1);     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<long long int> g[200001], rg[200001];   bool vis[200001];   long long int par[200001];   long long int sz[200001], dnom[200001];   long long int ans = 0, no = 0;   long long int n, m, S;   deque<long long int> top;   void dfs(long long int x) {     vis[x] = 1;     for (auto v : g[x]) {       if (!vis[v]) dfs(v);     }     top.push_front(x);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> m >> S;     S--;     vector<pair<long long int, long long int> > g1[n];     long long int dis[n];     for (int i = 0; i < n; i++) {       dis[i] = LLONG_MAX / 2;     }     for (int i = 0; i < m; i++) {       long long int u, v, w;       cin >> u >> v >> w;       u--;       v--;       g1[u].push_back({v, w});       g1[v].push_back({u, w});     }     set<pair<long long int, long long int> > second;     second.insert({0, S});     dis[S] = 0;     while (!second.empty()) {       pair<long long int, long long int> p = *second.begin();       second.erase(second.begin());       if (vis[p.second]) continue;       vis[p.second] = 1;       long long int cd = p.first, u = p.second;       for (auto v : g1[u]) {         if (cd == dis[v.first] + v.second) {           g[v.first].push_back(u);           rg[u].push_back(v.first);         }         if (dis[v.first] > cd + v.second) {           dis[v.first] = cd + v.second;           second.insert({dis[v.first], v.first});         }       }     }     memset(vis, 0, sizeof(vis));     dfs(S);     long long int ans = 0;     dnom[top[0]] = -1;     for (int i = 1; i < top.size(); i++) {       long long int u = top[i];       long long int x = dnom[rg[u][0]];       bool flag = 1;       for (auto par : rg[u]) {         if (dnom[par] != x) {           flag = 0;           break;         }       }       if (!flag || x == -1)         dnom[u] = u;       else         dnom[u] = x;       sz[dnom[u]]++;       ans = max(ans, sz[dnom[u]]);     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, w[26][26];     int money, cost, temp;     char a[100002], b[100002], x, y;     char result[100002];     for (int i = 0; i < 26; ++i) {       for (int j = 0; j < 26; ++j) {         w[i][j] = 1073741823;       }       w[i][i] = 0;     }     cin >> a >> b;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> x >> y >> cost;       x -=  a ;       y -=  a ;       w[x][y] = min(w[x][y], cost);     }     for (int k = 0; k < 26; ++k) {       for (int i = 0; i < 26; ++i) {         for (int j = 0; j < 26; ++j) {           w[i][j] = min(w[i][j], w[i][k] + w[k][j]);         }       }     }     n = strlen(a);     money = strlen(b) == n ? 0 : 1073741823;     for (int i = 0; i < n && money < 1073741823; ++i) {       cost = 1073741823;       for (int j = 0; j < 26; ++j) {         temp = w[a[i] -  a ][j] + w[b[i] -  a ][j];         if (temp < cost) {           cost = temp;           result[i] =  a  + j;         }       }       money += cost;     }     if (money < 1073741823) {       result[n] =   0 ;       cout << money << endl << result << endl;     } else {       cout <<  -1  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e4 + 7;   const long long M = 1e3 + 7;   const long long INF = 1e18;   long long n, m, k, a[N], g, r, ans;   bool h[N][M], ok;   queue<pair<long long, long long> > q;   vector<long long> v;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> m;     for (long long i = 1; i <= m; i++) cin >> a[i];     cin >> g >> r;     sort(a + 1, a + m + 1);     v.push_back(1);     while (!v.empty()) {       for (long long i : v) q.push({i, g});       v.clear();       ans += g;       while (!q.empty()) {         long long node = q.front().first, t = q.front().second;         q.pop();         if (h[node][t]) continue;         h[node][t] = 1;         if (node == m) {           ans -= t;           ok = 1;           break;         }         if (t == 0) v.push_back(node);         if (node > 1 && a[node] - a[node - 1] <= t)           q.push({node - 1, t - a[node] + a[node - 1]});         if (a[node + 1] - a[node] <= t)           q.push({node + 1, t - a[node + 1] + a[node]});       }       if (ok == 1) break;       ans += r;     }     if (ok)       cout << ans << endl;     else       cout << -1 << endl;   }