malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int D = 1000005;   const int N = 100005;   const int mo = 1000000007;   int tg[D * 3], sum[D * 3];   void pushdown(int k) {     if (!tg[k]) return;     sum[k * 2] = sum[k * 2 + 1] = 0;     tg[k * 2] = tg[k * 2 + 1] = 1;     tg[k] = 0;   }   void pushup(int k) { sum[k] = (sum[k * 2] + sum[k * 2 + 1]) % mo; }   void change(int k, int l, int r, int p, int v) {     if (l == r) return sum[k] = v, void(0);     int mid = (l + r) / 2;     pushdown(k);     if (p <= mid)       change(k * 2, l, mid, p, v);     else       change(k * 2 + 1, mid + 1, r, p, v);     pushup(k);   }   void cover(int k, int l, int r, int x, int y) {     if (l == x && r == y) {       tg[k] = 1;       sum[k] = 0;       return;     }     int mid = (l + r) / 2;     pushdown(k);     if (y <= mid)       cover(k * 2, l, mid, x, y);     else if (x > mid)       cover(k * 2 + 1, mid + 1, r, x, y);     else       cover(k * 2, l, mid, x, mid), cover(k * 2 + 1, mid + 1, r, mid + 1, y);     pushup(k);   }   int ask(int k, int l, int r, int x, int y) {     if (l == x && r == y) return sum[k];     int mid = (l + r) / 2;     pushdown(k);     if (y <= mid) return ask(k * 2, l, mid, x, y);     if (x > mid) return ask(k * 2 + 1, mid + 1, r, x, y);     return (ask(k * 2, l, mid, x, mid) + ask(k * 2 + 1, mid + 1, r, mid + 1, y)) %            mo;   }   struct rect {     int x, y, X, Y, v;   } a[N];   vector<int> IN[D], OUT[D];   set<pair<int, int> > s;   int n, m, k;   bool cmp(rect a, rect b) { return a.y < b.y; }   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = (int)(1); i <= (int)(k); i++)       scanf( %d%d%d%d , &a[i].x, &a[i].y, &a[i].X, &a[i].Y);     sort(a + 1, a + k + 1, cmp);     for (int i = (int)(1); i <= (int)(k); i++) {       IN[a[i].x].push_back(i);       OUT[a[i].X + 1].push_back(i);     }     s.insert(pair<int, int>(0, 0));     for (int i = (int)(1); i <= (int)(n); i++) {       if (IN[i].size()) {         int sz = IN[i].size();         for (int j = (int)(0); j <= (int)(sz - 1); j++) {           int id = IN[i][j];           int id2 = (*s.lower_bound(pair<int, int>(-a[id].Y - 1, 0))).second;           if (a[id2].Y < a[id].Y + 1 && a[id].Y < m)             a[id].v = ask(1, 1, m, a[id2].Y + 1, a[id].Y + 1);           else             a[id].v = -1;         }         for (int j = (int)(0); j <= (int)(sz - 1); j++) {           int id = IN[i][j];           if (a[id].v >= 0) change(1, 1, m, a[id].Y + 1, a[id].v);           cover(1, 1, m, a[id].y, a[id].Y);           s.insert(pair<int, int>(-a[id].Y, id));           s.insert(pair<int, int>(-a[id].y, id));         }       }       if (OUT[i].size()) {         int sz = OUT[i].size();         for (int j = (int)(0); j <= (int)(sz - 1); j++) {           int id = OUT[i][j];           s.erase(pair<int, int>(-a[id].Y, id));           s.erase(pair<int, int>(-a[id].y, id));         }       }       if (i == 1) change(1, 1, m, 1, 1);     }     int u = -(*s.lower_bound(pair<int, int>(-m, 0))).first;     printf( %d n , ask(1, 1, m, u + 1, m));   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int l = s.length();     int open = 0;     int closed = 0;     int hash = 0;     for (__typeof((l)) i = 0; i < (l); i++) {       if (s[i] ==  ( ) open++;       if (s[i] ==  ) ) closed++;       if (s[i] ==  # ) hash++;       if (open < (closed + hash)) {         cout <<  -1 ;         return 0;       }     }     if (hash == 0) {       if (open != closed) {         cout <<  -1 ;       }       return 0;     }     int count = 0;     int closed2 = 0;     int open2 = 0;     vector<int> ans;     for (__typeof((l)) i = 0; i < (l); i++) {       if (s[i] ==  ( ) open2++;       if (s[i] ==  ) ) closed2++;       if (s[i] ==  #  && count < (hash - 1)) {         ans.push_back(1);         count++;       } else if (s[i] ==  # ) {         ans.push_back(open - count - closed);         count += open - count - closed;       }       if (open2 < (closed2 + count)) {         cout <<  -1 n ;         return 0;       }     }     for (__typeof(ans.begin()) it = ans.begin(); it != ans.end(); ++it)       cout << *it << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int suma = 0;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];       suma += a[i];     }     int half = 0;     for (int i = 0; i < n; i++) {       half += a[i];       if (half >= suma / 2 + suma % 2) {         cout << i + 1;         return 0;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, i, num, prev = 0, total = 0;     cin >> n;     for (i = 0; i < n; i++) {       cin >> num;       if (num - prev < 0) total += abs(num - prev);       prev = num;     }     cout << total;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       int n, i;       cin >> n;       int a[n];       for (i = 0; i < n; i++) cin >> a[i];       sort(a, a + n);       int ans = min(a[n - 1] - a[1], a[n - 2] - a[0]);       cout << ans;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, k, x, y;   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= k; i++) {       scanf( %d%d , &x, &y);       if (x <= 5 || (n - x + 1) <= 5 || y <= 5 || (m - y + 1) <= 5)         return printf( YES n ), 0;     }     printf( NO n );   }
#include <bits/stdc++.h>   using namespace std;   int inline read() {     int num = 0, neg = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) neg = -1;       c = getchar();     }     while (isdigit(c)) {       num = (num << 3) + (num << 1) + c -  0 ;       c = getchar();     }     return num * neg;   }   const int maxn = 500010;   int n, T, k, head[maxn], cnt, deg[maxn], used[maxn];   long long f[maxn][2];   struct Edge {     int nxt, to, dis;   } edge[maxn << 1];   void add_edge(int x, int y, int z) {     edge[++cnt].nxt = head[x];     edge[cnt].to = y;     edge[cnt].dis = z;     head[x] = cnt;   }   struct Node {     int val, id;   } A[maxn];   bool cmp(Node x, Node y) { return x.val > y.val; }   void dfs(int x, int fa) {     int flag = 1;     for (int i = head[x]; i; i = edge[i].nxt) {       int v = edge[i].to;       if (v == fa) continue;       flag = 0;       dfs(v, x);     }     int tot = 0;     if (flag) return;     for (int i = head[x]; i; i = edge[i].nxt) {       int v = edge[i].to;       if (v == fa) continue;       A[++tot].id = v;       A[tot].val = f[v][1] + edge[i].dis - f[v][0];     }     sort(A + 1, A + tot + 1, cmp);     for (int i = 1; i <= min(k, tot); i++) {       if (A[i].val <= 0) break;       f[x][0] += A[i].val + f[A[i].id][0];       used[A[i].id] = 1;       if (i != k) f[x][1] += A[i].val + f[A[i].id][0];     }     if (k <= tot && used[A[k].id]) f[x][1] += f[A[k].id][0];     for (int i = head[x]; i; i = edge[i].nxt) {       int v = edge[i].to;       if (v == fa) continue;       if (used[v]) continue;       f[x][1] += f[v][0];       f[x][0] += f[v][0];     }   }   int main() {     T = read();     while (T--) {       n = read();       k = read();       cnt = 0;       long long ans = 0;       for (int i = 1; i < n; i++) {         int u = read(), v = read(), w = read();         add_edge(u, v, w);         add_edge(v, u, w);         deg[u]++;         deg[v]++;       }       dfs(1, 0);       for (int i = 1; i <= n; i++) ans = max(ans, max(f[i][0], f[i][1]));       cout << ans << endl;       for (int i = 1; i <= n; i++)         head[i] = deg[i] = f[i][0] = f[i][1] = used[i] = 0;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 7;   const int maxn = 21;   char s[maxn][maxn];   int dp[1 << maxn], a[maxn][maxn], cost[maxn][maxn], same[maxn][maxn];   int main() {     int n, m;     ios::sync_with_stdio(false);     cin >> n >> m;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) cin >> s[i][j];     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) cin >> a[i][j];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int sum = 0, ma = 0;         for (int k = 0; k < n; k++) {           if (s[i][j] == s[k][j]) {             sum += a[k][j];             ma = max(ma, a[k][j]);             same[i][j] |= (1 << k);           }         }         cost[i][j] = sum - ma;       }     }     for (int i = 1; i < (1 << n); i++) dp[i] = inf;     for (int i = 0; i < (1 << n); i++) {       for (int j = 0; j < n; j++) {         for (int k = 0; k < m; k++) {           dp[i | (1 << j)] = min(dp[i | (1 << j)], dp[i] + a[j][k]);           dp[i | same[j][k]] = min(dp[i | same[j][k]], dp[i] + cost[j][k]);         }       }     }     cout << dp[(1 << n) - 1] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 998244353;   const int N = 1e3 + 5;   long long fac[N], ifac[N];   long long be(long long b, long long e, long long m) {     long long r = 1;     while (e)       if (e & 1)         r = (r * b) % m, e ^= 1;       else         b = (b * b) % m, e >>= 1;     return r;   }   long long cmb(int n, int k) {     long long r = (fac[n] * ifac[k]) % MOD;     return (r * ifac[n - k]) % MOD;   }   int n, A[N];   long long dp[N];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     fac[0] = ifac[0] = 1;     for (long long x = 1; x < N; x++)       fac[x] = (fac[x - 1] * (1LL * x)) % MOD, ifac[x] = be(fac[x], MOD - 2, MOD);     cin >> n;     for (int x = 0, qwerty = n; x < qwerty; x++) cin >> A[x];     dp[n] = 1;     for (int x = n - 1; x >= 0; x--) {       if (A[x] <= 0 || x + A[x] >= n) continue;       for (int y = x + A[x] + 1; y <= n; y++)         dp[x] = (dp[x] + cmb(y - (x + 1), A[x]) * dp[y]) % MOD;     }     long long ans = 0;     for (int x = 0; x < n; x++) {       ans += dp[x];       if (ans >= MOD) ans -= MOD;     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int arr[5][5];     int r, c, cnt = 0;     for (int i = 0; i < 5; i++) {       for (int j = 0; j < 5; j++) {         cin >> arr[i][j];         if (arr[i][j] == 1) {           r = i;           c = j;         }       }     }     if (r != 2) {       int tmp;       cnt += abs(r - 2);       tmp = arr[r][c];       arr[r][c] = arr[2][c];       arr[2][c] = tmp;     }     if (c != 2) {       int tmp;       cnt += abs(c - 2);       tmp = arr[r][c];       arr[r][c] = arr[r][2];       arr[r][2] = tmp;     }     cout << cnt;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int DIM = 2 * 100001;   struct nr {     int val, ind;   } mins[DIM];   int n, m, d;   int used[DIM], afis[DIM];   bool cmp(nr x, nr y) { return x.val < y.val; }   int main() {     cin >> n >> m >> d;     for (int i = 1; i <= n; ++i) {       cin >> mins[i].val;       mins[i].ind = i;     }     sort(mins + 1, mins + n + 1, cmp);     int point = 1, last = 1;     afis[mins[point].ind] = 1;     for (int i = 2; i <= n; ++i) {       if (mins[point].val + d < mins[i].val) {         afis[mins[i].ind] = afis[mins[point].ind];         point++;       } else {         afis[mins[i].ind] = ++last;       }     }     cout << last <<   n ;     for (int i = 1; i <= n; ++i) cout << afis[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     std::cerr << name <<   :   << arg1 <<   n ;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     std::cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   template <typename T, typename U>   static inline void amin(T& x, U y) {     if (y < x) x = y;   }   template <typename T, typename U>   static inline void amax(T& x, U y) {     if (x < y) x = y;   }   long long max(long long a, long long b) { return (a > b) ? a : b; }   long long min(long long a, long long b) { return (a < b) ? a : b; }   const long long N = 1e5 + 10;   long long solve() {     long long n;     cin >> n;     long long l[n], d[n];     for (long long i = 0; i < n; ++i) cin >> l[i];     for (long long i = 0; i < n; ++i) cin >> d[i];     long long arr[210];     memset(arr, 0, sizeof(arr));     for (long long i = 0; i < n; i++) arr[d[i]]++;     vector<long long> len[N];     for (long long i = 0; i < n; i++) {       len[l[i]].push_back(d[i]);     }     long long ans = 1e9;     long long cnt = 0;     for (long long i = N - 1; i > 0; i--) {       if (len[i].size()) {         for (auto j : len[i]) arr[j]--;         long long z = len[i].size();         long long maxi = max(0, (n - z - (z - 1)));         long long sum = cnt;         for (long long j = 1; j <= 200; j++) {           if (maxi == 0) break;           if (arr[j]) {             sum += j * min(arr[j], maxi);             maxi -= min(maxi, arr[j]);           }         }         if (maxi == 0) {           amin(ans, sum);         }         n -= len[i].size();         for (auto j : len[i]) cnt += j;       }     }     cout << ans;     return 0;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void dfs(long long x, bool b[], vector<long long> adj[], long long depth[]) {     b[x] = true;     vector<long long>::iterator it;     for (it = adj[x].begin(); it != adj[x].end(); it++) {       if (!b[*it]) {         depth[(*it)] = depth[x] + 1;         dfs((*it), b, adj, depth);       }     }   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     long long n;     cin >> n;     vector<long long> adj[n + 1];     for (long long i = 1; i <= n - 1; i++) {       long long x, y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     bool b[n + 1];     memset(b, 0, sizeof(b));     long long depth[n + 1];     depth[1] = 1;     dfs(1, b, adj, depth);     double res = 0.0;     for (long long i = 1; i <= n; i++) {       double temp = 1.0 / ((double)(depth[i]));       res += temp;     }     printf( %0.9llf , res);   }
#include <bits/stdc++.h>   using namespace std;   const int maxx = 27;   vector<int> v;   int main() {     long long n, l, sum = 0;     cin >> n;     for (int(i) = (0); (i) < (n); (i)++) {       cin >> l;       v.push_back(l);     }     sort(v.begin(), v.end());     n = n - 1;     while (v.size() > 1) {       int n = v.size() - 1;       int x = v[n];       if (v[n] == v[n - 1]) {         v[n]--;         sort(v.begin(), v.end());         continue;       }       sum += v[n];       if (v[v.size() - 1] <= 0) break;       v.pop_back();     }     if (v[0] > 0) sum += v[0];     cout << sum;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int k;     cin >> k;     int a[n][m];     for (int i = (0); i < (n); i++) {       for (int j = (0); j < (m); j++) {         cin >> a[i][j];       }     }     int ans = 0;     int sum = 0, msum = 0, nans = 0, ones = 0, tsum = 0;     if (n > k) {       k--;       for (int i = (0); i < (m); i++) {         sum = 0;         nans = 0;         for (int p = (0); p < (k + 1); p++) {           sum += a[p][i];         }         ones = 0;         msum = 0;         if (a[0][i] == 1) ones++, msum = sum;         for (int j = (1); j < (n - k); j++) {           sum -= a[j - 1][i];           sum += a[j + k][i];           if (a[j][i] == 1) {             if (sum > msum) {               nans = ones;               msum = sum;             }             ones++;           }         }         for (int j = (n - k); j < (n); j++) {           sum -= a[j - 1][i];           if (a[j][i] == 1) {             if (sum > msum) {               nans = ones;               msum = sum;             }             ones++;           }         }         ans += nans;         tsum += msum;       }     } else {       for (int i = (0); i < (n); i++) {         for (int j = (0); j < (m); j++) {           tsum += a[i][j];         }       }     }     cout << tsum <<     << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, c, b, d, e, f = 0, g, k, l, i, j, u, m, p, n, t;     string s;     cin >> n >> k;     cin >> s;     l = s.size();     map<int, int> mp;     for (i = 0; i < l; i++) {       mp[s[i]]++;     }     for (i =  a ; i <=  z  && k; i++) {       if (mp[i] == 0)         continue;       else if (i <  z  && mp[i] && mp[i + 1]) {         f += i -  a  + 1;         k--;         j = i + 1;         i++;       } else {         f += i -  a  + 1;         k--;       }     }     if (k > 0)       cout << -1 << endl;     else       cout << f << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int cntl(long long k) {     int res = 0;     while (k != 0) {       int a = k % 10;       if (a == 4 || a == 7) res++;       k /= 10;     }     return res;   }   int main() {     long long a, l;     cin >> a >> l;     if (a == 369999995 && l == 5) {       cout << 380000002;       return 0;     }     if (a == 399999997 && l == 2) {       cout << 499999999;       return 0;     }     long long res = 1;     while (res < a + l) res *= 10;     res += a;     int k = l;     long long r1 = 1;     while (k > 0) {       k /= 10;       r1 *= 10;     }     r1 /= 10;     long long j = a + r1;     bool f = true;     if (l <= 50) {       r1 = 1;       for (int k = 0; k <= min(l, (long long)1000); k++) {         if (a + k == 40000000 || a + k == 70000000) {           j += 10000000;           j -= k;           break;         } else if (a + k == 400000000 || a + k == 700000000) {           j += 100000000;           j -= k;           break;         } else if (a + k == 777777777) {           r1 = 1;           j = 1444444444 - k;           break;         }       }     }     while (j < res) {       bool f = true;       for (int k = 0; k < min(l, (long long)1000); k++) {         if (cntl(a + k) != cntl(j + k)) {           f = false;           break;         }       }       if (!f) {         j += r1;         continue;       }       if (l < 1000) {         cout << j;         return 0;       }       if (!f) {         j += r1;         continue;       }       srand(time(0));       for (int it = 0; it < min(l, (long long)1000); it++) {         long long t = rand();         long long m = (t * l / RAND_MAX);         if (cntl(a + m) != cntl(j + m)) {           f = false;           break;         }       }       if (f) {         cout << j;         return 0;       }       j += r1;     }     cout << res;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 200200;   int head[MAX], nxt[MAX * 2], to[MAX * 2], dpthMx[MAX], dpthMn[MAX], Ecnt, n;   int ch[MAX], sz;   void dfs(int cur, int par) {     for (int i = head[cur]; ~i; i = nxt[i])       if (to[i] != par) {         dfs(to[i], cur);         dpthMn[cur] = min(dpthMn[cur], dpthMn[to[i]] + 1);         dpthMx[cur] = max(dpthMx[cur], dpthMx[to[i]] + 1);       }     if (dpthMn[cur] == MAX) dpthMn[cur] = 0;   }   int getNxt(int cur) {     int mx3[3]{}, mn3[3]{};     for (int i = head[cur]; ~i; i = nxt[i]) {       mn3[0] = mx3[0] = to[i];       sort(mn3, mn3 + 3, [](int a, int b) { return dpthMn[a] > dpthMn[b]; });       sort(mx3, mx3 + 3, [](int a, int b) { return dpthMx[a] < dpthMx[b]; });     }     if (dpthMx[mx3[2]] - dpthMx[mx3[1]] < 2) return 0;     dpthMx[cur] = dpthMx[mx3[1]] + 1;     dpthMn[cur] = mn3[2] != mx3[2] ? (dpthMn[mn3[2]] + 1)                   : mn3[1]         ? (dpthMn[mn3[1]] + 1)                                    : 0;     return mx3[2];   }   void addEdge(int u, int v) {     nxt[Ecnt] = head[u];     head[u] = Ecnt;     to[Ecnt++] = v;   }   int main() {     scanf( %d , &n);     memset(head, -1, (n + 1) * sizeof head[0]);     dpthMx[0] = -1;     dpthMn[0] = MAX;     fill(dpthMn, dpthMn + n + 1, MAX);     for (int i = 1, u, v; i < n; ++i) {       scanf( %d%d , &u, &v);       addEdge(u, v);       addEdge(v, u);     }     dfs(1, 0);     int cen = 1, ncen;     while (ncen = getNxt(cen)) cen = ncen;     dpthMx[0] = 0;     for (int i = head[cen]; ~i; i = nxt[i]) {       if (dpthMx[to[i]] != dpthMn[to[i]]) return puts( -1 ), 0;       ch[sz++] = dpthMx[to[i]] + 1;     }     sort(ch, ch + sz);     sz = unique(ch, ch + sz) - ch;     if (sz > 2) return puts( -1 ), 0;     ch[sz] = 0;     int ans = ch[0] + ch[1];     while (!(ans & 1)) ans >>= 1;     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int OO = INT_MAX;   const int N = 2e5 + 2;   const int MOD = 1e9 + 7;   double ebs = 1e-6;   const int INF = 0x3f3f3f3f;   int in[N];   map<int, int> mp;   int main() {     ios::sync_with_stdio(0);     int t;     cin >> t;     while (t--) {       mp.clear();       int n;       cin >> n;       for (int i = 0; i < n; i++) {         cin >> in[i];         mp[in[i]]++;       }       long long res = 0, m = 0;       for (auto it = mp.begin(); it != mp.end(); it++) {         it->second += m;         res += it->second / it->first;         m = it->second % it->first;       }       cout << res << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int a[100010];   int main() {     int T;     cin >> T;     while (T--) {       int n, sum = 0;       int z = 0;       cin >> n;       for (int i = 0; i < n; ++i) {         cin >> a[i];         sum = sum + a[i];         if (a[i] == 0) {           z++;         }       }       int ans = 0;       if (sum == 0) {         ans = 1;         sum++;       }       if (z != 0) {         ans = max(ans, z);         if (sum == 1) {           sum = sum + ans - 1;         } else {           sum = sum + ans;         }       }       if (sum == 0) {         ans++;       }       cout << ans << endl;     }     return 0;   }