malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       vector<long long> v(n);       for (long long i = 0; i < n; i++) cin >> v[i];       vector<long long> peaks(n, 0);       for (long long i = 1; i < n - 1; i++) {         if (v[i] > v[i - 1] && v[i] > v[i + 1]) {           peaks[i] = 1;         }       }       for (long long i = 1; i < n; i++) {         peaks[i] = peaks[i] + peaks[i - 1];       }       long long ans = 0;       long long pos = 0;       for (long long l = 0, r = k - 1; r < n; l++, r++) {         if (peaks[r - 1] - peaks[l] > ans) {           ans = peaks[r - 1] - peaks[l];           pos = l;         }       }       cout << ans + 1 <<     << pos + 1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int size = 508;   int num[size];   int Abs(int x) { return x > 0 ? x : -x; }   int main() {     int n, sum, m, i, x, L, R, c, t, v, up;     cin >> n;     sum = m = 0;     up = 1000000;     for (i = 1; i <= 2 * n - 1; i++) {       cin >> num[i];       if (num[i] < 0) m++;       sum += Abs(num[i]);       if (up > Abs(num[i])) up = Abs(num[i]);     }     if (n & 1) {       cout << sum << endl;       return 0;     }     if (m & 1) {       cout << sum - 2 * up << endl;     } else       cout << sum << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     double a, d;     int n;     cin >> a >> d >> n;     scanf( %lf%lf%d , &a, &d, &n);     for (int i = 0; i < n; i++) {       double temp = fmod((i + 1) * d, 4 * a);       if (temp < a)         printf( %lf %lf n , temp, 0.0);       else if (temp < a * 2)         printf( %lf %lf n , a, temp - a);       else if (temp < a * 3)         printf( %lf %lf n , 3 * a - temp, a);       else         printf( %lf %lf n , 0.0, 4 * a - temp);     }     return 0;   }
#include <bits/stdc++.h>   const int mxn = 105;   const int inf = 505;   int n;   int m;   int ans;   int row[mxn];   int col[mxn];   int mat[mxn][mxn];   inline void calcRow() {     for (int i = 1; i <= n; ++i) {       row[i] = inf;       for (int j = 1; j <= m; ++j)         if (row[i] > mat[i][j]) row[i] = mat[i][j];       for (int j = 1; j <= m; ++j) mat[i][j] -= row[i];       ans += row[i];     }   }   inline void calcCol() {     for (int j = 1; j <= m; ++j) {       col[j] = inf;       for (int i = 1; i <= n; ++i)         if (col[j] > mat[i][j]) col[j] = mat[i][j];       for (int i = 1; i <= n; ++i) mat[i][j] -= col[j];       ans += col[j];     }   }   signed main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) scanf( %d , &mat[i][j]);     if (n <= m)       calcRow(), calcCol();     else       calcCol(), calcRow();     bool flag = true;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         if (mat[i][j]) flag = false;     if (flag) {       printf( %d n , ans);       for (int i = 1; i <= n; ++i)         for (int j = 1; j <= row[i]; ++j) printf( row %d n , i);       for (int j = 1; j <= m; ++j)         for (int i = 1; i <= col[j]; ++i) printf( col %d n , j);     } else       puts( -1 );   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1;   const long long MOD = 1;   long long _gcd(long long a, long long b) {     if (b == 0) return a;     return _gcd(b, a % b);   }   long long gcd(long long a, long long b) {     a = abs(a), b = abs(b);     if (a < b) swap(a, b);     return _gcd(a, b);   }   long long qpow(long long a, long long n) {     long long rt = 1;     while (n) {       if (n & 1) rt = (rt * a) % MOD;       a = a * a % MOD;       n >>= 1;     }     return rt;   }   long long factor[MAXN];   long long inv[MAXN];   void cal_factor() {     factor[0] = 1;     for (int u = 1; u < MAXN; u++) {       factor[u] = (factor[u - 1] * u) % MOD;     }   }   void cal_factor_inv() {     inv[0] = 1;     inv[MAXN - 1] = qpow(factor[MAXN - 1], MOD - 2);     for (int u = MAXN - 2; u >= 1; u--) {       inv[u] = inv[u + 1] * (u + 1) % MOD;     }   }   void init_factor() {     cal_factor();     cal_factor_inv();   }   long long C(long long n, long long k) {     if (n < k) return 0;     return factor[n] * qpow(factor[n - k], MOD - 2) % MOD *            qpow(factor[k], MOD - 2) % MOD;   }   void solve() {     int a[6];     for (int u = 0; u < 6; u++) cin >> a[u];     int n;     cin >> n;     vector<pair<int, int>> b;     for (int u = 0; u < n; u++) {       int tb;       cin >> tb;       for (int k = 0; k < 6; k++) {         b.push_back({tb - a[k], u});       }     }     sort(b.begin(), b.end());     int ptr1 = 0, ptr2 = 0;     vector<int> vis(n, 0);     int cnt = 0;     int ans = 2e9;     for (; ptr2 < n * 6; ptr2++) {       if (vis[b[ptr2].second] == 0) cnt++;       vis[b[ptr2].second]++;       while (cnt == n && ptr1 <= ptr2 && vis[b[ptr1].second]) {         ans = min(ans, b[ptr2].first - b[ptr1].first);         vis[b[ptr1].second]--;         if (vis[b[ptr1].second] == 0) cnt--;         ptr1++;       }     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     solve();   }
#include <bits/stdc++.h>   struct point {     double x, y;   } p, q, u;   double v, vp, r, R, k;   double po, qo, pa, qa, angp, angq, angpq;   point rorate(point a, double ang) {     point c;     c.x = a.x * cos(ang) - a.y * sin(ang);     c.y = a.x * sin(ang) + a.y * cos(ang);     return c;   }   double disp2o(point a) { return a.x * a.x + a.y * a.y; }   double cal(double pq) {     if (pq * pq < po || pq * pq < qo) return pq;     angpq = acos((po + qo - (pq * pq)) / (2 * sqrt(po * qo)));     return k + (angpq - angp - angq) * r;   }   int bi(double t) {     double d, dis, ang;     ang = t * vp / R;     q = rorate(u, ang);     dis = sqrt((p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y));     d = fabs(p.x * q.y - p.y * q.x) / dis;     if (d < r) dis = cal(dis);     if (dis > v * t + 1e-8) return 1;     if (dis < v * t - 1e-8) return -1;     return 0;   }   int main() {     double lt, rt, mid;     int t;     while (~scanf( %lf%lf%lf , &u.x, &u.y, &vp)) {       scanf( %lf%lf%lf%lf , &p.x, &p.y, &v, &r);       qo = disp2o(u);       R = sqrt(qo);       po = disp2o(p);       angp = acos(r / sqrt(po));       angq = acos(r / R);       k = sqrt(po - (r * r)) + sqrt(qo - (r * r));       lt = 0;       rt = 1e4;       while (rt > lt + 1e-8) {         mid = (rt + lt) / 2;         t = bi(mid);         if (t == 1)           lt = mid;         else if (t == -1)           rt = mid;         else           break;       }       printf( %.10lf n , mid);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m;   void in() { cin >> n >> m; }   void out() {     int s = 0;     if (n > m) {       cout << n - m;       return;     }     while (n < m) {       s++;       n <<= 1;     }     int diff = n - m;     n >>= 1;     long long x = 2;     long long c = 0;     while (x <= diff && c < s) {       c++;       x *= 2;     }     x /= 2;     while (diff > 0) {       diff -= x;       s++;       while (diff < x) {         x /= 2;       }     }     cout << s;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;     while (t--) {       in();       out();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e6 + 5;   const int K = 20;   vector<int> P[N];   int s[K];   int t[K];   int L[1 << K];   int R[1 << K];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int n, m, k;     cin >> n >> m >> k;     char f;     int c0 = 0, c1 = 0;     for (int i = 0; i < k; i++) {       cin >> f;       s[i] = f -  0 ;       c0 += s[i];     }     for (int i = 0; i < k; i++) {       cin >> f;       t[i] = f -  0 ;       c1 += t[i];     }     for (int i = 0; i <= n; i++) {       for (int j = 0; j < k; j++) P[i].push_back(j);     }     int l, r;     for (int i = 1; i <= n; i++) {       cin >> l >> r;       l--;       r--;       P[i][l] = r;       P[i][r] = l;       vector<int> nw;       for (auto x : P[i]) {         nw.push_back(P[i - 1][x]);       }       P[i] = nw;     }     for (int i = 0; i < (1 << k); i++) {       L[i] = (int)1e9;       R[i] = -1;     }     int mask;     for (int i = 0; i <= n - 1; i++) {       mask = 0;       for (int j = 0; j < k; j++) {         if (s[j]) mask |= (1 << P[i][j]);       }       L[mask] = min(L[mask], i);     }     for (int i = 1; i <= n; i++) {       mask = 0;       for (int j = 0; j < k; j++) {         if (t[j]) mask |= (1 << P[i][j]);       }       R[mask] = max(R[mask], i);     }     int nex;     for (int i = (1 << k) - 1; i >= 0; i--) {       for (int j = 0; j < k; j++) {         if ((i & (1 << j))) continue;         nex = (i | (1 << j));         L[i] = min(L[i], L[nex]);         R[i] = max(R[i], R[nex]);       }     }     int mx = 0;     int fl = 0, fr = n;     for (int i = 0; i < (1 << k); i++) {       if (L[i] == (int)1e9 || R[i] == -1) continue;       if (R[i] - L[i] >= m) {         if (__builtin_popcount(i) > mx) {           mx = __builtin_popcount(i);           fl = L[i], fr = R[i];         }       }     }     cout << k + 2 * mx - c0 - c1 <<   n ;     cout << fl + 1 <<     << fr <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 123, INF = 1e9, mod = 1e9 + 7, N = 2e5 + 123;   struct btr {     map<int, long long> dp;     set<pair<long long, int> > st;     long long mn, add;   };   vector<int> g[maxn];   int n, m;   btr a[maxn];   void Merge(btr &a, btr &b) {     if (a.dp.size() < b.dp.size()) swap(a, b);     long long x = a.add, y = b.add;     a.add += b.mn + y;     b.add += a.mn + x;     for (auto x : b.dp) {       int v = x.first;       long long c = x.second + b.add - a.add;       if (!a.dp.count(v) || a.dp[v] > c) {         a.st.erase({a.dp[v], v});         a.dp[v] = c;         a.st.insert({a.dp[v], v});       }     }     if (!a.st.empty()) a.mn = a.st.begin()->first;   }   void dfs(int v, int p) {     if (!a[v].st.empty()) a[v].mn = a[v].st.begin()->first;     btr tmp;     bool flag = 0;     for (auto to : g[v]) {       if (to == p) continue;       dfs(to, v);       if (!flag)         swap(tmp, a[to]);       else         Merge(tmp, a[to]);       flag = 1;       a[to].dp.clear();       a[to].st.clear();     }     if (flag) {       for (auto x : a[v].dp) {         int u = x.first;         long long c = x.second + tmp.mn;         if (!tmp.dp.count(u) || tmp.dp[u] > c) {           tmp.st.erase({tmp.dp[u], u});           tmp.dp[u] = c;           tmp.st.insert({tmp.dp[u], u});         }       }       swap(a[v], tmp);     }     if (v != 1 && a[v].dp.count(v)) {       a[v].st.erase({a[v].dp[v], v});       a[v].dp.erase(v);       if (!a[v].st.empty()) a[v].mn = a[v].st.begin()->first;     }     if (a[v].st.empty()) {       cerr << v << endl;       printf( -1 );       exit(0);     }   }   int main() {     scanf( %d%d , &n, &m);     if (n == 1) {       printf( 0 );       exit(0);     }     for (int i = 1; i < n; i++) {       int v, u;       scanf( %d%d , &v, &u);       g[v].push_back(u);       g[u].push_back(v);     }     for (int i = 0; i < m; i++) {       int u, v, c;       scanf( %d%d%d , &v, &u, &c);       if (!a[v].dp.count(u) || a[v].dp[u] > c) {         a[v].st.erase({a[v].dp[u], u});         a[v].dp[u] = c;         a[v].st.insert({a[v].dp[u], u});       }     }     dfs(1, 1);     printf( %lld , 0ll + a[1].mn + a[1].add);   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T &x) {     x = 0;     char ch = getchar();     int f = 1;     while (!isdigit(ch)) {       if (ch ==  - ) f *= -1;       ch = getchar();     };     while (isdigit(ch)) {       x = x * 10 + ch - 48;       ch = getchar();     }     x *= f;   }   int n;   vector<int> tv;   int lst[1310000][26];   bool vis[1310000];   int main() {     read(n);     int tsu = 0;     int cu;     for (int i = 0; i < n * 2; i++) {       read(cu);       tsu += cu;       tv.push_back(cu);     }     sort(tv.begin(), tv.end());     int ans = 1e9, rans[30][2];     int csz = 0;     for (int j = n * 2 - 1; j >= n + 1; j--) csz += tv[j];     for (int j = 0; j <= csz; j++) memset(lst[j], 0, sizeof(lst[j]));     memset(vis, 0, sizeof(vis));     lst[0][0] = 99999;     vis[0] = 1;     int used[100] = {0};     for (int i = 1; i >= 1; i--) {       int sua = 0, sub = 0;       for (int j = 0; j < i; j++) sua += tv[j];       sub += tv[i];       for (int j = n * 2 - 1; j >= i + 1; j--) {         if (used[j]) continue;         used[j] = 1;         int cv = tv[j];         for (int k = csz; k >= cv; k--) {           if (!vis[k - cv]) continue;           for (int l = n; l >= 1; l--) {             if (lst[k - cv][l - 1] != 0 && lst[k][l] == 0) {               lst[k][l] = j;               vis[k] = 1;             }           }         }       }       int cans = 1e9, cbst = 0;       ;       for (int j = 0; j <= csz; j++) {         if (lst[j][n - i]) {           if (max(j + sua + sub, tsu - sua - sub - j + sub + tv[0]) < cans) {             cans = max(j + sua + sub, tsu - sua - sub - j + sub + tv[0]);             cbst = j;           }         }       }       if (cans < ans) {         vector<int> ntv = tv;         ans = cans;         for (int j = 0; j < i; j++) {           rans[j][0] = ntv[j];           ntv[j] = -1;         }         int cp = cbst, cn = n - i;         for (int j = i; j < n; j++) {           rans[j][0] = ntv[lst[cp][cn]];           ntv[lst[cp][cn]] = -1;           cp -= rans[j][0];           cn--;         }         int p = n - 1;         for (int j = 0; j < 2 * n; j++) {           if (ntv[j] != -1) {             rans[p][1] = ntv[j];             p--;           }         }       }     }     for (int i = 0; i < n; i++) cout << rans[i][0] <<    ;     cout << endl;     for (int i = 0; i < n; i++) cout << rans[i][1] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   int n;   double p;   double C2(double x) {     if (x < 1) return 0;     return x * (x - 1) / 2;   }   double C3(double x) {     if (x < 2) return 0;     return x * (x - 1) * (x - 2) / 6;   }   int main() {     std::cin >> n >> p;     for (int i = 0; i <= n; i++)       if ([&](int x) {             double an = (double)C2(n - x) * x / C3(n) * (1.0 / 3 + 1.0 / 6) +                         (double)(n - x) * C2(x) / C3(n) + (double)C3(x) / C3(n);             return an >= p;           }(i))         return std::cout << i, 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, st, ed, i, j, ep = 1, h[1005];   long long d[1005][1005], dis[1005];   queue<int> q;   struct node {     int y, ne, v;   } e[1000005];   void add(int a, int b, int v) {     e[ep].y = b;     e[ep].v = v;     e[ep].ne = h[a];     h[a] = ep;     ep++;   }   int main() {     scanf( %d%d , &n, &m);     scanf( %d%d , &st, &ed);     for (i = 1; i <= m; i++) {       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       add(a, b, c);       add(b, a, c);     }     memset(d, 0x3f, sizeof(d));     for (i = 1; i <= n; i++) {       d[i][i] = 0;       q.push(i);       while (!q.empty()) {         int s = q.front();         q.pop();         for (j = h[s]; j; j = e[j].ne) {           if (d[i][s] + e[j].v < d[i][e[j].y]) {             d[i][e[j].y] = d[i][s] + e[j].v;             q.push(e[j].y);           }         }       }     }     memset(h, 0, sizeof(h));     ep = 1;     for (i = 1; i <= n; i++) {       int a, b;       scanf( %d%d , &a, &b);       for (j = 1; j <= n; j++)         if (d[i][j] <= a && i != j) add(i, j, b);     }     memset(dis, 0x3f, sizeof(dis));     dis[st] = 0;     q.push(st);     while (!q.empty()) {       int s = q.front();       q.pop();       for (i = h[s]; i; i = e[i].ne) {         if (dis[s] + e[i].v < dis[e[i].y]) {           dis[e[i].y] = dis[s] + e[i].v;           q.push(e[i].y);         }       }     }     if (dis[ed] < (1LL << 60))       printf( %I64d n , dis[ed]);     else       puts( -1 );   }
#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;   }   int flag1 = 0, flag2 = 0, flag3 = 0, ff3, flag4 = 0;   int n, a[200000 + 5], x, y;   map<int, bool> mp;   int main() {     n = read();     read();     x = read();     y = read();     for (int i = 1; i <= n; ++i) mp[a[i] = read()] = 1;     for (int i = 1; i <= n; ++i) {       if (mp[a[i] - x]) flag1 = i;       if (mp[a[i] - y]) flag2 = i;       if (mp[a[i] - y + x] && (a[i] - y >= 0 || a[i] + x <= a[n]))         flag3 = i, ff3 = (a[i] - y >= 0) ? a[i] - y : a[i] + x;       ;       if (mp[a[i] - y - x]) flag4 = i;     }     if (flag1 && flag2)       puts( 0 );     else if (flag1 | flag2)       printf( 1 n%d , flag1 ? y : x);     else if (flag3)       printf( 1 n%d , ff3);     else if (flag4)       printf( 1 n%d , a[flag4] - x);     else       printf( 2 n%d %d , x, y);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int r, d;     cin >> r >> d;     int n;     cin >> n;     d = r - d;     int ans = 0;     for (int i = 0; i < n; i++) {       int x, y, rx;       cin >> x >> y >> rx;       long double distance = sqrt(x * x + y * y);       long double temp = (rx);       if (distance - d - temp >= 0) {         if (r - distance - temp >= 0) ans++;       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using lint = long long int;   lint liters(int l, vector<int> &arr) {     lint sum = 0;     for (int k : arr) sum += (lint)(k - l);     return sum;   }   int main() {     int n, m = 1000000010;     lint s;     vector<int> arr;     cin >> n >> s;     arr.resize(n);     for (int i = 0; i < n; i++) {       cin >> arr[i];       m = min(m, arr[i]);     }     if (s > liters(0, arr))       cout << -1 << endl;     else {       int start = 0, end = m;       while (start < end) {         int mid = (start + end) / 2 + 1;         if (s <= liters(mid, arr))           start = mid;         else           end = mid - 1;       }       cout << start << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long bin_pow(long long a, long long b) {     if (b == 0) return 1;     if (b % 2 == 0) {       long long t = bin_pow(a, b / 2);       return t * t % 1000000007;     } else       return a * bin_pow(a, b - 1) % 1000000007;   }   long long t = 1, n, m, k = 0, sum = 0, l = 0, r = 0, x = 0, y = 0, z = 0,             ans = 0, mn = LLONG_MAX, mx = LLONG_MIN;   struct cat {     long long a, b, c;   };   vector<cat> edges;   vector<long long> graph[100000];   long long z1 = 1, color[100000];   void dfs(long long v) {     color[v] = 1;     for (auto u : graph[v]) {       if (color[u] == 1) {         z1 = 0;         return;       }       if (color[u] == 0) dfs(u);     }     color[v] = 2;   }   bool tr(long long x) {     z1 = 1;     for (int i = 0; i < n; i++) graph[i].clear(), color[i] = 0;     for (int i = 0; i < m; i++) {       if (edges[i].c <= x) break;       long long x1 = edges[i].a, y1 = edges[i].b;       graph[x1].push_back(y1);     }     for (int i = 0; i < n; i++) {       if (color[i] == 0) dfs(i);     }     return z1;   }   bool cmp(cat a, cat b) { return a.c > b.c; }   vector<long long> order;   bool used[100000];   void dfs1(long long v) {     used[v] = true;     for (auto u : graph[v]) {       if (used[u]) continue;       dfs1(u);     }     order.push_back(v);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     string s, s1;     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> x >> y >> z;       x--, y--;       cat f;       f.a = x, f.b = y, f.c = z;       edges.push_back(f);     }     vector<cat> c = edges;     ans = -1;     sort(edges.begin(), edges.end(), cmp);     for (long long b = 1e9; b >= 1; b /= 2) {       while (!tr(ans + b)) ans += b;     }     ans++;     cout << ans <<    ;     vector<long long> d;     for (int i = 0; i < n; i++) {       graph[i].clear();     }     for (int i = 0; i < m; i++) {       if (c[i].c <= ans) continue;       graph[c[i].a].push_back(c[i].b);     }     for (int i = 0; i < n; i++) {       if (used[i]) continue;       dfs1(i);     }     reverse(order.begin(), order.end());     vector<long long> pos(n);     for (int i = 0; i < n; i++) {       pos[order[i]] = i;     }     for (int i = 0; i < m; i++) {       if (c[i].c <= ans) {         if (pos[c[i].a] <= pos[c[i].b])           ;         else           d.push_back(i);       }     }     cout << d.size() <<   n ;     for (auto u : d) cout << u + 1 <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 1;   int n, x[N];   char c[N];   vector<pair<int, int>> segs;   long long ans;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n;     int last = -1;     for (int i = 0; i < n; i++) {       cin >> x[i] >> c[i];       if (c[i] ==  G ) {         segs.emplace_back(last, i);         last = i;       }     }     segs.emplace_back(last, n);     for (auto p : segs) {       int l, r;       tie(l, r) = p;       int res = 0;       vector<int> rs, bs;       for (int i = l + 1; i < r; i++) {         if (c[i] ==  R )           rs.push_back(i);         else           bs.push_back(i);       }       if (l != -1 && r != n) {         res = x[r] - x[l];         if (!rs.empty()) {           int rsub = max(x[rs.front()] - x[l], x[r] - x[rs.back()]);           for (int i = 1; i < rs.size(); i++)             rsub = max(x[rs[i]] - x[rs[i - 1]], rsub);           res += x[r] - x[l] - rsub;         }         if (!bs.empty()) {           int bsub = max(x[bs.front()] - x[l], x[r] - x[bs.back()]);           for (int i = 1; i < bs.size(); i++)             bsub = max(x[bs[i]] - x[bs[i - 1]], bsub);           res += x[r] - x[l] - bsub;         }         if (!rs.empty() && !bs.empty()) res = min(2 * (x[r] - x[l]), res);       } else {         if (l != -1) {           if (!rs.empty()) res += x[rs.front()] - x[l];           if (!bs.empty()) res += x[bs.front()] - x[l];         }         if (r != n) {           if (!rs.empty()) res += x[r] - x[rs.back()];           if (!bs.empty()) res += x[r] - x[bs.back()];         }         res += (!rs.empty() ? x[rs.back()] - x[rs.front()] : 0) +                (!bs.empty() ? x[bs.back()] - x[bs.front()] : 0);       }       ans += res;     }     cout << ans;   }