malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   constexpr int MAXN = 1 << 30;   struct tournament {     struct Node {       Node* parent;       set<int, greater<int>> blocked;       Node* left = nullptr;       Node* right = nullptr;       Node(Node* parent) : parent(parent) {}     }* root = new Node(nullptr);     set<int, greater<int>>::iterator it;     void update(int x, int start, int end, int lo = 0, int hi = MAXN,                 Node* node = nullptr) {       if (node == nullptr) node = root;       if (lo >= end || hi <= start) return;       if (lo >= start && hi <= end) {         node->blocked.insert(x);         return;       }       int mid = (lo + hi) / 2;       if (mid > start) {         if (node->left == nullptr) node->left = new Node(node);         update(x, start, end, lo, mid, node->left);       }       if (mid < end) {         if (node->right == nullptr) node->right = new Node(node);         update(x, start, end, mid, hi, node->right);       }     }     int query(int x, int pos) {       Node* node = root;       int lo = 0, hi = MAXN;       int sol = -1;       while (node != nullptr) {         if ((it = node->blocked.lower_bound(x)) != node->blocked.end() &&             *it > sol)           sol = *it;         int mid = (lo + hi) / 2;         if (pos < mid)           hi = mid, node = node->left;         else           lo = mid, node = node->right;       }       return sol;     }   } rows, cols;   set<int> bio;   int unzipA[200100], unzipB[200100];   pair<int, int> a[200100], b[200100];   char dirs[200100];   int main() {     ios_base::sync_with_stdio(false);     int n, q, i, j, x;     char dir;     cin >> n >> q;     rows.update(-1, 0, MAXN), cols.update(-1, 0, MAXN);     for (i = 0; i < q; ++i) {       cin >> b[i].first >> a[i].first >> dirs[i];       --a[i].first, --b[i].first;       a[i].second = b[i].second = i;     }     sort(a, a + q);     sort(b, b + q);     new (unzipA - 1) int;     new (unzipB - 1) int;     unzipA[-1] = unzipB[-1] = -1;     for (i = 0; i < q; ++i) {       unzipA[i] = a[i].first;       unzipB[i] = b[i].first;       a[i].first = b[i].first = i;     }     sort(a, a + q, [](const pair<int, int>& a, const pair<int, int>& b) {       return a.second < b.second;     });     sort(b, b + q, [](const pair<int, int>& a, const pair<int, int>& b) {       return a.second < b.second;     });     for (int k = 0; k < q; ++k) {       i = a[k].first;       j = b[k].first;       dir = dirs[k];       if (bio.count(MAXN + i - j)) {         cout <<  0 n ;         continue;       }       bio.insert(MAXN + i - j);       switch (dir) {         case  U :           x = rows.query(i, j);           cout << unzipA[i] - unzipA[x] << endl;           cols.update(j, x, i);           break;         case  L :           x = cols.query(j, i);           cout << unzipB[j] - unzipB[x] << endl;           rows.update(i, x + 1, j + 1);           break;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     int a, b, p, pos;     int i, cost, j, k, l;     string s;     while (t--) {       cin >> a >> b >> p;       cost = 0;       cin >> s;       pos = s.length() - 2;       i = s.length() - 2;       if (s[i] ==  A ) {         cost += a;         if (cost > p) {           cout << s.length() << endl;           continue;         }       }       if (s[i] ==  B ) {         cost += b;         if (cost > p) {           cout << s.length() << endl;           continue;         }       }       while (i > 0) {         if (s[i] == s[i - 1]) {           i--;           pos = i;         }         if (s[i] != s[i - 1] && i > 0) {           if (s[i - 1] ==  A ) {             cost += a;           }           if (s[i - 1] ==  B ) {             cost += b;           }           if (cost <= p) {             pos = i - 1;             i--;           }           if (cost > p) {             break;           }         }       }       cout << pos + 1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int const MOD = 1e9 + 7;   double const PI = acos(-1);   long long const oo = 8e18;   int const N = 3e5 + 100;   int const M = 1e6 + 100;   int n;   pair<int, int> a[N], p1[N], p2[N];   pair<int, int> intersection(pair<int, int> x, pair<int, int> y) {     if (x.second < y.first || y.second < x.first)       return {-1, -1};     else       return {max(x.first, y.first), min(x.second, y.second)};   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %d%d , &a[i].first, &a[i].second);     sort(a, a + n);     for (int i = 0; i < n; ++i) {       if (i == 0)         p1[i] = a[i];       else {         p1[i] = intersection(a[i], p1[i - 1]);       }     }     for (int i = n - 1; i >= 0; --i) {       if (i == n - 1)         p2[i] = a[i];       else {         p2[i] = intersection(a[i], p2[i + 1]);       }     }     int mx = 0;     for (int i = 0; i < n; ++i) {       pair<int, int> temp;       if (i == 0) {         temp = p2[i + 1];       } else if (i == n - 1) {         temp = p1[i - 1];       } else {         temp = intersection(p1[i - 1], p2[i + 1]);       }       mx = max(mx, temp.second - temp.first);     }     cout << mx << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, x;     cin >> n >> x;     int ar[n], i, cnt = 0;     for (i = 0; i < n; i++) {       cin >> ar[i];       cnt += ar[i];     }     cnt += n - 1;     if (cnt == x)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5;   bool isprime[32000];   vector<int> prime;   int a[maxn + 50], num[maxn + 50], factor[maxn + 50], now[maxn + 50];   int k, n, len = 0;   long long ans = 0;   bool check() {     for (int i = 1; i <= len; ++i)       if (now[i] < num[i]) return false;     return true;   }   void make(int x, int type) {     for (int i = 1; i <= len; ++i) {       if (x % factor[i] != 0) continue;       if (factor[i] == 1) {         now[i] += type;         continue;       }       while (x % factor[i] == 0) {         x /= factor[i];         now[i] += type;       }       if (x == 1) break;     }   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     isprime[1] = 1;     for (int i = 2; i <= sqrt(k); ++i)       if (!isprime[i]) {         prime.push_back(i);         for (int j = i + i; j <= sqrt(k); j += i) isprime[j] = 1;       }     ++len;     factor[len] = 1;     num[len] = 1;     for (int i = 0; i < prime.size(); ++i) {       if (k % prime[i] != 0) continue;       factor[++len] = prime[i];       while (k % prime[i] == 0) {         k /= prime[i];         ++num[len];       }       if (k == 1) break;     }     if (k != 1) factor[++len] = k, num[len] = 1;     int r = 0;     for (int l = 1; l <= n; ++l) {       while (!check() && r < n) {         ++r;         make(a[r], 1);       }       if (check()) ans += n - r + 1;       make(a[l], -1);     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1000500;   const int INF = 1000000000, mod = 1000000007;   const long long LLINF = 1000000000000000000ll;   char s[N];   int z[N];   int cnt[N];   int n;   void calcZ() {     int l = 0, r = 0;     for (int i = 1; i < n; ++i) {       if (i <= r) {         z[i] = min(z[i - l], r - i + 1);       }       while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {         z[i]++;       }       if (i + z[i] >= r) {         l = i;         r = i + z[i] - 1;       }       cnt[z[i]]++;     }   }   int main() {     scanf( %s , &s);     n = strlen(s);     calcZ();     int cur = 0;     for (int i = n; i >= 1; --i) {       cur += cnt[i];       if (cur >= 2 && z[n - i] == i) {         for (int j = 0; j < i; ++j) {           putchar(s[j]);         }         exit(0);       }     }     puts( Just a legend );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool vis[10010][51];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int m, n;     cin >> m >> n;     for (int i = 0; i < m; i++) {       int k;       cin >> k;       for (int j = 0; j < k; j++) {         int temp;         cin >> temp;         vis[temp][i] = true;       }     }     for (int i = 0; i < m; i++) {       for (int j = i + 1; j < m; j++) {         int flag = 0;         for (int k = 1; k < n + 1; k++) {           if (vis[k][i] && vis[k][j]) {             flag = 1;             break;           }         }         if (flag == 0) {           cout <<  impossible  << endl;           return 0;         }       }     }     cout <<  possible  << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int dp[4][N];   string str2[N], str3[N];   set<string> ans;   int n;   int cal(int len, int idx) {     if (dp[len][idx] != -1) return dp[len][idx];     if (idx == n - 1) return dp[len][idx] = 1;     ;     int x = 0;     if (idx + 2 < n) {       if (len == 2 && str2[idx] == str2[idx + 2]) {       } else         x |= cal(2, idx + 2);     }     if (idx + 3 < n) {       if (len == 3 && str3[idx] == str3[idx + 3]) {       } else         x |= cal(3, idx + 3);     }     x = x ? 1 : 0;     return dp[len][idx] = x;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     string s;     cin >> s;     n = s.length();     for (int i = 0; i < n - 1; ++i) {       str2[i + 1] = s.substr(i, 2);       if (i + 2 < n) str3[i + 2] = s.substr(i, 3);     }     for (int i = 0; i < 4; ++i) {       for (int j = 0; j <= n; ++j) dp[i][j] = -1;     }     for (int i = 6; i < n; ++i) {       if (dp[2][i] == -1) cal(2, i);       if (i > 6 && dp[3][i] == -1) cal(3, i);     }     for (int i = 5; i < n; ++i) {       if (dp[2][i] > 0) ans.insert(str2[i]);       if (dp[3][i] > 0) ans.insert(str3[i]);     }     cout << ans.size() << endl;     for (auto I : ans) cout << I << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   const long long INF = 4e18 + 42;   long long l[N];   char type[N];   void get_stam(long long need, long long &stam, long long &have, long long &ans,                 long long cost);   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> l[i];     }     for (int i = 1; i <= n; ++i) {       cin >> type[i];     }     long long swim_res = 0, walk_res = INF;     long long stam = 0, s_fly = 0, w_fly = 0, ans = 0;     for (int i = 1; i <= n; ++i) {       if (type[i] ==  L ) {         get_stam(l[i] - stam, stam, s_fly, ans, 1);         get_stam(l[i] - stam, stam, w_fly, ans, 2);         get_stam(l[i] - stam, stam, swim_res, ans, 3);         get_stam(l[i] - stam, stam, walk_res, ans, 5);         stam -= l[i];         ans += l[i];       } else if (type[i] ==  W ) {         long long fly = min(2 * l[i], stam + l[i]), swim = 2 * l[i] - fly;         stam += (swim - fly) / 2;         ans += (3 * swim + fly) / 2;         s_fly += fly;         swim_res = INF;       } else {         get_stam(l[i] - stam, stam, s_fly, ans, 1);         long long fly = min(2 * l[i], stam + l[i]), walk = 2 * l[i] - fly;         stam += (walk - fly) / 2;         ans += (5 * walk + fly) / 2;         w_fly += fly;       }     }     cout << ans << endl;   }   void get_stam(long long need, long long &stam, long long &have, long long &ans,                 long long cost) {     if (need <= 0) {       return;     }     long long take = min(have, need);     ans += cost * take;     have -= take;     stam += take;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   int a[1005][1005];   int main() {     int N, K, D, i, j;   loop:     while (~scanf( %d%d%d , &N, &K, &D)) {       memset(a, 0, sizeof(a));       for (j = 0; j < N; ++j) {         a[D - 1][j] = j;       }       for (i = D - 1; i > 0; --i) {         for (j = 0; j < N; ++j) {           a[i - 1][j] += a[i][j] / K;           a[i][j] %= K;         }       }       for (i = 0; i < D; ++i) {         for (j = 0; j < N; ++j) {           if (a[i][j] >= K) {             puts( -1 );             goto loop;           }         }       }       for (i = 0; i < D; ++i) {         for (j = 0; j < N; ++j) {           printf( %d%s , a[i][j] + 1, j == N - 1 ?   n  :    );         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void setIO() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   const long double PI = 4 * atan((long double)1);   const int INF = 1e9 + 7;   const long long _INF = 1e18;   int main() {     setIO();     int t;     cin >> t;     while (t--) {       int x, k = 0;       cin >> x;       int y = x;       while (y) {         y /= 10;         k++;       }       cout << 10 * (x % 10 - 1) + k * (k + 1) / 2 << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   using dd = long double;   struct Tree {     struct Node {       dd mul;       dd add;       dd sum;       Node() : mul(1), add(0), sum(0) {}     };     int S;     vector<Node> drz;     void update(int v) { drz[v].sum = drz[2 * v].sum + drz[2 * v + 1].sum; }     Tree(const vector<int>& tab) {       int n = tab.size();       S = 1;       while (S < n) {         S *= 2;       }       drz.resize(2 * S);       for (int i = 0; i < n; i++) {         drz[i + S].sum = tab[i];       }       for (int v = S - 1; v; v--) {         update(v);       }     }     void receive(int v, int len, dd mul, dd add) {       drz[v].sum = drz[v].sum * mul + add * len;       drz[v].mul *= mul;       drz[v].add = drz[v].add * mul + add;     }     void push(int v, int len) {       dd mul = drz[v].mul;       dd add = drz[v].add;       receive(2 * v, len / 2, mul, add);       receive(2 * v + 1, len / 2, mul, add);       drz[v].mul = 1;       drz[v].add = 0;     }     void mul_add_rec(int v, int vl, int vr, int l, int r, dd mul, dd add) {       if (vl > r || l > vr) {         return;       }       if (l <= vl && vr <= r) {         receive(v, vr - vl + 1, mul, add);         return;       }       push(v, vr - vl + 1);       int vc = (vl + vr) / 2;       mul_add_rec(2 * v, vl, vc, l, r, mul, add);       mul_add_rec(2 * v + 1, vc + 1, vr, l, r, mul, add);       update(v);     }     void mul_add(int l, int r, dd mul, dd add) {       mul_add_rec(1, 0, S - 1, l, r, mul, add);     }     dd sum_rec(int v, int vl, int vr, int l, int r) {       if (vl > r || l > vr) {         return 0;       }       if (l <= vl && vr <= r) {         return drz[v].sum;       }       push(v, vr - vl + 1);       int vc = (vl + vr) / 2;       return sum_rec(2 * v, vl, vc, l, r) + sum_rec(2 * v + 1, vc + 1, vr, l, r);     }     dd sum(int l, int r) { return sum_rec(1, 0, S - 1, l, r); }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n, q;     cin >> n >> q;     vector<int> tab(n + 1);     for (int i = 1; i <= n; i++) {       cin >> tab[i];     }     Tree tree(tab);     while (q--) {       int t;       cin >> t;       if (t == 1) {         int l1, r1, l2, r2;         cin >> l1 >> r1 >> l2 >> r2;         auto sum1 = tree.sum(l1, r1);         auto sum2 = tree.sum(l2, r2);         int len1 = r1 - l1 + 1;         int len2 = r2 - l2 + 1;         tree.mul_add(l1, r1, 1.0 * (len1 - 1) / len1, 1.0 * sum2 / len1 / len2);         tree.mul_add(l2, r2, 1.0 * (len2 - 1) / len2, 1.0 * sum1 / len1 / len2);       } else {         int l, r;         cin >> l >> r;         cout << setprecision(10) << fixed << tree.sum(l, r) <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int day[100005];   int cnt[100005];   int vis[100005];   int n, m;   int jud(int x) {     memset(vis, 0, sizeof(vis));     int i, j;     int sum = 0, cntt = 0;     ;     for (i = x; i >= 1; i--) {       if (day[i] && vis[day[i]] == 0) {         cntt++;         vis[day[i]] = 1;         sum += cnt[day[i]];       } else if (sum)         sum--;     }     if (cntt == m && sum == 0)       return 1;     else       return 0;   }   int main() {     int i, j;     scanf( %d %d , &n, &m);     for (i = 1; i <= n; i++) scanf( %d , &day[i]);     for (i = 1; i <= m; i++) scanf( %d , &cnt[i]);     int left, right, mid;     int ans = -1;     left = 1, right = n;     while (left <= right) {       mid = (left + right) >> 1;       if (jud(mid)) {         right = mid - 1;         ans = mid;       } else {         left = mid + 1;       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, j, t, k, x, y;     scanf( %d , &t);     int a[10] = {0}, b[10] = {0};     for (j = 0; j < t; j++) {       scanf( %d%d%d , &k, &x, &y);       if (k == 1) {         a[0] = a[0] + x;         a[1] = a[1] + y;       } else {         b[0] = b[0] + x;         b[1] = b[1] + y;       }     }     if (a[0] >= a[1])       printf( LIVE n );     else       printf( DEAD n );     if (b[0] >= b[1])       printf( LIVE n );     else       printf( DEAD n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     vector<string> a;     vector<string> ra;     cin >> n >> m;     for (int i = 0; i < n; ++i) {       string s;       cin >> s;       a.push_back(s);       reverse(s.begin(), s.end());       ra.push_back(s);     }     vector<int> selected;     vector<int> arr(n, -1);     vector<int> pallindrome;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         if (ra[i] == a[j]) {           arr[i] = j;           if (arr[j] != i) {             selected.push_back(i);           } else if (i == j) {             pallindrome.push_back(i);           }         }       }     }     cout << (selected.size() * 2 * m) + (pallindrome.size() > 0 ? 1 : 0) * m          << endl;     for (int i = 0; i < selected.size(); ++i) {       cout << a[selected[i]];     }     if (pallindrome.size() > 0) {       cout << a[pallindrome[0]];     }     for (int i = selected.size() - 1; i >= 0; --i) {       cout << ra[selected[i]];     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct comp {     bool operator()(long long int a, long long int b) { return a > b; }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n, k;     cin >> n >> k;     vector<pair<long long int, long long int> > pow(n);     vector<long long int> mon(n);     vector<long long int> res(n);     priority_queue<long long int, vector<long long int>, comp> least;     for (int(i) = (0); (i) < (n); (i)++) {       long long int power;       cin >> power;       pow[i] = make_pair(power, i);     }     sort(pow.begin(), pow.end());     for (int(i) = (0); (i) < (n); (i)++) {       cin >> mon[i];     }     if (k == 0) {       for (int(i) = (0); (i) < (n); (i)++) {         if (i)           cout <<     << mon[i];         else           cout << mon[i];       }       return 0;     }     least.push(mon[pow[0].second]);     res[pow[0].second] = mon[pow[0].second];     for (int(i) = (1); (i) < (min(n, k + 1)); (i)++) {       res[pow[i].second] = res[pow[i - 1].second] + mon[pow[i].second];       least.push(mon[pow[i].second]);     }     for (int(i) = (k + 1); (i) < (n); (i)++) {       long long int toremove = least.top();       least.pop();       res[pow[i].second] = res[pow[i - 1].second] + mon[pow[i].second] - toremove;       least.push(mon[pow[i].second]);     }     for (int(i) = (0); (i) < (n); (i)++) {       if (i)         cout <<     << res[i];       else         cout << res[i];     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n;   char str[1000010];   long long cntu[1000010];   long long cntd[1000010];   long long sumu[1000010];   long long sumd[1000010];   long long CNTU;   long long CNTD;   int main() {     scanf( %I64d%s , &n, str + 1);     for (long long i = 1; i <= n; i++) {       cntu[i] = cntu[i - 1];       cntd[i] = cntd[i - 1];       sumu[i] = sumu[i - 1];       sumd[i] = sumd[i - 1];       if (str[i] ==  U ) {         CNTU++;         cntu[i]++;         sumu[i] += i;       } else {         CNTD++;         cntd[i]++;         sumd[i] += i;       }     }     for (long long i = 1; i <= n; i++)       if (str[i] ==  U ) {         if (cntu[i - 1] >= CNTD - cntd[i]) {           long long k =               upper_bound(cntu + 1, cntu + n + 1, cntu[i - 1] - CNTD + cntd[i]) -               cntu;           printf( %I64d  ,                  (-(sumu[i - 1] - sumu[k - 1]) + (sumd[n] - sumd[i])) * 2 + n -                      i + 1);         } else {           long long k =               lower_bound(cntd + 1, cntd + n + 1, cntd[i] + cntu[i - 1] + 1) -               cntd;           printf( %I64d  , (-sumu[i - 1] + (sumd[k] - sumd[i]) - i) * 2 + i);         }       } else {         if (cntu[i - 1] <= CNTD - cntd[i]) {           long long k =               lower_bound(cntd + i, cntd + n + 1, cntd[i] + cntu[i - 1]) - cntd;           printf( %I64d  , (-sumu[i - 1] + (sumd[k] - sumd[i])) * 2 + i);         } else {           long long k = upper_bound(cntu + 1, cntu + n + 1,                                     cntu[i - 1] - CNTD + cntd[i] - 1) -                         cntu;           printf( %I64d  ,                  (i - (sumu[i - 1] - sumu[k - 1]) + (sumd[n] - sumd[i])) * 2 + n -                      i + 1);         }       }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int M = 7002;   const int N = 100010;   int n, q;   bitset<M> num[M];   bitset<M> sq[M];   bitset<M> a[N];   vector<int> squareFree;   int main() {     cin >> n >> q;     for (int i = 1; i < M; ++i) {       int good = 1;       for (int j = 2; j * j <= i; ++j) {         if (i % (j * j) == 0) {           good = 0;           break;         }       }       if (good) squareFree.push_back(i);     }     for (int i = 1; i < M; ++i) {       for (int j = 1; j * j <= i; ++j) {         if (i % j == 0) {           num[i][j] = num[i][i / j] = 1;         }       }     }     for (int i = 1; i < M; ++i) {       for (int j : squareFree) {         if (i * j > 7000) break;         sq[i][i * j] = 1;       }     }     while (q--) {       int cmd, x, y, z, v;       scanf( %d %d , &cmd, &x);       if (cmd == 1) {         scanf( %d , &v);         a[x] = num[v];       } else if (cmd == 2) {         scanf( %d %d , &y, &z);         a[x] = a[y] ^ a[z];       } else if (cmd == 3) {         scanf( %d %d , &y, &z);         a[x] = a[y] & a[z];       } else {         scanf( %d , &v);         int ans = (sq[v] & a[x]).count() % 2;         printf( %d , ans);       }     }     puts(  );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long double EPS = 1e-9;   long long cel(long long a, long long b) { return ((a - 1) / b + 1); }   long long gcd(long long a, long long b) {     if (a < b) swap(a, b);     return (b == 0) ? a : gcd(b, a % b);   }   long long MIN(long long a, int b) {     long long ans;     (a >= b) ? ans = b : ans = a;     return ans;   }   long long MAX(long long a, int b) {     long long ans;     (a >= b) ? ans = a : ans = b;     return ans;   }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   long long po(long long x, long long y) {     long long ans = 1;     while (y) {       if (y & 1) {         ans = (ans * x) % 1000000007;       }       y >>= 1;       x = (x * x) % 1000000007;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, t, x, y, m, k;     cin >> n >> m;     vector<int> v(1000001, 0);     vector<int> A(n), B(n), C(m), D(m);     for (int i = 0; i < n; i++) {       cin >> A[i] >> B[i];     }     for (int i = 0; i < m; i++) {       cin >> C[i] >> D[i];     }     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         if (C[i] - A[j] >= 0) {           v[C[i] - A[j]] = max(v[C[i] - A[j]], D[i] - B[j] + 1);         }       }     }     for (int i = 1000000 - 1; i >= 0; i--) {       v[i] = max(v[i + 1], v[i]);     }     int ans = 1000000007;     for (int i = 0; i < 1000001; i++) {       y = ans;       ans = min(ans, i + v[i]);     }     cout << ans;     return 0;   }