malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   void task();   int main() {     srand(time(0));     task();     return 0;   }   const int INF = 0x3f3f3f3f;   const int N = 1e5 + 10;   const int M = 1e5 + 10;   int n, x;   int l[N];   int r[N];   long long int dp[2 * N + 10];   vector<int> v;   void task() {     scanf( %d %d , &n, &x);     v.push_back(x);     for (int i = 0; i < n; ++i) {       scanf( %d %d , &l[i], &r[i]);       v.push_back(l[i]);       v.push_back(r[i]);     }     sort((v.begin()), (v.end()));     int m = ((int)((v).size()));     memset(dp, 0x3f, sizeof(dp));     for (int i = 0; i < m; ++i)       if (v[i] == x) dp[i] = 0;     for (int i = 0; i < n; ++i) {       for (int j = 1; j < m; ++j) dp[j] = min(dp[j], dp[j - 1] + v[j] - v[j - 1]);       for (int j = m - 2; j >= 0; --j)         dp[j] = min(dp[j], dp[j + 1] + v[j + 1] - v[j]);       for (int j = 0; j < m; ++j)         if (v[j] < l[i])           dp[j] += l[i] - v[j];         else if (v[j] > r[i])           dp[j] += v[j] - r[i];     }     cout << *min_element(dp, dp + m);   }
#include <bits/stdc++.h>   #define pb push_back   #define mp make_pair   #define fr first   #define sc second   #define MOD 1000000007   #define len(x) x.size()   #define min3(a, b, c) min(a, min(b, c))   #define max3(a, b, c) max(a, max(b, c))   #define all(v) v.begin(), v.end()   #define alla(a,n) a, a + n      using namespace std;      typedef long long ll;   typedef unsigned long long ull;   typedef pair<ll, ll> pll;   typedef vector<ll> vll;   typedef vector<pll> vpll;   typedef vector<vll> vvll;   typedef vector<string> vs;         void run()   {   #ifndef ONLINE_JUDGE    freopen( input.txt ,  r , stdin) ;    freopen( output.txt ,  w , stdout) ;   #endif    ios_base::sync_with_stdio(false);    cin.tie(NULL) ; cout.tie(NULL) ;   }      void solve()   {    int t;    cin >> t;    while (t--)    {     int n, d;     cin >> n >> d;     int arr[n];     for (int i = 0; i < n; i++)     {      cin >> arr[i];     }        int flag = 0, min_val = 0;        for (int i = 0; i < n; i++)     {      if (arr[i] > d)      {       flag = 1;      }     }        if (flag == 0)     {      cout <<  YES  << endl;     }     else     {      sort(arr, arr + n);      // cout << arr[0] << arr[1];      min_val = arr[0] + arr[1];         if (min_val <= d)      {       cout <<  YES  << endl;      }      else      {       cout <<  NO  << endl;      }     }    }   }      int32_t main()   {    run();    solve();       return 0 ;   }
#include <bits/stdc++.h>   using namespace std;   long long Answer = 0, idx[300100], edges[300100], dis[300100];   int n, m, x, y, w, u;   vector<pair<pair<int, int>, int> > v[300100];   priority_queue<pair<int, int>, vector<pair<int, int> >,                  greater<pair<int, int> > >       Q;   void dik() {     for (int i = 1; i <= n; i++) dis[i] = 1e18;     Q.push({0, u});     dis[u] = 0;     while (!Q.empty()) {       pair<int, int> x = Q.top();       Q.pop();       for (int i = 0; i < v[x.second].size(); i++) {         int V = v[x.second][i].first.first;         int cost = v[x.second][i].first.second;         int id = v[x.second][i].second;         if (dis[V] > dis[x.second] + cost ||             (dis[V] == dis[x.second] + cost && edges[idx[V]] > edges[id])) {           dis[V] = dis[x.second] + cost;           idx[V] = id;           Q.push({dis[V], V});         }       }     }   }   int main() {     memset(idx, -1, sizeof(idx));     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> x >> y >> w;       v[x].push_back({{y, w}, i + 1});       v[y].push_back({{x, w}, i + 1});       edges[i + 1] = w;     }     cin >> u;     dik();     for (int i = 1; i <= n; i++) {       if (idx[i] != -1) {         Answer += edges[idx[i]];       }     }     cout << Answer << endl;     for (int i = 1; i <= n; i++) {       if (idx[i] != -1) cout << idx[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int nxt() {     int x;     cin >> x;     return x;   }   int gettm(char c) {     if (c ==  G ) {       return 5;     } else if (c ==  W ) {       return 3;     } else if (c ==  L ) {       return 1;     } else {       assert(false);     }   }   int main() {     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());     cin.sync_with_stdio(0);     int n = nxt();     vector<long long> a(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     string s;     cin >> s;     deque<pair<int, long long>> water_shit, ground_shit;     bool was_water = false;     long long ans = 0;     for (int i = 0; i < n; ++i) {       if (s[i] ==  G ) {         ground_shit.push_back({i, a[i]});       } else if (s[i] ==  W ) {         was_water = true;         water_shit.push_back({i, a[i]});       } else {         long long need = a[i];         while (need && !water_shit.empty()) {           long long tmp = min(need, water_shit.back().second);           need -= tmp;           water_shit.back().second -= tmp;           ans += 4 * tmp;           if (water_shit.back().second == 0) {             water_shit.pop_back();           }         }         while (need && !ground_shit.empty()) {           long long tmp = min(need, ground_shit[0].second);           need -= tmp;           ground_shit[0].second -= tmp;           ans += 6 * tmp;           if (ground_shit[0].second == 0) {             ground_shit.pop_front();           }         }         if (was_water) {           ans += 4 * need;         } else {           ans += 6 * need;         }       }     }     long long cur = 0;     long long totally_used = 0;     for (int i = 0, j = 0; i < (int)ground_shit.size(); ++i) {       while (j < (int)water_shit.size() &&              water_shit[j].first < ground_shit[i].first) {         cur += water_shit[j].second;         ++j;       }       long long tmp = min(cur, ground_shit[i].second);       ground_shit[i].second -= tmp;       cur -= tmp;       totally_used += tmp;       ans += 4 * tmp;       ans += ground_shit[i].second * 3;     }     totally_used *= -1;     for (auto p : water_shit) {       totally_used += p.second;     }     ans += 2 * totally_used;     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     scanf( %d %d , &n, &m);     vector<int> vec(n + 1, 0);     vector<pair<int, pair<int, int>>> arr;     for (int i = 0; i < m; i++) {       int si, di, ci;       scanf( %d %d %d , &si, &di, &ci);       arr.push_back(make_pair(di, make_pair(si, ci)));     }     bool is_possible = true;     for (int i = 1; i <= n; i++) {       bool exam_time = false;       for (int j = 0; j < m; j++) {         if (arr[j].first == i) {           exam_time = true;           if (arr[j].second.second != 0) {             is_possible = false;           } else {             vec[i] = m + 1;           }           break;         }       }       if (exam_time) continue;       int best = -1, mini = (int)1e9;       for (int j = 0; j < m; j++) {         if (arr[j].first > i && arr[j].second.first <= i &&             arr[j].second.second != 0) {           if (arr[j].first < mini) {             mini = arr[j].first;             best = j;           }         }       }       if (best != -1) {         vec[i] = best + 1;         arr[best].second.second--;       }     }     if (is_possible) {       for (int i = 1; i <= n; i++) {         printf( %d  , vec[i]);       }     } else {       printf( -1 n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int arr[1005], half;   pair<int, int> info[1005];   bool cmp(pair<int, int> A, pair<int, int> B) {     if (A.first == B.first)       return A.second < B.second;     else       return A.first < B.first;   }   int main() {     int n, k;     scanf( %d %d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %d , arr + i);       half = arr[i] / 2 + (arr[i] % 2);       info[i] = pair<int, int>(half, arr[i]);     }     sort(info, info + n, cmp);     int lvl = k, cnt = 0;     for (int i = 0; i < n; i++) {       if (info[i].first <= lvl) {         lvl = max(k, info[i].second);       } else {         int num = info[i].second;         while (lvl < num) {           num = (num / 2) + (num % 2);           if (lvl < num) cnt++;         }         lvl = info[i].second;       }     }     printf( %d , cnt);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n, m, i, min, count, a[101], sum, count0;     cin >> t;     while (t--) {       cin >> n >> m;       count = 0;       sum = 0;       count0 = 0;       for (i = 0; i < (n * m); i++) {         cin >> a[i];         if (a[i] < 0) {           count++;           a[i] = -1 * a[i];         }         if (a[i] == 0) count0 = 1;         sum += a[i];       }       if (count % 2 != 0 && count0 != 1) {         min = a[0];         for (i = 0; i < (n * m); i++) {           if (a[i] < min) min = a[i];         }         cout << sum - (2 * min) << endl;       } else         cout << sum << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long> > v;   bool cmp(pair<long long, long long> &a, pair<long long, long long> &b) {     if (a.first == b.first) return a.second < b.second;     return b.first < a.first;   }   long long func(pair<long long, long long> &a, pair<long long, long long> &b,                  long long d) {}   int main() {     long long n;     long long m, d;     scanf( %lld %lld %lld , &n, &m, &d);     pair<long long, long long> a[n];     pair<long long, long long> b[m];     for (long long i = 0; i < n; i++) {       cin >> a[i].first >> a[i].second;     }     sort(a, a + n, cmp);     for (long long i = 0; i < m; i++) {       cin >> b[i].first >> b[i].second;     }     sort(b, b + m, cmp);     long long posi = 1, totalw = 0, posj = 1, totals = 0;     totalw = a[0].second + b[0].second;     totals = a[0].first + b[0].first;     if (totalw > d) return cout << 0, 0;     while (posj < m && totalw + b[posj].second <= d) {       totalw += b[posj].second;       totals += b[posj].first;       posj++;     }     long long mx = totals;     while (posi < n) {       totalw += a[posi].second;       totals += a[posi].first;       posi++;       while (totalw > d) {         if (posj <= 1) return cout << mx, 0;         posj--;         totals -= b[posj].first;         totalw -= b[posj].second;       }       mx = max(mx, totals);     }     cout << mx << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 10;   const long long inf = 1e15;   const int mod = 1e6;   void faster() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   const long long N = 3e5 + 10;   int n, m;   long long a[N];   int main() {     faster();     cin >> n;     if (n % 10 < 10 - (n % 10))       cout << n - n % 10 << endl;     else       cout << n + (10 - (n % 10)) << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000000 + 10, MOD = (int)1e+9 + 7;   long long fac[MAXN], Ifac[MAXN], T;   int k, w;   long long Pow(long long a, long long b) {     long long d = 1;     for (; b; d = (b & 1) ? d * a % MOD : d, a = a * a % MOD, b /= 2)       ;     return d;   }   long long A(int x, int y) { return fac[x] * Ifac[x - y] % MOD; }   int main() {     cin >> k >> w;     fac[0] = Ifac[0] = 1;     for (int i = 1; i <= k; ++i)       fac[i] = fac[i - 1] * i % MOD,       Ifac[i] = Ifac[i - 1] * Pow(i, MOD - 2) % MOD;     long long CC = 0, t = Pow(k, w - 2);     for (int i = 1; i <= k; ++i) {       if (i < w)         CC = (CC + A(k, i) * A(k, i) % MOD * t % MOD * i % MOD) % MOD;       else         CC = (CC + A(k, i) * A(k + w - 1 - i, w - 1) % MOD * w) % MOD;       if (i < k) {         t = t * Pow(k, MOD - 2) % MOD;         if (i + 2 <= w)           CC = (CC + A(k, i) * A(k, i + 1) % MOD * t % MOD * i % MOD) % MOD;         else           CC =               (CC + A(k, i + 1) * A(k - i + w - 2, w - 2) % MOD * (w - 1) % MOD) %               MOD;       }     }     cout << (CC + MOD) % MOD << endl;     fclose(stdin);     fclose(stdout);     return 0;   }
#include <bits/stdc++.h>   int main() {     long long int a, b, c, d, x;     int t;     scanf( %d , &t);     while (t--) {       scanf( %lld%lld%lld%lld , &a, &b, &c, &d);       x = a / d + (((a / d) / b) * c);       printf( %lld n , x);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int arr[4];     int t = 0;     int s = 0;     for (int i = 0; i < 4; i++) cin >> arr[i];     for (int m = 0; m < 4; m++) {       for (int j = 0; j < 4; j++) {         for (int k = 0; k < 4; k++) {           if (arr[m] < arr[j] + arr[k] && arr[j] < arr[m] + arr[k] &&               arr[k] < arr[m] + arr[j] && m != j && j != k && k != m) {             t += 1;           } else if (arr[m] == arr[j] + arr[k] && m != j && j != k && k != m) {             s += 1;           }         }       }     }     if (t == 0 && s != 0)       cout <<  SEGMENT ;     else if (t != 0)       cout <<  TRIANGLE ;     else if (t == 0 && s == 0)       cout <<  IMPOSSIBLE ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9 + 7;   const double EPS = 1e-9;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     long long x, y, z;     cin >> x >> y >> z;     long long sasha = x % z;     long long masha = y % z;     if ((sasha + masha) >= z) {       cout << ((x + y) / z) <<     << (z - max(sasha, masha));     } else       cout << (x / z + y / z) <<     << 0;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   struct state {     int l, r, v, d;   } node[N];   int n, m, cnt, bit[2][N * 2];   vector<int> g[N];   void add(int x, int val, int *b) {     while (x <= n * 2) {       b[x] += val;       x += (x & (-x));     }   }   int get(int x, int *b) {     int ans = 0;     while (x > 0) {       ans += b[x];       x -= (x & (-x));     }     return ans;   }   void dfs(int c, int fa, int d) {     node[c].l = cnt++;     node[c].d = d;     for (int i = 0; i < g[c].size(); i++) {       if (g[c][i] == fa) continue;       dfs(g[c][i], c, 1 - d);     }     node[c].r = cnt++;   }   void init() {     cnt = 1;     scanf( %d%d , &n, &m);     memset(bit, 0, sizeof(bit));     for (int i = 1; i <= n; i++) scanf( %d , &node[i].v);     int a, b;     for (int i = 1; i < n; i++) {       scanf( %d%d , &a, &b);       g[a].push_back(b);       g[b].push_back(a);     }     dfs(1, -1, 0);   }   void solve() {     int o, a, b;     for (int i = 0; i < m; i++) {       scanf( %d , &o);       if (o == 1) {         scanf( %d%d , &a, &b);         add(node[a].l, b, bit[node[a].d]);         add(node[a].r + 1, -b, bit[node[a].d]);       } else {         scanf( %d , &a);         printf( %d n , node[a].v + get(node[a].l, bit[node[a].d]) -                            get(node[a].l, bit[1 - node[a].d]));       }     }   }   int main() {     init();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<long long> v;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long t, n, x;     cin >> t;     while (t--) {       cin >> n;       long long m[n + 5];       bool flag[n + 5];       memset(m, 0, sizeof(m));       memset(flag, false, sizeof(flag));       for (long long i = 0; i < n; i++) {         cin >> x;         v.push_back(x);         m[x]++;       }       long long mx = *max_element(v.begin(), v.end());       for (long long i = 0; i < n; i++) {         long long sum = v[i];         for (long long j = i + 1; j < n; j++) {           sum += v[j];           if (sum > mx) break;           if (m[sum] && !flag[sum]) flag[sum] = true;         }       }       long long ans = 0;       for (long long i = 0; i <= n; i++) {         if (flag[i]) ans += m[i];       }       cout << ans << endl;       v.clear();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   const int MAX_N = 100005;   long long cost[MAX_N];   int r[MAX_N];   struct dish {     int idx, cnt;     long long c;     bool operator<(const dish &d) const {       if (c != d.c) return c < d.c;       return idx < d.idx;     }   };   int main() {     cin >> n >> m;     for (int i = 0; i < n; ++i) cin >> r[i];     for (int i = 0; i < n; ++i) cin >> cost[i];     multiset<dish> s;     for (int i = 0; i < n; ++i) {       s.insert({i, r[i], cost[i]});     }     vector<long long> ans(m);     for (int i = 0; i < m; ++i) {       long long tempCost = 0;       int dIdx, dCnt;       cin >> dIdx >> dCnt;       --dIdx;       auto it = s.find({dIdx, r[dIdx], cost[dIdx]});       if (it != s.end()) {         dish d = *it;         s.erase(it);         int buy = min(dCnt, r[dIdx]);         d.cnt -= buy;         r[dIdx] -= buy;         dCnt -= buy;         tempCost += d.c * buy;         if (d.cnt) s.insert(d);       }       while (!s.empty() && dCnt) {         auto it = s.begin();         dish d = *it;         s.erase(it);         int buy = min(dCnt, d.cnt);         d.cnt -= buy;         r[d.idx] -= buy;         dCnt -= buy;         tempCost += d.c * buy;         if (d.cnt) s.insert(d);       }       if (dCnt) break;       ans[i] = tempCost;     }     for (int i = 0; i < m; ++i) cout << ans[i] <<   n ;     return 0;   }
/* -> Written by <-      -----------     |K_A_Z_A_M_A|     |___________|     |    ___    |     |   (^_^)   |     |  /( | )  |     |____|_|____|       H O A N G   */      #include <bits/stdc++.h>      #define Task             running    #define F               first   #define S               second   #define pb              push_back   #define bit(x, i)       ((x >> (i)) & 1)   #define inf             1e9 + 7   #define INF             1e18 + 7   #define ll              long long   #define pii             pair <int, int>   #define debug(x)        cerr << #x <<   is   << x <<   n ;      using namespace std;      const int MOD = 1e9 + 7;   const int maxn = 1e5 + 5;   const int base = 3e5 + 1;      int n, q;   int a[300005];   vector <int> p[300005];      void Solve(int test_case) {       cin >> n >> q;       for (int i = 1; i <= n; ++ i) {           cin >> a[i];           p[a[i]].pb(i);       }       mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());       for (int i = 1; i <= q; ++ i) {           int l, r; cin >> l >> r;           int ans = 1;           for (int times = 1; times <= 40; ++ times) {               int u = a[uniform_int_distribution<int>(l,r)(rng)];               int f = upper_bound(p[u].begin(), p[u].end(), r) - lower_bound(p[u].begin(), p[u].end(), l);               ans = max(ans, 2 * f - (r - l + 1));           }           cout << ans <<   n ;       }   }         int main(){       ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);       if (fopen(Task .inp ,  r )) {           freopen(Task .inp ,  r , stdin);           freopen(Task .out ,  w , stdout);       }   //    Prepare();       int test_case = 1;   //     cin >> test_case;       for (int i = 1; i <= test_case; ++ i) {           Solve(i);       }       return 0;   }
#include <bits/stdc++.h>   int main(void) {     int m, n;     char a[100001], b[100001];     scanf( %s , a);     scanf( %s , b);     n = strlen(a);     m = strlen(b);     if (n == m) {       if (strcmp(a, b) == 0)         printf( -1 );       else         printf( %d , m);     } else if (n > m) {       printf( %d , n);     } else       printf( %d , m);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     map<int, int> m;     int sum = 0, a;     for (int i = 0; i < 5; i++) {       cin >> a;       m[a]++;       sum += a;     }     int temp = 0;     for (auto i : m) {       if (i.second == 2) temp = max(temp, i.first * 2);       if (i.second >= 3) temp = max(temp, i.first * 3);     }     cout << sum - temp;   }
#include <bits/stdc++.h>   using namespace std;   const int max_n = 5005, inf = 1011111111;   int n, p[max_n], b[max_n];   int pos[max_n];   vector<pair<vector<int>, pair<vector<int>, vector<int>>>> all;   vector<pair<vector<int>, pair<vector<int>, vector<int>>>> my_all;   void find_b() {     for (int i = 0; i < n; ++i) {       pos[p[i]] = i;     }     for (int i = 0; i < n; ++i) {       b[i] = pos[i];     }   }   int ans[2][max_n];   int res[max_n], tm, used[max_n];   int main() {     cin >> n;     for (int i = 0; i + 1 < n; ++i) {       for (int j = 0; j < 2; ++j) {         cout <<  ?   << i + j <<     << i << endl;         cout.flush();         cin >> ans[j][i];       }     }     int cnt = 0;     for (int i = 0; i < n; ++i) {       p[0] = i;       for (int j = 0; j + 1 < n; ++j) {         int x = ans[0][j] ^ ans[1][j];         p[j + 1] = p[j] ^ x;       }       find_b();       int f = 1;       for (int j = 0; j + 1 < n; ++j) {         for (int k = 0; k < 2; ++k) {           if (ans[k][j] != (p[j + k] ^ b[j])) {             f = 0;             break;           }         }       }       ++tm;       for (int j = 0; j < n; ++j) {         if (p[j] >= n) {           f = 0;           break;         }         if (used[p[j]] == tm) {           f = 0;           break;         }         used[p[j]] = tm;       }       if (0) {         for (int j = 0; j < n; ++j) {           cout << p[j] <<    ;         }         cout << endl;         for (int j = 0; j < n; ++j) {           cout << b[j] <<    ;         }         cout <<  f =   << f << endl;       }       cnt += f;       if (f) {         for (int j = 0; j < n; ++j) {           res[j] = p[j];         }       }     }     cout <<  !  << endl;     cout << cnt << endl;     for (int i = 0; i < n; ++i) {       cout << res[i] <<    ;     }     cout << endl;     cout.flush();     return 0;   }
#include<bits/stdc++.h>   using namespace std;   typedef long long int ll;   int main()   {       ll t,i,maxi,sumi,mini;       cin>>t;       while(t--)       {           ll n;           cin>>n;           ll a[n];           for(i=0;i<n;i++)           cin>>a[i];           maxi=-1;           mini=a[0];           sumi=0;           for(i=0;i<n;i++)           {               sumi=sumi+a[i];               if(maxi<a[i])               maxi=a[i];               if(mini>a[i])               mini=a[i];           }           mini=0;           if(maxi*(n-1)>sumi)               {                   mini+=maxi*(n-1)-sumi;                   sumi=maxi*(n-1);               }                      mini+=((n-1)-sumi%(n-1))%(n-1);           cout<<mini<<endl;          }   }