malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   using namespace std;   unsigned int a[100000 + 5], b[100000 + 5], p;   int f(int x) { return ~((x - 1) >> 31); }   int main() {     int n, t, l, r, k, i;     long long ans = 0;     for (scanf( %d , &n); n--;) {       scanf( %d%d%d , &t, &l, &r);       if (t == 1) {         scanf( %d , &k);         p = k > 0 ? k - 1 : -k - 1;         if (k > 0)           for (i = l; i < r; ++i) a[i] = (a[i] - 1 < p ? a[i] - 1 : p) + 1;         else           for (i = l; i < r; ++i) b[i] = (b[i] - 1 < p ? b[i] - 1 : p) + 1;       } else {         for (ans = 0, i = l; i < r; ++i) ans += (a[i] + b[i]) & f(a[i]) & f(b[i]);         printf( %I64d n , ans);       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long mod = 1e9;   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a << endl;     err(++it, args...);   }   long long powm(long long a, long long b, long long mod) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % mod;       a = (a * a) % mod;       b >>= 1;     }     return res;   }   bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {     return (a.first > b.first);   }   void solve() {     long long n;     cin >> n;     vector<long long> a(n);     for (long long i = 0; i < (n); i++) {       cin >> a[i];     };     if (n == 1) {       cout <<  YES n ;       cout << 1 <<     << a[0] << endl;       return;     }     long long x = 0, y = 0, m = 0;     for (long long i = 0; i < (n - 1); i++) {       m = max({m, a[i], a[i + 1]});       if (abs(a[i] - a[i + 1]) == 0) {         cout <<  NO n ;         return;       }       if (abs(a[i] - a[i + 1]) != 1 and y == 0) {         y = abs(a[i] - a[i + 1]);       } else if (abs(a[i] - a[i + 1]) != 1 and y != 0) {         if (y != abs(a[i] - a[i + 1])) {           cout <<  NO n ;           return;         }       }     }     if (y == 0) {       cout <<  YES n ;       cout << (mod) <<     << (mod) << endl;       return;     }     for (long long i = 0; i < (n - 1); i++) {       if (abs(a[i] - a[i + 1]) == 1) {         if (a[i] % y == 0 and a[i + 1] == a[i] + 1) {           cout <<  NO n ;           return;         }         if (a[i + 1] % y == 0 and a[i] == a[i + 1] + 1) {           cout <<  NO n ;           return;         }       }     }     cout <<  YES n ;     cout << (m / y) + 1 <<     << y << endl;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();   }
#include <bits/stdc++.h>   long long a[100001];   int main() {     int n;     scanf( %d , &n);     int i, j, k;     int m = n / 2;     for (i = 1; i < n; i += 2) {       scanf( %I64d , &a[i]);     }     long long cur = 100000000000001LL;     for (i = n - 1; i >= 1; i -= 2) {       bool ok = false;       for (k = 2 - a[i] % 2; (long long)(k)*k <= a[i]; k += 2) {         if (a[i] % k) continue;         long long miden = a[i] / k;         if ((miden - k) % 2) continue;         long long upper = (a[i] / k) + (k - 1);         if (upper + 2 < cur) {           ok = true;           long long lower = (a[i] / k) - (k - 1);           a[i + 1] = ((upper + 2) + (cur - 2)) * ((cur - (upper + 2)) / 2) / 2;           cur = lower;           break;         }       }       if (!ok) {         break;       }     }     a[0] = (1 + (cur - 2)) * ((cur - 1) / 2) / 2;     if (a[0] > 0 && i < 1) {       puts( Yes );       for (i = 0; i < n; i++) {         printf( %I64d%c , a[i],    n [i == n - 1]);       }     } else {       puts( No );     }   }
#include <bits/stdc++.h>   const int N = 2001000;   int a[N], l[N], r[N], c[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     int t1 = 0, t2 = -1;     for (int i = 1; i <= n; i++)       if (a[i] > t2) t2 = a[i], t1 = i;     for (int i = 1; i < t1; i++) a[n + i] = a[i];     for (int i = t1; i <= n; i++) a[i - t1 + 1] = a[i];     for (int i = n + 1; i < n + t1; i++) a[i - t1 + 1] = a[i];     for (int i = n; i >= 1; --i) {       r[i] = i + 1;       while (r[i] <= n && a[i] > a[r[i]]) r[i] = r[r[i]];       if (r[i] <= n && a[i] == a[r[i]]) {         c[i] = c[r[i]] + 1;         r[i] = r[r[i]];       }     }     for (int i = 1; i <= n; i++) {       l[i] = i - 1;       while (l[i] >= 1 && a[i] > a[l[i]]) l[i] = l[l[i]];       if (l[i] >= 1 && a[i] == a[l[i]]) {         l[i] = l[l[i]];       }     }     long long ans = 0;     for (int i = 1; i <= n; i++) {       ans += c[i];       if (a[i] == a[1]) continue;       ans += 2;       if (l[i] == 1 && r[i] == n + 1) ans--;     }     printf( %I64d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   using namespace std;   const int N = 20;   pair<double, double> p1[N], p2[N];   int n, m;   bool tag;   int p[N], vis[N];   bool ok(pair<double, double> a, pair<double, double> b, pair<double, double> c,           pair<double, double> d) {     if (!(min(a.first, b.first) <= max(c.first, d.first) &&           min(c.second, d.second) <= max(a.second, b.second) &&           min(c.first, d.first) <= max(a.first, b.first) &&           min(a.second, b.second) <= max(c.second, d.second)))       return 0;     double u, v, w, z;     u = (c.first - a.first) * (b.second - a.second) -         (b.first - a.first) * (c.second - a.second);     v = (d.first - a.first) * (b.second - a.second) -         (b.first - a.first) * (d.second - a.second);     w = (a.first - c.first) * (d.second - c.second) -         (d.first - c.first) * (a.second - c.second);     z = (b.first - c.first) * (d.second - c.second) -         (d.first - c.first) * (b.second - c.second);     return (u * v <= 0.00000001 && w * z <= 0.00000001);   }   void dfs(int now) {     bool flag;     if (tag) return;     if (now > n) {       flag = 1;       for (int i = 1; i <= n; i++)         for (int j = i + 1; j <= n; j++)           if (!ok(p1[i], p2[p[i]], p1[j], p2[p[j]])) return;       tag = 1;       return;     }     for (int i = 1; i <= m; i++)       if (!vis[i]) {         p[now] = i;         dfs(now + 1);       }   }   int main() {     n = read();     m = read();     for (int i = 1; i <= n; i++) scanf( %lf%lf , &p1[i].first, &p1[i].second);     for (int i = 1; i <= m; i++) scanf( %lf%lf , &p2[i].first, &p2[i].second);     if (n != m) {       puts( No );       return 0;     }     tag = 0;     dfs(1);     if (tag)       puts( Yes );     else       puts( No );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int BITS(long long __a) {     short int __c = 0;     __a <<= 1;     while (__a) __c += ((__a >>= 1) & 1);     return __c;   }   inline bool prime(int n) {     for (int i = 3; i * i <= n; i += 2)       if (n % i == 0) return false;     return true;   }   set<int> s;   int N, M, V[100010], A[100010];   int main() {     cin >> N >> M;     for (int i = 0; i < N; i++) cin >> V[i];     s.insert(V[N - 1]);     A[N - 1] = 1;     for (int i = N - 2; i >= 0; i--) {       s.insert(V[i]);       A[i] = ((int)s.size());     }     for (int i = 0; i < M; i++) {       int j;       cin >> j;       cout << A[j - 1] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char s[100100];   char s1[111];   char s2[110];   int main() {     scanf( %s%s%s , s, s1, s2);     int flag1, flag2;     int n = strlen(s);     int n1 = strlen(s1);     int n2 = strlen(s2);     flag1 = flag2 = 0;     int i, j;     for (i = 0; i < n; i++) {       for (j = 0; j < n1; j++) {         if (s[i + j] != s1[j]) break;       }       if (j == n1) {         break;       }     }     i += n1;     for (; i < n; i++) {       for (j = 0; j < n2; j++) {         if (s[i + j] != s2[j]) break;       }       if (j == n2) {         flag1 = 1;         break;       }     }     for (i = 0; i < n; i++) {       for (j = 0; j < n2; j++) {         if (s[i + n2 - j - 1] != s2[j]) break;       }       if (j == n2) {         break;       }     }     i += n2;     for (; i < n; i++) {       for (j = 0; j < n1; j++) {         if (s[i + n1 - j - 1] != s1[j]) break;       }       if (j == n1) {         flag2 = 1;         break;       }     }     if (flag1 == 0 && flag2 == 0) {       printf( fantasy n );     }     if (flag1 == 0 && flag2 == 1) {       printf( backward n );     }     if (flag1 == 1 && flag2 == 0) {       printf( forward n );     }     if (flag1 == 1 && flag2 == 1) {       printf( both n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, d, sum = 0;     cin >> n >> d;     vector<pair<int, int> > ins(n);     for (int i = 0; i < n; i++) {       cin >> ins[i].first;       ins[i].second = i + 1;     }     sort(ins.begin(), ins.end());     int i = 0;     while (i < n and ((sum + ins[i].first) <= d)) {       sum += ins[i].first;       i++;     }     cout << i << endl;     for (int j = 0; j < i; j++) {       if (j > 0) cout <<    ;       cout << ins[j].second;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double dp[1050000];   int n, k;   double p[25];   double ans[25];   int main() {     memset(dp, 0, sizeof(dp));     memset(ans, 0, sizeof(ans));     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) scanf( %lf , p + i);     dp[0] = 1;     int st = (1 << n);     for (int i = 1; i < st; i++) {       double sp = 0;       int o = 0;       for (int j = 0; j < n; j++) {         if (i & 1 << j)           o++;         else           sp += p[j];       }       if (o > k) continue;       for (int j = 0; j < n; j++)         if ((i & 1 << j) && p[j] > 0.000000001) {           dp[i] += dp[i - (1 << j)] * p[j] / (sp + p[j]);           ans[j] += dp[i - (1 << j)] * p[j] / (sp + p[j]);         }     }     for (int i = 0; i < n; i++) cout << ans[i] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   set<string> st;   const int N = 1e6 + 5;   int a[3000];   int c1 = 0, c2;   string s, c;   int na, nb, m, ans = 0, mx = 0;   int main() {     int n, a, b;     cin >> n;     while (n--) {       cin >> a >> b;       if (a == 2) {         cout << b / a << endl;         continue;       }       if (a == 1) {         cout << b << endl;         continue;       }       if (a % 2 == 1) {         cout << b / (a - a / 2) << endl;       } else         cout << b / (a - (a / 2 - 1)) << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   set<long long> myset;   int base;   int N;   long long A[100005];   map<long long, int> Map;   int ANS[600000];   int T[1200000];   int Lo[300000];   int ql, qu;   int P[300000];   void update(int ind) {     ind += base;     T[ind] = ind - base;     while (1) {       ind /= 2;       if (ind < 1) break;       T[ind] = T[2 * ind];       if (ANS[T[2 * ind + 1]] > ANS[T[ind]]) T[ind] = T[2 * ind + 1];     }   }   int query(int ind, int cl, int cu) {     if (cu < ql || cl > qu) return -1;     if (cl >= ql && cu <= qu) return T[ind];     int ansl = query(2 * ind, cl, (cl + cu) / 2);     int ansr = query(1 + 2 * ind, 1 + (cl + cu) / 2, cu);     if (ansl == -1 || ansr == -1) return (ansl + ansr + 1);     if (ANS[ansl] >= ANS[ansr]) return ansl;     return ansr;   }   int main() {     int n;     int d;     cin >> n >> d;     for (__typeof((__typeof(n))0) i((__typeof(n))0); i < n; i++) {       scanf( %lld , &A[i]);       myset.insert(A[i]);       myset.insert(A[i] - d);       myset.insert(A[i] + d);     }     int c = 0;     for (__typeof(myset.begin()) it(myset.begin()); it != myset.end(); it++)       Map[*it] = c++;     N = c;     base = 1;     while (base < N) base *= 2;     for (int i = base; i < 2 * base; i++) T[i] = i - base;     for (int i = base - 1; i > 0; i--) {       T[i] = T[2 * i];       if (ANS[T[2 * i + 1]] > ANS[T[i]]) T[i] = T[2 * i + 1];     }     for (int i = 0; i < n; i++) {       long long h = A[i];       int x = Map[h];       int ans = -1;       qu = Map[h - d];       ql = 0;       ans = query(1, 0, base - 1);       ql = Map[h + d];       qu = N - 1;       int ans2 = query(1, 0, base - 1);       if (ans == -1 || ANS[ans] < ANS[ans2]) ans = ans2;       if (ans == -1 || ANS[ans] == 0) {         ANS[x] = 1;         P[i] = -1;       } else {         ANS[x] = 1 + ANS[ans];         P[i] = Lo[ans];       }       update(x);       Lo[x] = i;     }     int p = 0;     for (int i = 0; i < N; i++)       if (ANS[i] > ANS[p]) p = i;     p = Lo[p];     vector<int> TP;     while (p >= 0) {       TP.push_back(p + 1);       p = P[p];     }     reverse(TP.begin(), TP.end());     cout << (int)(TP.size()) << endl;     for (__typeof(TP.begin()) i(TP.begin()); i != TP.end(); i++)       cout << *i <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x1, y1, x2, y2;     long long int answ = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x1 >> y1 >> x2 >> y2;       answ += (x2 - x1 + 1) * (y2 - y1 + 1);     }     cout << answ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int arr[10][10];   double me[10][10];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n = 10;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         cin >> arr[i][j];       }     }     me[0][0] = 0.;     me[0][1] = 6.;     me[0][2] = 6.;     me[0][3] = 6.;     me[0][4] = 6.;     me[0][5] = 6.;     double fix = 1. / 6.;     for (int i = 0; i < n; i++) {       if (i % 2 == 0) {         for (int j = (i == 0) ? 6 : 0; j < n; j++) {           double best = 1e6;           for (int k = 0; k <= (1 << 6) - 1; k++) {             double summ = 1;             for (int s = 0; s < 6; s++) {               if (((k >> s) & 1) == 1) {                 int posx = (j - s - 1 >= 0)                                ? j - s - 1                                : n - (((j - s - 1) % 10 + 10) % 10) - 1;                 int posy = (j - s - 1 >= 0 || i == 0) ? i : i - 1;                 summ += fix * me[posy - arr[posy][posx]][posx];               } else {                 int posx = (j - s - 1 >= 0)                                ? j - s - 1                                : n - (((j - s - 1) % 10 + 10) % 10) - 1;                 int posy = (j - s - 1 >= 0 || i == 0) ? i : i - 1;                 summ += fix * me[posy][posx];               }             }             best = min(best, summ);           }           me[i][j] = best;         }       } else {         for (int j = n - 1; j >= 0; j--) {           double best = 1e6;           for (int k = 0; k <= (1 << 6) - 1; k++) {             double summ = 1;             for (int s = 0; s < 6; s++) {               if (((k >> s) & 1) == 1) {                 int posx = (j + s + 1 < n) ? j + s + 1                                            : n - ((j + s + 1) % 10 + 10) % 10 - 1;                 int posy = (j + s + 1 < n || i == 0) ? i : i - 1;                 summ += fix * me[posy - arr[posy][posx]][posx];               } else {                 int posx = (j + s + 1 < n) ? j + s + 1                                            : n - ((j + s + 1) % 10 + 10) % 10 - 1;                 int posy = (j + s + 1 < n || i == 0) ? i : i - 1;                 summ += fix * me[posy][posx];               }             }             best = min(best, summ);           }           me[i][j] = best;         }       }     }     cout.precision(9);     cout << fixed << me[9][0];   }
#include <bits/stdc++.h>   using namespace std;   const int MX = 1000006;   const long long inf = 1e18;   long long n, m, p, a[MX], ca[MX], b[MX], cb[MX], mn[MX];   long long x[MX], y[MX], z[MX];   long long st[4 * MX + 20], lz[4 * MX + 20], res = -inf;   vector<int> pos[MX];   void push(int i, int j, int pos) {     if (i < j) {       lz[pos * 2] += lz[pos];       lz[pos * 2 + 1] += lz[pos];     }     st[pos] += lz[pos];     lz[pos] = 0;   }   void update(int i, int j, int pos, int a, int b, long long k) {     if (lz[pos]) push(i, j, pos);     if (j < a || b < i) return;     if (a <= i && j <= b) {       lz[pos] = k;       push(i, j, pos);       return;     }     int m = (i + j) / 2;     update(i, m, pos * 2, a, b, k);     update(m + 1, j, pos * 2 + 1, a, b, k);     st[pos] = max(st[pos * 2], st[pos * 2 + 1]);   }   long long query(int i, int j, int pos, int a, int b) {     if (lz[pos]) push(i, j, pos);     if (j < a || b < i) return -inf;     if (a <= i && j <= b) return st[pos];     int m = (i + j) / 2;     return max(query(i, m, pos * 2, a, b), query(m + 1, j, pos * 2 + 1, a, b));   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m >> p;     fill(mn, mn + MX, inf);     for (int i = 1; i <= n; i++) {       cin >> a[i] >> ca[i];       mn[a[i]] = min(mn[a[i]], ca[i]);     }     for (int i = 1; i < MX; i++) update(1, MX - 1, 1, i, i, -mn[i]);     fill(mn, mn + MX, inf);     for (int i = 1; i <= m; i++) {       cin >> b[i] >> cb[i];       mn[b[i]] = min(mn[b[i]], cb[i]);     }     for (int i = 1; i <= p; i++) {       cin >> x[i] >> y[i] >> z[i];       pos[y[i]].push_back(i);     }     for (int i = 1; i < MX - 1; i++) {       for (int j : pos[i - 1]) update(1, MX - 1, 1, x[j] + 1, MX - 1, z[j]);       push(1, MX - 1, 1);       res = max(res, st[1] - mn[i]);     }     cout << res <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 2e9;   const long long INFL = 2e18;   const int N = 1e6, M = N;   int n, m, G = 0;   vector<int> adj[N + 5];   int gr[N + 5], p[N + 5];   vector<int> pth[N + 5];   vector<int> PTH_myEndlessLove;   int Pos[N + 5];   void dfs(int u) {     gr[u] = G;     PTH_myEndlessLove.push_back(u);     for (int i = (0), _b = (((int)(adj[u]).size())); i < _b; ++i) {       int v = adj[u][i];       if (gr[v]) continue;       dfs(v);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m;     for (int i = (1), _b = (n); i <= _b; ++i) cin >> p[i];     int u, v;     while (m--) {       cin >> u >> v;       adj[p[u]].push_back(p[v]);       adj[p[v]].push_back(p[u]);     }     for (int i = (1), _b = (n); i <= _b; ++i)       if (!gr[p[i]]) {         PTH_myEndlessLove.clear();         ++G;         dfs(p[i]);         sort(PTH_myEndlessLove.begin(), PTH_myEndlessLove.end());         reverse(PTH_myEndlessLove.begin(), PTH_myEndlessLove.end());         pth[G] = PTH_myEndlessLove;       }     for (int i = (1), _b = (n); i <= _b; ++i) {       cout << pth[gr[p[i]]][Pos[gr[p[i]]]] <<    ;       ++Pos[gr[p[i]]];     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char a[10005];   char b[1000005];   vector<int> poj[10005];   int alen, blen;   long long total;   int pos;   vector<int>::iterator it;   int main() {     scanf( %s , a);     scanf( %s , b);     alen = strlen(a), blen = strlen(b);     pos = 0;     total = 1;     for (int i = 0; i <= alen - 1; i++) {       poj[a[i] -  a ].push_back(i);     }     for (int i = 0; i <= 25; i++) {       sort(poj[i].begin(), poj[i].end());     }     for (int i = 0; i <= blen - 1; i++) {       if (poj[b[i] -  a ].empty()) {         printf( -1 n );         return 0;       }       it = lower_bound(poj[b[i] -  a ].begin(), poj[b[i] -  a ].end(), pos);       if (it == poj[b[i] -  a ].end()) {         it = poj[b[i] -  a ].begin();         total++;       }       pos = (*it) + 1;     }     printf( %d n , total);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n{0}, a{0}, m{0}, s{0};     cin >> n;     for (long long int i = 1; i <= n; i++) {       cin >> a;       s += a;       m = max(m, a);     }     if (s % 2 == 1 || s < 2 * m) {       cout <<  NO ;       return 0;     }     cout <<  YES ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 0xffffff;   int LEN;   class Mcmf {    public:     struct T {       int from, to, val;       int next, cost;     } edge[200001];     int len;     int visit[200001], pre[200001], dist[200001], que[200001], vis[200001],         pos[200001];     void init() {       memset(vis, -1, sizeof(vis));       len = 0;     }     void add(int from, int to, int val, int cost) {       edge[len].from = from, edge[len].to = to, edge[len].val = val,       edge[len].cost = cost;       edge[len].next = vis[from], vis[from] = len++;       edge[len].from = to, edge[len].to = from, edge[len].val = 0,       edge[len].cost = -cost;       edge[len].next = vis[to], vis[to] = len++;     }     int SPFA(int s, int t, int n) {       int i, to, k;       for (i = 0; i <= n; i++) {         pre[i] = -1, visit[i] = 0;       }       int head, tail;       head = tail = 0;       for (i = 0; i <= n; i++) dist[i] = inf;       que[tail++] = s, pre[s] = s, dist[s] = 0, visit[s] = 1;       while (head != tail) {         int from = que[head++];         visit[from] = 0;         for (k = vis[from]; k != -1; k = edge[k].next) {           to = edge[k].to;           if (edge[k].val > 0 && dist[from] + edge[k].cost < dist[to]) {             dist[to] = dist[from] + edge[k].cost;             pre[to] = from;             pos[to] = k;             if (!visit[to]) {               visit[to] = 1;               que[tail++] = to;             }           }         }       }       if (pre[t] != -1 && dist[t] < inf) return 1;       return 0;     }     int mnCostFlow(int s, int t, int n) {       if (s == t) {       }       int flow = 0, cost = 0;       while (SPFA(s, t, n)) {         int from, mn = inf;         for (from = t; from != s; from = pre[from])           mn = min(mn, edge[pos[from]].val);         flow += mn;         cost += dist[t] * mn;         for (from = t; from != s; from = pre[from]) {           edge[pos[from]].val -= mn;           edge[pos[from] ^ 1].val += mn;         }       }       if (flow != LEN) return -1;       return cost;     }   };   int n, q;   char s[10010];   int cnt[27];   int tmp[27];   Mcmf mcf;   int main() {     mcf.init();     scanf( %s , s);     LEN = strlen(s);     for (int i = 0; i < LEN; ++i) {       cnt[s[i] -  a ]++;     }     int len;     scanf( %d , &q);     int st = q + 26, en = st + 1, num;     for (int i = 0; i < q; ++i) {       scanf( %s , s);       len = strlen(s);       memset(tmp, 0, sizeof(tmp));       for (int k = 0; k < len; ++k) ++tmp[s[k] -  a ];       for (int k = 0; k < 26; ++k) mcf.add(i, q + k, tmp[k], 0);       scanf( %d , &num);       mcf.add(st, i, num, i + 1);     }     for (int i = 0; i < 26; ++i) mcf.add(q + i, en, cnt[i], 0);     int ans = mcf.mnCostFlow(st, en, en + 1);     printf( %d n , ans);   }