malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long dx[] = {1, 0, -1, 0};   long long dy[] = {0, 1, 0, -1};   bool myComparison1(const pair<long long, long long> &a,                      const pair<long long, long long> &b) {     return a.second < b.second;   }   bool myComparison2(const pair<long long, long long> &a,                      const pair<long long, long long> &b) {     if (a.first == b.first) {       return (a.second < b.second);     }     return (a.first < b.first);   }   long long gcd(long long x, long long y) {     if (y == 0)       return x;     else       return gcd(y, x % y);   }   long long expo(long long n, long long m, long long p) {     long long r = 1;     n = n % p;     while (m > 0) {       if (m % 2) r = (r * n) % p;       n = (n * n) % p;       m = m / 2;     }     return r % p;   }   bool isPrime(long long n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (long long i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return false;     return true;   }   vector<long long> z_function(string s) {     long long n = (long long)s.length();     vector<long long> z(n);     for (long long i = 1, l = 0, r = 0; i < n; ++i) {       if (i <= r) z[i] = min(r - i + 1, z[i - l]);       while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];       if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;     }     return z;   }   long long distance(long long x, long long y) { return (sqrt(x * 2 + y * 2)); }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n;     cin >> n;     vector<long long> x, y;     for (long long i = 0; i < n; i++) {       long long c, d;       cin >> c >> d;       x.push_back(c);       y.push_back(d);     }     long long max = -1;     long long X = 0, Y = 0;     for (long long i = 0; i < n; i++) {       if (x[i] + y[i] > max) {         X = x[i];         Y = y[i];         max = X + Y;       }     }     cout << max;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     srand(time(NULL));     ;     int T = 1;     while (T--) {       int a[3];       for (int i = 0; i < int(3); i++) cin >> a[i];       sort(a, a + 3);       cout << a[2] - a[0];     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[5];   int c[100];   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> a[0] >> a[1];     for (int i = 1; i <= n; i++) cin >> c[i];     int m = min(a[0], a[1]);     int ans = 0;     for (int i = 1; i <= n / 2 + n % 2; i++) {       if (c[i] == 2 && c[n - i + 1] == 2) {         if (i != n / 2 + n % 2)           ans += 2 * m;         else if (n % 2 == 0)           ans += 2 * m;         else           ans += m;       } else if (c[i] == 2)         ans += a[c[n - i + 1]];       else if (c[n - i + 1] == 2)         ans += a[c[i]];       else if (c[i] != c[n - i + 1]) {         cout << -1;         return 0;       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 510;   bitset<510> can[maxn][2][64];   int N, M;   long long ans, best[maxn][2];   inline long long dfs(int now, int last) {     if (best[now][last ^ 1] != -1)       return best[now][last ^ 1];     else       best[now][last ^ 1] = 0;     for (int i = 59; i >= 0; --i)       if (can[now][last ^ 1][i].any()) {         for (int j = 1; j <= N; ++j)           if (can[now][last ^ 1][i][j])             best[now][last ^ 1] =                 max(dfs(j, last ^ 1) + (1LL << i), best[now][last ^ 1]);         break;       }     return best[now][last ^ 1];   }   int main() {     scanf( %d %d , &N, &M);     while (M--) {       int a, b, c;       scanf( %d %d %d , &a, &b, &c);       can[a][c][0][b] = 1;     }     for (int i = 1; i <= 62; ++i)       for (int j = 1; j <= N; ++j)         for (int k = 0; k < 2; ++k)           for (int l = 1; l <= N; ++l) {             if (!can[j][k][i - 1][l]) continue;             can[j][k][i] |= can[l][k ^ 1][i - 1];           }     for (int i = 1; i <= N; ++i)       if (can[i][0][60].any()) puts( -1 ), exit(0);     memset(best, -1, sizeof(best));     for (int i = 1; i <= N; ++i) ans = max(ans, dfs(i, 1));     if (ans > 1000000000000000000LL) ans = -1;     printf( %I64d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005;   int vis[maxn], p[maxn];   int plen;   void prime() {     memset(vis, 0, sizeof(vis));     plen = 0;     for (int i = 2, k = 4; i < maxn; ++i, k += i + i - 1) {       if (!vis[i]) {         p[plen++] = i;         if (k < maxn) {           for (int j = k; j < maxn; j += i) {             vis[j] = 1;           }         }       }     }   }   struct node {     int id;     int cnt;     int color;   } need[maxn];   bool cmp(const node &a, const node &b) { return a.cnt > b.cnt; }   bool cmp1(const node &a, const node &b) { return a.id < b.id; }   char buf[maxn], out[maxn];   int cnt[maxn];   int main(int argc, char *argv[]) {     prime();     while (scanf( %s , buf) != EOF) {       memset(vis, -1, sizeof(vis));       memset(cnt, 0, sizeof(cnt));       int len = strlen(buf);       for (int i = 0; i < len; i++) {         cnt[buf[i] -  a ]++;       }       int colornum = 0;       for (int i = 0; i < plen && p[i] <= len; i++) {         int color = -1;         for (int j = 1; j <= len / p[i]; j++) {           if (vis[j * p[i]] != -1) {             color = vis[j];             break;           }         }         if (color == -1) {           color = colornum++;         }         for (int j = 1; j <= len / p[i]; j++) {           vis[j * p[i]] = color;         }       }       for (int i = 0; i < maxn; i++) {         need[i].id = i;         need[i].cnt = 0;       }       for (int i = 2; i <= len; i++) {         need[vis[i]].cnt++;       }       sort(need, need + colornum, cmp);       int flag = 0;       for (int i = 0; i < colornum; i++) {         int maxcolor = 0, k = -1;         for (int j = 0; j < 26; j++) {           if (cnt[j] > maxcolor) {             maxcolor = cnt[j];             k = j;           }         }         if (k == -1 || maxcolor < need[i].cnt) {           flag = 1;           break;         } else {           need[i].color = k;           cnt[k] -= need[i].cnt;         }       }       if (!flag) {         printf( YES n );         for (int i = 0; i < 26; i++) {           if (cnt[i]) {             out[1] = i +  a ;             break;           }         }         sort(need, need + colornum, cmp1);         for (int i = 2; i <= len; i++) {           out[i] = need[vis[i]].color +  a ;         }         out[len + 1] = 0;         printf( %s n , out + 1);       } else {         printf( NO n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     {       long long int i, j, k, y, n, x, m, a, b, ans = 0, sum = 0, cnt = 0;       long long int c, mx = 0;       string s, z, taj =  YES ;       cin >> s;       map<char, long long int> mp;       n = s.size();       for (i = 0; i < n; i++) mp[s[i]]++;       if (!mp[ 4 ] and !mp[ 7 ])         cout << -1;       else if (mp[ 4 ] >= mp[ 7 ])         cout << 4;       else         cout << 7;       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int v[11][100010];   int vmap[11][100010];   int pointer[11];   int main() {     int n, m;     scanf( %d %d , &n, &m);     long long ans = 0;     for (int j = 0; j < m; j++) {       for (int i = 0; i < n; i++) {         scanf( %d , &v[j][i]);         v[j][i]--;         vmap[j][v[j][i]] = i;       }     }     bool isNew = true;     long long sz;     for (int i = 0; i < n; i++) {       if (isNew) {         for (int j = 1; j < m; j++) pointer[j] = vmap[j][v[0][i]];         sz = 1;         ans += 1;         isNew = false;       } else {         bool check = true;         sz++;         for (int j = 1; j < m && check; j++) {           pointer[j]++;           if (pointer[j] >= n || v[j][pointer[j]] != v[0][i]) check = false;         }         if (check)           ans += sz;         else {           isNew = true;           i--;         }       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200010;   int n, s;   int a[N];   int vis[N];   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> s;     for (int i = 0; i < n; ++i) cin >> a[i];     int cnt = 0;     if (a[s - 1]) {       cnt++;       a[s - 1] = 0;     }     sort(a, a + n);     memset(vis, false, sizeof false);     vis[0] = true;     for (int i = 1; i < n; ++i) vis[a[i]] = true;     int cmax = 0;     for (int i = 1; i < n; ++i) {       if (!a[i]) {         while (cmax < n && vis[cmax]) cmax++;         if (cmax < n) {           vis[cmax] = true;         }         cnt++;       } else {         int pos = n - 1;         for (int j = i; j <= pos; ++j) {           if (!vis[a[j] - 1]) {             while (pos > j && cmax < a[j] - 1) {               while (cmax < a[j] - 1 && vis[cmax]) cmax++;               vis[cmax] = true;               pos--;               cnt++;               if (cmax == a[j] - 1) break;             }             if (!vis[a[j] - 1]) {               while (cmax < a[j] - 1 && vis[cmax]) cmax++;               vis[cmax] = true;               cnt++;             }           }         }         break;       }     }     cout << cnt << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int arr[n], i;     for (i = 0; i < n; i++) cin >> arr[i];     int ind = 0, price;     int max = arr[0];     for (i = 1; i < n; i++) {       if (arr[i] > max) {         max = arr[i];         ind = i;       }     }     sort(arr, arr + n);     price = arr[n - 2];     cout << ind + 1 <<     << price;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   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;   }   template <typename T>   static inline void sd(vector<T> &x) {     sort((x).begin(), (x).end(), greater<T>());   }   long long n, m;   const long long N = 3e5 + 5;   vector<long long> g[N];   vector<long long> dep(N), parent(N), vis(N);   void dfs(long long v, long long par = -1) {     vis[v] = 1;     for (long long ch : g[v]) {       if (ch != par and !vis[ch]) {         dep[ch] = dep[v] + 1;         parent[ch] = v;         dfs(ch, v);       }     }   }   void solve() {     cin >> n >> m;     for (long long i = 0; i < n + 1; ++i) {       g[i].clear();       vis[i] = 0;     }     auto path = [&](long long a, long long b) {       bool flag = false;       if (dep[a] < dep[b]) {         swap(a, b);         flag = true;       }       vector<long long> v[2];       while (dep[a] > dep[b]) {         v[0].push_back(a);         a = parent[a];       }       while (a != b) {         v[0].push_back(a);         v[1].push_back(b);         a = parent[a];         b = parent[b];       }       v[0].push_back(a);       while (!v[1].empty()) {         v[0].push_back(v[1].back());         v[1].pop_back();       }       if (flag) {         reverse(v[0].begin(), v[0].end());       }       return v[0];     };     vector<long long> cnt(n + 1);     for (long long i = 0; i < m; ++i) {       long long e, f;       cin >> e >> f;       g[e].push_back(f);       g[f].push_back(e);     }     dfs(1);     vector<vector<long long> > output;     long long q;     cin >> q;     for (long long j = 0; j < q; ++j) {       long long a, b;       cin >> a >> b;       cnt[a]++;       cnt[b]++;       output.push_back(path(a, b));     }     long long k = 0;     for (long long i = 1; i < n + 1; ++i) {       if ((cnt[i] & 1) == 1) {         k++;       }     }     if (k == 0) {       cout <<  YES n ;       for (auto &i : output) {         cout << i.size() <<   n ;         for (long long j : i) {           cout << j <<    ;         }         cout <<   n ;       }     } else {       cout <<  NO n ;       cout << k / 2 <<   n ;     }   }   signed main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     long long testcases = 1;     while (testcases--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 0x3f3f3f3f3f3f3f3f;   template <typename T>   inline void read(T& X) {     X = 0;     int w = 0;     char ch = 0;     while (!isdigit(ch)) {       w |= ch ==  - ;       ch = getchar();     }     while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();     if (w) X = -X;   }   long long a[20], p[20];   int main() {     int t;     for (int i = 0; i <= 9; i++) p[i] = pow(10, i);     scanf( %d , &t);     while (t--) {       long long k, n;       long long ans = 0;       scanf( %lld %lld , &n, &k);       for (int i = 1; i <= n; i++) {         scanf( %lld , &a[i]);         a[i] = p[a[i]];       }       for (int i = 1; i <= n; i++) {         if (i == n) {           ans = ans + (k + 1) * a[i];           break;         }         long long now = (a[i + 1] - a[i]) / a[i];         if (now <= k) {           ans = ans + a[i + 1] - a[i];           k = k - now;         } else {           ans = ans + a[i] * (k + 1);           break;         }       }       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5010;   long long n, a[N], dp[2][N], mini[2][N];   vector<long long> v;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       long long x;       cin >> x;       v.push_back(x);       a[i] = x;     }     sort(v.begin(), v.end());     v.resize(unique(v.begin(), v.end()) - v.begin());     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= v.size(); j++) {         int r = i % 2;         dp[r][j] = mini[1 - r][j] + abs(a[i - 1] - v[j - 1]);         if (j == 1) {           mini[r][j] = dp[r][j];         } else           mini[r][j] = min(dp[r][j], mini[r][j - 1]);       }     }     cout << mini[n % 2][v.size()];     cin.get();   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int p;     cin >> p;     while (p--) {       int y = 0, n, z = 0, x = 0;       cin >> n;       int a[n];       for (int i = 0; i < n; i++) {         cin >> a[i];         if (a[i] == 0) z++;       }       for (int i = 0; i < n; i++) {         if (a[i] == 1)           break;         else           x++;       }       for (int i = n - 1; i >= 0; i--) {         if (a[i] == 1)           break;         else           x++;       }       cout << z - x - y <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int a[210000];   long long dp[210000][2];   const long long INF = 1LL << 60;   int N;   void init() {     for (int i = 0; i < 210000; i++)       for (int j = 0; j < 2; j++) dp[i][j] = INF;   }   long long solve(int x, bool t) {     if (x <= 0 || x > N) return 0;     if (x == 1) return -1;     if (dp[x][t] == -INF) return dp[x][t] = -1;     if (dp[x][t] != INF) return dp[x][t];     dp[x][t] = -INF;     int i2 = x;     if (t)       i2 += a[x];     else       i2 -= a[x];     long long r = solve(i2, !t);     long long res;     if (r == -1)       res = -1;     else       res = a[x] + r;     return dp[x][t] = res;   }   int main() {     cin >> N;     for (int i = 0; i < N - 1; i++) cin >> a[i + 2];     init();     for (int i = 1; i < N; i++) {       long long r = solve(1 + i, 0);       if (r != -1) r += i;       cout << r << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, x, y, z;     scanf( %d%d%d%d%d%d , &a, &b, &c, &x, &y, &z);     bool can = true;     int more = 0;     int less = 0;     if (a > x)       more += (a - x) / 2;     else       less += x - a;     if (b > y)       more += (b - y) / 2;     else       less += y - b;     if (c > z)       more += (c - z) / 2;     else       less += z - c;     if (more >= less)       printf( Yes n );     else       printf( No n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long N, T;     cin >> N;     list<long long> L;     for (int i = 0; i < N; i++) {       cin >> T;       L.push_back(T);     }     long long K, possible = 1, curr = -1, orig = 0;     cin >> K;     vector<pair<long long, long long> > operations;     long long sum = *L.begin();     list<long long>::iterator it = L.begin();     for (int i = 0; i < K && possible; i++) {       cin >> T;       ++curr;       list<long long> L2;       if (it == L.end())         possible = 0;       else {         sum = *it;         if (T != sum) {           if (T < sum)             possible = 0;           else {             sum = 0;             long long maz = *it;             while (it != L.end() && T > sum) {               sum += *it;               L2.push_back(*it);               maz = max(maz, *it);               it++;             }             if (T != sum)               possible = 0;             else {               list<long long>::iterator it2 = L2.begin();               while (it2 != L2.end() && *it2 != maz) ++it2, ++curr;               if (it2 == L2.begin()) {                 if (it2 != L2.end()) ++it2;                 ++curr;                 while (it2 != L2.end() && *it2 == maz) ++it2, ++curr;                 if (it2 == L2.end())                   possible = 0;                 else {                   list<long long>::iterator it3 = it2;                   --it2;                   --curr;                   do {                     operations.push_back(make_pair(curr, 1));                     ++it3;                   } while (it3 != L2.end());                   while (it2 != L2.begin()) {                     --it2;                     operations.push_back(make_pair(curr, 0));                     --curr;                   }                 }               } else if (it2 == L2.end())                 possible = 0;               else {                 list<long long>::iterator it3 = it2;                 do {                   --it3;                   operations.push_back(make_pair(curr, 0));                   --curr;                 } while (it3 != L2.begin());                 ++it2;                 while (it2 != L2.end()) {                   operations.push_back(make_pair(curr, 1));                   ++it2;                 }               }             }           }         } else           ++it;       }     }     if (it != L.end() || operations.size() != N - K) possible = 0;     if (possible) {       cout <<  YES  << endl;       for (int i = 0; i < operations.size(); i++) {         cout << operations[i].first + 1 <<                  << (operations[i].second == 0 ?  L  :  R ) << endl;       }     } else       cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 4;   vector<int> e[N];   int n, siz[N];   long long ans;   int sn = 0, sx = 0;   int en[N], ex[N];   void dfs(int u, int fa) {     siz[u] = 1;     for (auto v : e[u]) {       if (v == fa) continue;       dfs(v, u);       siz[u] += siz[v];       en[u] += en[v];       ans += 1ll * ex[u] * en[v] * siz[v];     }     ans += 1ll * ex[u] * (sn - en[u]) * (n - siz[u]);   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d %d , &u, &v);       e[u].push_back(v);       e[v].push_back(u);     }     for (int i = 1; i <= n; i++)       scanf( %d %d , &en[i], &ex[i]), sn += en[i], sx += ex[i];     dfs(1, 0);     printf( %.10f n , (double)(ans / (1.0 * sx * sn)));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T bigmod(T p, T e, T M) {     long long ret = 1;     for (; e > 0; e >>= 1) {       if (e & 1) ret = (ret * p) % M;       p = (p * p) % M;     }     return (T)ret;   }   template <class T>   inline T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <class T>   inline T modinverse(T a, T M) {     return bigmod(a, M - 2, M);   }   string S, P;   int dp[2001][2002], dp1[2001][2011], len[2001];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long a, b, c, d = 0, e, f = 0, g, h = 0, x, y, z = 0;     cin >> S >> P;     a = S.size(), b = P.size();     for (int i = (0); i < (b); ++i) dp1[a][i] = 1000000;     for (int i = a - 1; i >= 0; i--) {       for (int j = 0; j < b; j++) {         if (S[i] == P[j]) {           dp1[i][j] = dp1[i + 1][j + 1];         } else           dp1[i][j] = 1 + dp1[i + 1][j];       }       len[i] = dp1[i][0];     }     for (int i = 1; i <= a; i++) dp[a][i] = -100000;     for (int i = a - 1; i >= 0; i--) {       for (int j = 0; j <= a; j++) {         dp[i][j] = -100000;         if (j > (a - i)) {           continue;         }         dp[i][j] = max(dp[i][j], dp[i + 1][j]);         if (j > 0) dp[i][j] = max(dp[i][j], dp[i + 1][j - 1]);         if (len[i] <= j && (i + len[i] + b) <= a)           dp[i][j] = max(dp[i][j], 1 + dp[i + len[i] + b][j - len[i]]);       }     }     for (int i = 0; i <= a; i++) cout << dp[0][i] <<    ;     cout << endl;   }
#include<bits/stdc++.h>   using namespace std;   typedef long long ll;   ll n,m,pos,qwq,delta,a[1009],b[1009],p[1009][1009];   inline ll read(){    ll s = 0,w = 1;    char ch = getchar();    while (ch >  9  || ch <  0 ){ if (ch ==  - ) w = -1; ch = getchar();}    while (ch <=  9  && ch >=  0 ) s = (s << 1) + (s << 3) + (ch ^ 48),ch = getchar();    return s * w;   }   int main(){    m = read(),n = read();    for (ll i = 1;i <= n;i += 1) for (ll j = 1;j <= m;j += 1) p[i][j] = read(),a[i] += p[i][j],b[i] += p[i][j] * p[i][j];    for (ll i = 2;i < n;i += 1) if (a[i] - a[i - 1] != (a[n] - a[1]) / (n - 1)){pos = i,delta = a[i] - a[i - 1] - (a[n] - a[1]) / (n - 1); break;}    if (pos <= 3) qwq = b[n - 2] + b[n] - 2 * b[n - 1];    else qwq = b[1] + b[3] - 2 * b[2];    for (ll i = 1;i <= m;i += 1) if (b[pos - 1] + b[pos + 1] - 2 * (b[pos] - p[pos][i] * p[pos][i] + (p[pos][i] - delta) * (p[pos][i] - delta)) == qwq){printf( %lld %lld ,pos - 1,p[pos][i] - delta); return 0;}    return 0;   }