malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   inline int in();   inline void wr(int);   inline int gcd(int, int);   int main(int argc, char** argv) {     register int T = in();     while (T--) {       register int p1 = in(), p2 = in(), k = in();       if (p1 > p2) p1 ^= p2 ^= p1 ^= p2;       if (k == 1) {         puts( REBEL );         continue;       }       register int g = gcd(p1, p2);       p1 /= g, p2 /= g;       if ((k - 1) * 1ll * p1 + 1 < p2)         puts( REBEL );       else         puts( OBEY );     }   }   inline int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }   inline int in() {     register char c = getchar();     register int x = 0, f = 1;     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) f = -1;     for (; c >=  0  && c <=  9 ; c = getchar())       x = (x << 1) + (x << 3) + (c & 15);     return x * f;   }   inline void wr(int x) {     if (x < 0) putchar( - ), x = -x;     if (x / 10) wr(x / 10);     putchar(x % 10 +  0 );   }
#include <bits/stdc++.h>   using namespace std;   struct rua {     char s[100001];     int n, a[100001], b[100001], x, y;     void _() {       int cnt = 0;       for (int i = 1; i <= n; i++)         if (s[i] ==  A )           a[i] = ++cnt, b[i] = b[i - 1];         else           a[i] = cnt = 0, b[i] = b[i - 1] + 1;     }     void get(int l, int r) {       x = b[r] - b[l - 1];       if (!x)         y = r - l + 1;       else         y = a[r];     }   } s, t;   int Q, a, b, c, d;   int main() {     scanf( %s , s.s + 1), s.n = strlen(s.s + 1);     scanf( %s , t.s + 1), t.n = strlen(t.s + 1);     s._(), t._();     scanf( %d , &Q);     while (Q--) {       scanf( %d%d%d%d , &a, &b, &c, &d);       s.get(a, b), t.get(c, d);       if (!s.x && t.x)         if (s.y > t.y)           s.x += 2, s.y = t.y;         else           s.y--;       if (s.y < t.y) {         printf( 0 );         continue;       }       if ((s.y - t.y) % 3) s.x += 2;       if (s.x > t.x || (t.x - s.x) & 1) {         printf( 0 );         continue;       }       printf( 1 );     }     return printf(  n ), 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   vector<pair<int, int> > adj[100005];   int dp[300005];   int x[300005];   int main() {     ios_base::sync_with_stdio(0);     int i, j;     cin >> n >> m;     while (m--) {       int u, v, e;       cin >> u >> v >> e;       adj[e].push_back(make_pair(u, v));     }     for (i = 1; i <= 1e5; i++) {       for (j = 0; j < adj[i].size(); j++) {         int u = adj[i][j].second;         x[u] = 0;       }       for (j = 0; j < adj[i].size(); j++) {         int u = adj[i][j].first;         int v = adj[i][j].second;         x[v] = max(x[v], dp[u] + 1);       }       for (j = 0; j < adj[i].size(); j++) {         int u = adj[i][j].second;         dp[u] = max(dp[u], x[u]);       }     }     int res = 0;     for (i = 1; i <= n; i++) res = max(res, dp[i]);     cout << res;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, c, i, l;     cin >> n;     int ar[n];     l = 0;     for (i = 0; i < n; i++) {       cin >> ar[i];       if (ar[i] == 1) l++;     }     c = 0;     if (l == n) {       cout << c << endl;       exit(0);     }     for (i = 1; i < n - 1; i++) {       if (ar[i - 1] == 1 && ar[i + 1] == 1 && ar[i] == 0) {         c++;         ar[i + 1] = 0;       }     }     cout << c << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char a[6][6];   char tl[2] = { x ,  o };   int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};   int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};   bool judge(int x, int y, int nu) {     for (int i = 0; i < 8; i++) {       int xx = x + dx[i];       int yy = y + dy[i];       if (a[xx][yy] == tl[nu]) {         xx = x - dx[i];         yy = y - dy[i];         if (a[xx][yy] ==  . ) return true;       }       if (a[xx][yy] ==  . ) {         xx = xx + dx[i];         yy = yy + dy[i];         if (a[xx][yy] == tl[nu]) return true;       }     }     return false;   }   void solve() {     int num1 = 0, num2 = 0;     for (int i = 1; i <= 4; i++)       for (int j = 1; j <= 4; j++) {         cin >> a[i][j];         if (a[i][j] ==  x ) num1++;         if (a[i][j] ==  o ) num2++;       }     for (int i = 0; i < 6; i++) a[i][0] = a[0][i] = a[6][i] = a[i][6] =  * ;     int be = 0;     if (num2 < num1) be = 1;     for (int i = 1; i <= 4; i++)       for (int j = 1; j <= 4; j++) {         if (a[i][j] == tl[be] && judge(i, j, be)) {           cout <<  YES  << endl;           return;         }       }     cout <<  NO  << endl;     return;   }   int main() {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> arr[200010];   int a, b, ans = 1;   int main() {     pair<int, int> arr[200010];     int n;     cin >> n;     for (int i = 0; i < n; i++)       cin >> a >> b, arr[i].first = a + b, arr[i].second = a - b;     sort(arr, arr + n);     int tmp = arr[0].first;     for (int i = 1; i < n; i++)       if (arr[i].second >= tmp) ans++, tmp = arr[i].first;     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int a[210000][3];   set<pair<int, int> > st;   int nxt[210000][2];   int x[210000];   vector<pair<int, int> > re;   map<long long, int> mp;   int pp[210000];   long long hsh(int x, int y, int z) {     if (y > z) swap(y, z);     if (x > y) swap(x, y);     if (y > z) swap(y, z);     return ((long long)x << 40) + ((long long)y << 20) + z;   }   void init() {     cin >> n;     mp.clear();     st.clear();     re.clear();     for (int i = 0; i < n + 2; i++) nxt[i][0] = nxt[i][1] = 0;     pair<int, int> tmp;     for (int i = 0; i < n - 2; i++) {       scanf( %d%d%d , &a[i][0], &a[i][1], &a[i][2]);       mp[hsh(a[i][0], a[i][1], a[i][2])] = i + 1;       for (int j = 0; j < 3; j++) {         tmp = {a[i][j], a[i][(j + 1) % 3]};         if (tmp.first > tmp.second) swap(tmp.first, tmp.second);         if (st.count(tmp)) {           st.erase(tmp);           re.push_back(tmp);         } else           st.insert(tmp);       }     }     for (auto p : st) {       int x = p.first;       int y = p.second;       if (nxt[x][0])         nxt[x][1] = y;       else         nxt[x][0] = y;       if (nxt[y][0])         nxt[y][1] = x;       else         nxt[y][0] = x;     }     int pos = 1, lst = nxt[pos][0];     int cnt = 1;     while (1) {       printf( %d  , pos);       pp[pos] = cnt;       x[cnt++] = pos;       if (nxt[pos][0] == lst)         lst = pos, pos = nxt[pos][1];       else         lst = pos, pos = nxt[pos][0];       if (pos == 1) break;     }     cout << endl;     for (int i = 1; i <= n; i++) {       nxt[x[i]][0] = x[i - 1];       nxt[x[i]][1] = x[i + 1];     }     nxt[x[1]][0] = x[n];     nxt[x[n]][1] = x[1];   }   bool cmp(pair<int, int> p1, pair<int, int> p2) {     int x1 = pp[p1.second] - pp[p1.first];     int x2 = pp[p2.second] - pp[p2.first];     if (x1 < 0) x1 += n;     if (x2 < 0) x2 += n;     if (x1 > n / 2) x1 = n - x1;     if (x2 > n / 2) x2 = n - x2;     return x1 < x2;   }   bool visit[210000];   void work() {     for (int i = 1; i <= n - 2; i++) visit[i] = 1;     sort(re.begin(), re.end(), cmp);     int t;     for (auto p : re) {       int x = p.first;       int y = p.second;       int t;       if (nxt[x][0] == nxt[y][1]) {         t = nxt[x][0];         nxt[x][0] = y;         nxt[y][1] = x;       } else {         t = nxt[x][1];         nxt[x][1] = y;         nxt[y][0] = x;       }       int xx = mp[hsh(x, y, t)];       cout << xx <<    ;       visit[xx] = 0;     }     for (int i = 1; i <= n - 2; i++)       if (visit[i]) cout << i;     cout << endl;   }   int main() {     int t;     cin >> t;     while (t--) {       init();       work();     }     return ~~(0 ^ 0 ^ 0);   }
#include <bits/stdc++.h>   template <class T>   const T& min(const T& a, const T& b, const T& c) {     return min(a, min(b, c));   }   template <class T>   const T& max(const T& a, const T& b, const T& c) {     return max(a, max(b, c));   }   using namespace std;   int gcd(long long a, long long b) {     while (true) {       if (a < b) swap(a, b);       if (b == 0) return a;       a = a % b;     }   }   int main() {     int n, k;     cin >> n >> k;     if (k > n) {       cout <<  -1 ;       return 0;     }     if (k == 1) {       if (n == 1)         cout <<  a ;       else         cout <<  -1 ;       return 0;     }     if (k == 2) {       for (__typeof(n) i = 0; i < n; i++) {         cout << (((i % 2) == 0) ?  a  :  b );       }       return 0;     }     int written = 0;     for (__typeof(n - (k - 2)) i = 0; i < n - (k - 2); i++) {       cout << (((i % 2) == 0) ?  a  :  b );     }     for (__typeof(k - 2) i = 0; i < k - 2; i++) {       char c =  c  + i;       cout << c;     }   }   template <class T>   string toStr(T t) {     std::stringstream strstream;     string resStr;     strstream << t;     strstream >> resStr;     return resStr;   }
#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0, f = 0;     char ch = getchar();     while (!isdigit(ch)) f |= ch ==  - , ch = getchar();     while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^  0 ), ch = getchar();     return f ? -x : x;   }   inline void write(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) write(x / 10);     putchar(x % 10 + 48);   }   inline void writebl(long long x) {     write(x);     putchar(   );   }   inline void writeln(long long x) {     write(x);     putchar(  n );   }   const long long N = 4e6 + 5;   long long ch[N][3], cnt = 1, val[N];   void ins(long long x) {     long long u = 1;     for (long long i = (30); i >= (0); --i) {       long long v = (x >> i) & 1;       if (!ch[u][v]) ch[u][v] = ++cnt;       u = ch[u][v];     }   }   long long n, a[N];   long long dfs(long long u) {     long long r1 = 0, r2 = 0;     if (!ch[u][0] && !ch[u][1]) return 1;     if (!ch[u][0]) return dfs(ch[u][1]);     if (!ch[u][1]) return dfs(ch[u][0]);     return max(dfs(ch[u][0]), dfs(ch[u][1])) + 1;   }   signed main() {     n = read();     for (long long i = (1); i <= (n); ++i) a[i] = read(), ins(a[i]);     writeln(n - dfs(1));   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, digit;     int S = 0, max = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> digit;       S += digit;       if (digit > max) max = digit;     }     S = abs(S - max);     cout << abs(S - max) + 1;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int q;     cin >> q;     while (q--) {       int n;       cin >> n;       long long dp[n][3];       long long a[n], b[n];       for (int i = 0; i < n; i++) {         cin >> a[i] >> b[i];       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < 3; j++) {           dp[i][j] = 2000000000000000000;         }       }       dp[0][0] = 0;       dp[0][1] = b[0];       dp[0][2] = 2 * b[0];       for (long long i = 1; i < n; i++) {         for (long long j = 0; j < 3; j++) {           for (long long k = 0; k < 3; k++) {             if (a[i] + j != a[i - 1] + k) {               dp[i][j] = min(dp[i][j], dp[i - 1][k] + j * b[i]);             }           }         }       }       cout << min(dp[n - 1][0], min(dp[n - 1][1], dp[n - 1][2])) <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int sol = 0;     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= i; ++j) {         for (int k = i - j; k <= j; ++k) {           if (((k ^ j) ^ i) == 0 && k + j > i) ++sol;         }       }     }     cout << sol <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   long long n, k;   long long ans;   long long matriz[505][505];   int main() {     ans = 0;     cin >> n >> k;     long long aux = (n - k + 1) * n;     aux = n * n - aux + 1;     long long curr1 = 1;     long long curr2 = aux;     for (int i = 0; i < n; ++i) {       int j = 0;       for (; j < k - 1; ++j) {         matriz[i][j] = curr1;         curr1++;       }       ans += curr2;       matriz[i][j] = curr2;       curr2++;       j++;       for (; j < n; ++j) {         matriz[i][j] = curr2;         curr2++;       }     }     cout << ans << endl;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n - 1; ++j) {         cout << matriz[i][j] <<    ;       }       cout << matriz[i][n - 1] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int IN() {     int x = 0, f = 0, ch;     for (; (ch = getchar()) <  0  || ch >  9 ;) f = (ch ==  - );     for (; ch >=  0  && ch <=  9 ; (ch = getchar())) x = x * 10 + ch -  0 ;     return f ? -x : x;   }   int M, edge[1005][1005], son[1005][1005], idx[1005][1005];   inline void Plu(int& a, int b) {     a += b;     if (a >= 1000000007) a -= 1000000007;   }   struct Tree {     int N, D, st[1005];     int F[1005][15], G[1 << 15];     struct Lin {       int v, next;     } E[1005 << 1];     void Link(int u, int v) {       E[++D] = (Lin){v, st[u]};       st[u] = D;       E[++D] = (Lin){u, st[v]};       st[v] = D;     }     void Init(int kind) {       N = IN();       for (int i = 2, lim = N; i <= lim; i++) {         int u = IN(), v = IN();         Link(u, v);         if (kind) {           edge[u][++*edge[u]] = v;           edge[v][++*edge[v]] = u;         }       }       if (kind) M = N;     }     void DFS(int u, int f) {       for (int i = st[u], v; i; i = E[i].next)         if ((v = E[i].v) != f) DFS(v, u);       for (int a = 1, lim = M; a <= lim; a++) {         int U = (1 << *son[a]) - 1;         for (int j = 0, lim = U; j <= lim; j++) G[j] = 0;         G[0] = 1;         for (int i = st[u], v; i; i = E[i].next)           if ((v = E[i].v) != f) {             for (int s = U, lim = 0; s >= lim; s--)               if (G[s]) {                 for (int j = 1, lim = *son[a]; j <= lim; j++) {                   int b = son[a][j], t = 1 << (idx[a][b] - 1);                   if (!(t & s)) Plu(G[s | t], 1ll * G[s] * F[v][b] % 1000000007);                 }               }           }         F[u][a] = G[U];       }     }     int Work(int Rt) {       for (int i = 1, lim = N; i <= lim; i++)         for (int j = 1, lim = M; j <= lim; j++) F[i][j] = 0;       DFS(1, 0);       int ret = 0;       for (int i = 1, lim = N; i <= lim; i++) Plu(ret, F[i][Rt]);       return ret;     }   } A, B;   int Pow(int a, int b) {     int ret = 1;     for (; b; b >>= 1, a = 1ll * a * a % 1000000007)       if (b & 1) ret = 1ll * ret * a % 1000000007;     return ret;   }   void DFS(int u, int f) {     for (int i = 1, lim = *edge[u]; i <= lim; i++) {       int v = edge[u][i];       if (v != f) {         son[u][++*son[u]] = v;         idx[u][v] = *son[u];         DFS(v, u);       }     }   }   int main(int argc, char* argv[]) {     A.Init(0);     B.Init(1);     int retA = 0, retB = 0;     for (int i = 1, lim = M; i <= lim; i++) {       for (int j = 0, lim = M; j <= lim; j++)         for (int k = 0, lim = M; k <= lim; k++) son[j][k] = idx[j][k] = 0;       DFS(i, 0);       Plu(retA, A.Work(i));       Plu(retB, B.Work(i));     }     cout << 1ll * retA * Pow(retB, 1000000007 - 2) % 1000000007 << endl;     cerr << retA <<     << retB << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int sqr(long long int a) { return a * a; }   int main() {     ios::sync_with_stdio(false);     long long int a, n, m;     string str;     cin >> n;     long long int arr[n + 1], brr[n + 1], crr[n + 1];     long long int sum = 0, maxx = 0;     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     sort(arr, arr + n);     for (int i = 0; i < n / 2; i++) {       sum += sqr(arr[i] + arr[n - i - 1]);     }     cout << sum;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   class A {    public:     int a;     int b;     int c;     int d;   } aa[100005];   int a[103][103];   int main() {     int n, m, q;     cin >> n >> m >> q;     int i;     for (i = 0; i < q; i++) {       cin >> aa[i].a >> aa[i].b;       if (aa[i].a == 1 || aa[i].a == 2) {         aa[i].c = 0;         aa[i].d = 0;       } else {         cin >> aa[i].c;         cin >> aa[i].d;       }     }     int j;     for (j = q - 1; j >= 0; j--) {       int x = aa[j].a;       int y = aa[j].b;       int z = aa[j].c;       int w = aa[j].d;       if (x == 1) {         int g = a[y][m];         for (i = m; i >= 2; i--) {           a[y][i] = a[y][i - 1];         }         a[y][1] = g;       } else if (x == 2) {         int g = a[n][y];         for (i = n; i >= 2; i--) {           a[i][y] = a[i - 1][y];         }         a[1][y] = g;       } else {         a[y][z] = w;       }     }     for (i = 1; i <= n; i++) {       for (j = 1; j <= m; j++) {         cout << a[i][j] <<    ;       }       cout << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int n, m, d, ans;   struct node;   struct edge {     node *ed;     edge *next;   } E[N << 1], *newE = E;   struct node {     edge *son;     node *pa;     bool flag;     int dist[2], dis;     void dfs() {       dist[0] = dist[1] = dis = -1e8;       if (flag) dist[0] = 0;       for (edge *e = son; e; e = e->next)         if (e->ed != pa) {           e->ed->pa = this;           e->ed->dfs();           if (e->ed->dist[0] + 1 > dist[0]) {             dist[1] = dist[0];             dist[0] = e->ed->dist[0] + 1;           } else if (e->ed->dist[0] + 1 > dist[1]) {             dist[1] = e->ed->dist[0] + 1;           }         }     }     void dfs2() {       if (flag)         if (dis < 0) dis = 0;       if (max(dis, dist[0]) <= d) ans++;       for (edge *e = son; e; e = e->next)         if (e->ed != pa) {           e->ed->dis =               max(dis, e->ed->dist[0] == dist[0] - 1 ? dist[1] : dist[0]) + 1;           e->ed->dfs2();         }     }   } V[N];   int main() {     scanf( %d%d%d , &n, &m, &d);     for (int i = 0; i < m; i++) {       int p;       scanf( %d , &p);       V[p].flag = 1;     }     for (int i = 1; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       *newE = (edge){V + y, V[x].son}, V[x].son = newE++;       *newE = (edge){V + x, V[y].son}, V[y].son = newE++;     }     V[1].dfs();     V[1].dfs2();     printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   int n, a[20];   vector<long long> v;   int main() {     cin >> n;     int s = 1;     for (long long i = 1; i < 1e5; ++i) v.push_back(i);     while (v.size() > 8e4) v.erase(v.begin());     for (int i = 0; i < n; ++i) scanf( %d , &a[i]);     sort(a, a + n);     for (int i = 0; i < n; ++i) printf( %d  , a[i]);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     string s;     cin >> s;     s =  0  + s;     vector<int> v(int(s.size()), 0);     for (int i = int(s.size()) - 1; i >= 0; i--) {       if (s[i] ==  1 ) {         v[i] = -1;         while (s[i] !=  0 ) i--;         v[i] = 1;       }     }     for (int i = 1; i < int(v.size()); i++) {       if (v[i] == -1 and v[i - 1] == 1) {         v[i - 1] = 0;         v[i] = 1;       } else if (v[i] == 1 and v[i - 1] == -1) {         v[i - 1] = 0;         v[i] = -1;       }     }     int count = 0;     for (int i = 0; i < int(v.size()); i++)       if (v[i] != 0) count++;     cout << count << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, c1, c2;       cin >> n;       int x, y;       x = n / 3;       y = n % 3;       if (y == 1) {         c1 = x + 1;         c2 = x;       } else if (y == 2) {         c1 = x;         c2 = x + 1;       } else {         c1 = x;         c2 = x;       }       cout << c1 <<     << c2 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   int n, m, k;   int t, x;   int c[200010];   int f1[200010];   int f2[200010];   vector<int> w, a, b;   vector<int> d;   vector<ll> ww, aa, bb, dd;   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> k;     for (int i = 0; i < n; i++) cin >> c[i];     cin >> t;     for (int i = 0; i < t; i++) {       cin >> x;       f1[x - 1] = 1;     }     cin >> t;     for (int i = 0; i < t; i++) {       cin >> x;       f2[x - 1] = 1;     }     for (int i = 0; i < n; i++) {       if (f1[i] && f2[i])         w.push_back(c[i]);       else if (f1[i])         a.push_back(c[i]);       else if (f2[i])         b.push_back(c[i]);       else         d.push_back(c[i]);     }     sort(w.begin(), w.end());     sort(a.begin(), a.end());     sort(b.begin(), b.end());     sort(d.begin(), d.end());     ww.push_back(0);     aa.push_back(0);     bb.push_back(0);     dd.push_back(0);     for (int i = 0; i < w.size(); i++) ww.push_back(ww.back() + w[i]);     for (int i = 0; i < a.size(); i++) aa.push_back(aa.back() + a[i]);     for (int i = 0; i < b.size(); i++) bb.push_back(bb.back() + b[i]);     for (int i = 0; i < d.size(); i++) dd.push_back(dd.back() + d[i]);     ll ans = 1e18 + 1;     for (int i = 0; i <= w.size(); i++) {       ll cost = ww[i];       int need = max(0, k - i);       if (need > a.size() || need > b.size() || i + need * 2 > m) continue;       cost += aa[need];       cost += bb[need];       int re = m - i - 2 * need;       if (a.size() - need + b.size() - need + d.size() < re) continue;       if (re > 0) {         int l = 0;         int r = 1e9 + 1;         while (l < r) {           int mid = (l + r) / 2;           int cnt = 0;           int h = upper_bound(a.begin(), a.end(), mid) - a.begin();           cnt += max(0, h - need);           h = upper_bound(b.begin(), b.end(), mid) - b.begin();           cnt += max(0, h - need);           h = upper_bound(d.begin(), d.end(), mid) - d.begin();           cnt += max(0, h);           if (cnt >= re)             r = mid;           else             l = mid + 1;         }         {           int mid = l;           int cnt = 0;           int h = upper_bound(a.begin(), a.end(), mid) - a.begin();           cnt += max(0, h - need);           if (h > need) cost += aa[h] - aa[need];           h = upper_bound(b.begin(), b.end(), mid) - b.begin();           cnt += max(0, h - need);           if (h > need) cost += bb[h] - bb[need];           h = upper_bound(d.begin(), d.end(), mid) - d.begin();           cnt += max(0, h);           cost += dd[h];           if (cnt > re) cost -= mid * (cnt - re);         }       }       ans = min(ans, cost);     }     if (ans > 1e17) ans = -1;     cout << ans << endl;     return 0;   }