malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, m, sx, sy, ma[1005][1005], vis[1005][1005], ans, t, tot = 99999999;   int dx[5] = {1, -1, 0, 0};   int dy[5] = {0, 0, 1, -1};   char maa[1005][1005];   void bfs(int u, int v) {     queue<int> qx;     queue<int> qy;     queue<int> qt;     qx.push(u), qy.push(v), qt.push(0);     vis[u][v] = 1;     while (!qx.empty()) {       int x = qx.front();       qx.pop();       int y = qy.front();       qy.pop();       int t = qt.front();       qt.pop();       if (t > tot) continue;       if (ma[x][y] == -2) {         tot = t;       }       if (ma[x][y] > 0) ans += ma[x][y];       for (int i = 0; i < 4; i++) {         int xx = x + dx[i], yy = y + dy[i];         if (!vis[xx][yy] && ma[xx][yy] != -1 && xx <= n && xx > 0 && yy <= m &&             yy > 0) {           vis[xx][yy] = 1;           qx.push(xx), qy.push(yy), qt.push(t + 1);         }       }     }   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %s , maa[i] + 1);       for (int j = 1; j <= m; j++) {         if (maa[i][j] ==  T )           ma[i][j] = -1;         else if (maa[i][j] ==  S )           ma[i][j] = -2;         else if (maa[i][j] ==  E ) {           ma[i][j] = 0;           sx = i, sy = j;         } else           ma[i][j] = maa[i][j] -  0 ;       }     }     bfs(sx, sy);     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long k, a, b;     cin >> k >> a >> b;     long long k1, k2, r1, r2;     if (a >= 0) {       k1 = a / k;       r1 = a % k;     } else {       k1 = a / k;       r1 = a % k;       if (r1) {         k1 -= 1;         r1 += k;       }     }     if (b >= 0) {       k2 = b / k;       r2 = b % k;     } else {       k2 = b / k;       r2 = b % k;       if (r2) {         k2 -= 1;         r2 += k;       }     }     if (r1) {       cout << (k2 - k1) <<   n ;       ;     } else {       cout << (k2 - k1 + 1) <<   n ;       ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10, T = 3010, size = 1 << 20, mod = 998244353, inf = 2e9;   const long long INF = 1e15;   template <class o>   void qr(o &x) {     char c = getchar();     x = 0;     int f = 1;     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) x = x * 10 + c -  0 , c = getchar();     x *= f;   }   template <class o>   void qw(o x) {     if (x / 10) qw(x / 10);     putchar(x % 10 +  0 );   }   template <class o>   void pr1(o x) {     if (x < 0) x = -x, putchar( - );     qw(x);     putchar(   );   }   template <class o>   void pr2(o x) {     if (x < 0) x = -x, putchar( - );     qw(x);     putchar(  n );   }   int n, k, m, Q, a[N], b[N], c[N], R[N], lim[N], val[N], id[N], tot, ans, cnt;   int bl[T], br[T], pos[N], w[T][1 << 14], tag[N];   void ask() {     if (cnt)       puts( -1 );     else       pr2(ans);   }   void add(int x, int y, int z) {     if (x > y) return;     int p = pos[x], q = pos[y];     if (p == q) {       for (auto i = x; i <= y; i++) w[p][val[i]]--, w[p][val[i] ^= z]++;       return;     }     for (auto i = p + 1; i <= q - 1; i++) tag[i] ^= z;     for (auto i = x; i <= br[p]; i++) w[p][val[i]]--, w[p][val[i] ^= z]++;     for (auto i = bl[q]; i <= y; i++) w[q][val[i]]--, w[q][val[i] ^= z]++;   }   int count(int x, int y) {     if (x > y) return 0;     int p = pos[x], q = pos[y], s = 0;     if (p == q) {       for (auto i = x; i <= y; i++) s += (val[i] != tag[p]);       return s;     }     for (auto i = p + 1; i <= q - 1; i++) s += w[i][tag[i]];     for (auto i = x; i <= br[p]; i++) s += (val[i] == tag[p]);     for (auto i = bl[q]; i <= y; i++) s += (val[i] == tag[q]);     return y - x + 1 - s;   }   void modify(int x, int y) {     if (x > n) return;     int z = x % k;     cnt -= count(lim[z], R[z]);     ans -= count(id[x], R[z]);     add(id[x], R[z], y);     cnt += count(lim[z], R[z]);     ans += count(id[x], R[z]);   }   int main() {     qr(n);     qr(k);     m = n - k + 1;     qr(Q);     for (auto i = 1; i <= n; i++) qr(a[i]);     for (auto i = 1; i <= n; i++) qr(b[i]), c[i] = a[i] ^ b[i];     for (auto i = n; i >= 1; i--) c[i] ^= c[i - 1];     for (auto i = 0; i <= k - 1; i++) {       int s = (n - i) / k + (i > 0), j, d = 0;       for (j = !i ? k : i; s--; j += k) val[++tot] = d = c[j] ^ d, id[j] = tot;       j -= k;       R[i] = tot;       if (j > m)         lim[i] = tot, cnt += !!val[tot];       else         lim[i] = tot + 1;     }     for (auto i = 1; i <= tot; i++) ans += !!val[i];     int len = max(sqrt(1.0 * n / k + 0.5), ceil(1.0 * n / 3000)), t = n / len;     for (auto i = 1; i <= t; i++) bl[i] = br[i - 1] + 1, br[i] = i * len;     if (R[t] < n) t++, bl[t] = br[t - 1] + 1, br[t] = n;     for (auto i = 1; i <= t; i++)       for (auto j = bl[i]; j <= br[i]; j++) pos[j] = i, w[i][val[j]]++;     ask();     while (Q--) {       char s[5];       int x, y;       scanf( %s , s);       qr(x);       qr(y);       if (s[0] ==  a )         modify(x, a[x] ^ y), modify(x + 1, a[x] ^ y), a[x] = y;       else         modify(x, b[x] ^ y), modify(x + 1, b[x] ^ y), b[x] = y;       ask();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, d;   long long dp[150005][5];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cin >> n >> m >> d;     long long pt;     long long prev, curr;     for (long long i = 0; i < m; i++) {       long long a, b, t;       cin >> a >> b >> t;       if (i == 0) {         prev = 0;         curr = 1;         for (long long j = 1; j <= n; j++) dp[j][i] = b - abs(a - j);         pt = t;       } else {         for (long long j = 1; j <= n; j++) dp[j][curr] = b - abs(a - j);         long long s = (t - pt) * d;         pt = t;         deque<long long> mq;         for (long long j = 1; j <= s + 1 && j <= n; j++) {           while (!mq.empty() && dp[mq.back()][prev] < dp[j][prev]) mq.pop_back();           mq.push_back(j);         }         for (long long j = 1; j <= n; j++) {           while (!mq.empty() && mq.front() < j - s) mq.pop_front();           dp[j][curr] += dp[mq.front()][prev];           if (j + s + 1 <= n) {             while (!mq.empty() && j + s + 1 <= n &&                    dp[mq.back()][prev] < dp[j + 1 + s][prev])               mq.pop_back();             mq.push_back(j + s + 1);           }         }         swap(prev, curr);       }     }     long long sol = dp[1][prev];     for (int i = 2; i <= n; i++)       if (sol < dp[i][prev]) sol = dp[i][prev];     cout << sol << endl;     return 0;   }
#include<bits/stdc++.h>   #define int long long   using namespace std;   inline int read()   {       char c = getchar();       int x = 0, f = 1;       while (c <  0  || c >  9 )       {           if (c ==  - )               f = -1;           c = getchar();       }       while (c >=  0  && c <=  9 )       {           x = (x << 1) + (x << 3) + (c ^ 48);           c = getchar();       }       return x * f;   }   template <class cl>   void put(cl x)   {       if (x < 0)           putchar( - ), x = -x;       if (x > 9)           put(x / 10);       putchar(x % 10 +  0 );       return;   }   int n;   int a[55];   signed main()   {       int t;       t=read();       int n;       while(t--)       {           n=read();           int zheng=0,fu=0;           for(int i=1;i<=n;i++)           {               a[i]=read();               if(a[i]>=0) zheng++;               else fu++;           }           if( fu !=0)           {               puts( no );continue;           }           else            {               puts( yes );               sort(a+1,a+1+n);               if(a[n]>=0)               {                   cout<<a[n]+1<<  n ;                   for(int i=0;i<=a[n];i++)                   {                       cout<<i<<   ;                   }                   cout<<  n ;               }           }       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   long n, m, k, c = 1, s = 1;   int main() {     cin >> n >> m >> k;     m -= n;     while (m > 0) {       if (k - c >= 1) s++;       if (k + c <= n) s++;       m -= s;       c++;     }     cout << c;     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   void fast() {     std::ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);   }   int main() {     int n, k;     scanf( %d%d , &n, &k);     priority_queue<int> q;     int val;     while (n--) cin >> val, q.push(val);     while (!q.empty())       if (k % q.top())         q.pop();       else         return !printf( %d , k / q.top());   }
#include <bits/stdc++.h>   int main() {     char a[110], b[110];     scanf( %s , a);     getchar();     scanf( %s , b);     int len = strlen(a);     for (int i = 0; i < len; i++) {       if (a[i] < b[i]) {         printf( -1 );         return 0;       }     }     for (int i = 0; i < len; i++) {       if (a[i] == b[i])         printf( z );       else         printf( %c , b[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m;   vector<long long> a;   vector<long long> res;   int main() {     scanf( %I64d , &n);     for (long long i = 1; i <= n; ++i) {       scanf( %I64d , &m);       for (long long j = 0; j < m; ++j) {         a.push_back(i);       }     }     scanf( %I64d , &m);     for (long long i = 0; i < m; ++i) {       scanf( %I64d , &n);       res.push_back(a[n - 1]);     }     for (long long i = 0; i < (long long)res.size(); ++i) {       printf( %I64d n , res[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long t, n, y, i, j, a[5001];   int main() {     cin >> t;     for (j = 1; j <= t; j++) {       cin >> n;       y = -1;       cin >> a[1];       for (i = 2; i <= n; i++) {         cin >> a[i];         if (y == -1) {           if (a[1] != a[i]) y = i;         }       }       if (y == -1) {         cout <<  NO  << endl;         continue;       } else         cout <<  YES  << endl;       for (i = 2; i <= n; i++) {         if (a[i] == a[1])           cout << y <<     << i << endl;         else           cout << 1 <<     << i << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int q;   long long n;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> q;     while (q--) {       cin >> n;       int res = 0;       while (n != 1) {         if (n % 2 == 0) {           n /= 2;           res++;         } else if (n % 3 == 0) {           n = (n / 3) * 2;           res++;         } else if (n % 5 == 0) {           n = (n / 5) * 4;           res++;         } else           break;       }       if (n != 1)         cout << -1 << endl;       else         cout << res << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e18;   const long long N = 2e5 + 33;   void solve() {     int64_t n;     cin >> n;     vector<int64_t> v(n + 1);     for (int64_t i = 0; i < n; i++) {       cin >> v[i];     }     vector<int64_t> ans(n + 10, 0);     for (int64_t i = 0; i < n; i++) {       vector<int64_t> mp(n + 10, 0);       int64_t maxi = 0, num = 60000;       for (int64_t j = i; j < n; j++) {         mp[v[j]]++;         if (maxi < mp[v[j]]) {           maxi = mp[v[j]];           num = v[j];         }         if (maxi == mp[v[j]] && num > v[j]) num = v[j];         ans[num]++;       }     }     for (int64_t i = 1; i <= n; i++) cout << ans[i] <<    ;   }   signed main() {     int64_t t = 1;     while (t--) {       solve();       cout << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int L[n];     for (int i = 0; i < n; ++i) scanf( %d , &L[i]);     int imos[n + 1];     fill(imos, imos + n + 1, 0);     for (int i = 0; i < n; ++i) {       L[i] = min(L[i], i);       ++imos[i - L[i]];       --imos[i];     }     int t = 0, ans = n;     for (int i = 0; i <= n; ++i) {       t += imos[i];       if (t > 0) --ans;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 4e+18;   const int N = 100010;   struct rec {     long long a, b, c;   };   int cas, n;   long long x[N], y[N], z[N];   rec get(long long la, long long ra, long long lb, long long rb, long long lc,           long long rc, long long labc, long long rabc, int k) {     la -= k, ra -= k, lb -= k, rb -= k, lc -= k, rc -= k;     labc -= 3 * k, rabc -= 3 * k;     if (la % 2 != 0) la++;     if (ra % 2 != 0) ra--;     if (lb % 2 != 0) lb++;     if (rb % 2 != 0) rb--;     if (lc % 2 != 0) lc++;     if (rc % 2 != 0) rc--;     if (labc % 2 != 0) labc++;     if (rabc % 2 != 0) rabc--;     if (la > ra || lb > rb || lc > rc || labc > rabc) return (rec){inf, inf, inf};     if (rabc < la + lb + lc) return (rec){inf, inf, inf};     if (labc > ra + rb + rc) return (rec){inf, inf, inf};     if (labc <= la + lb + lc && la + lb + lc <= rabc)       return (rec){la + k, lb + k, lc + k};     if (labc <= ra + rb + rc && ra + rb + rc <= rabc)       return (rec){ra + k, rb + k, rc + k};     if (labc <= la + lb + rc) return (rec){la + k, lb + k, labc - la - lb + k};     if (labc <= la + rb + rc) return (rec){la + k, labc - la - rc + k, rc + k};     return (rec){labc - rb - rc + k, rb + k, rc + k};   }   rec check(long long mid) {     long long la, lb, lc, labc, ra, rb, rc, rabc;     la = lb = lc = labc = -inf;     ra = rb = rc = rabc = inf;     for (int i = 1; i <= n; i++) {       la = max(la, -x[i] + y[i] + z[i] - mid);       ra = min(ra, -x[i] + y[i] + z[i] + mid);       lb = max(lb, x[i] - y[i] + z[i] - mid);       rb = min(rb, x[i] - y[i] + z[i] + mid);       lc = max(lc, x[i] + y[i] - z[i] - mid);       rc = min(rc, x[i] + y[i] - z[i] + mid);       labc = max(labc, x[i] + y[i] + z[i] - mid);       rabc = min(rabc, x[i] + y[i] + z[i] + mid);     }     if (la > ra || lb > rb || lc > rc || labc > rabc) return (rec){inf, inf, inf};     rec tmp = get(la, ra, lb, rb, lc, rc, labc, rabc, 0);     if (tmp.a != inf) return tmp;     tmp = get(la, ra, lb, rb, lc, rc, labc, rabc, 1);     if (tmp.a != inf) return tmp;     return (rec){inf, inf, inf};   }   int main() {     scanf( %d , &cas);     while (cas--) {       scanf( %d , &n);       for (int i = 1; i <= n; i++) scanf( %lld%lld%lld , &x[i], &y[i], &z[i]);       long long l = 0, r = inf;       while (l < r) {         long long mid = (l + r) >> 1;         if (check(mid).a != inf)           r = mid;         else           l = mid + 1;       }       rec ans = check(l);       printf( %lld %lld %lld n , (ans.b + ans.c) / 2, (ans.a + ans.c) / 2,              (ans.a + ans.b) / 2);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   vector<int> v;   map<int, int> mp;   int main() {     ios::sync_with_stdio(0);     int T;     cin >> T;     while (T--) {       string s1, s2;       cin >> s1 >> s2;       reverse(s1.begin(), s1.end());       reverse(s2.begin(), s2.end());       int len1 = s1.size();       int len2 = s2.size();       int pos1 = -1, pos2 = -1;       for (int i = 0; i < len2; i++) {         if (s2[i] ==  1 ) {           pos2 = i;           break;         }       }       for (int i = pos2; i < len1; i++) {         if (s1[i] ==  1 ) {           pos1 = i;           break;         }       }       if (pos1 == pos2)         cout << 0 << endl;       else         cout << pos1 - pos2 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e18;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, x, y, s1, s2, i, j, a, b, c, r = 0;     cin >> n >> s1 >> s2;     long long x1[n + 1], x2[n + 1], y1[n + 1], y2[n + 1];     for (i = 1; i <= n; i++) {       cin >> x >> y;       x1[i] = min(x, s1), x2[i] = max(x, s1), y1[i] = min(y, s2),       y2[i] = max(y, s2);     }     vector<pair<long long, long long> > v1;     if (s1 - 1 >= 0) v1.push_back({s1 - 1, s2});     if (s1 + 1 <= 1e9) v1.push_back({s1 + 1, s2});     if (s2 - 1 >= 0) v1.push_back({s1, s2 - 1});     if (s2 + 1 <= 1e9) v1.push_back({s1, s2 + 1});     for (i = 0; i < v1.size(); i++) {       x = v1[i].first, y = v1[i].second, c = 0;       for (j = 1; j <= n; j++) {         if (x >= x1[j] && x <= x2[j] && y >= y1[j] && y <= y2[j]) c++;       }       if (c >= r) r = c, a = x, b = y;     }     cout << r <<   n ;     cout << a <<     << b <<   n ;   }
#include <bits/stdc++.h>   int arr[101];   int N;   int min = 999999999;   int memo[101][3];   int findMin(int idx, int prev) {     int min_Rest = 999999999;     if (idx == N + 1) return 0;     if (memo[idx][prev] != -1) return memo[idx][prev];     if (prev != 1 && (arr[idx] == 1 || arr[idx] == 3))       min_Rest = ((min_Rest) < (findMin(idx + 1, 1)) ? (min_Rest)                                                      : (findMin(idx + 1, 1)));     if (prev != 2 && (arr[idx] == 2 || arr[idx] == 3))       min_Rest = ((min_Rest) < (findMin(idx + 1, 2)) ? (min_Rest)                                                      : (findMin(idx + 1, 2)));     min_Rest =         ((min_Rest) < (1 + findMin(idx + 1, 0)) ? (min_Rest)                                                 : (1 + findMin(idx + 1, 0)));     memo[idx][prev] = min_Rest;     return min_Rest;   }   int main() {     int T, TC;     {       int i, j;       scanf( %d , &N);       for (i = 1; i <= N; i++) scanf( %d , &arr[i]);       min = 999999999;       for (i = 0; i <= N; i++)         for (j = 0; j < 3; j++) memo[i][j] = -1;       printf( %d n , findMin(1, 0));     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool debug = false;   int n, m, k;   int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};   long long a[305];   int pt[20];   int main() {     scanf( %d , &n);     for (int(i) = 0; (i) < (int)(n); (i)++) scanf( %lld , a + i + 1);     long long ans = 0;     int t;     pt[0] = 1;     for (int i = 1; i < 20; i++) pt[i] = 2 * pt[i - 1];     for (int i = 1; i < n; i++) {       ans += a[i];       printf( %lld n , ans);       for (t = 0; i + pt[t] <= n; t++)         ;       a[i + pt[t - 1]] += a[i];     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int q;     cin >> q;     while (q--) {       int a, b, c;       cin >> a >> b >> c;       int temp = min(a, b);       if (temp * 3 <= a + b + c) {         cout << temp << endl;         continue;       } else {         cout << (a + b + c) / 3 << endl;         continue;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s, t;     cin >> s >> t;     string ans;     long cnt = 0;     for (long i = 0; i < s.size(); i++) {       if (s[i] == t[i])         ans +=  1 ;       else {         if (cnt % 2)           ans += s[i];         else           ans += t[i];         cnt++;       }     }     int d1 = 0, d2 = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] != ans[i]) d1++;       if (t[i] != ans[i]) d2++;     }     if (d1 == d2)       cout << ans << endl;     else       cout <<  impossible  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> v[2 * 100001];   int color[2 * 100001];   void dfs(int s, int p, int clr, int pclr) {     color[s] = clr;     int y = 1;     for (int i = 0; i < v[s].size(); i++) {       int u = v[s][i];       if (u == p) continue;       while (y == clr || y == pclr) {         y++;       }       dfs(u, s, y, clr);       y++;     }   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     memset(color, 0, sizeof color);     dfs(1, 0, 1, -1);     int ans = 0;     for (int i = 1; i <= n; i++) {       ans = max(ans, color[i]);     }     cout << ans << endl;     for (int i = 1; i <= n; i++) {       cout << color[i] <<    ;     }     return 0;   }
#include<bits/stdc++.h>   using namespace std;         int main()   {       long long int t;       cin>>t;       while(t--)       {           long long int i,j,k,l,m,n,c1=0,c2=0,ans=0,res=0;           map<long long int,long long int>mp,mp1;           vector<long long int>v,v1,v2;           set<long long int>st,st1;           vector<pair<long long int,long long int>>vp,vp1;           cin>>n;           long long int arr1[n];           for(i=0;i<n;i++)cin>>arr1[i];           m=*min_element(arr1,arr1+n);           for(i=0;i<n;i++)           {               if(arr1[i]==m)c1++;               if((arr1[i]&m)!=m)c2++;           }           if(c1<2||c2!=0)cout<<0<<endl;           else           {ans=c1*(c1-1);           for(i=n-2;i>=1;i--)           ans=(ans*i)%1000000007;           cout<<ans<<endl;           }       }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     s =  00  + s;     int l = s.length();     int fi = 0, fj = 0, i, j, k, num;     for (i = 0; i < l; i++) {       for (j = i + 1; j < l; j++) {         for (k = j + 1; k < l; k++) {           num = 100 * (s[i] -  0 ) + 10 * (s[j] -  0 ) + (s[k] -  0 );           if (num % 8 == 0) {             fi = fj = 1;             break;           }         }         if (fj) break;       }       if (fi) break;     }     if (fi) {       cout <<  YES  << endl;       for (int l = 2; l <= k; l++) {         if (l < i)           cout << s[l];         else {           if (l == i || l == j || l == k) cout << s[l];         }       }     } else {       cout <<  NO ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long power(long long a, long long n, long long lelo) {     if (n == 0) return 1;     if (n == 1) return a;     if (n == 2) return (a * a) % lelo;     if (n % 2)       return (a * power(a, n - 1, lelo)) % lelo;     else       return power(power(a, n / 2, lelo), 2, lelo) % lelo;   }   void swap(long long &a, long long &b) {     long long tm = a;     a = b;     b = tm;   }   const long long N = 1e+5;   bool comp(const pair<long long, long long> &p1,             const pair<long long, long long> &p2) {     if (p1.first != p2.first)       return p1.first > p2.first;     else       return p1.second < p2.second;   }   long long n, k;   vector<long long> v;   bool chk(long long x) {     for (long long(i) = (0); (i) < (n); (i)++)       if (abs(v[i] - x) > k) return 0;     return 1;   }   void solve() {     cin >> n >> k;     v.resize(n);     for (long long(i) = (0); (i) < (n); (i)++) cin >> v[i];     sort((v).begin(), (v).end());     long long t1 = v[0] + k, t2 = v[n - 1] - k;     if (v[n - 1] - v[0] <= k) {       cout << v[0] + k;       return;     }     if (t2 > t1) {       cout << -1;       return;     }     cout << t1;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long double tt = 1.0 * clock();     long long t = 1;     cin >> t;     for (long long(i) = (1); (i) < (t + 1); (i)++) {       solve();       if (i < t) cout <<   n ;     }     tt = 1.000000000 * (clock() - tt) / CLOCKS_PER_SEC;     return 0;   }