malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int nmax = 5e5 + 42;   int n, m;   struct edges {     int u, v, w;     int id_original;   };   edges inp[nmax], all[nmax];   bool cmp(edges a, edges b) { return a.w < b.w; }   int parent[nmax];   int root(int node) {     if (parent[node] == node) return node;     parent[node] = root(parent[node]);     return parent[node];   }   int max_cost = 0;   int group[nmax], id = 0;   bool can_be[nmax];   bool cmp2(int edge_1, int edge_2) { return group[edge_1] < group[edge_2]; }   int sz, test[nmax];   map<pair<int, int>, vector<int> > types;   int main() {     scanf( %i%i , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %i%i%i , &inp[i].u, &inp[i].v, &inp[i].w);       inp[i].id_original = i;       all[i] = inp[i];     }     sort(all + 1, all + m + 1, cmp);     for (int i = 1; i <= n; i++) parent[i] = i;     for (int i = 1; i <= m; i++) {       int j = i;       while (j <= m && all[i].w == all[j].w) j++;       j--;       max_cost = all[i].w;       types = {};       for (int k = i; k <= j; k++) {         if (root(all[k].u) != root(all[k].v)) {           can_be[all[k].id_original] = 1;           pair<int, int> work = {root(all[k].u), root(all[k].v)};           if (work.first > work.second) swap(work.first, work.second);           types[work].push_back(all[k].id_original);         }       }       for (auto w : types) {         id++;         for (auto k : w.second) group[k] = id;       }       for (int k = i; k <= j; k++) parent[root(all[k].u)] = root(all[k].v);       i = j;     }     for (int i = 1; i <= n; i++) parent[i] = i;     int q;     scanf( %i , &q);     for (int i = 1; i <= q; i++) {       scanf( %i , &sz);       for (int j = 1; j <= sz; j++) scanf( %i , &test[j]);       bool can = 1;       for (int j = 1; j <= sz; j++)         if (can_be[test[j]] == 0) {           can = 0;           break;         }       sort(test + 1, test + sz + 1, cmp2);       for (int j = 2; j <= sz; j++)         if (group[test[j - 1]] == group[test[j]]) can = 0;       for (int j = 1; j <= sz; j++)         if (root(inp[test[j]].u) == root(inp[test[j]].v))           can = 0;         else           parent[root(inp[test[j]].u)] = root(inp[test[j]].v);       for (int j = 1; j <= sz; j++)         parent[inp[test[j]].u] = inp[test[j]].u,         parent[inp[test[j]].v] = inp[test[j]].v;       if (can)         printf( YES n );       else         printf( NO n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       x = (x << 1) + (x << 3) + c - 48;       c = getchar();     }     return x * f;   }   const int N = 510;   const int mod = 998244353;   int n, cnt1, cnt2, cnt3, cnt4;   int s[N][N], A[N][N], C[N][N];   int a[N], b[N];   int f[N], g[N], G[N];   int tmp[N], ans[N];   int vis[N << 1], nxt[N];   int ru[N];   void dfs(int u, int fa) {     vis[u] = 1;     if (nxt[u]) {       if (!vis[nxt[u]])         dfs(nxt[u], fa);       else         cnt3++;     } else {       if (fa > n && u > n)         cnt4++;       else if (fa <= n && u > n)         cnt1++;       else if (fa > n && u <= n)         cnt2++;     }   }   void init() {     s[0][0] = C[0][0] = A[0][0] = 1;     for (int i = 1; i <= n; i++) {       C[i][0] = A[i][0] = 1;       for (int j = 1; j <= i; j++) {         C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;         A[i][j] = (A[i - 1][j] + 1ll * j * A[i - 1][j - 1] % mod) % mod;         s[i][j] = (1ll * (i - 1) * s[i - 1][j] % mod + s[i - 1][j - 1]) % mod;       }     }   }   void getans(int n, int *f) {     for (int i = 0; i <= n; i++) {       for (int j = i; j <= n; j++) {         f[i] = (f[i] +                 1ll * C[n][j] * s[j][i] % mod * A[n - j + cnt4][n - j] % mod) %                mod;       }     }     memset(G, 0, sizeof G);     for (int i = 0; i <= n; i++) {       for (int j = i, fz = 1; j <= n; j++, fz = -fz)         G[i] = (G[i] + 1ll * C[j][i] * f[j] % mod * fz + mod) % mod;     }     memcpy(f, G, sizeof(G));   }   int main() {     n = read();     for (int i = 1; i <= n + n; i++) vis[i] = 1;     for (int i = 1; i <= n; i++) a[i] = read();     for (int i = 1; i <= n; i++) b[i] = read();     for (int i = 1; i <= n; i++) {       int x = a[i] ? a[i] : i + n;       int y = b[i] ? b[i] : i + n;       vis[x] = vis[y] = 0;       if (x <= n || y <= n) nxt[x] = y, ru[y]++;     }     for (int i = 1; i <= n + n; i++)       if (!vis[i] && !ru[i]) dfs(i, i);     for (int i = 1; i <= n + n; i++)       if (!vis[i]) dfs(i, i);     init();     getans(cnt1, f);     getans(cnt2, g);     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= i; j++) {         tmp[i] = (tmp[i] + 1ll * f[j] * g[i - j] % mod) % mod;       }     }     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= i; j++) {         ans[i] = (ans[i] + 1ll * tmp[j] * s[cnt4][i - j] % mod) % mod;       }       ans[i] = 1ll * ans[i] * A[cnt4][cnt4] % mod;     }     for (int i = 0; i < n; i++)       printf( %d  , (n - i - cnt3 < 0) ? 0 : ans[n - i - cnt3]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = numeric_limits<int>::max();   const long long LLINF = numeric_limits<long long>::max();   const unsigned long long ULLINF = numeric_limits<unsigned long long>::max();   const double PI = acos(-1.0);   vector<int> ans;   void solve(int n, int p, int maxN) {     if (!p) return;     int mx = n - (p - 1);     int cur = min(maxN, mx);     ans.push_back(cur);     solve(n - cur, p - 1, maxN);   }   vector<char> type;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     char c;     type.push_back(1);     int p = 1, m = 0;     cin >> c;     while (c !=  = ) {       if (c ==  + ) {         p++;         type.push_back( + );       } else if (c ==  - ) {         type.push_back( - );         m++;       }       cin >> c;     }     int n;     cin >> n;     if (!p) {       cout <<  Impossible ;       return 0;     }     if (!m) {       if (p > n) {         cout <<  Impossible ;         return 0;       }       cout <<  Possible n ;       solve(n, p, n);       cout << ans.back() <<    ;       ans.pop_back();       while (ans.size()) {         cout <<  +   << ans.back() <<    ;         ans.pop_back();       }       cout <<  =   << n;       return 0;     }     vector<int> positive, negative;     bool found = 0;     for (int pos = n; pos <= n * p; pos++) {       int neg = pos - n;       if (neg / m > 0 && n * m >= neg) {         if (p > pos) continue;         solve(pos, p, n);         positive = ans;         solve(neg, m, n);         negative = ans;         found = 1;         break;       }     }     if (!found) {       cout <<  Impossible ;       return 0;     }     cout <<  Possible n ;     cout << positive.back() <<    ;     positive.pop_back();     for (int i = 1; i < type.size(); i++) {       if (type[i] ==  + ) {         cout <<  +   << positive.back() <<    ;         positive.pop_back();       } else {         cout <<  -   << negative.back() <<    ;         negative.pop_back();       }     }     cout <<  =   << n;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using namespace std::chrono;   inline long long lrand() {     long long x = 0;     for (int i = 0; i < (4); i++) {       x <<= 16;       x ^= rand();     }     return x;   }   string fname(string s, vector<long long> v) {     string x =  C:/leet/  + s;     for (auto& k : (v)) x +=  _  + to_string(k);     x +=  .txt ;     return x;   }   long long now() {     return duration_cast<milliseconds>(system_clock::now().time_since_epoch())         .count();   }   template <typename T>   void W(vector<T>& v) {     for (int i = 0; i < (v.size()); i++) cout << v[i] <<    ;   }   int main(int argc, char** argv) {     cin.sync_with_stdio(false);     long long n;     scanf( %lld , &n);     vector<long long> a(100007, 0), x(100007, 0);     for (int i = (1); i <= (n); i++) scanf( %lld , &a[i]);     for (int i = (1); i <= (n); i++) {       x[i] = x[i - 1];       x[i] = max(x[i], a[i] + 1);     }     for (int i = n - 1; i >= 1; i--) {       x[i] = max(x[i], x[i + 1] - 1);     }     long long sum = 0;     for (int i = 1; i <= n; i++) {       sum += x[i] - a[i] - 1;     }     printf( %lld n , sum);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> e[200100], g[200100];   int n, m, q;   int low[200100], dfn[200100], sta[200100], idx, top;   bool in[200100];   void tarjan(int u, int pre) {     low[u] = dfn[u] = ++idx;     sta[top++] = u;     in[u] = true;     for (auto v : g[u]) {       if (v == pre) continue;       if (!dfn[v]) {         tarjan(v, u);         if (low[u] > low[v]) low[u] = low[v];         if (low[v] >= dfn[u]) {           ++m;           e[u].push_back(m);           e[m].push_back(u);           int vn;           do {             vn = sta[--top];             e[vn].push_back(m);             e[m].push_back(vn);             in[vn] = 0;           } while (vn != v);         }       } else if (in[v] && low[u] > dfn[v])         low[u] = dfn[v];     }   }   int fa[200100][20], dep[200100], dis[200100];   void dfs(int u, int pre) {     fa[u][0] = pre;     for (int i = 1; i < 20; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1];     dep[u] = dep[pre] + 1;     dis[u] = dis[pre] + (u <= n);     for (auto v : e[u])       if (v != pre) dfs(v, u);   }   int LCA(int u, int v) {     if (dep[u] > dep[v]) swap(u, v);     int hu = dep[u], hv = dep[v];     int tu = u, tv = v;     for (int det = hv - hu, i = 0; det; det >>= 1, i++)       if (det & 1) tv = fa[tv][i];     if (tu == tv) return tu;     for (int i = 19; i >= 0; --i) {       if (fa[tu][i] == fa[tv][i]) continue;       tu = fa[tu][i];       tv = fa[tv][i];     }     return fa[tu][0];   }   int main() {     scanf( %d%d%d , &n, &m, &q);     while (m--) {       int u, v;       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     m = n;     tarjan(1, -1);     dfs(1, 1);     while (q--) {       int u, v;       scanf( %d%d , &u, &v);       int lca = LCA(u, v);       int ans = dis[u] + dis[v] - 2 * dis[lca] - (lca > n);       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int a[1000001], has[1000001];   int main() {     long long int n, i;     cin >> n;     for (i = 0; i < n; i++) {       cin >> a[i];       has[a[i]] = 1;     }     long long int c = 0;     for (i = 1; i <= n; i++)       if (has[i]) c++;     cout << n - c;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 100;   const long long inf = 1e18;   vector<int> g[maxn];   bool visit[maxn];   int cnt = 0;   void dfs(int u) {     cnt = 0;     queue<int> q;     q.push(u);     visit[u] = true;     cnt++;     while (!q.empty()) {       int u = q.front();       q.pop();       for (auto v : g[u])         if (visit[v] == false) {           q.push(v);           visit[v] = true;           cnt++;         }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int n, m;     cin >> n >> m;     vector<int> st;     int u, v;     memset(visit, false, sizeof visit);     for (int i = 1; i <= m; i++) {       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);       st.push_back(u);       st.push_back(v);     }     int ans = 0;     for (auto v : st) {       if (visit[v] == false) {         dfs(v);         ans += (cnt - 1);         cnt = 0;       }     }     cout << (m - ans) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<vector<int>> graph(200005), revGraph(200005);   vector<int> vis(200005, 0), revVis(200005, 0);   bool dfs(int u, vector<vector<int>> &curGraph, vector<int> &curVis) {     curVis[u] = -1;     for (auto v : curGraph[u]) {       if (curVis[v] == -1) return false;       if (curVis[v] == 1) continue;       if (!dfs(v, curGraph, curVis)) return false;     }     curVis[u] = 1;     return true;   }   int main() {     cin.tie(0);     ios_base::sync_with_stdio(false);     int n, edges;     cin >> n >> edges;     int first, second;     for (int i = 0; i < edges; ++i) {       cin >> first >> second;       graph[first].push_back(second);       revGraph[second].push_back(first);     }     int qtt = 0;     string ans =   ;     for (int i = 1; i <= n; ++i) {       if (!vis[i] && !revVis[i]) {         ++qtt;         ans.push_back( A );       } else {         ans.push_back( E );       }       if (!dfs(i, graph, vis) || !dfs(i, revGraph, revVis)) {         qtt = -1;         break;       }     }     cout << qtt <<   n ;     if (qtt != -1) {       cout << ans <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   string to_string(const vector<T>& vc, int w) {     if (vc.empty()) return   ;     if (w + 1 == vc.size()) return to_string(vc[w]);     return to_string(vc[w]) +  ,  + to_string(vc, w + 1);   }   template <typename T>   string to_string(const vector<T>& vc) {     return  {  + to_string(vc, 0) +  } ;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   class DebugStream {   } LOG;   template <typename T>   DebugStream& operator<<(DebugStream& s, const T&) {     return s;   }   const int MAX = 200200;   int a[MAX];   int dp[MAX];   int cnt[MAX];   bool isLast[MAX], isFirst[MAX];   int b[MAX], pointer[MAX], q[MAX];   int main() {     int T;     scanf( %d , &T);     for (int t = 0; t < int(T); t++) {       int n;       scanf( %d , &n);       for (int i = 0; i < int(n); i++)         scanf( %d , a + i), isLast[i] = false, isFirst[i] = false, b[i] = a[i];       map<int, int> dic, last, first, invLast;       for (int i = n - 1; i >= 0; i--) {         if (dic[a[i]] == 0) {           last[a[i]] = i;           isLast[i] = true;           invLast[a[i]] = i;         }         dic[a[i]]++;         cnt[i] = dic[a[i]];       }       dic.clear();       for (int i = 0; i < int(n); i++) {         if (dic[a[i]] == 0) {           first[a[i]] = i;           isFirst[i] = true;         }         dic[a[i]]++;       }       map<int, int> nxt;       sort(b, b + n);       for (int i = 0; i < int(n - 1); i++) {         nxt[b[i]] = b[i + 1];       }       nxt[b[n - 1]] = -1;       dic.clear();       for (int i = n - 1; i >= 0; i--) {         if (dic.count(a[i])) {           q[i] = dic[a[i]];         } else {           q[i] = -1;         }         dic[a[i]] = i;         if (nxt[a[i]] != -1 && dic.count(nxt[a[i]]) == 1) {           pointer[i] = dic[nxt[a[i]]];         } else {           pointer[i] = -1;         }       }       int ans = 0;       for (int i = n - 1; i >= 0; i--) {         int j = i;         dp[i] = cnt[j];         j = invLast[a[i]];         if (pointer[j] != -1) {           if (isFirst[pointer[j]]) {             dp[i] += dp[pointer[j]];           } else {             dp[i] += cnt[pointer[j]];           }         }         ans = max(ans, dp[i]);         42;       }       printf( %d n , n - ans);     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   int n, x[N], y[N];   struct pt {     int s, id, ans;   } p[N], w[N];   bool cmpID(pt A, pt B) { return A.id < B.id; }   bool cmpSw(pt A, pt B) { return (A.s < B.s) || (A.s == B.s && A.id < B.id); }   bool cmpSp(pt A, pt B) {     return (A.s < B.s) ||            (A.s == B.s && x[A.id] <= x[B.id] && y[A.id] <= y[B.id]);   }   int maxX, lb[N << 2], rb[N << 2], maxv[N << 2];   void build(int l, int r, int i) {     lb[i] = l;     rb[i] = r;     maxv[i] = -1;     if (l < r) {       build(l, ((lb[i] + rb[i]) / 2), ((i) << 1));       build(((lb[i] + rb[i]) / 2) + 1, r, (((i) << 1) + 1));     }   }   void modify(int p, int v, int i) {     if (lb[i] == rb[i])       maxv[i] = max(maxv[i], v);     else {       if (p <= ((lb[i] + rb[i]) / 2))         modify(p, v, ((i) << 1));       else         modify(p, v, (((i) << 1) + 1));       maxv[i] = max(maxv[((i) << 1)], maxv[(((i) << 1) + 1)]);     }   }   int findmax(int l, int r, int i) {     if (l > r) return -1;     if (l == lb[i] && r == rb[i]) return maxv[i];     return max(findmax(l, min(r, ((lb[i] + rb[i]) / 2)), ((i) << 1)),                findmax(max(l, ((lb[i] + rb[i]) / 2) + 1), r, (((i) << 1) + 1)));   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= (n); i++) {       scanf( %d%d , &x[i], &y[i]);       p[i].s = y[i] - x[i];       p[i].id = i;       maxX = max(x[i], maxX);     }     for (int i = 1; i <= (n); i++) {       scanf( %d , &w[i].s);       w[i].id = i;     }     sort(p + 1, p + n + 1, cmpSp);     sort(w + 1, w + n + 1, cmpSw);     bool flag = true;     for (int i = 1; i <= (n); i++) {       w[i].ans = p[i].id;       if (w[i].s != p[i].s) flag = false;     }     if (!flag) {       printf( NO n );       return 0;     }     sort(w + 1, w + n + 1, cmpID);     build(0, maxX, 1);     for (int i = 1; i <= (n); i++) {       if (findmax(x[w[i].ans], maxX, 1) >= y[w[i].ans]) flag = false;       modify(x[w[i].ans], y[w[i].ans], 1);     }     if (!flag) {       printf( NO n );       return 0;     }     printf( YES n );     for (int i = 1; i <= (n); i++) printf( %d %d n , x[w[i].ans], y[w[i].ans]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 3100;   int n, m;   vector<int> eds[MAXN];   vector<int> reds[MAXN];   vector<pair<int, int> > go[MAXN];   vector<pair<int, int> > rgo[MAXN];   int dd[MAXN];   int ddg[MAXN][MAXN];   void bfs(int v) {     for (int i = 0; i < n; ++i) dd[i] = -1;     dd[v] = 0;     queue<int> qu;     qu.push(v);     while (!qu.empty()) {       int x = qu.front();       qu.pop();       for (int u : eds[x])         if (dd[u] == -1) dd[u] = dd[x] + 1, qu.push(u);     }   }   void rbfs(int v) {     for (int i = 0; i < n; ++i) dd[i] = -1;     dd[v] = 0;     queue<int> qu;     qu.push(v);     while (!qu.empty()) {       int x = qu.front();       qu.pop();       for (int u : reds[x])         if (dd[u] == -1) dd[u] = dd[x] + 1, qu.push(u);     }   }   int main() {     cin >> n >> m;     for (int i = 0; i < m; ++i) {       int a, b;       cin >> a >> b;       --a;       --b;       eds[a].push_back(b);       reds[b].push_back(a);     }     for (int i = 0; i < n; ++i) {       bfs(i);       int mn1 = i, mn2 = i, mn3 = i;       for (int j = 0; j < n; ++j) {         if (i == j) continue;         if (dd[j] == -1) continue;         if (dd[mn3] < dd[j]) mn3 = j;         if (dd[mn3] > dd[mn2]) swap(mn2, mn3);         if (dd[mn2] > dd[mn1]) swap(mn1, mn2);       }       go[i].push_back(make_pair(mn1, dd[mn1]));       go[i].push_back(make_pair(mn2, dd[mn2]));       go[i].push_back(make_pair(mn3, dd[mn3]));       for (int j = 0; j < n; ++j) ddg[i][j] = dd[j];       rbfs(i);       mn1 = i, mn2 = i, mn3 = i;       for (int j = 0; j < n; ++j) {         if (i == j) continue;         if (dd[j] == -1) continue;         if (dd[mn3] < dd[j]) mn3 = j;         if (dd[mn3] > dd[mn2]) swap(mn2, mn3);         if (dd[mn2] > dd[mn1]) swap(mn1, mn2);       }       rgo[i].push_back(make_pair(mn1, dd[mn1]));       rgo[i].push_back(make_pair(mn2, dd[mn2]));       rgo[i].push_back(make_pair(mn3, dd[mn3]));     }     int bst = -1;     int ba = 0, bb = 0, bc = 0, bd = 0;     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) {         if (i == j) continue;         if (ddg[i][j] == -1) continue;         for (int i1 = 0; i1 < (int)rgo[i].size(); ++i1)           for (int j1 = 0; j1 < (int)go[j].size(); ++j1) {             if (rgo[i][i1].second <= 0) continue;             if (go[j][j1].second <= 0) continue;             int a = rgo[i][i1].first;             int b = go[j][j1].first;             if (a == b || a == j || b == i) continue;             int sum = ddg[a][i] + ddg[i][j] + ddg[j][b];             if (sum > bst) {               ba = a;               bb = i;               bc = j;               bd = b;               bst = sum;             }           }       }     cout << ba + 1 <<     << bb + 1 <<     << bc + 1 <<     << bd + 1 <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long i, j, n, c[223456], d[223456], a[456789], l, r, s, t, tt, k, x, y, z,       m, b[1111111];   string p, q, du, qq;   vector<long long> u[345678], uu, ui;   multiset<long long> mm;   int main() {     cin >> n >> t;     for (i = 0; i < n; i++) {       cin >> a[i];     }     m = t;     cin >> k;     for (i = 0; i < n; i++) {       cin >> b[i];       mm.insert(b[i]);       while (t < a[i]) {         if (mm.empty()) {           cout << -1;           return 0;         }         s += *mm.begin();         mm.erase(mm.begin());         t += k;       }     }     cout << s;     return 0;   }
#include <bits/stdc++.h>   using namespace std;            #define ios   ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)   #define FOR(i,m)  for(int i = 0 ;i  < m ;i++)   #define mem(a ,b)  memset(a,  (b) , sizeof(a))   #define all(cont)  cont.begin(),cont.end()   #define mp   make_pair   #define pb   push_back   #define F   first   #define S   second   #define cn   cout<<  n          typedef pair<int,int>  pi;   typedef vector<int>  vi;   typedef long long   ll;   typedef long double  ld;   typedef unsigned long long int  ull;         const int maxN = 1e5 + 1;   const int mod  = 1e9 + 7;               void solve(){       int n;    cin >> n;    int a[n];       for(int i = 0 ;i <  n;i++) cin >> a[i];       cout<<n/2<<  n ;    for(int i = 0 ; i + 1< n; i+=2 ){     cout<<i+1<<   <<i+2<<   <<min(a[i] , a[i+1]) <<    <<mod<<  n ;    }                  }               int main(){               int T = 1;    cin >> T;        while(T--){     solve();    }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct code {     int x, y;   };   int res, k;   string s;   int gcd(int a, int b) {     while (b) {       a %= b;       swap(a, b);     }     return a;   }   int a[100001], ans;   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     int ans = 0;     while (1) {       int ok = 0;       for (int i = 2; i <= n; i++) {         if (a[1] > a[i]) ok++;       }       if (ok == n - 1) break;       int q = -1e9, id = 0;       for (int j = 2; j <= n; j++) {         if (q < a[j]) {           q = a[j];           id = j;         }       }       a[1]++;       a[id]--;       ans++;     }     cout << ans;   }