malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   template <class T, class S>   ostream& operator<<(ostream& os, const pair<T, S>& v) {     return os <<  (  << v.first <<  ,   << v.second <<  ) ;   }   template <class T>   ostream& operator<<(ostream& os, const vector<T>& v) {     os <<  [ ;     for (int i = int(0); i <= int((static_cast<int>((v).size())) - 1); ++i) {       if (i) os <<  ,  ;       os << v[i];     }     return os <<  ] ;   }   template <class T>   bool setmax(T& _a, T _b) {     if (_a < _b) {       _a = _b;       return true;     }     return false;   }   template <class T>   bool setmin(T& _a, T _b) {     if (_b < _a) {       _a = _b;       return true;     }     return false;   }   template <class T>   T gcd(T _a, T _b) {     return _b == 0 ? _a : gcd(_b, _a % _b);   }   int main() {     int n, k;     cin >> n >> k;     vector<int> a(n);     iota((a).begin(), (a).end(), 0);     int cur = 0;     while (k--) {       int x;       cin >> x;       int next = (cur + x) % static_cast<int>((a).size());       cout << a[next] + 1 <<    ;       a.erase(a.begin() + next);       cur = next % static_cast<int>((a).size());     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:536870912 )   #pragma warning(disable : 4996)   using namespace std;   const int N = 305;   int dp[N * 2][N][N], ar[N][N];   int n;   pair<int, int> getC(int t, int i) {     return t <= n - 1 ? make_pair(t - i, i) : make_pair(n - 1 - i, t - n + 1 + i);   }   int getS(int t) { return t <= n - 1 ? t + 1 : (2 * n - 1 - t); }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) scanf( %d , &ar[i][j]);     }     memset(dp, 128, sizeof(dp));     ;     dp[0][0][0] = ar[0][0];     for (int i = 0; i < 2 * n - 2; i++) {       int t = getS(i + 1), c = getS(i);       for (int a = 0; a < c; a++) {         for (int b = 0; b < c; b++) {           for (int p = (i < n - 1 ? 0 : -1); p < (i < n - 1 ? 2 : 1); p++) {             for (int q = (i < n - 1 ? 0 : -1); q < (i < n - 1 ? 2 : 1); q++) {               auto g = getC(i + 1, a + p), h = getC(i + 1, b + q);               if (g.first >= n || g.second >= n || h.first >= n ||                   h.second >= n || g.first < 0 || g.second < 0 || h.first < 0 ||                   h.second < 0)                 continue;               int sum = dp[i][a][b] + ar[g.first][g.second];               if (g != h) sum += ar[h.first][h.second];               dp[i + 1][a + p][b + q] = max(dp[i + 1][a + p][b + q], sum);             }           }         }       }     }     printf( %d , dp[2 * n - 2][0][0]);     return 0;   }
#include <bits/stdc++.h>   struct node {     int* children;     node() {       children = new int[2];       children[0] = children[1] = -1;     }     int& operator[](int i) { return children[i]; }   };   struct trie {     std::vector<node> nodes;     int root;     trie() {       nodes.push_back(node());       root = 0;     }     void insert(long long x) {       int u = this->root;       for (int i = 41; i >= 0; --i) {         int v = ((x & (1LL << i)) ? 1 : 0);         int next = nodes[u][v];         if (next == -1) {           next = nodes[u][v] = nodes.size();           nodes.push_back(node());         }         u = next;       }     }     long long maximize(long long x) {       int u = this->root;       long long ans = 0;       for (int i = 41; i >= 0; --i) {         int cnt = 0, v;         if (nodes[u][0] != -1) {           cnt++;           v = 0;         }         if (nodes[u][1] != -1) {           cnt++;           v = 1;         }         if (cnt == 0)           return ans;         else if (cnt == 1) {           if (v) ans ^= (1LL << i);           u = nodes[u][v];         } else {           v = (x & (1LL << i) ? 1 : 0) ^ 1;           if (v) ans ^= (1LL << i);           u = nodes[u][v];         }       }       return ans;     }   };   int N;   long long partial_xor[100001];   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(NULL);     std::cout.tie(NULL);     std::cin >> N;     for (int i = 0; i < N; ++i) {       std::cin >> partial_xor[i];       if (i) partial_xor[i] ^= partial_xor[i - 1];     }     long long xor_sum = partial_xor[N - 1];     long long ans = xor_sum;     trie t;     for (int i = 0; i < N; ++i) {       ans = std::max(ans, partial_xor[i]);       if (i) {         ans = std::max(ans, partial_xor[i - 1] ^ xor_sum);         ans = std::max(ans, t.maximize(partial_xor[i - 1]) ^ partial_xor[i - 1]);       }       t.insert(partial_xor[i] ^ xor_sum);     }     std::cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   multiset<int> maxH, maxV;   set<pair<int, int> > vH, vV;   int main() {     int w, h, n, x;     std::set<pair<int, int> >::iterator it, itL, itR;     char ch;     cin >> w >> h >> n;     vH.insert(make_pair(0, h));     vH.insert(make_pair(h, 0));     vV.insert(make_pair(0, w));     vV.insert(make_pair(w, 0));     maxH.insert(h);     maxV.insert(w);     for (int i = 0; i < n; i++) {       cin >> ch >> x;       if (ch ==  H ) {         vH.insert(make_pair(x, 0));         it = vH.find(make_pair(x, 0));         pair<int, int> lp, p, rp;         it--;         lp = *it;         it++;         p = *it;         it++;         rp = *it;         vH.insert(make_pair((int)lp.first, x - lp.first));         vH.erase(vH.find(lp));         vH.insert(make_pair(x, rp.first - x));         vH.erase(vH.find(p));         lp = make_pair(lp.first, x - lp.first);         p = make_pair(x, rp.first - x);         maxH.erase(maxH.find(rp.first - lp.first));         maxH.insert(lp.second);         maxH.insert(p.second);         cout << 1ll * (*maxV.rbegin()) * (*maxH.rbegin()) << endl;       } else {         vV.insert(make_pair(x, 0));         it = vV.find(make_pair(x, 0));         pair<int, int> lp, p, rp;         it--;         lp = (*it);         it++;         p = (*it);         it++;         rp = (*it);         vV.insert(make_pair(lp.first, x - lp.first));         vV.erase(vV.find(lp));         vV.insert(make_pair(x, rp.first - x));         vV.erase(vV.find(p));         lp = make_pair(lp.first, x - lp.first);         p = make_pair(x, rp.first - x);         maxV.erase(maxV.find(rp.first - lp.first));         maxV.insert(lp.second);         maxV.insert(p.second);         cout << 1ll * (*maxH.rbegin()) * (*maxV.rbegin()) << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = (long long)1e18;   const long long mod = (long long)1e9 + 7;   const double eps = (double)1e-9;   const double pi = acos(-1.0);   const int dx[] = {0, 0, 1, 0, -1};   const int dy[] = {0, 1, 0, -1, 0};   const int N = 100500;   int n, x, y, a, b, T;   long long k;   multiset<long long> st, st1;   long long check(int j) {     st1 = st;     long long ans = 0;     for (int i = 1; i <= j; ++i)       if (i % a == 0 && i % b == 0) {         ans += *st1.begin() * (x + y);         st1.erase(st1.begin());       }     for (int i = 1; i <= j; ++i)       if (i % a == 0 && i % b != 0) {         ans += *st1.begin() * x;         st1.erase(st1.begin());       }     for (int i = 1; i <= j; ++i)       if (i % b == 0 && i % a != 0) {         ans += *st1.begin() * y;         st1.erase(st1.begin());       }     return -ans;   }   void solve() {     cin >> n;     st.clear();     for (int i = 1; i <= n; ++i) {       long long x;       cin >> x;       st.insert(-x / 100);     }     cerr << st.size() << endl;     cin >> x >> a >> y >> b >> k;     if (x < y) swap(x, y), swap(a, b);     int l = 1, r = n;     while (l <= r) {       int md = (l + r) >> 1;       if (check(md) >= k)         r = md - 1;       else         l = md + 1;     }     if (r == n) r = -2;     cout << r + 1 << endl;   }   int main() {     cin.tie(NULL);     cout.tie(NULL);     ios_base::sync_with_stdio(false);     cin >> T;     while (T--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<long long, long long> add_pair(pair<long long, long long> a,                                       pair<long long, long long> b) {     return make_pair(a.first + b.first, a.second + b.second);   }   long long power(long long a, long long b) {     long long res = 1;     a = a % ((long long)1e9 + 7);     while (b > 0) {       if (b & 1) {         res = (res * a) % ((long long)1e9 + 7);         b--;       }       a = (a * a) % ((long long)1e9 + 7);       b >>= 1;     }     return res;   }   long long fermat_inv(long long y) { return power(y, ((long long)1e9 + 7) - 2); }   long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }   long long mul(long long a, long long b) {     return (a * (b % ((long long)1e9 + 7))) % ((long long)1e9 + 7);   }   long long sub(long long a, long long b) {     return ((a - b) % ((long long)1e9 + 7) + ((long long)1e9 + 7)) %            ((long long)1e9 + 7);   }   long long add(long long a, long long b) {     return ((a + b) % ((long long)1e9 + 7) + ((long long)1e9 + 7)) %            ((long long)1e9 + 7);   }   vector<vector<long long> > G;   map<long long, long long> m[100005];   vector<long long> b;   long long ans;   void dfs(long long rt, long long par) {     m[rt][b[rt]]++;     if (rt != par) {       for (auto it = (m[par]).begin(); it != (m[par]).end(); it++) {         m[rt][gcd(b[rt], it->first)] += it->second;       }     }     for (auto it = (m[rt]).begin(); it != (m[rt]).end(); it++) {       ans = add(ans, mul(it->first, it->second));     }     for (auto it = (G[rt]).begin(); it != (G[rt]).end(); it++) {       if (*it != par) {         dfs(*it, rt);       }     }   }   void solve() {     long long n, u, v, i;     cin >> n;     G = vector<vector<long long> >(n + 1, vector<long long>());     b = vector<long long>(n + 1);     for (i = (0); i < (n); i += (1)) cin >> b[i + 1];     for (i = (0); i < (n - 1); i += (1)) {       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     ans = 0;     dfs(1, 1);     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   int add(int a, int b) {     long long x = a + b;     if (x >= 1000000009) x -= 1000000009;     if (x < 0) x += 1000000009;     return x;   }   long long mul(long long a, long long b) { return (a * b) % 1000000009; }   long long pw(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % 1000000009;       a = (a * a) % 1000000009;       b >>= 1;     }     return ans;   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   long long rand_seed() {     long long a = rng();     return a;   }   int n;   map<int, pair<int, int> > pos;   map<pair<int, int>, int> id;   vector<set<int> > p, ch;   bool canrem(int i) {     int ok = 1;     for (auto j : p[i]) ok &= (ch[j].size() > 1);     return ok;   }   void rem(int i) {     for (auto j : p[i]) ch[j].erase(i);     for (auto j : ch[i]) p[j].erase(i);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     p.resize(n);     ch.resize(n);     for (int x, y, i = 0; i < n; i++) {       cin >> y >> x;       pos[i] = {x, y};       id[{x, y}] = i;     }     for (int i = 0; i < n; i++) {       int x = pos[i].first, y = pos[i].second;       for (int z = -1; z < 2; z++) {         if (id.count({x + 1, y + z})) p[i].insert(id[{x + 1, y + z}]);         if (id.count({x - 1, y + z})) ch[i].insert(id[{x - 1, y + z}]);       }     }     set<int> can;     for (int i = 0; i < n; i++) {       int x = pos[i].first, y = pos[i].second;       if (canrem(i)) can.insert(i);     }     long long ans = 0;     for (int i = 0; i < n; i++) {       int j;       if (i % 2 == 0)         j = *can.rbegin();       else         j = *can.begin();       ans = mul(ans, n);       ans = add(ans, j);       int x = pos[j].first;       int y = pos[j].second;       rem(j);       can.erase(j);       id.erase(pos[j]);       for (int z = -2; z < 3; z++) {         if (id.count({x, y + z})) can.erase(id[{x, y + z}]);         if (id.count({x - 1, y + z})) can.erase(id[{x - 1, y + z}]);       }       for (int z = -2; z < 3; z++) {         if (id.count({x, y + z}) && canrem(id[{x, y + z}]))           can.insert(id[{x, y + z}]);         x--;         if (id.count({x, y + z}) && canrem(id[{x, y + z}]))           can.insert(id[{x, y + z}]);         x++;       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, x;     cin >> n;     long long b = n, i(0), c(0);     for (int i = n + 1;; i++) {       b = i;       if (b < 0) b *= -1;       while (b != 0) {         x = b % 10;         if (x == 8) {           c = 1;           break;         }         b /= 10;       }       if (c == 1) {         cout << i - n << endl;         return 0;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e10;   long long s[300005];   int main() {     int T;     long long n, k;     scanf( %d , &T);     while (T--) {       scanf( %lld %lld , &n, &k);       for (long long i = 1; i <= n; i++) scanf( %lld n , &s[i]);       long long ans = inf, v;       for (long long i = k + 1; i <= n; i++) {         if (ans > (s[i] - s[i - k])) ans = s[i] - s[i - k], v = i;       }       long long kk = (s[v] + s[v - k]) / 2;       cout << kk << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long deal(long long x, long long y) {     int a = x + y;     return (x + 1) * x * (y + 1) / 2 + y * (y + 1) / 2 * (x + 1);   }   long long max(long long a, long long b) { return a > b ? a : b; }   int main() {     long long m, b;     long long ans = -1;     scanf( %lld%lld , &m, &b);     for (int i = 0;; i += m) {       long long ys = b - i / m;       if (ys < 0) break;       ans = max(ans, deal(i, ys));     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 101;   int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};   const int sz = sizeof(prime) / sizeof(int), V = 58;   int pos[V + 1], dp[N][1 << sz], n, A[N], minPrime[V + 1], trace[N][1 << sz];   void prepare() {     for (int i = 2; i * i <= V; ++i)       if (!minPrime[i])         for (int j = i * i; j <= V; j += i)           if (!minPrime[j]) minPrime[j] = i;     for (int i = 2; i <= V; ++i)       if (!minPrime[i]) minPrime[i] = i;     for (int i = 0; i < sz; ++i) pos[prime[i]] = i;   }   int solve(int idx, int msk) {     if (idx > n) return 0;     int &res = dp[idx][msk];     if (res != INT_MAX) return res;     int s, val, d;     for (int x = 1; x <= 58; ++x) {       val = x;       d = -1;       s = 0;       while (val > 1) {         if (minPrime[val] != d) {           d = minPrime[val];           s |= (1 << pos[d]);         }         val /= d;       }       if (!(s & msk)) {         val = abs(x - A[idx]) + solve(idx + 1, msk | s);         if (val < res) {           res = val;           trace[idx][msk] = x;         }       }     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) cin >> A[i];     fill(&dp[0][0], &dp[0][0] + sizeof(dp) / sizeof(int), INT_MAX);     prepare();     solve(1, 0);     int msk = 0, val, d, s;     for (int i = 1; i <= n; ++i) {       val = trace[i][msk];       cout << val <<    ;       d = -1;       s = 0;       while (val > 1) {         if (minPrime[val] != d) {           d = minPrime[val];           s |= (1 << pos[d]);         }         val /= d;       }       msk |= s;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 3;   struct node {     int nonzero, val;     void merge(node &n1, node &n2, int range) {       if (val)         nonzero = range;       else         nonzero = n1.nonzero + n2.nonzero;     }     void update(int x) { val += x; }   } tree[7 * MAX];   void update(int root, int l, int r, int u, int v, int val) {     if (u <= l and v >= r) {       tree[root].update(val);       tree[root].merge(tree[2 * root], tree[2 * root + 1], r - l + 1);       return;     }     int mid = (l + r) / 2;     if (u <= mid) update(2 * root, l, mid, u, v, val);     if (v > mid) update(2 * root + 1, mid + 1, r, u, v, val);     tree[root].merge(tree[2 * root], tree[2 * root + 1], r - l + 1);   }   vector<int> G[MAX], intervals[MAX];   int st[MAX], en[MAX], Time = 0, ans[MAX], n, m;   void dfs(int x, int par) {     st[x] = ++Time;     for (int i = 0; i < G[x].size(); i++)       if (G[x][i] != par) dfs(G[x][i], x);     en[x] = Time;   }   void dfs2(int x, int par) {     if (intervals[x].size()) intervals[x].push_back(x);     for (int i = 0; i < intervals[x].size(); i++)       update(1, 1, n, st[intervals[x][i]], en[intervals[x][i]], 1);     ans[x] = max(0, tree[1].nonzero - 1);     for (int i = 0; i < G[x].size(); i++)       if (G[x][i] != par) dfs2(G[x][i], x);     for (int i = 0; i < intervals[x].size(); i++)       update(1, 1, n, st[intervals[x][i]], en[intervals[x][i]], -1);   }   int main() {     scanf( %d , &n);     ;     scanf( %d , &m);     ;     memset(tree, 0, sizeof tree);     for (int i = 0; i < n - 1; i++) {       int a, b;       scanf( %d , &a);       ;       scanf( %d , &b);       ;       G[a].push_back(b);       G[b].push_back(a);     }     dfs(1, -1);     while (m--) {       int a, b;       scanf( %d , &a);       ;       scanf( %d , &b);       ;       intervals[a].push_back(b);       intervals[b].push_back(a);     }     dfs2(1, -1);     for (int i = 1; i < n + 1; i++) printf( %d  , ans[i]);     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int pri[100003], top, n;   bool ntp[100002];   struct node {     int a, b;     bool operator<(const node &rhs) const { return b > rhs.b; }   };   void euler() {     for (int i = 2; i <= 100000; ++i) {       if (!ntp[i]) pri[top++] = i;       for (int j = 0; j < top && pri[j] * i <= 100000; ++j) {         ntp[pri[j] * i] = 1;         if (i % pri[j] == 0) break;       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     euler();     int t;     cin >> t;     while (t--) {       cin >> n;       int bn = n;       vector<node> v;       int sz = 1;       for (int i = 0; i < top && n != 1; ++i) {         if (n % pri[i] == 0) {           node x;           x.a = pri[i];           x.b = 0;           while (n % pri[i] == 0) ++x.b, n /= pri[i];           sz *= (x.b + 1);           v.push_back(x);         }       }       if (n != 1) {         node x;         x.a = n;         x.b = 1;         v.push_back(x);       }       if (v.size() == 0) {         cout <<  NO n ;       } else if (v.size() == 1) {         if (v[0].b < 6) {           cout <<  NO n ;         } else {           cout <<  YES n ;           const int &t = v[0].a;           cout << t <<     << t * t <<     << bn / (t * t * t) <<   n ;         }       } else if (v.size() == 2) {         if (v[0].b + v[1].b < 4) {           cout <<  NO n ;         } else {           cout <<  YES n ;           cout << v[0].a <<     << v[1].a <<     << bn / v[0].a / v[1].a <<   n ;         }       } else {         cout <<  YES n ;         cout << v[0].a <<     << v[1].a <<     << bn / v[0].a / v[1].a <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   using min_queue = priority_queue<T, vector<T>, greater<T>>;   const long long MOD = 1e9 + 7;   vector<int> g[1010];   long long edg[1010][1010];   map<pair<int, int>, long long> res;   int leaf[1010];   int N;   void dfs(int v, int p) {     leaf[v] = v;     for (int w : g[v]) {       if (w == p) continue;       dfs(w, v);       leaf[v] = leaf[w];     }   }   bool dfs3(int v, int l2, int p, long long r) {     if (v == l2) return 1;     for (int w : g[v]) {       if (w == p) continue;       if (dfs3(w, l2, v, r)) {         edg[v][w] += r;         edg[w][v] += r;         return 1;       }     }     return 0;   }   void add(int l1, int l2, long long r) {     if (r == 0) return;     if (l1 > l2) swap(l1, l2);     res[{l1, l2}] += r;     dfs3(l1, l2, 0, -r);   }   void dfs2(int v, int p, long long r, int l1, int l2) {     add(leaf[v], l1, r / 2);     add(leaf[v], l2, r / 2);     add(l1, l2, -r / 2);     vector<int> ww;     for (int w : g[v]) {       if (w != p) ww.push_back(w);     }     for (int w : g[v]) {       long long r = edg[v][w];       if (w == p) continue;       if (ww.size() > 1) {         if (ww[0] == w)           l2 = leaf[ww[1]];         else           l2 = leaf[ww[0]];       }       dfs2(w, v, r, l1, l2);     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> N;     for (int i = 0; i < N - 1; i++) {       int a, b;       cin >> a >> b;       long long val;       cin >> val;       g[a].push_back(b);       g[b].push_back(a);       edg[a][b] = edg[b][a] = val;     }     bool good = 1;     int rt = 0;     for (int i = 1; i <= N; i++) {       if (g[i].size() != 2) {         if (rt == 0 || g[i].size() >= 3) rt = i;         continue;       }       if (edg[i][g[i][0]] != edg[i][g[i][1]]) {         good = 0;         break;       }     }     if (!good) {       cout <<  NO n ;       return 0;     }     cout <<  YES n ;     dfs(rt, 0);     if (g[rt].size() == 1) {       long long v = edg[rt][g[rt][0]];       cout << 1 << endl;       cout << rt <<     << leaf[rt] <<     << v << endl;       return 0;     }     set<int> ch;     for (int w : g[rt]) {       if (ch.size() >= 3) break;       ch.insert(w);     }     for (int w : g[rt]) {       set<int> ch1 = ch;       ch1.erase(w);       int l1 = leaf[*ch1.begin()];       int l2 = leaf[*ch1.rbegin()];       dfs2(w, rt, edg[rt][w], l1, l2);     }     int cnt = 0;     for (auto pr : res)       if (pr.second != 0 && pr.first.first != pr.first.second) cnt++;     cout << cnt <<   n ;     for (auto pr : res) {       if (pr.second != 0 && pr.first.first != pr.first.second)         cout << pr.first.first <<     << pr.first.second <<     << pr.second              << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ;     return x * f;   }   const int MAXN = 150010;   const int INF = 2147483600;   int N;   long long A, B, C, D;   inline long long Query(long long t) {     if (t * D > C || t < 0) return INF;     return (t * (t + 1) / 2 * D) * B - (t + 1) * A;   }   inline long long Qt(long long l, long long r) {     long long fans = INF, pos = 0;     r /= D;     while (l <= r) {       long long mid = (l + r) >> 1;       if (Query(mid + 1) - Query(mid) < 0)         l = mid + 1;       else         pos = mid, r = mid - 1;     }     return min(Query(pos - 1), min(Query(pos), Query(pos + 1)));   }   int main() {     int T = read();     while (T--) {       A = read(), B = read(), C = read(), D = read();       if (A > C * B) {         puts( -1 );         continue;       }       if (D >= C) {         printf( %lld n , A);       } else {         long long l = 0, r = C - 1;         long long ans = Qt(0, C);         printf( %lld n , max(-ans, 0LL));       }     }     return 0;   }