malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long size, seg1[2 * 200005], seg2[2 * 200005], a[200005], b[200005],       seg3[2 * 200005], seg4[2 * 200005];   long long temp = 0;   long long f(long long u, long long v) { return min(u, v); }   long long query(long long l, long long r, long long u, long long v,                   long long seg[]) {     long long mid = (u + v) / 2;     if ((l == u) && (r == v))       return seg[(size + u) / (r - l + 1) - 1];     else if (l > mid)       return query(l, r, mid + 1, v, seg);     else if (r <= mid)       return query(l, r, u, mid, seg);     else       return f(query(l, mid, u, mid, seg), query(mid + 1, r, mid + 1, v, seg));   }   void update(long long ind, long long num, long long seg[]) {     long long pos = size + ind - 1;     seg[pos] = num;     while (pos) {       seg[(pos - 1) / 2] = f(seg[pos], seg[pos + pos % 2 * 2 - 1]);       pos--;       pos /= 2;     }   }   int main() {     ios::sync_with_stdio(false);     long long i, t, j, k, l, m, n;     cin >> n;     long long INF = 1;     for (size = 1; size < n; size *= 2)       ;     for (i = 0; i < n; i++) {       cin >> a[i];       update(i, a[i], seg1);     }     long long maxi = -INF, cnt = 0;     for (i = 0; i < n - 1; i++) {       maxi = max(a[i], maxi);       if (maxi <= query(i + 1, n - 1, 0, size - 1, seg1)) {         cnt++;         maxi = -INF;       }     }     cout << cnt + 1 << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, s;     cin >> n >> s;     vector<int> a(n);     --s;     for (int i = 0; i < n; ++i) cin >> a[i];     int ans = 0;     if (a[s] != 0) {       a[s] = 0;       ++ans;     }     for (int i = 0; i < n; ++i)       if ((i != s) && (a[i] == 0)) a[i] = n;     map<int, int> workers;     for (int i = 0; i < n; ++i) ++workers[a[i]];     int count = n - 1;     int max = n;     while (!workers.count(max)) --max;     for (int i = 1; count; ++i) {       if (workers[i])         count -= workers[i];       else {         ++ans;         --count;         --workers[max];         while (!workers[max]) --max;       }     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   struct debugger {     template <typename T>     debugger& operator,(const T& v) {       cerr << v <<    ;       return *this;     }   } dbg;   char ibuf[4096];   int ipt = 4096;   int readUInt() {     while (ipt < 4096 && ibuf[ipt] <  0 ) ipt++;     if (ipt == 4096) {       fread(ibuf, 1, 4096, stdin);       ipt = 0;       while (ipt < 4096 && ibuf[ipt] <  0 ) ipt++;     }     int n = 0;     char neg = 0;     if (ipt != 0 && ibuf[ipt - 1] ==  - ) neg = 1;     while (ipt < 4096 && ibuf[ipt] >=  0 ) n = (n * 10) + (ibuf[ipt++] -  0 );     if (ipt == 4096) {       fread(ibuf, 1, 4096, stdin);       ipt = 0;       while (ipt < 4096 && ibuf[ipt] >=  0 ) n = (n * 10) + (ibuf[ipt++] -  0 );     }     return neg ? -n : n;   }   int testcase;   void solve() {     int n;     scanf( %d , &n);     ;     int d;     scanf( %d , &d);     ;     int l;     scanf( %d , &l);     ;     int v[n];     int d1 = d;     if (d > 0) {       for (int i = 0; i < n; ++i) {         if (i % 2 == 0) {           if (d1 > 0) {             v[i] = min(l, d1);             d1 -= v[i];           } else {             v[i] = 1;             d1 -= 1;           }         } else {           v[i] = 1;           d1 += 1;         }       }     } else {       for (int i = 0; i < n; ++i) {         if (i % 2 == 0) {           v[i] = 1;           d1 -= 1;         } else {           if (d1 < 0) {             v[i] = min(l, -d1);             d1 += v[i];           } else {             d1 += 1;           }         }       }     }     if (d1 == 1 && l > 1) {       for (int i = 0; i < n; i++) {         if (i % 2 == 0 && v[i] < l) {           v[i]++;           d1 = 0;           break;         } else if (i % 2 == 1 && v[i] > 1) {           v[i]--;           d1 = 0;           break;         }       }     }     if (d1 == -1 && l > 1) {       for (int i = 0; i < n; i++) {         if (i % 2 == 0 && v[i] > 1) {           v[i]--;           d1 = 0;           break;         } else if (i % 2 == 1 && v[i] < l) {           v[i]++;           d1 = 0;           break;         }       }     }     if (d1 != 0) {       printf( -1 n );     } else {       for (int i = 0; i < n; ++i) {         if (i) printf(   );         printf( %d , v[i]);       }       printf(  n );     }   }   int main() {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     register int res = 0;     register char c = getchar(), f = 1;     while (c < 48 || c > 57) {       if (c ==  - ) f = 0;       c = getchar();     }     while (c >= 48 && c <= 57)       res = (res << 3) + (res << 1) + (c & 15), c = getchar();     return f ? res : -res;   }   inline void write(long long x) {     register char c[21], len = 0;     if (!x) return putchar( 0 ), void();     if (x < 0) x = -x, putchar( - );     while (x) c[++len] = x % 10, x /= 10;     while (len) putchar(c[len--] + 48);   }   const int N = 1e5 + 10, L = 20;   struct node {     int sz, a[L];     inline node(int x = -1) {       if (~x) {         sz = 1;         for (int i = 0; i < L; ++i, x >>= 1) a[i] = x & 1;       } else         sz = 0, memset(a, 0, sizeof(a));     }     inline node operator+(const node& p) const {       node r = *this;       r.sz += p.sz;       for (int i = 0; i < L; ++i) r.a[i] += p.a[i];       return r;     }     inline node operator^(int x) const {       node r = *this;       for (int i = 0; i < L; ++i, x >>= 1) r.a[i] = x & 1 ? sz - a[i] : a[i];       return r;     }   } a[N], sum[N];   inline void write(const node& x) {     long long r = 0;     for (int i = 0; i < L; ++i) r += (long long)x.a[i] << i;     write(r);   }   int n, m, rt, w[N], lc[N], rc[N], tag[N], tot = 0;   inline void pu(int x) { sum[x] = sum[lc[x]] + sum[rc[x]] + a[x]; }   int build(int l, int r) {     if (l > r) return 0;     int mid = (l + r) >> 1, x = mid;     w[x] = ++tot;     lc[x] = build(l, mid - 1), rc[x] = build(mid + 1, r);     pu(x);     return x;   }   inline void add(int x, int t) {     a[x] = a[x] ^ t, sum[x] = sum[x] ^ t, tag[x] ^= t;   }   inline void pd(int x) {     if (tag[x]) add(lc[x], tag[x]), add(rc[x], tag[x]), tag[x] = 0;   }   int merge(int x, int y) {     if (!x || !y) return x | y;     if (w[x] < w[y]) {       pd(x), rc[x] = merge(rc[x], y), pu(x);       return x;     } else {       pd(y), lc[y] = merge(x, lc[y]), pu(y);       return y;     }   }   void split(int rt, int k, int& x, int& y) {     if (!rt) return x = y = 0, void();     pd(rt);     if (sum[lc[rt]].sz < k) {       x = rt;       split(rc[x], k - sum[lc[x]].sz - 1, rc[x], y);       pu(x);     } else {       y = rt;       split(lc[y], k, x, lc[y]);       pu(y);     }   }   int op, l, r, x, y, z;   int main() {     n = read();     for (int i = 1; i <= n; ++i) sum[i] = a[i] = read();     m = read();     rt = build(1, n);     while (m--) {       op = read(), l = read(), r = read();       if (op == 1) {         split(rt, r, x, z);         split(x, l - 1, x, y);         write(sum[y]), putchar(  n );         rt = merge(merge(x, y), z);       } else {         split(rt, r, x, z);         split(x, l - 1, x, y);         add(y, read());         rt = merge(merge(x, y), z);       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long a, b, k, t, maxi, l, r, i, ans;   long long dp0[200005];   long long dp1[200005];   long long s[200005];   int main() {     cin >> a >> b >> k >> t;     maxi = k * t;     dp1[maxi] = 1;     while (t) {       t--;       for (i = 0; i <= 2 * maxi; i++) {         dp0[i] = dp1[i];         dp1[i] = 0;       }       for (i = 0; i <= 2 * maxi; i++) {         l = i - k;         if (l >= 0) {           dp1[l] += dp0[i];           if (dp1[l] > 1000000007) dp1[l] -= 1000000007;         }         r = i + k + 1;         if (r <= 2 * maxi) {           dp1[r] -= dp0[i];           if (dp1[r] < 0) dp1[r] += 1000000007;         }       }       for (i = 1; i <= 2 * maxi; i++) {         dp1[i] += dp1[i - 1];         if (dp1[i] > 1000000007) dp1[i] -= 1000000007;       }     }     s[0] = dp1[0];     for (i = 1; i <= 2 * maxi; i++) {       s[i] = s[i - 1] + dp1[i];       if (s[i] > 1000000007) s[i] -= 1000000007;     }     for (i = 0; i <= 2 * maxi; i++) {       r = a - b + i - 1;       if (r < 0) continue;       if (r > 2 * maxi) r = 2 * maxi;       ans += (s[r] * dp1[i]) % 1000000007;       if (ans > 1000000007) ans -= 1000000007;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   int main(int argc, char** argv) {     int32_t num_queries;     std::cin >> num_queries;     for (int32_t i = 0; i < num_queries; i++) {       int32_t num_sticks;       std::cin >> num_sticks;       if (num_sticks == 2)         std::cout << 2 << std::endl;       else if (num_sticks % 2 == 0)         std::cout << 0 << std::endl;       else         std::cout << 1 << std::endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void read(long long a = 0) {     freopen( in ,  r , stdin);     if (a) freopen( out ,  w , stdout);   }   vector<vector<pair<long long, long long> > > ad;   long long over;   long long dfs(long long a) {     over = a;     if (ad[a].size() == 0) return 1000000007;     return min(ad[a][0].second, dfs(ad[a][0].first));   }   int main() {     long long n;     cin >> n;     long long m;     ad = vector<vector<pair<long long, long long> > >(n + 1);     cin >> m;     bool vis[10000] = {}, good[10000] = {};     while (m--) {       long long x, y, d;       cin >> x >> y >> d;       x--, y--;       vis[y] = 1;       good[x] = 1;       ad[x].push_back(make_pair(y, d));     }     vector<pair<pair<long long, long long>, long long> > ans;     for (int i = 0; i < n; i++) {       if (!vis[i] && good[i]) {         pair<pair<long long, long long>, long long> tmp;         tmp.first.first = i;         tmp.second = dfs(i);         tmp.first.second = over;         ans.push_back(tmp);       }     }     sort(ans.begin(), ans.end());     cout << ans.size() <<   n ;     for (int i = 0; i < ans.size(); i++)       cout << ans[i].first.first + 1 <<     << ans[i].first.second + 1 <<                << ans[i].second <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   template <typename U, typename V>   void Min(U &a, const V &b) {     if (a > b) a = b;   }   template <typename U, typename V>   void Max(U &a, const V &b) {     if (a < b) a = b;   }   template <typename U, typename V>   void add(U &a, const V &b) {     a = (a + b) % 1000000007;   }   template <typename U>   U gcd(U a, U b) {     if (a == 0) return b;     if (b == 0) return a;     if (a >= b)       return gcd(a % b, b);     else       return gcd(a, b % a);   }   int pow(int a, int b) {     int ans = 1;     while (b) {       if (b & 1) ans = 1LL * ans * a % 1000000007;       a = 1LL * a * a % 1000000007;       b >>= 1;     }     return ans;   }   int pow(int a, long long b, int c) {     int ans = 1;     while (b) {       if (b & 1) ans = 1LL * ans * a % c;       a = 1LL * a * a % c;       b >>= 1;     }     return ans;   }   pair<int, int> a[100005];   int main() {     int ca = 0, T, i, j, k, m, n, K;     scanf( %d , &T);     while (T--) {       scanf( %d , &n);       for (int i = 0; i < n; i++) {         scanf( %d , &m);         int mx = 0;         for (int i = 0; i < m; i++) scanf( %d , &k), k -= i, Max(mx, k);         a[i] = {mx, m};       }       sort(a, a + n);       int ans = 0;       long long cur = 0;       for (int i = 0; i < n; i++) {         if (cur >= a[i].first) {           cur += a[i].second;         } else {           ans += a[i].first - cur;           cur = a[i].first + a[i].second;         }       }       printf( %d n , ans + 1);     }   }
#include <bits/stdc++.h>   using namespace std;   struct __timestamper {};   const int MAXN = 100100;   int val[MAXN];   long long dp[MAXN][4];   vector<int> g[MAXN];   void dfs(int v, int p) {     for (int u : g[v])       if (u != p) dfs(u, v);     long long max0 = 0, smax0 = 0, tmax0 = 0;     int max0v = -1, smax0v = -1;     long long max1 = 0, smax1 = 0;     int max1v = -1;     long long max2 = 0, smax2 = 0;     int max2v = -1;     long long max3 = 0, smax3 = 0;     for (int u : g[v])       if (u != p) {         long long t = dp[u][0];         int tv = u;         if (t > max0) {           swap(t, max0);           swap(max0v, tv);         }         if (t > smax0) {           swap(t, smax0);           swap(smax0v, tv);         }         if (t > tmax0) {           swap(t, tmax0);         }         t = dp[u][1];         if (t > max1) {           swap(t, max1);           max1v = u;         }         if (t > smax1) swap(t, smax1);         t = dp[u][2];         if (t > max2) {           swap(t, max2);           max2v = u;         }         if (t > smax2) swap(t, smax2);         t = dp[u][3];         if (t > max3) {           swap(t, max3);         }         if (t > smax3) swap(t, smax3);       }     dp[v][0] = max0 + val[v];     dp[v][1] = max(max1, max0 + smax0 + val[v]);     dp[v][2] = max2 + val[v];     if (max0v == max1v) {       dp[v][2] = max(dp[v][2], max(max0 + smax1, max1 + smax0) + val[v]);     } else {       dp[v][2] = max(dp[v][2], max0 + max1 + val[v]);     }     dp[v][3] = max3;     dp[v][3] = max(dp[v][3], max1 + smax1);     if (max0v == max2v) {       dp[v][3] = max(dp[v][3], max(max0 + smax2, max2 + smax0) + val[v]);     } else {       dp[v][3] = max(dp[v][3], max0 + max2 + val[v]);     }     for (int u : g[v])       if (u != p) {         long long cur = val[v] + dp[u][1];         if (u == max0v) {           cur += smax0 + tmax0;         } else if (u == smax0v) {           cur += max0 + tmax0;         } else {           cur += max0 + smax0;         }         dp[v][3] = max(dp[v][3], cur);       }   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &val[i]);     for (int i = 1; i < n; i++) {       int a, b;       scanf( %d%d , &a, &b);       --a, --b;       g[a].push_back(b);       g[b].push_back(a);     }     dfs(0, -1);     printf( %lld n , *max_element(dp[0], dp[0] + 4));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const int N = 7e4;   const int INF = 1e9;   struct d {     int ad, xr;   } a[N];   stack<int> s;   vector<pair<int, int>> v;   int n;   int main() {     ios::sync_with_stdio(0);     cin.tie();     cout.tie();     cin >> n;     int A, B;     for (int i = 0; i < n; ++i) {       cin >> A >> B;       if (A == 0) continue;       if (A == 1) s.push(i);       a[i] = {A, B};     }     while (!s.empty()) {       int x = s.top();       s.pop();       if (a[x].ad == 1) {         v.push_back({x, a[x].xr});         a[x].ad--;         --a[a[x].xr].ad;         a[a[x].xr].xr ^= x;         if (a[a[x].xr].ad == 1) s.push(a[x].xr);       }     }     cout << v.size() <<   n ;     for (auto x : v) cout << x.first <<     << x.second <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   template <class T1, class T2>   ostream& operator<<(ostream& out, const pair<T1, T2>& p) {     return (out << p.first <<     << p.second << endl);   }   const int INF = 1000000017;   const long long LLINF = 1000000000000000017LL;   const long double inf = 1.0 / 0.0;   const long double eps = 1e-10;   const int MOD = 1e9 + 7;   const int MOD2 = 1e9 + 9;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int T;     cin >> T;     while (T--) {       int c, d;       cin >> c >> d;       if (c == d) {         if (c == 0) {           cout << 0;         } else {           cout << 1;         }       } else if (abs(c - d) % 2 == 0) {         cout << 2;       } else {         cout << -1;       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e3, MOD = 1e9 + 7, UNDEF = -1;   int readInt() {     bool minus = false;     int result = 0;     char ch;     ch = getchar();     while (true) {       if (ch ==  - ) break;       if (ch >=  0  && ch <=  9 ) break;       ch = getchar();     }     if (ch ==  - )       minus = true;     else       result = ch -  0 ;     while (true) {       ch = getchar();       if (ch <  0  || ch >  9 ) break;       result = result * 10 + (ch -  0 );     }     if (minus)       return -result;     else       return result;   }   char readChar() {     char result = getchar();     while (isspace(result)) {       result = getchar();     }     return result;   }   char w[N + 1][N + 1];   pair<pair<short, short>, pair<short, short> > cur;   int n, m;   int dp1[2 * N + 1][2 * N + 1], dp2[2 * N + 1][2 * N + 1];   int S[2 * N + 1], cnt[2 * N + 1];   int curx, cury;   bool ispal(string s) {     string d = s;     reverse(s.begin(), s.end());     return d == s;   }   int o(int x1, int y1) { return x1 + S[x1 + y1] - S[x1 + y1 - 1]; }   void add(int x1, int y1, int x2, int y2) {     dp1[curx][cury] += dp2[o(x1, y1)][o(x2, y2)];     if (dp1[curx][cury] >= MOD) {       dp1[curx][cury] -= MOD;     }   }   vector<pair<short, short> > p1[2 * N + 1], p2[2 * N + 1];   int main() {     n = readInt(), m = readInt();     int s = (n + m - 2) / 2;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cnt[i + j]++;         w[i][j] = readChar();         p1[i + j - 2].push_back(make_pair(i, j));         p2[n + m - 2 - (i + j - 2)].push_back(make_pair(i, j));       }     }     for (int i = 2; i <= n + m; i++) {       S[i] = S[i - 1] + cnt[i];     }     if (w[1][1] != w[n][m]) {       cout << 0;       return 0;     }     int ans = n * n + 2 * (n - 1) * n;     for (int i = s; i >= 0; i--) {       for (int j = 0; j < p1[i].size(); j++) {         for (int k = 0; k < p2[i].size(); k++) {           int x1, y1, x2, y2;           x1 = p1[i][j].first;           y1 = p1[i][j].second;           x2 = p2[i][k].first;           y2 = p2[i][k].second;           if (w[x1][y1] == w[x2][y2]) {             if (x1 <= x2 && y1 <= y2) {               if (x2 - x1 + y2 - y1 <= 1) {                 dp1[o(x1, y1)][o(x2, y2)] = 1;               } else {                 curx = o(x1, y1);                 cury = o(x2, y2);                 if (x1 == x2) {                   add(x1, y1 + 1, x2, y2 - 1);                 } else if (y1 == y2) {                   add(x1 + 1, y1, x2 - 1, y2);                 } else {                   add(x1, y1 + 1, x2 - 1, y2);                   add(x1 + 1, y1, x2, y2 - 1);                   if (y2 - 1 >= y1 + 1) {                     add(x1, y1 + 1, x2, y2 - 1);                   }                   if (x2 - 1 >= x1 + 1) {                     add(x1 + 1, y1, x2 - 1, y2);                   }                 }               }             }           }         }       }       for (int i = 0; i <= 2 * n + m; i++) {         for (int j = 0; j <= 2 * n + m; j++) {           dp2[i][j] = dp1[i][j];           dp1[i][j] = 0;         }       }     }     cout << dp2[o(1, 1)][o(n, m)];   }
#include <bits/stdc++.h>   using namespace std;   int main() {     stringstream ss;     string x, s1, s2, s3;     long long int a1 = 0, a2 = 0, a3 = 0, sum = -1, q = 0;     cin >> x;     int z = x.size();     for (int i = 1; i < (z - 1); i++) {       for (int j = 1; j < (z - i); j++) {         q = 0;         stringstream ss1, ss2, ss3;         s1 = x.substr(0, i);         s2 = x.substr(i, j);         s3 = x.substr((i + j), (z - i - j));         if ((s1[0] ==  0  && s1.size() > 1) || (s2[0] ==  0  && s2.size() > 1) ||             (s3[0] ==  0  && s3.size() > 1))           continue;         {           ss1 << s1;           ss1 >> a1;           ss1.clear();           ss2 << s2;           ss2 >> a2;           ss2.clear();           ss3 << s3;           ss3 >> a3;           ss3.clear();           if (a1 > 1000000 || a2 > 1000000 || a3 > 1000000) continue;           q = a1 + a2 + a3;           if (q > sum) sum = q;         }       }     }     cout << sum << endl;   }
#include <bits/stdc++.h>   const int MOD = 998244353;   int prefix[505];   int dp[505];   void addmod(int& x, int y) { x = (x + y) % MOD; }   int main() {     int N, K;     scanf( %d %d , &N, &K);     dp[0] = 1;     for (int k = 1; k <= N; k++) {       for (int i = 1; i <= N; i++) {         dp[i] = 0;         for (int j = 1; j <= std::min(i, k); j++) {           addmod(dp[i], dp[i - j]);         }       }       prefix[k] = dp[N];     }     int j = N;     int total = 0;     for (int i = 1; i <= N; i++) {       while (i * j >= K) j--;       addmod(total, int64_t(prefix[i] + MOD - prefix[i - 1]) * prefix[j] % MOD);     }     printf( %d n , total * 2 % MOD);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int i, n, l, miao, lala;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     cin.ignore();     while (n--) {       char s[105];       miao = 0;       lala = 0;       cin.getline(s, 105);       int l = strlen(s);       if (l > 4 && s[0] ==  m  && s[1] ==  i  && s[i + 2] ==  a  &&           s[i + 3] ==  o  && s[i + 4] ==  . )         miao = 1;       if (l > 4 && s[l - 5] ==  l  && s[l - 4] ==  a  && s[l - 3] ==  l  &&           s[l - 2] ==  a  && s[l - 1] ==  . )         lala = 1;       if (miao == 1 && lala == 1 || miao == 0 && lala == 0)         cout <<  OMG>.< I don t know! n ;       else if (miao == 1)         cout <<  Rainbow s n ;       else if (lala == 1)         cout <<  Freda s n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int kval(int a) {     if (a == 0) return 10;     int kvala = a;     if (kvala % 10)       kvala = 10 + ((kvala / 10) * 10) - kvala;     else       kvala = 0;     return kvala;   }   int compare(int a, int b) {     if (kval(a) >= kval(b))       return 0;     else       return 1;   }   int main() {     int num, k, t;     vector<int> v2;     cin >> num >> t;     long long sum = 0;     for (int i = 0; i < num; i++) {       cin >> k;       sum = sum + (k / 10);       if (k < 100) v2.push_back(k);     }     int i;     int size = v2.size();     sort(v2.begin(), v2.end(), compare);     for (i = 0; i < size; i++) {       if (v2[i] % 10) {         if (kval(v2[i]) <= t) {           t = t - kval(v2[i]);           v2[i] += kval(v2[i]);           sum++;         } else           break;       }     }     if (i == size) {       sort(v2.begin(), v2.end());       for (int i = size - 1; i >= 0; i--) {         if (v2[i] < 100) {           int val = v2[i] + t;           if (val >= 100) val = 100;           sum += ((val - v2[i]) / 10);           t = t - (val - v2[i]);         } else           continue;       }     }     cout << sum << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int z;   struct pi {     bool operator()(const pair<int, int>& p, const pair<int, int>& q) {       if (0) {       } else if (abs(p.first - z) < abs(q.first - z)) {         return 1;       } else if (abs(p.first - z) > abs(q.first - z)) {         return 0;       } else {         if (0) {         } else if (p.first < q.first) {           return 0;         } else if (p.first > q.first) {           return 1;         } else {           if (0) {           } else if (p.first > z) {             if (p.second < q.second) {               return 1;             } else {               return 0;             }           } else if (p.first < z) {             if (p.second < q.second) {               return 0;             } else {               return 1;             }           } else {             return 0;           }         }       }     }   };   int main() {     int i;     int n, k;     while (cin >> n >> k) {       string s[10];       cin >> s[0];       for (i = 1; i < 10; ++i) s[i] = s[0];       pair<int, int> a[10][10100];       for (i = 0; i < n; ++i) a[0][i] = make_pair(s[0][i] -  0 , i);       for (i = 1; i < 10; ++i) copy(a[0], a[0] + n, a[i]);       int mn = 1 << 30, mnz;       for (z = 0; z < 10; ++z) {         sort(a[z], a[z] + n, pi());         int sm = 0;         for (i = 0; i < k; ++i) {           sm += abs(a[z][i].first - z);           s[z][a[z][i].second] =  0  + z;         }         if (0) {         } else if (mn > sm) {           mn = sm;           mnz = z;         } else if (mn < sm) {         } else {           if (s[mnz] > s[z]) mnz = z;         }       }       cout << mn << endl;       cout << s[mnz] << endl;     }     return 0;   }