malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5, M = N * 3;   int n, i, j, l, r, scnt, s1;   bool bb[M];   int w1[M], bbel[M];   unsigned long long w2[M];   struct seg {     int l, r, c, bel;     bool operator<(const seg& rhs) const { return r < rhs.r; }   };   set<seg> S;   vector<int> ve[N], in[N], out[N];   void del(seg s) {     S.erase(s);     out[i].push_back(s.c);   }   void ins(seg s) {     s.c = ++scnt;     w1[scnt] = s.r - s.l + 1;     w2[scnt] = 1ll * w1[scnt] * s.bel;     bbel[scnt] = s.bel;     S.insert(s);     in[i].push_back(s.c);     ve[s.bel].push_back(s.c);   }   unsigned long long s2, s3, s4, k;   void rmv(int x) {     if (bb[x]) bb[x] = 0, s1 -= w1[x], s2 -= w2[x];   }   void calc(int lm) {     s1 = s2 = s3 = s4 = 0;     memset(bb + 1, 0, scnt);     for (i = 1, j = 0; i <= n; ++i) {       for (int x : out[i]) rmv(x);       for (int x : in[i])         if (bbel[x] >= j) bb[x] = 1, s1 += w1[x], s2 += w2[x];       for (; s1 > lm || !j; ++j)         for (int x : ve[j]) rmv(x);       s3 += s2 - 1ll * s1 * (j - 1);       s4 += i - j + 1;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> k;     S.insert((seg){1, 1 << 30, 0, 0});     for (i = 1; i <= n; ++i) {       cin >> l >> r;       --r;       auto it = S.lower_bound((seg){0, l, 0, 0});       if (it->l < l) ins((seg){it->l, l - 1, 0, it->bel});       if (it->r >= r) {         seg tmp = *it;         del(*it);         if (r < tmp.r) tmp.l = r + 1, ins(tmp);       } else {         for (; it != S.end() && it->r <= r;) del(*it++);         if (it != S.end() && it->l <= r) {           seg tmp = *it;           del(*it);           tmp.l = r + 1;           ins(tmp);         }       }       ins((seg){l, r, 0, i});     }     calc(1 << 30);     unsigned long long ts3 = s3;     k = s4 - k;     int le = 0, ri = 1e9, mid;     for (; le < ri;) {       mid = le + ri >> 1;       calc(mid);       if (s4 >= k)         ri = mid;       else         le = mid + 1;     }     calc(le);     cout << ts3 - (s3 - 1ll * (s4 - k) * le) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int w, h;   char g[100][100], d[100][100];   bool use[100][100];   int dx[] = {-1, 0, 1, 0};   int dy[] = {0, -1, 0, 1};   bool is_valid(int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; }   bool check() {     for (int i = 0; i < h; i++) {       for (int j = 0; j < w; j++) {         if (g[i][j] ==  B  && !use[i][j]) return true;       }     }     return false;   }   void dfs(int y, int x, int dir, bool can) {     if (!is_valid(y, x)) return;     if (g[y][x] ==  W ) return;     use[y][x] = true;     if (!can)       dfs(y + dy[dir], x + dx[dir], dir, can);     else {       dfs(y + dy[dir], x + dx[dir], dir, can);       for (int i = 0; i < 4; i++) {         if (i == dir) continue;         dfs(y + dy[i], x + dx[i], i, false);       }     }     return;   }   int main() {     cin >> h >> w;     for (int i = 0; i < h; i++) {       for (int j = 0; j < w; j++) {         cin >> g[i][j];         d[i][j] = g[i][j];       }     }     for (int i = 0; i < h; i++) {       for (int j = 0; j < w; j++) {         memset(use, 0, sizeof(use));         if (g[i][j] ==  W ) continue;         for (int k = 0; k < 4; k++) {           dfs(i, j, k, true);         }         if (check()) {           cout <<  NO  << endl;           return 0;         }       }     }     cout <<  YES  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   constexpr int MAX_N = 5000;   constexpr int INF = numeric_limits<int>::max();   int N;   int H[MAX_N + 1];   int DP[MAX_N + 1][MAX_N + 1];   void print(int* ptr, int n) {     printf( [  );     while (n > 0) {       printf( %d  , *ptr);       ++ptr;       --n;     }     printf( ] n );   }   int main() {     cin >> N;     if (N < 2) {       cout << 0 << endl;       return 0;     }     for (int i = 0; i < N; i++) {       cin >> H[i];     }     DP[0][0] = H[0];     for (int i = 1; i < N; i++) {       if (DP[0][i - 1] == -1) {         DP[0][i] = -1;       } else {         DP[0][i] = (H[i] >= H[i - 1]) ? H[i] : -1;       }     }     if (DP[0][N - 1] != -1) {       cout << 0 << endl;       return 0;     }     for (int n = 1; n < N; n++) {       DP[n][0] = H[0];       for (int i = 1; i < N; i++) {         DP[n][i] = INF;         if (DP[n - 1][i] != -1) {           DP[n][i] = min(DP[n][i], DP[n - 1][i]);         }         if (DP[n][i - 1] != -1 && DP[n][i - 1] <= H[i]) {           DP[n][i] = min(DP[n][i], H[i]);         }         if (DP[n - 1][i - 1] != -1) {           DP[n][i] = min(DP[n][i], H[i] + DP[n - 1][i - 1]);         }         int S = H[i];         for (int m = n - 1; m >= 0; m--) {           int j = i - (n - m);           S += H[j];           if (j < 0 || (j > 0 && DP[m][j - 1] == -1)) break;           if ((j == 0) || S >= DP[m][j - 1]) {             DP[n][i] = min(DP[n][i], S);             break;           }         }       }       for (int j = 1; j < N; j++) {         DP[n][j] = (DP[n][j] == INF) ? -1 : DP[n][j];       }       if (DP[n][N - 1] != -1) {         cout << n << endl;         return 0;       }     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   const double PI = acos(-1.0);   using namespace std;   const double EPS = 1e-9;   const int N = 5e5 + 9;   int n, mem[3009][2 * 3009], nxt[3009];   pair<int, int> a[3009];   bool com(pair<int, int> a, pair<int, int> b) {     if (a.first < b.first)       return 1;     else if (a.first == b.first) {       if (a.second > b.second)         return 1;       else         return 0;     } else       return 0;   }   int dp(int l, int mx) {     if (l > n || a[l].first > mx) return 0;     int& ret = mem[l][mx];     if (~ret) return ret;     ret = dp(l + 1, mx);     if (a[l].second <= mx)       ret = max(ret, 1 + dp(l + 1, a[l].second) + dp(nxt[l], mx));     return ret;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       cin >> n;       map<int, int> mp;       for (int i = 1; i <= n; i++) {         cin >> a[i].first >> a[i].second;         mp[a[i].first];         mp[a[i].second];       }       int id = 0;       for (auto& p : mp) p.second = ++id;       for (int i = 1; i <= n; i++) {         a[i].first = mp[a[i].first];         a[i].second = mp[a[i].second];       }       sort(a + 1, a + 1 + n, com);       for (int i = 1; i <= n; i++) {         nxt[i] = n + 1;         for (int j = i + 1; j <= n; j++) {           if (a[j].first > a[i].second) {             nxt[i] = j;             break;           }         }       }       for (int i = 0; i <= n + 2; i++)         for (int j = 0; j <= n * 2 + 9; j++) mem[i][j] = -1;       cout << dp(1, n * 2 + 2) << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int dis[10001][1001];   int pos[10001];   int g, r, n, m;   void bfs() {     deque<pair<int, pair<int, int> > > fila;     fila.push_front({0, {0, 0}});     while (!fila.empty()) {       int v = fila.front().first;       int s = fila.front().second.first;       int t = fila.front().second.second;       fila.pop_front();       if (dis[v][s] != -1) continue;       dis[v][s] = t;       if (v) {         if (s < g and s + (pos[v] - pos[v - 1]) <= g and             dis[v - 1][s + (pos[v] - pos[v - 1])] == -1) {           fila.push_front({v - 1, {s + (pos[v] - pos[v - 1]), t}});         }       }       if (v < m - 1) {         if (s < g and s + (pos[v + 1] - pos[v]) <= g and             dis[v + 1][s + (pos[v + 1] - pos[v])] == -1) {           fila.push_front({v + 1, {s + (pos[v + 1] - pos[v]), t}});         }       }       if (s == g and dis[v][0] == -1) {         fila.push_back({v, {0, t + 1}});       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     memset(dis, -1, sizeof(dis));     cin >> n >> m;     int l[m];     for (int i = 0; i < m; i++) cin >> l[i];     sort(l, l + m);     for (int i = 0; i < m; i++) pos[i] = l[i];     cin >> g >> r;     bfs();     int res = -1;     for (int i = 0; i < m - 1; i++) {       if (dis[i][0] == -1) continue;       if ((pos[m - 1] - pos[i]) > g) continue;       int atual = dis[i][0] * (g + r) + (pos[m - 1] - pos[i]);       if (res == -1)         res = atual;       else         res = min(res, atual);     }     cout << res << endl;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   inline void in(int &x) { scanf( %d , &x); }   inline void in(int &x, int &y) { scanf( %d %d , &x, &y); }   inline void in(int &x, int &y, int &z) { scanf( %d %d , &x, &y, &z); }   inline void out(int x) { printf( %d  , x); }   inline void outln(int x) { printf( %d n , x); }   inline void outln(int x, int y) { printf( %d %d n , x, y); }   inline int _abs(int x) { return x > 0 ? x : -x; }   const int SIZE = (int)1e7 + 10;   int n, m;   int a[SIZE], b[SIZE], minp[SIZE];   int pa[SIZE], pb[SIZE];   void precalc() {     for (int i = 2; i * i < SIZE; ++i) {       if (!minp[i]) {         for (int j = i * i; j < SIZE; j += i) {           if (!minp[j]) minp[j] = i;         }       }     }     for (int i = 2; i < SIZE; ++i)       if (!minp[i]) minp[i] = i;   }   void factor(int *mas, int val) {     for (; val != 1; val /= minp[val]) {       ++mas[minp[val]];     }   }   void output(int *a, int *pa, int size) {     for (int i = 0; i < (int)(size); i++) {       int val = a[i], res = 1;       for (; val != 1; val /= minp[val]) {         if (pa[minp[val]])           --pa[minp[val]];         else           res *= minp[val];       }       out(res);     }   }   int main() {     precalc();     in(n, m);     for (int i = 0; i < (int)(n); i++) {       in(a[i]);       factor(pa, a[i]);     }     for (int i = 0; i < (int)(m); i++) {       in(b[i]);       factor(pb, b[i]);     }     for (int i = 2; i < SIZE; ++i) {       pa[i] = pb[i] = min(pa[i], pb[i]);     }     outln(n, m);     output(a, pa, n);     printf(  n );     output(b, pb, m);     return 0;   }
#include <bits/stdc++.h>   const int N = 1000005;   std::vector<int> e[N];   int a[N], n, m, x, y, r[N], c[N], id[N], cnt, cnt2, s[N], vis[N];   void dfs(int x) {     if (vis[x]) return;     vis[x] = 1;     for (auto u : e[x]) {       dfs(u);       a[x] |= a[u];     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &x, &y);       e[x].push_back(y);       r[y]++;       c[x]++;     }     for (int i = 1; i <= n; i++)       if (!c[i]) a[i] = 1 << cnt, id[i] = cnt++;     for (int i = 1; i <= n; i++)       if (!r[i]) {         dfs(i);         s[cnt2++] = a[i];       }     for (int i = 1; i < (1 << cnt) - 1; i++) {       int t = 0, S = 0, t2 = 0;       for (int j = 0; j < cnt; j++)         if (i & (1 << j)) t++, S |= s[j];       for (int j = 0; j < cnt; j++)         if (S & (1 << j)) t2++;       if (t2 <= t) {         puts( NO );         return 0;       }     }     puts( YES );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long A, B, n;     cin >> A >> B >> n;     long long i, j;     if (A == 0) {       if (B == 0) {         cout << 0;       } else         cout <<  No solution ;       return 0;     }     if (B % A) {       cout <<  No solution ;       return 0;     }     long long rez = B / A;     if (rez == 1) {       cout << 1;       return 0;     }     if (B == 0) {       cout << 0;       return 0;     }     for (long long i = -1002; i <= 1002; i++) {       long long cur = i;       for (j = 2; j <= n; j++) {         cur *= i;         if (abs(cur) > 1000000000) break;       }       if (cur == rez) {         cout << i;         return 0;       }     }     cout <<  No solution ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void geth(int in, int& out) {     cout << in << endl;     cin >> out;     if (out != 1 && out != -1) {       exit(0);     }   }   int main() {     ios_base::sync_with_stdio(false);     int n, mx;     cin >> mx >> n;     std::vector<int> pp(n);     for (auto&& x : pp) {       geth(1, x);     }     int l = 2;     int r = mx;     int tr = 0;     while (l < r) {       int mid = (l + r) / 2, res;       geth(mid, res);       res *= pp[tr];       if (res > 0) {         l = mid + 1;       } else if (res < 0) {         r = mid - 1;       }       tr = (tr + 1) % n;     }     geth(l, r);   }
#include <bits/stdc++.h>   using namespace std;   map<string, int> sz = {       { S , 0}, { M , 1}, { L , 2}, { XL , 3}, { XXL , 4},   };   string st[5] = { S ,  M ,  L ,  XL ,  XXL };   int a[5];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cerr.tie(nullptr);     for (int i = 0; i < 5; i++) cin >> a[i];     int n;     cin >> n;     while (n--) {       string s;       cin >> s;       int x = sz[s];       int bi = -1, bv = 123123123;       for (int i = 0; i < 5; i++) {         if (!a[i]) continue;         int v = 2 * abs(i - x) + (i < x);         if (v < bv) {           bv = v;           bi = i;         }       }       a[bi]--;       cout << st[bi] <<   n ;     }   }
#include<bits/stdc++.h>   using namespace std;      const int N = 3e5 + 9;   const long long inf = 2e18;      int a[N], c[N], n, m;   long long s1[N], s2[N];      long long f(int mid) {     for (int i = mid; i <= n; i++) {       if (a[i] < i - mid + 1) return 0;     }     return 1;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t; cin >> t;     while (t--) {       cin >> n >> m;       for (int i = 1; i <= n; i++) {         cin >> a[i];       }       for (int i = 1; i <= m; i++) {         cin >> c[i];         s2[i] = s2[i - 1] + c[i];       }       sort(a + 1, a + n + 1);       for (int i = 1; i <= n; i++) {         s1[i] = s1[i - 1] + c[a[i]];       }       int l = 1, r = n; int z = n + 1;       while (l <= r) {         int mid = l + r >> 1;         if (f(mid)) {           z = mid; r = mid - 1;         }         else {           l = mid + 1;         }       }       long long ans = inf;       for (int i = z; i <= n + 1; i++) {         ans = min(ans, s1[i - 1] + s2[n - i + 1]);       }       cout << ans <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     while (n--) {       unsigned long long a, b, c;       cin >> a >> b >> c;       unsigned long long d;       d = (a * a + (b + c) * (b + c));       d = sqrt(d) - 1;       cout << d <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     ;     unsigned long long x, y;     cin >> x >> y;     if (x < y) {       return cout << -1, 0;     }     if ((x - y) & 1) {       return cout << -1, 0;     }     long long z = (x - y) / 2;     if (y & z) {       cout << -1;       return 0;     }     long long a = z;     long long b = x - z;     if (a > b) swap(a, b);     cout << a <<     << b <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int N, type[100005];   int Tm, in[100005], out[100005];   int par[100005][20];   bool comp[100005][20][2];   vector<int> G[100005];   void dfs(int nd) {     in[nd] = ++Tm;     for (int i = 0; i < G[nd].size(); i++) {       int ch = G[nd][i];       if (ch == par[nd][0]) continue;       comp[ch][0][type[ch]] = 1;       dfs(ch);     }     out[nd] = ++Tm;   }   int LCA(int u, int v) {     if ((in[u] <= in[v] && out[u] >= out[v])) return u;     for (int i = 20 - 1; i >= 0; i--)       if (!(in[par[u][i]] <= in[v] && out[par[u][i]] >= out[v])) u = par[u][i];     return par[u][0];   }   bool get(int u, int lca, int fl) {     if ((in[u] <= in[lca] && out[u] >= out[lca])) return false;     bool x = true;     for (int i = 20 - 1; i >= 0; i--)       if (!(in[par[u][i]] <= in[lca] && out[par[u][i]] >= out[lca])) {         x = x && comp[u][i][fl];         u = par[u][i];       }     x = x && comp[u][0][fl];     return x;   }   int main() {     scanf( %d , &N);     for (int i = 1; i <= N; i++) {       scanf( %d , &par[i][0]);       scanf( %d , &type[i]);       if (par[i][0] != -1) G[par[i][0]].push_back(i);     }     for (int i = 1; i <= N; i++)       if (par[i][0] == -1) {         par[i][0] = i;         dfs(i);       }     for (int i = 1; i < 20; i++)       for (int j = 1; j <= N; j++) {         par[j][i] = par[par[j][i - 1]][i - 1];         for (int fl = 0; fl < 2; fl++)           comp[j][i][fl] = comp[j][i - 1][fl] && comp[par[j][i - 1]][i - 1][fl];       }     int Q;     scanf( %d , &Q);     for (int i = 0; i < Q; i++) {       int tp, u, v;       scanf( %d , &tp);       scanf( %d , &u);       scanf( %d , &v);       int lca = LCA(u, v);       if (v == lca) {         puts( NO );       } else {         if (tp == 1) {           if (u == lca && get(v, lca, 0))             puts( YES );           else             puts( NO );         } else {           if (u == lca && get(v, lca, 1))             puts( YES );           else if (u != lca && get(u, lca, 0) && get(v, lca, 1))             puts( YES );           else             puts( NO );         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   int x[200000], y[200000];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, s[2];     cin >> n >> s[0] >> s[1];     for (int i = 0; i < n; i++) cin >> x[i] >> y[i];     int quadrant[5] = {         0,     };     int line[5] = {         0,     };     for (int i = 0; i < n; i++) {       if (x[i] > s[0] && y[i] > s[1]) quadrant[1]++;       if (x[i] < s[0] && y[i] > s[1]) quadrant[2]++;       if (x[i] < s[0] && y[i] < s[1]) quadrant[3]++;       if (x[i] > s[0] && y[i] < s[1]) quadrant[4]++;       if (x[i] > s[0] && y[i] == s[1]) line[1]++;       if (x[i] == s[0] && y[i] > s[1]) line[2]++;       if (x[i] < s[0] && y[i] == s[1]) line[3]++;       if (x[i] == s[0] && y[i] < s[1]) line[4]++;     }     int dir[5] = {         0,     };     dir[1] = quadrant[1] + quadrant[4] + line[1];     dir[2] = quadrant[1] + quadrant[2] + line[2];     dir[3] = quadrant[2] + quadrant[3] + line[3];     dir[4] = quadrant[3] + quadrant[4] + line[4];     int maxdir, maxval = 0;     for (int i = 1; i <= 4; i++) {       if (maxval < dir[i]) {         maxdir = i;         maxval = dir[i];       }     }     cout << maxval <<   n ;     if (maxdir == 1)       cout << s[0] + 1 <<     << s[1] <<   n ;     else if (maxdir == 2)       cout << s[0] <<     << s[1] + 1 <<   n ;     else if (maxdir == 3)       cout << s[0] - 1 <<     << s[1] <<   n ;     else       cout << s[0] <<     << s[1] - 1 <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> Left;   vector<int> Right;   map<pair<int, int>, int> excep;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie();     cin >> n;     for (int i = 0; i < n; i++) {       int l, r;       cin >> l >> r;       Left.push_back(l);       Right.push_back(r);       excep[{l, r}] = 1;     }     sort(Left.begin(), Left.end(), greater<int>());     sort(Right.begin(), Right.end());     if (excep[{Left[0], Right[0]}] == 1)       cout << max(0, Right[1] - Left[1]);     else       cout << max(0, max(Right[0] - Left[1], Right[1] - Left[0]));   }
#include <bits/stdc++.h>   using namespace std;   long long int n, k;   vector<long long int> od, ev;   long long int dps[200005], dpd[200005];   void buildDp() {     dps[0] = 0LL;     dps[1] = k - 1;     dpd[0] = 1LL;     dpd[1] = k - 2;     for (int i = 2; i < 200005; i++) {       dps[i] = ((dps[i - 2] * (k - 1)) % 998244353LL +                 ((dps[i - 1] * (k - 2)) % 998244353LL)) %                998244353LL;       dpd[i] = ((dpd[i - 2] * (k - 1)) % 998244353LL +                 ((dpd[i - 1] * (k - 2)) % 998244353LL)) %                998244353LL;     }   }   vector<pair<long long int, int>> p;   long long int solve(vector<long long int> v) {     p.clear();     long long int ans = 1LL;     int vz = (int)v.size();     if (vz == 0) return 1LL;     for (int i = 0; i < vz; i++)       if (v[i] != -1) p.push_back(make_pair(v[i], i));     int pz = (int)p.size();     if (pz > 0) {       long long int nf = p[0].second;       long long int nb = vz - 1 - p[pz - 1].second;       for (int i = 0; i < nf; i++) ans = (ans * (k - 1)) % 998244353LL;       for (int i = 0; i < nb; i++) ans = (ans * (k - 1)) % 998244353LL;       if (pz >= 2) {         for (int i = 0; i < pz - 1; i++) {           int gap = p[i + 1].second - p[i].second - 1;           if (p[i].first == p[i + 1].first)             ans = (ans * dps[gap]) % 998244353LL;           else             ans = (ans * dpd[gap]) % 998244353LL;         }       }     } else {       ans = k;       for (int i = 0; i < vz - 1; i++) ans = (ans * (k - 1)) % 998244353LL;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(0);     ;     cin >> n >> k;     for (int i = 0; i < n; i++) {       long long int temp;       cin >> temp;       if (i & 1LL)         od.push_back(temp);       else         ev.push_back(temp);     }     buildDp();     cout << (solve(ev) * solve(od)) % 998244353LL;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   #pragma GCC diagnostic ignored  -Wmissing-declarations    inline int safe_mul(const int x, const int y)       __attribute__((warn_unused_result));   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<int> a(n);     for (int& x : a) cin >> x;     int const inf = 200001;     vector<vector<int>> te(inf + 2);     for (int i = 0; i < m; ++i) {       int d, t;       cin >> d >> t;       te[d].push_back(t - 1);     }     int l = 0;     int r = inf;     while (r - l > 1) {       int mid = (l + r) >> 1;       auto tmpa = a;       int val = mid;       int sum = 0;       for (int i = mid; i >= 1; --i) {         for (int x : te[i]) {           int cur = min(val, i);           int need = min(tmpa[x], cur);           cur -= need;           val = min(val, cur);           tmpa[x] -= need;           sum += need;         }       }       if (accumulate(begin(tmpa), end(tmpa), 0LL) * 2 <= mid - sum)         r = mid;       else         l = mid;     }     if (r == inf) {       int mid = inf - 1;       auto tmpa = a;       int val = mid;       int64_t sum = 0;       for (int i = mid; i >= 1; --i) {         for (int x : te[i]) {           int cur = min(val, i);           int need = min(tmpa[x], cur);           cur -= need;           val = min(val, cur);           tmpa[x] -= need;           sum += need;         }       }       sum += accumulate(begin(tmpa), end(tmpa), 0LL) * 2;       cout << sum << endl;     } else {       cout << r << endl;     }   }