malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int INF = 1e9;   int n, m;   char mt[1505][1505];   const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};   bool flag = false;   pair<int, int> vis[1505][1505];   void dfs(int x, int y) {     int ax = (x % n + n) % n, ay = (y % m + m) % m;     if (mt[ax][ay] ==  # ) return;     if (vis[ax][ay] != make_pair(INF, INF) && vis[ax][ay] != make_pair(x, y)) {       flag = true;       return;     }     if (vis[ax][ay] != make_pair(INF, INF)) return;     vis[ax][ay] = make_pair(x, y);     for (int i = 0; i < 4; i++) dfs(x + dx[i], y + dy[i]);   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> m;     int sx, sy;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) vis[i][j] = make_pair(INF, INF);     for (int i = 0; i < n; i++) {       cin >> mt[i];       for (int j = 0; j < m; j++)         if (mt[i][j] ==  S ) sx = i, sy = j;     }     dfs(sx, sy), cout << (flag == true ?  Yes  :  No );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct bian {     int next, point, w;   } b[10000];   int d[2100][2100], n, x[2100], f[2100], pd[2100], pre[2100], p[2100], len;   long long e[2100][2100];   int getmin() {     int num = 2e9, where = 0;     for (int i = 1; i <= n; i++)       if (pd[i] == 0 && f[i] < num) {         num = f[i];         where = i;       }     return where;   }   void ade(int k1, int k2, int k3) {     b[++len] = (bian){p[k1], k2, k3};     p[k1] = len;   }   void add(int k1, int k2, int k3) {     ade(k1, k2, k3);     ade(k2, k1, k3);   }   void dfs(int k1, int k2, long long k3, int k4) {     e[k4][k1] = k3;     for (int i = p[k1]; i; i = b[i].next) {       int j = b[i].point;       if (j != k2) dfs(j, k1, k3 + b[i].w, k4);     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) scanf( %d , &d[i][j]);     for (int i = 1; i <= n; i++)       if (d[i][i] != 0) {         cout <<  NO  << endl;         return 0;       }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (d[i][j] != d[j][i]) {           cout <<  NO  << endl;           return 0;         }     memset(f, 0x3f, sizeof f);     f[1] = 0;     memset(pd, 0x00, sizeof pd);     for (int i = 1; i <= n; i++) {       int k1 = getmin();       if (k1 != 1 && f[k1] == 0) {         cout <<  NO  << endl;         return 0;       }       if (pre[k1]) add(pre[k1], k1, f[k1]);       pd[k1] = 1;       for (int j = 1; j <= n; j++)         if (pd[j] == 0 && d[k1][j] < f[j]) {           pre[j] = k1;           f[j] = d[k1][j];         }     }     for (int i = 1; i <= n; i++) dfs(i, 0, 0, i);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (e[i][j] != d[i][j]) {           cout <<  NO  << endl;           return 0;         }     cout <<  YES  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[5], r[5] = {0, 2, 3, 4, 1};   int main() {     for (int i = 1; i <= 4; i++) scanf( %d , &a[i]);     for (; a[1] != 1 || a[2] != 1 || a[3] != 1 || a[4] != 1;) {       int x = rand() % 4 + 1;       if (a[x] == 1 && a[r[x]] == 1) continue;       if ((a[x] % 2 == 0) && (a[r[x]] % 2 == 0))         printf( /%d n , x), a[x] /= 2, a[r[x]] /= 2;       else if (a[x] % 2 == 1)         printf( +%d n , x), a[x]++, a[r[x]]++;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, mn, mx, l, t, mn2 = INT_MAX, mx2 = INT_MIN;   set<long long> q;   int main() {     cin >> n >> m >> mn >> mx;     long long a[m];     for (int i = 0; i < m; i++) {       cin >> a[i];       if (a[i] < mn2) mn2 = a[i];       if (a[i] > mx2) mx2 = a[i];       if (a[i] >= mn && a[i] <= mx) t++;     }     if (t == m && m <= n) {       if (mn2 != mn && mx2 != mx && t + 2 <= n)         cout <<  Correct ;       else if ((mn2 == mn && t + 1 <= n) || (mx2 == mx && t + 1 <= n))         cout <<  Correct ;       else if (mn2 == mn && mx2 == mx && t <= n)         cout <<  Correct ;       else         cout <<  Incorrect ;     } else       cout <<  Incorrect ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     {       ios_base::sync_with_stdio(false);       cin.tie(0);     };     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       vector<int> matching;       vector<int> vis(3 * n + 1, 0);       for (int i = 1; i <= m; i++) {         int u, v;         cin >> u >> v;         if (!vis[u] and !vis[v]) {           matching.push_back(i);           vis[u] = vis[v] = 1;         }       }       if (matching.size() >= n) {         cout <<  Matching  << endl;         for (int i = 0; i < n; i++) cout << matching[i] <<    ;         cout << endl;       } else {         cout <<  IndSet  << endl;         int seen = 0;         for (int i = 1; i <= 3 * n and seen < n; i++) {           if (!vis[i]) {             cout << i <<    ;             seen++;           }         }         cout << endl;       }     }   }
#include <bits/stdc++.h>   char x[200005];   int dp[200005], maxdp[26];   int main() {     int n;     scanf( %d , &n);     scanf( %s , x + 1);     for (int i = 1; i <= n; i++) {       x[i] = x[i] -  a ;       dp[i] = 1;     }     maxdp[x[1]] = 1;     for (int i = 2; i <= n; i++) {       for (int j = x[i] + 1; j < 26; j++) {         dp[i] = dp[i] < maxdp[j] + 1 ? maxdp[j] + 1 : dp[i];       }       if (dp[i] > maxdp[x[i]]) maxdp[x[i]] = dp[i];     }     int ans = -1;     for (int i = 1; i <= n; i++) {       if (dp[i] > ans) ans = dp[i];     }     printf( %d n , ans);     for (int i = 1; i <= n; i++) printf( %d  , dp[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t, m;     cin >> t >> m;     vector<int> A;     A.resize(m);     int c = 0;     for (int i = 0; i < m; ++i) A[i] = 0;     for (int i = 0; i < t; ++i) {       string z;       int tmp;       cin >> z;       if (z[0] ==  d ) {         for (int j = 0; j < m - 1; ++j) {           for (int k = m - 1; k > j; k--) {             if (A[k] != 0 && 0 == A[k - 1]) {               int tmp2 = A[k];               A[k] = A[k - 1];               A[k - 1] = tmp2;             }           }         }       } else if (z[0] ==  e ) {         cin >> tmp;         int j = 0;         while (j < m && A[j] != tmp) j++;         if (j == m || tmp == 0) cout <<  ILLEGAL_ERASE_ARGUMENT  << endl;         while (j < m && A[j] == tmp) {           A[j] = 0;           j++;         }       } else {         cin >> tmp;         int empty = 0;         int j = 0;         while (j < m && empty < tmp) {           if (A[j] == 0)             empty++;           else             empty = 0;           j++;         }         if (j == m && empty < tmp)           cout <<  NULL  << endl;         else {           c++;           for (int k = j - 1; k > j - 1 - empty; --k) {             A[k] = c;           }           cout << c << endl;         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   inline int Rint() {     int x;     scanf( %d , &x);     return x;   }   const int MOD = 1e9 + 7;   const int MAXN = 1e5 + 10;   int alll(char* str_a, char* str_b, int n) {     int ret = 1;     for (int i = 0; i < n; ++i) {       if (str_a[i] ==  ? ) ret = ret * 10ll % MOD;       if (str_b[i] ==  ? ) ret = ret * 10ll % MOD;     }     return ret;   }   int upp(char* str_a, char* str_b, int n) {     int ans = 1;     for (int i = 0; i < n; ++i) {       if (str_a[i] == str_b[i]) {         if (str_a[i] !=  ? )           ;         else           ans = ans * 55ll % MOD;       } else if (str_a[i] ==  ? ) {         ans = ans * (long long)( 9  - str_b[i] + 1) % MOD;       } else if (str_b[i] ==  ? ) {         ans = ans * (long long)(str_a[i] -  0  + 1) % MOD;       } else if (str_a[i] >= str_b[i]) {       } else         return 0;     }     return ans;   }   int equ(char* str_a, char* str_b, int n) {     int ret = 1;     for (int i = 0; i < n; ++i)       if (str_a[i] ==  ?  || str_b[i] ==  ? ) {         if (str_a[i] ==  ?  && str_b[i] ==  ? ) ret = ret * 10ll % MOD;       } else if (str_a[i] != str_b[i])         return 0;     return ret;   }   int n;   char str_a[MAXN], str_b[MAXN];   int main() {     while (cin >> n) {       scanf( %s , str_a);       scanf( %s , str_b);       int all = alll(str_a, str_b, n);       int mod1 = upp(str_a, str_b, n);       int mod2 = upp(str_b, str_a, n);       int mod3 = equ(str_a, str_b, n);       int ans = ((mod1 + mod2 - mod3) % MOD + MOD) % MOD;       ans = (all - ans + MOD) % MOD;       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct ip {     long long a, b, c, d, m;   };   ip getip(long long num, int mm) {     return {(num & (255 << 24)) >> 24, (num & (255 << 16)) >> 16,             (num & (255 << 8)) >> 8, num & 255, mm};   }   vector<ip> numtoip(pair<long long, long long> p) {     long long s = p.first, e = p.second;     vector<ip> ans;     while (s < e) {       int k = (!s) ? 32 : __builtin_ctzll(s);       while (s + (1ll << k) - 1 >= e) k--;       ans.push_back(getip(s, 32 - k));       s += 1ll << k;     }     return ans;   }   pair<long long, long long> iptonum(ip x) {     long long n =         x.a * (1 << 24) + x.b * (1 << 16) + x.c * (1 << 8) + x.d * (1 << 0);     bitset<32> low(n), high(n);     for (int i = 0, ThxDem = 32 - x.m; i < ThxDem; ++i) low[i] = 0, high[i] = 1;     return {low.to_ullong(), high.to_ullong() + 1};   }   struct disjoint_intervals {     set<pair<long long, long long> > s;     void insert(pair<long long, long long> v) {       if (v.first >= v.second) return;       auto at = s.lower_bound(v);       auto it = at;       if (at != s.begin() && (--at)->second >= v.first) v.first = at->first, --it;       for (; it != s.end() && it->first <= v.second; s.erase(it++))         v.second = max(v.second, it->second);       s.insert(v);     }     bool intersect(pair<long long, long long> v) {       auto it = s.upper_bound({v.first, -2});       return (it != s.end() && it->first < v.second) ||              (it != s.begin() && prev(it)->second > v.first);     }   };   disjoint_intervals w, b;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 0, ThxDem = n; i < ThxDem; ++i) {       string s;       cin >> s;       s +=  . ;       int now = 0, white = s[0] ==  + ;       vector<int> v;       for (int i = 1, ThxDem = ((int)(s).size()); i < ThxDem; ++i) {         if (s[i] >=  0  && s[i] <=  9 )           now = 10 * now + s[i] -  0 ;         else           v.push_back(now), now = 0;       }       if (v.size() < 5) v.push_back(32);       auto ran = iptonum({v[0], v[1], v[2], v[3], v[4]});       if (white)         w.insert(ran);       else         b.insert(ran);     }     for (auto x : w.s) {       if (b.intersect(x)) {         cout << -1 << endl;         return 0;       }     }     vector<pair<long long, long long> > good = {{-1, 0}}, ans;     for (auto x : w.s) good.push_back(x);     good.push_back({1ll << 32, (1ll << 32) + 1ll});     for (int i = 1, ThxDem = ((int)(good).size()); i < ThxDem; ++i)       ans.push_back({good[i - 1].second, good[i].first});     vector<ip> res;     for (auto xx : ans) {       vector<ip> x = numtoip(xx);       for (auto aux : x) {         auto rr = iptonum(aux);         if (b.intersect(rr)) res.push_back(aux);       }     }     cout << res.size() << endl;     for (auto x : res)       cout << x.a <<  .  << x.b <<  .  << x.c <<  .  << x.d <<  /  << x.m << endl;   }
#include <bits/stdc++.h>   using namespace std;   string imp =  Impossible n ;   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   clock_t startTime;   double getCurrentTime() {     return (double)(clock() - startTime) / CLOCKS_PER_SEC;   }   const int N = 1e5 + 1e3;   long long MOD = 1e9 + 7;   vector<int> aa[N];   int a[N], b[N], c[N];   int n, m, k, i, j;   bool d[N];   string s;   bool iscons(char c) {     if (c !=  a  && c !=  e  && c !=  i  && c !=  o  && c !=  u ) return true;     return false;   }   int main() {     startTime = clock();     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int tests = 1;     while (tests--) {       cin >> s;       int ns = s.size();       if (ns < 3) {         cout << s;         return 0;       }       int cons = 0;       if (iscons(s[1])) {         if (iscons(s[0]))           cons += 2;         else           cons++;       }       cout << s[0] << s[1];       for (int i = 2; i < ns; i++) {         if (iscons(s[i])) {           cons++;           if (cons == 3) {             if (s[i] == s[i - 1] && s[i - 1] == s[i - 2]) cons = 2;           }         } else           cons = 0;         if (cons == 3) {           cout <<    ;           cons = 1;         }         cout << s[i];       }     }     return 0;   }
#include <bits/stdc++.h>      using namespace std;   void mex(string &s)   {       s.back()++;       for(int i=s.length()-1;i>=1;i--)       {           if(s[i]> z )           {               s[i]= a ;               s[i-1]++;           }                  }       if(s[0]> z )           {               s.push_back( a );               s[0]= a ;           }   }      int main()   {       int t;cin>>t;       while(t--)       {           int n;cin>>n;           string s;cin>>s;           string k= a ;           while(s.find(k,0)!=string::npos)           {               mex(k);           }           cout<<k<<endl;       }   }
#include <bits/stdc++.h>   using namespace std;   int a[105];   int b[105];   int main() {     int n, m, i, j;     scanf( %d%d , &n, &m);     for (i = 1; i <= n; i++) scanf( %d%d , &a[i], &b[i]);     int con = 0;     for (i = 1; i <= 10000; i++) {       int s = 0;       for (j = 1; j <= n; j++) s += (i * a[j] / b[j] + 1);       if (s == m) con++;     }     printf( %d n , con);   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   using namespace std;   int n, m, k, second;   int a[500005];   int b[500005];   int cnt[500005];   int rbd[500005];   int stat[500005];   int app[500005];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> m >> k >> n >> second;     for (int i = (1); i <= (signed)(m); i++) {       cin >> a[i];     }     for (int i = (1); i <= (signed)(second); i++) {       int x;       cin >> x;       b[x]++;     }     int rb = 0;     int sat = 0;     for (int i = (1); i <= (signed)(m); i++) {       while (rb <= m && sat < second) {         rb++;         int c = a[rb];         if (b[c] > cnt[c]) {           sat++;         }         cnt[c]++;       }       rbd[i] = max(rb, i + k - 1);       int c = a[i];       if (b[c] >= cnt[c]) sat--;       cnt[c]--;     }     int dc = 0;     for (int i = (1); i <= (signed)(m); i++) {       if (rbd[i] == m + 1) break;       int bef = (i - 1) / k;       int aft = (m - rbd[i]) / k;       if (bef + aft + 1 >= n) {         for (int j = (i); j <= (signed)(rbd[i]); j++) {           int c = a[j];           if (b[c] > app[c]) {             stat[j] = 1;           }           app[c]++;         }         int added = second;         for (int j = (i); j <= (signed)(rbd[i]); j++) {           if (added < k && stat[j] == 0) {             added++;             stat[j] = 1;           } else if (added == k && stat[j] == 0) {             stat[j] = -1;             dc++;           }         }         int td = (i - 1) % k;         if (bef >= n) {           td += (bef + 1 - n) * k;         }         for (int i = (1); i <= (signed)(td); i++) {           stat[i] = -1;           dc++;         }         cout << dc <<   n ;         for (int i = (1); i <= (signed)(m); i++) {           if (stat[i] == -1) cout << i <<    ;         }         return 0;       }     }     cout <<  -1 n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     string t, s;     cin >> s >> t;     n = t.size();     vector<long long> pi(n + 1, 0);     for (long long i = (long long)1; i != (long long)n; i = i + 1) {       long long j = pi[i - 1];       while (j >= 0 && t[i] != t[j]) {         j = j > 0 ? pi[j - 1] : -1;       }       pi[i] = j + 1;     }     vector<long long> dp1(s.size() + 1, 0), dp2(s.size() + 1, 0),         dp3(s.size() + 1, 0);     vector<long long> pos;     long long curpi = 0;     for (long long i = (long long)0; i != (long long)s.size(); i = i + 1) {       long long j = curpi < n ? curpi : pi[curpi - 1];       while (j >= 0 && s[i] != t[j]) {         j = j > 0 ? pi[j - 1] : -1;       }       curpi = j + 1;       if (curpi == n) {         pos.push_back(i);       }     }     if (pos.size() == 0) {       cout << 0 <<   n ;       return 0;     }     long long j = 0;     for (long long i = (long long)0; i != (long long)s.size(); i = i + 1) {       if (j < pos.size() && i == pos[j]) {         j++;         dp1[i + 1] = (dp3[i + 1 - n] + i + 2 - n) % mod;         dp2[i + 1] = (dp1[i + 1] + dp2[i]) % mod;       } else {         dp1[i + 1] = dp1[i];         dp2[i + 1] = (dp2[i] + dp1[i + 1]) % mod;       }       dp3[i + 1] = (dp3[i] + dp2[i + 1]) % mod;     }     cout << dp2[s.size()] <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int a[101], b[101];   int main() {     int n, m;     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i];     sort(a, a + n);     cin >> m;     for (int i = 0; i < m; ++i) cin >> b[i];     sort(b, b + m);     cout << a[n - 1] <<     << b[m - 1] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 505;   int dx[] = {0, 0, 1, -1};   int dy[] = {1, -1, 0, 0};   char dc[] = { R ,  L ,  D ,  U };   string ans;   int n;   bool grid[MAX][MAX], vis[MAX][MAX];   int dp[MAX][MAX][4];   bool valid(int x, int y, int dir) {     return dp[x][y][dir] = ~dp[x][y][dir]                 ? dp[x][y][dir]                            : (!x || !y || x > n || y > n) ? 0                            : grid[x][y]                   ? 1                                         : valid(x + dx[dir], y + dy[dir], dir);   }   void dfs1(int x, int y) {     vis[x][y] = 1;     if (!grid[x][y]) ans +=  1 , grid[x][y] = 1;     for (int i = 0; i < 4; ++i) {       int nx = x + dx[i], ny = y + dy[i];       if (vis[nx][ny] || !valid(nx, ny, i)) continue;       ans += dc[i];       dfs1(nx, ny);       ans += dc[i ^ 1];     }   }   void dfs0(int x, int y) {     grid[x][y] = 0;     for (int i = 0; i < 4; ++i) {       int nx = x + dx[i], ny = y + dy[i];       if (!grid[nx][ny]) continue;       ans += dc[i];       dfs0(nx, ny);       ans += dc[i ^ 1];     }     ans +=  2 ;   }   int main() {     int sx, sy;     memset(dp, -1, sizeof dp);     cin >> n >> sx >> sy;     for (int i = 1; i <= n; ++i) {       vis[0][i] = vis[i][0] = vis[0][n + 1] = vis[n + 1][0] = 1;       for (int j = 1; j <= n; ++j) scanf( %d , grid[i] + j);     }     dfs1(sx, sy);     dfs0(sx, sy);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j)         if (grid[i][j]) return puts( NO ), 0;     puts( YES );     puts(ans.c_str());     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Grass {     int sizee;     char dir;   } a[100005];   int n;   bool flag[100005];   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i].dir;     }     for (int i = 1; i <= n; i++) {       cin >> a[i].sizee;     }     int nowx = 1;     flag[nowx] = 1;     while (true) {       if (a[nowx].dir ==  > ) {         nowx += a[nowx].sizee;       } else {         nowx -= a[nowx].sizee;       }       if (nowx > n || nowx < 1) {         cout <<  FINITE ;         break;       }       if (flag[nowx] == 1) {         cout <<  INFINITE ;         break;       }       flag[nowx] = 1;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[200000], x, y, ans;   multimap<int, int> M;   pair<int, int> P;   int main() {     scanf( %d , &n);     for (int i = 1; i <= (n); ++i) scanf( %d , &a[i]);     scanf( %d%d , &x, &y);     sort(a + 1, a + n + 1);     n = unique(a + 1, a + n + 1) - a - 1;     for (int i = 1; i <= (n); ++i)       if (x - x % a[i] >= y) M.insert(make_pair(x - x % a[i], a[i]));     while (x > y) {       if (M.empty()) {         ans += x - y;         break;       }       multimap<int, int>::iterator it = M.begin();       if (it->first < x)         x = it->first;       else         --x;       ++ans;       for (it = M.end(); !M.empty() && (--it)->first > x; it = M.end()) {         P.first = x - x % it->second;         P.second = it->second;         M.erase(it);         if (P.first >= y) M.insert(P);       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, pair<int, int>>> adj[(long)(1e5)];   int sub[(long)(1e5)];   int w[(long)(1e5)];   int c[(long)(1e5)];   int dfs(int r, int p) {     int cnt = 0;     for (int i = 0; i < adj[r].size(); i++) {       if (adj[r][i].first != p) {         w[adj[r][i].first] = adj[r][i].second.first;         c[adj[r][i].first] = adj[r][i].second.second;         cnt += dfs(adj[r][i].first, r);       }     }     if (!cnt) cnt = 1;     sub[r] = cnt;     return cnt;   }   void solve() {     int n;     long long s;     scanf( %d%lld , &n, &s);     for (int i = 0; i < n; i++) adj[i].clear();     for (int i = 0; i < n - 1; i++) {       int u, v, ww, cc;       scanf( %d%d%d%d , &u, &v, &ww, &cc);       adj[u - 1].push_back({v - 1, {ww, cc}});       adj[v - 1].push_back({u - 1, {ww, cc}});     }     for (int i = 0; i < n; i++) sub[i] = 0;     dfs(0, -1);     vector<long long> vs[2];     for (int k = 0; k < 2; k++) {       long long sum = 0;       set<pair<long long, int>> di;       for (int i = 1; i < n; i++) {         if (c[i] == k + 1) {           di.insert({(1ll * sub[i] * w[i] - 1ll * sub[i] * (w[i] / 2)), i});           sum += 1ll * sub[i] * w[i];         }       }       vs[k].push_back(sum);       while (sum > 0) {         int i = di.rbegin()->second;         long long decr = di.rbegin()->first;         di.erase(prev(di.end()));         sum -= decr;         w[i] = w[i] / 2;         di.insert({(1ll * sub[i] * w[i] - 1ll * sub[i] * (w[i] / 2)), i});         vs[k].push_back(sum);       }     }     int flip = 0;     if (vs[0].size() > vs[1].size()) {       swap(vs[1], vs[0]);       flip = 1;     }     int ans = 1e9;     for (int i = 0; i < vs[0].size(); i++) {       int lo = 0, hi = vs[1].size() - 1;       while (lo <= hi) {         int mid = (lo + hi) / 2;         if (vs[0][i] + vs[1][mid] <= s)           hi = mid - 1;         else           lo = mid + 1;       }       if (vs[0][i] + vs[1][lo] <= s)         ans = min(ans, i * (1 + flip) + lo * (2 - flip));     }     printf( %d n , ans);   }   int main() {     int tc;     scanf( %d , &tc);     while (tc--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   struct Edge {     int to, nxt;   } e[N << 1];   int h[N], idx, a[N];   void Ins(int a, int b) {     e[++idx].to = b;     e[idx].nxt = h[a];     h[a] = idx;   }   long long dis[N], siz[N];   void dfs(int u, int fa) {     siz[u] = a[u];     for (int i = h[u]; i; i = e[i].nxt) {       int v = e[i].to;       if (v == fa) continue;       dfs(v, u);       siz[u] += siz[v];       dis[1] += siz[v];     }   }   long long sum;   void dfs1(int u, int fa) {     for (int i = h[u]; i; i = e[i].nxt) {       int v = e[i].to;       if (v == fa) continue;       dis[v] = dis[u] + sum - 2ll * siz[v];       dfs1(v, u);     }   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), sum += a[i];     for (int i = 2; i <= n; i++) {       int x, y;       scanf( %d%d , &x, &y);       Ins(x, y);       Ins(y, x);     }     dfs(1, 0);     dfs1(1, 0);     long long res = 0;     for (int i = 1; i <= n; i++) res = max(res, dis[i]);     printf( %lld n , res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, y, i, j;     int l = 0, t = 0, m = 0, max = 0;     cin >> n;     cin >> i >> j;     l = j;     while (t < n) {       t++;       cin >> x >> y;       m = j - x + y;       if (m > max) {         max = m;       }       j = m;     }     if (max == 0 && l > 0 || l > max)       cout << l;     else       cout << max;     return 0;   }