malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1010;   const double oo = 1E7, eps = 1E-7;   struct point {     double x, y;   } P[maxn];   int n;   double ansl, ansr;   point operator+(point a, point b) { return (point){a.x + b.x, a.y + b.y}; }   point operator-(point a, point b) { return (point){a.x - b.x, a.y - b.y}; }   double operator*(point a, point b) { return a.x * b.x + a.y * b.y; }   double operator/(point a, point b) { return a.x * b.y - b.x * a.y; }   double get(point A, point B) {     double a, b, c, res;     a = A.y - B.y;     b = B.x - A.x;     c = A / B;     if (a)       res = (-c - b * P[1].y) / a;     else       res = oo;     return res;   }   int main() {     int i, j;     double x, sita;     scanf( %d , &n);     scanf( %lf%lf , &P[n].x, &P[n].y);     for (i = 1; i < n; ++i) scanf( %lf%lf , &P[i].x, &P[i].y);     ansl = min(P[1].x, P[n].x), ansr = max(P[1].x, P[n].x);     for (i = 2; i < n; ++i) {       j = i + 1;       x = get(P[i], P[j]);       sita = (P[1].y - P[i].y) * (P[j].x - P[i].x);       if (sita > (P[j].y - P[i].y) * (ansl - P[i].x)) ansl = max(ansl, x);       if (sita > (P[j].y - P[i].y) * (ansr - P[i].x)) ansr = min(ansr, x);     }     if (ansr >= ansl)       printf( %.0lf , floor(ansr) - floor(ansl - eps));     else       printf( 0 );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int a[100001] = {0};   long long int d[100001] = {0};   long long int vis[100001] = {0};   long long int cons[100001] = {0};   vector<long long int> vec[100001];   void dfs(long long int x, long long int p) {     vis[x] = 1;     if (a[x] == 1) {       if (a[p] == 1) {         cons[x] = cons[p] + 1;       } else {         cons[x] = 1;       }       d[x] = max(d[p], cons[x]);     } else {       cons[x] = 0;       d[x] = d[p];     }     for (long long int i = 0; i < vec[x].size(); i++) {       if (vis[vec[x][i]] == 1)         continue;       else {         dfs(vec[x][i], x);       }     }   }   signed main() {     long long int n, m;     cin >> n >> m;     for (long long int i = 0; i < n; i++) cin >> a[i + 1];     for (long long int i = 0; i < n - 1; i++) {       long long int x, y;       cin >> x >> y;       vec[x].push_back(y);       vec[y].push_back(x);     }     dfs(1, 0);     long long int count = 0;     for (long long int i = 2; i <= n; i++) {       if (vec[i].size() == 1 && d[i] <= m) count++;     }     cout << count;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   long long read() {     long long x = 0, f = 1;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);     return x * f;   }   void write(long long x) {     if (x < 0) x = -x, putchar( - );     if (x >= 10) write(x / 10);     putchar(x % 10 +  0 );   }   void writeln(long long x) {     write(x);     puts(  );   }   const int N = 500000;   int E[N], X[N], R[N], D[N];   int u[N], v[N];   bool b[N];   int i, m, n, t, w, x, y, S_E;   inline void A_E(int x, int y) {     S_E++, E[S_E] = y, X[S_E] = R[x], R[x] = S_E, D[x]++;     return;   }   inline int O_E(int x) { return ((x - 1) ^ 1) + 1; }   inline void dfs(int x) {     for (int i = R[x]; R[x] = X[i], i != 0; i = R[x])       if (!b[i]) {         b[i] = true, b[O_E(i)] = true;         dfs(E[i]);         w++, u[w] = x, v[w] = E[i];       }     return;   }   int main() {     n = read(), m = read();     for (i = 1; i <= m; i++) {       x = read(), y = read();       A_E(x, y), A_E(y, x);     }     for (i = 1; i <= n; i++)       if (D[i] & 1)         if (t)           m++, A_E(i, t), A_E(t, i), t = 0;         else           t = i;     if (m & 1) m++, A_E(1, 1), A_E(1, 1);     writeln(m);     dfs(1);     for (i = w; i > 0; i--)       if (!(i & 1))         printf( %d %d n , u[i], v[i]);       else         printf( %d %d n , v[i], u[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     int a[100005];     cin >> n;     int fa = 1;     for (int i = 1; i <= n; i++) {       cin >> a[i];       if (a[i] != i) {         fa = 0;       }     }     if (fa == 1) {       cout <<  0 ;     } else {       int ji = n - 1;       while (a[ji] < a[ji + 1]) {         ji--;       }       cout << ji;     }   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:65777216 )   using namespace std;   char s[1111][1111];   int n, m;   int best;   int now;   int v[1111][1111];   vector<pair<int, int> > c;   bool u[1111][1111];   void dfs(int x, int y) {     if (u[x][y]) return;     u[x][y] = true;     for (int dx(-1); dx < (2); dx++)       for (int dy(-1); dy < (2); dy++) {         int nx = dx + x;         int ny = dy + y;         if (nx >= 0 && nx < n && ny >= 0 && ny < m)           if (s[nx][ny] ==  0 )             dfs(nx, ny);           else             c.push_back(pair<int, int>(nx, ny));         else           c.push_back(pair<int, int>(-1, -1));       }   }   int startx, starty;   int dfs2(int x, int y, int prevx = -1, int prevy = -1) {     if (x == startx && y == starty && prevx != -1) return 0;     int res = 1;     for (int dx(-1); dx < (2); dx++)       for (int dy(-1); dy < (2); dy++)         if (abs(dx) + abs(dy) == 1) {           int nx = x + dx;           int ny = y + dy;           if (nx >= 0 && nx < n && ny >= 0 && ny < m && v[nx][ny] == v[x][y])             if (nx != prevx || ny != prevy) {               res += dfs2(nx, ny, x, y);               if (prevx == -1) return res;             }         }     return res;   }   int main() {     cin >> n >> m;     for (int i(0); i < (n); i++) scanf(  %s , s[i]);     for (int i(1); i < (n); i++)       for (int j(1); j < (m); j++)         if (s[i][j] ==  1  && s[i][j - 1] ==  1  && s[i - 1][j - 1] ==  1  &&             s[i - 1][j] ==  1 )           best = 4;     for (int i(0); i < (n); i++)       for (int j(0); j < (m); j++)         if (s[i][j] ==  0  && !u[i][j]) {           c.clear();           dfs(i, j);           now++;           bool good = 1;           int num = 0;           for (int k(0); k < (c.size()); k++)             if (c[k].first == -1) {               good = 0;               break;             } else if (v[c[k].first][c[k].second] != now)               v[c[k].first][c[k].second] = now, num++;           for (int k(0); k < (c.size()); k++) {             int num = 0;             for (int dx(-1); dx < (2); dx++)               for (int dy(-1); dy < (2); dy++)                 if (abs(dx) + abs(dy) == 1) {                   int nx = c[k].first + dx;                   int ny = c[k].second + dy;                   if (nx >= 0 && nx < n && ny >= 0 && ny < m && s[nx][ny] ==  1 )                     if (v[nx][ny] == now) num++;                 }             if (num != 2) {               good = 0;               break;             }           }           if (good && num > 4) {             startx = c[0].first;             starty = c[0].second;             if (dfs2(c[0].first, c[0].second) == num) best = max(best, num);           }         }     cout << best << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string l, r;     cin >> l >> r;     vector<stack<int>> l_array;     vector<stack<int>> r_array;     for (int i = 0; i < 27; i++) {       stack<int> buf;       l_array.push_back(buf);       r_array.push_back(buf);     }     for (int i = 0; i < n; i++) {       if (l[i] ==  ? ) {         l_array[26].push(i);       } else {         l_array[l[i] -  a ].push(i);       }       if (r[i] ==  ? ) {         r_array[26].push(i);       } else {         r_array[r[i] -  a ].push(i);       }     }     vector<pair<int, int>> answer;     for (int i = 0; i < 26; i++) {       while (!l_array[i].empty() && !r_array[i].empty()) {         answer.push_back(make_pair(l_array[i].top(), r_array[i].top()));         l_array[i].pop();         r_array[i].pop();       }       while (!l_array[i].empty() && !r_array[26].empty()) {         answer.push_back(make_pair(l_array[i].top(), r_array[26].top()));         l_array[i].pop();         r_array[26].pop();       }       while (!r_array[i].empty() && !l_array[26].empty()) {         answer.push_back(make_pair(l_array[26].top(), r_array[i].top()));         l_array[26].pop();         r_array[i].pop();       }     }     while (!l_array[26].empty() && !r_array[26].empty()) {       answer.push_back(make_pair(l_array[26].top(), r_array[26].top()));       l_array[26].pop();       r_array[26].pop();     }     int count = answer.size();     cout << count << endl;     for (int i = 0; i < count; i++) {       cout << answer[i].first + 1 <<     << answer[i].second + 1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, z, x, c, sum, a[101][101], us[101][101];   char cc, stl;   set<long long> s;   void dfs(long long i, long long j) {     if (a[i][j] == 0 || us[i][j] == 1 || i > n || j > m || i < 1 || j < 1) return;     us[i][j] = 1;     if (s.count(a[i][j]) == 0) sum++, s.insert(a[i][j]);     if (a[i][j] == stl -  A  + 1) {       dfs(i + 1, j);       dfs(i - 1, j);       dfs(i, j + 1);       dfs(i, j - 1);     }   }   int main() {     cin >> n >> m >> stl;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> cc;         if (cc !=  . ) a[i][j] = cc -  A  + 1;         if (cc == stl) z = i, x = j;       }     }     s.insert((int)stl -  A  + 1);     dfs(z, x);     cout << sum <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int okX[] = {0, 0, 0, 1, 1, 2, 2, 2};   const int okY[] = {0, 1, 2, 0, 2, 0, 1, 2};   const int n = 8;   void normalize(int a[n]) {     vector<int> v(a, a + n);     sort(v.begin(), v.end());     map<int, int> m;     for (int i = 0; i < v.size(); ++i)       if (!m.count(v[i])) m.insert(make_pair(v[i], m.size()));     for (int i = 0; i < 8; ++i) a[i] = m[a[i]];   }   int main() {     int x[n], y[n];     for (int i = 0; i < n; ++i) cin >> x[i] >> y[i];     normalize(x);     normalize(y);     bool used[n];     memset(used, false, sizeof(used));     for (int i = 0; i < 8; ++i) {       bool ok = false;       for (int j = 0; j < 8; ++j)         if (x[i] == okX[j] && y[i] == okY[j] && !used[j])           ok = true, used[j] = true;       if (!ok) {         printf( ugly n );         return 0;       }     }     printf( respectable n );     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma )   #pragma GCC optimize( unroll-loops )   using namespace std;   long long MOD9 = 998244353;   long long MOD = 1000000007;   double eps = 1e-12;   void solve() {     int n;     cin >> n;     vector<pair<int, int> > v(n);     for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;     sort((v).begin(), (v).end());     int p = 0, c = 0;     for (int i = 1; i < n; i++) {       if (v[i].second < v[p].second)         c++;       else         p = i;     }     cout << c <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     { solve(); }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, d1, d2;   bool d[400001];   int a[200001];   int nextprime(int x) {     for (int i = x + 1; i <= 2 * n; ++i)       if (d[i]) return i;     return 2 * n + 1000000;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     int x;     for (int i = 1; i <= n; ++i) {       cin >> x;       d1 += x == 1;       d2 += x == 2;     }     memset(d, sizeof(d), 0);     for (int i = 2; i <= 2 * n; ++i) {       bool f = 1;       for (int j = 2; j * j <= i; ++j)         if (i % j == 0) {           f = 0;           break;         }       d[i] = f;     }     int prime = 0;     int t = n;     while (n > 0) {       int tmp = nextprime(prime);       while (tmp - prime >= 2 && d2 > 0 && n > 0) prime += 2, d2--, a[n] = 2, n--;       while (tmp - prime >= 1 && d1 > 0 && n > 0) prime += 1, d1--, a[n] = 1, n--;       while (tmp - prime >= 1 && d1 == 0 && d2 > 0)         prime += 2, d2--, a[n] = 2, n--;     }     for (int i = t; i > 0; --i) cout << a[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   long long pwr(long long base, long long p, long long mod = (1000000007LL)) {     long long ans = 1;     while (p) {       if (p & 1) ans = (ans * base) % mod;       base = (base * base) % mod;       p /= 2;     }     return ans;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   const double eps = 1e-7;   struct PT {     double x, y;     PT() {}     PT(double x, double y) : x(x), y(y) {}     bool operator<(const PT &rhs) const {       return make_pair(y, x) < make_pair(rhs.y, rhs.x);     }     bool operator==(const PT &rhs) const {       return make_pair(y, x) == make_pair(rhs.y, rhs.x);     }   };   double cross(PT p, PT q) { return p.x * q.y - p.y * q.x; }   double area2(PT a, PT b, PT c) {     return cross(a, b) + cross(b, c) + cross(c, a);   }   void ConvexHull(vector<PT> &pts) {     sort(pts.begin(), pts.end());     pts.erase(unique(pts.begin(), pts.end()), pts.end());     vector<PT> up, dn;     for (int i = 0; i < pts.size(); i++) {       while (up.size() > 1 && area2(up[up.size() - 2], up.back(), pts[i]) > eps)         up.pop_back();       while (dn.size() > 1 && area2(dn[dn.size() - 2], dn.back(), pts[i]) < -eps)         dn.pop_back();       up.push_back(pts[i]);       dn.push_back(pts[i]);     }     pts = dn;     for (int i = (int)up.size() - 2; i >= 1; i--) pts.push_back(up[i]);   }   vector<PT> arr;   set<PT> banned;   int main() {     int n1, n2;     scanf( %d%d , &n1, &n2);     int X, Y;     scanf( %d%d , &X, &Y);     while (n1--) {       int x, y;       scanf( %d%d , &x, &y);       banned.insert(PT(x, y));       arr.push_back(PT(x, y));     }     int max_x = -1, max_y = -1;     while (n2--) {       int x, y;       scanf( %d%d , &x, &y);       arr.push_back(PT(x, y));       max_x = max(max_x, x);       max_y = max(max_y, y);     }     arr.push_back(PT(0, max_y));     arr.push_back(PT(max_x, 0));     arr.push_back(PT(0, 0));     ConvexHull(arr);     for (auto it : arr)       if (banned.find(it) != banned.end()) {         printf( Max );         return 0;       }     printf( Min );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000 + 10;   const int MAXM = 100 + 10;   const int MOD = 1000000007;   const int INF = 1000000001;   int a[20][120];   int l[20], r[20];   int f[20], g[20];   bool w[20];   int main() {     ios::sync_with_stdio(false);     int n, m;     cin >> n >> m;     int L = m + 1, R = 0;     for (int i = n; i >= 1; i--) {       l[i] = m + 1;       for (int j = 0; j <= m + 1; j++) {         char c;         cin >> c;         a[i][j] = c -  0 ;         w[i] |= a[i][j];         if (a[i][j]) {           r[i] = j;           if (l[i] == m + 1) l[i] = j;         }       }     }     while (n > 1 && !w[n]) n--;     for (int i = 0; i <= m + 1; i++)       if (a[n][i]) {         if (i < L) L = i;         if (i > R) R = i;       }     f[1] = r[1] * 2;     g[1] = m + 1;     f[0] = -1;     g[0] = 1000000000;     for (int i = 2; i <= n; i++) {       f[i] = g[i - 1] + m + 2;       g[i] = f[i - 1] + m + 2;       f[i] = min(f[i], f[i - 1] + 1 + r[i] * 2);       g[i] = min(g[i], g[i - 1] + 1 + (m + 1 - l[i]) * 2);     }     cout << min(f[n - 1] + 1 + R, g[n - 1] + 1 + m + 1 - L) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     short n;     int n1, n2;     bool state = false;     char handle[11];     cin >> n;     for (short i = 0; i < n; i++) {       cin >> handle;       cin >> n1;       cin >> n2;       if (n1 >= 2400 && n2 > n1) state = true;     }     if (state)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int graph[27];   int grapht[27];   vector<bool> used(27, false);   vector<bool> opened(27, false);   string result;   void dfs(int node) {     used[node] = true;     opened[node] = true;     if (graph[node] == -1) {       result += (node +  a );       opened[node] = false;       return;     }     if (opened[graph[node]]) {       cout <<  NO ;       exit(0);     }     dfs(graph[node]);     opened[node] = false;     result += (node +  a );   }   void dfsn(int node) {     opened[node] = true;     if (graph[node] != -1) {       if (opened[graph[node]]) {         cout <<  NO ;         exit(0);       }       dfsn(graph[node]);     }     opened[node] = false;   }   set<int> letter;   int main() {     int n;     cin >> n;     fill(graph, graph + 27, -1);     fill(grapht, grapht + 27, -1);     while (n--) {       string s;       cin >> s;       for (int i = 0; i < s.size() - 1; i++) {         int b = s[i] -  a ;         int e = s[i + 1] -  a ;         if ((graph[b] != -1 && graph[b] != e) ||             (grapht[e] != -1 && grapht[e] != b)) {           cout <<  NO ;           exit(0);         }         graph[b] = e;         grapht[e] = b;       }       for (auto i = s.begin(); i != s.end(); i++) {         letter.insert(*i -  a );       }     }     for (int i = 0; i < 27; i++) {       dfsn(i);     }     for (int i = 26; i >= 0; i--) {       if (letter.find(i) != letter.end() && grapht[i] == -1) {         dfs(i);       }     }     reverse(result.begin(), result.end());     cout << result;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<ll, ll>;   const ll MOD = 1e9 + 7, N = 1e5 + 10;   void test() {     ll n;     cin >> n;     vector<ll> a(n);     for (ll i = 0; i < n; i++) {       cin >> a[i];     }     vector<ll> ans;     sort(a.begin(), a.end());     ll l = 0, h = n - 1;     while (l < h) {       ans.push_back(a[l]);       ans.push_back(a[h]);       l++, h--;     }     if (l == h) ans.push_back(a[l]);     reverse(ans.begin(), ans.end());     for (ll i = 0; i < n; i++) {       cout << ans[i] <<    n [i == n - 1];     }     return;   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     ll tt = 1;     cin >> tt;     for (ll i = 0; i < tt; i++) test();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   const int N = 1e4 + 10;   int main() {     string str;     cin >> str;     cout << str[0];     int flag = 0;     for (int i = 1; i < str.length(); i++) {       if (!(i + 2 >= str.length() || flag == 1)) {         if (str.substr(i, 2) ==  at ) {           cout <<  @ ;           i++;           flag = 1;           continue;         }       }       if (i + 3 < str.length()) {         if (str.substr(i, 3) ==  dot ) {           cout <<  . ;           i += 2;           continue;         }       }       cout << str[i];     }     return 0;   }
#include<bits/stdc++.h>   #define mod 998244353   using namespace std;   int n, A[2], f[200010], pw2;   int powmod(int x, int y){    int ans = 1;    for(;y;y >>= 1, x = 1ll * x * x % mod)     if(y & 1)ans = 1ll * x * ans % mod;    return ans;   }   int main(){    scanf( %d , &n);    A[0] = 1;    A[1] = 0;    pw2 = powmod(2, mod - 2);    for(int i = 1; i <= n; i++){     A[0] = 1ll * A[0] * pw2 % mod;     A[1] = 1ll * A[1] * pw2 % mod;     f[i] = A[!(i & 1)];     A[i & 1] += f[i];    }    printf( %d n , f[n]);   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100;   int gi() {     int w = 0;     bool q = 1;     char c = getchar();     while ((c <  0  || c >  9 ) && c !=  - ) c = getchar();     if (c ==  - ) q = 0, c = getchar();     while (c >=  0  && c <=  9 ) w = w * 10 + c -  0 , c = getchar();     return q ? w : -w;   }   const int mod = 1e9 + 7;   int f[N][N], g[N][N];   int inv[N];   inline void inc(int &x, int y) { (x += y) >= mod ? x -= mod : 0; }   inline int C(int n, int m) {     n += m - 1;     int ans = 1;     for (int i = 1; i <= m; i++)       ans = 1LL * ans * (n - i + 1) % mod * inv[i] % mod;     return ans;   }   int main() {     int n = gi(), m = gi(), i, j, k, t, s, lim = n + 1;     for (inv[0] = inv[1] = 1, i = 2; i < N; i++)       inv[i] = mod - 1LL * (mod / i) * inv[mod % i] % mod;     f[0][1] = 1;     for (i = 0; i <= n; i++)       for (j = 0; j <= lim; j++) {         if (f[i][j])           for (k = 0; k <= i && k + i <= n; k++)             for (t = 0; t <= lim; t++) {               if (k == i)                 if (t < j)                   inc(g[i + k + 1][t], 2LL * f[i][j] * f[k][t] % mod);                 else if (t == j)                   inc(g[i + k + 1][j], 1LL * f[i][j] * f[k][t] % mod);                 else                   break;               else                 inc(g[i + k + 1][min(j, t)], 2LL * f[i][j] * f[k][t] % mod);             }         if (g[i][j])           for (k = n - i; k >= 0; k--)             for (t = lim - j; t >= 0; t--)               if (f[k][t])                 for (s = min((n - k) / i, (lim - t) / j); s; s--)                   inc(f[k + i * s][t + j * s],                       1LL * f[k][t] * C(g[i][j], s) % mod);       }     printf( %d n , f[n][m]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool compare(const pair<long long, long long> &a,                const pair<long long, long long> &b) {     return a.second > b.second;   }   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(nullptr);     long long i, j;     string s;     cin >> s;     long long n = s.length();     long long f = 1;     if (n < 4) {       cout << 0;     } else {       long long sm = 0;       long long fsm = 0;       for (i = 0; i < n - 3; i++) {         if (s[i] ==  b  && s[i + 1] ==  e  && s[i + 2] ==  a  &&             s[i + 3] ==  r ) {           sm = i + 1;         }         fsm += sm;       }       cout << fsm;     }   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     long long ar[n];     for (long long i = 0; i < n; i++) cin >> ar[i];     sort(ar, ar + n);     long long dist = 0;     for (long long i = 0; i < n; i++) dist += (long long)abs(ar[i] - (i + 1));     cout << dist <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long T = 1;     while (T--) solve();     return 0;   }
#include <cstdio>   #include <cstring>   #include <iostream>   #include <algorithm>      using namespace std;      const int N = 100000;      long long n;      struct ab   {    long long a;    long long b;    bool operator < (const struct ab &c) const    {     return b < c.b;    }   } aa[N + 5];   long long sus[N + 5];      int main()   {    scanf( %lld , &n);    long long su = 0;    for (int i = 1; i <= n; ++i)    {     scanf( %lld%lld , &aa[i].a, &aa[i].b);     su += aa[i].a;    }    sort(aa + 1, aa + n + 1);    sus[n + 1] = 0;    for (int i = n; i >= 1; --i)    {     sus[i] = sus[i + 1] + aa[i].a;    }    long long ans = 0;    long long p = 0, ext = 0;    for (int i = 1; i <= n; ++i)    {     long long cc = aa[i].b - (p + ext);     if (sus[i + 1] <= ext)     {      if (aa[i].b <= p + ext)      {       ans += aa[i].a - (ext - sus[i + 1]);      }      else if (aa[i].b <= p + ext + aa[i].a - (ext - sus[i + 1]))      {       ans += p + ext + aa[i].a - (ext - sus[i + 1]) - aa[i].b;       ans += 2 * cc;      }      else      {       ans += 2 * (aa[i].a - (ext - sus[i + 1]));      }      break;     }     else if (aa[i].b <= p + ext)     {      ans += aa[i].a;      p += aa[i].a;     }     else if (sus[i + 1] - ext >= cc)     {      ans += 2 * cc;      ext += cc;      ans += aa[i].a;      p += aa[i].a;     }     else     {      ans += 2 * (sus[i + 1] - ext);      cc -= sus[i + 1] - ext;      if (cc >= aa[i].a)      {       ans += 2 * aa[i].a;      }      else      {       ans += 2 * cc + (aa[i].a - cc);      }      break;     }    }       // for (int i = 1; i <= n; ++i)   // {   //  if (aa[i].b >= su)   //  {   //   ans += 2 * (su - p - ext);   //   break;   //  }   //  else if (aa[i].b <= p + ext)   //  {   //   ans += aa[i].a;   //   p += aa[i].a;   //  }   //  else if (sus[i + 1] >= aa[i].b - p)   //  {   //   ans += 2 * (aa[i].b - p - ext);   //   ext += aa[i].b - p - ext;   //   ans += aa[i].a;   //   p += aa[i].a;   //  }   //  else if (sus[i + 1] < aa[i].b - p - ext)   //  {   //   ans +=    //  }   // }        printf( %lld , ans);    return 0;   }