malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   vector<int> gr[N];   bool access = true;   int p[N], h[N], a[N], g[N];   void dfs(int v, int ancestor = -1) {     a[v] = p[v];     int sum_g = 0;     for (int to : gr[v]) {       if (to == ancestor) continue;       dfs(to, v);       sum_g += g[to];       a[v] += a[to];     }     if ((a[v] + h[v]) % 2 == 0) {     } else       access = false;     g[v] = (a[v] + h[v]) / 2;     if (g[v] >= 0 && g[v] <= a[v]) {     } else       access = false;     if (sum_g <= g[v]) {     } else       access = false;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int q;     cin >> q;     while (q--) {       int n, m;       cin >> n >> m;       for (int i = 0; i < n; ++i) cin >> p[i];       for (int i = 0; i < n; ++i) cin >> h[i];       for (int i = 0; i < n - 1; ++i) {         int a, b;         cin >> a >> b;         --a, --b;         gr[a].push_back(b);         gr[b].push_back(a);       }       dfs(0);       cout << (access ?  YES  :  NO ) << endl;       access = true;       for (int i = 0; i < n; ++i) gr[i].clear();     }   }
#include <bits/stdc++.h>   using namespace std;   int n, A[6], B[(1 << 6)], C[100001], P[(1 << 6)], Q[(1 << 6)];   char s[100001];   void TakeInputs() {     string s;     cin >> s;     for (auto c : s) {       int i = c -  a ;       ++A[i];     }     n = s.size();     for (int i = 0; i < n; ++i) {       C[i] = (1 << 6) - 1;     }     int m;     cin >> m;     while (m--) {       int i, mask = 0;       cin >> i >> s;       for (auto c : s) {         int j = c -  a ;         mask |= (1 << j);       }       --i;       C[i] = mask;     }     for (int i = 0; i < n; ++i) {       ++B[C[i]];     }   }   bool Check() {     for (int i = 0; i < (1 << 6); ++i) {       P[i] = 0;       Q[i] = B[i];     }     for (int k = 0; k < 6; ++k) {       P[(1 << k)] = A[k];     }     for (int j = 0; j < 6; ++j) {       for (int i = 0; i < (1 << 6); ++i) {         if ((i & (1 << j)) > 0) {           P[i] += P[(i ^ (1 << j))];           Q[i] += Q[(i ^ (1 << j))];         }       }     }     bool ans = 1;     for (int i = 0; i < (1 << 6) && ans == 1; ans = (P[i] >= Q[i]), ++i)       ;     return ans;   }   int main() {     TakeInputs();     for (int i = 0; i < n; ++i) {       for (int k = 0; k < 6; ++k) {         if (A[k] == 0 || s[i] >=  a  || ((1 << k) & C[i]) == 0) {           continue;         }         --A[k];         --B[C[i]];         if (Check()) {           s[i] =  a  + k;         } else {           ++A[k];           ++B[C[i]];         }       }       if (!(s[i] >=  a )) {         break;       }     }     if (strlen(s) == n) {       printf( %s n , s);     } else {       printf( Impossible n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int team[101];   char c[101];   int dp[21][(1 << 20) + 5];   int a[1001];   int solve(int pos, int mask) {     if (pos == n) return 0;     if (dp[pos][mask] != -1) return dp[pos][mask];     int ans;     if (team[pos] == 1) {       ans = -(1 << 30);       for (int i = 0; i < m; i++) {         if (mask & (1 << i)) {           if (c[pos] ==  p )             ans = max(ans, a[i] + solve(pos + 1, mask ^ (1 << i)));           else             ans = max(ans, solve(pos + 1, mask ^ (1 << i)));         }       }     } else {       ans = (1 << 30);       for (int i = 0; i < m; i++) {         if (mask & (1 << i)) {           if (c[pos] ==  p )             ans = min(ans, solve(pos + 1, mask ^ (1 << i)) - a[i]);           else             ans = min(ans, solve(pos + 1, mask ^ (1 << i)));         }       }     }     return dp[pos][mask] = ans;   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     reverse(a, a + n);     cin >> m;     n = m;     memset(dp, -1, sizeof(dp));     for (int i = 0; i < m; i++) {       cin >> c[i] >> team[i];     }     cout << solve(0, (1 << m) - 1) << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     scanf( %lld , &n);     long long a, b, c;     scanf( %lld , &a);     scanf( %lld , &b);     scanf( %lld , &c);     if (a <= (b - c)) {       printf( %lld n , n / a);     } else {       n -= b;       long long x = 0;       if (n >= 0) {         x = n / (b - c);         n %= (b - c);       }       n += b;       if (n >= b) {         n = n - (b - c);         x++;       }       x += max(n / a, n / b);       printf( %lld n , x);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, kk, a[200];     scanf( %d%d , &n, &kk);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     int sum, tp, mx = a[0];     for (int i = 0; i < n; i++)       for (int j = i; j < n; j++) {         priority_queue<int> la, sm;         sum = 0;         for (int k = 0; k < n; k++)           if (k >= i && k <= j) {             sm.push(-a[k]);             sum += a[k];           } else             la.push(a[k]);         if (mx < sum) mx = sum;         int ts = sum;         for (int k = 0; k < kk; k++) {           sum += sm.top();           la.push(-sm.top());           sm.pop();           sum += la.top();           sm.push(-la.top());           la.pop();           if (ts >= sum) break;         }         if (mx < sum) mx = sum;       }     printf( %d , mx);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string s, p;   long long l, r, dp[32][3][3][3][3];   string ts(long long x) {     string ans;     while (x) {       ans.push_back( 0  + x % 2);       x /= 2;     }     while (ans.size() != 31) ans.push_back( 0 );     reverse(ans.begin(), ans.end());     return ans;   }   long long f(int x, int a, int b, int c, int d) {     if (x == s.size()) return 1;     if (dp[x][a][b][c][d] != -1) return dp[x][a][b][c][d];     long long ans = 0;     for (int i = 0; i <= 1; i++) {       for (int j = 0; j <= 1; j++) {         if (i == j && i) continue;         char aa = i +  0 , bb = j +  0 ;         if (a && s[x] > aa) continue;         if (b && p[x] < aa) continue;         if (c && s[x] > bb) continue;         if (d && p[x] < bb) continue;         int na = (a & (s[x] == aa));         int nb = (b & (p[x] == aa));         int nc = (c & (s[x] == bb));         int nd = (d & (p[x] == bb));         ans += f(x + 1, na, nb, nc, nd);       }     }     return dp[x][a][b][c][d] = ans;   }   int main() {     int t;     cin >> t;     while (t--) {       memset(dp, -1, sizeof dp);       cin >> l >> r;       s = ts(l), p = ts(r);       cout << f(0, 1, 1, 1, 1) << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     return !b ? a : gcd(b, a % b);   }   template <typename T>   T lcm(T a, T b) {     return a * (b / gcd(a, b));   }   template <typename T>   T sqr(T a) {     return a * a;   }   template <typename T>   T cube(T a) {     return a * a * a;   }   template <typename T>   inline void smin(T &a, T b) {     a = a < b ? a : b;   }   template <typename T>   inline void smax(T &a, T b) {     a = a > b ? a : b;   }   template <typename T>   inline void Int(T &n) {     n = 0;     int f = 1;     register int ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) f = -1;     for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch -  0 ;     n = n * f;   }   vector<string> split(const string &s, char c) {     vector<string> v;     stringstream second(s);     string x;     while (getline(second, x, c)) v.emplace_back(x);     return move(v);   }   void err(vector<string>::iterator it) {}   template <typename T, typename... Args>   void err(vector<string>::iterator it, T a, Args... args) {     cout << it->substr((*it)[0] ==    , it->length()) <<   =   << a <<  ,  ;     err(++it, args...);   }   inline int in() {     int n;     scanf( %d , &n);     return n;   }   inline long long Lin() {     long long n;     scanf( %lld , &n);     return n;   }   inline double Din() {     double n;     scanf( %lf , &n);     return n;   }   inline int add(int a, int b, int mod) {     a += b;     return a >= mod ? a - mod : a;   }   inline int sub(int a, int b, int mod) {     a -= b;     return a < 0 ? a + mod : a;   }   inline int mul(int a, int b, int mod) { return (long long)a * b % mod; }   const int N = 2e5 + 5;   const int inf = (int)2e9 + 5;   const long long Inf = (long long)1e18 + 5;   const int mod = (int)1e9 + 7;   const long long Mod = (long long)1e9 + 7;   vector<int> b[N];   int solve() {     int n = in(), k = in();     vector<int> a(n);     for (int i = 0; i < n; i++) {       a[i] = in();     }     for (int i = 0; i < n; i++) {       int x = a[i], cur = 0;       while (x >= 1) {         b[x].push_back(cur);         x /= 2;         cur++;       }     }     long long ans = Inf;     for (int i = 0; i <= 2e5; i++) {       sort(b[i].begin(), b[i].end());       if ((int)b[i].size() >= k) {         long long cur = 0;         for (int j = 0; j < k; j++) {           cur += b[i][j];         }         smin(ans, cur);       }     }     printf( %lld n , ans);     return 0;   }   int main() {     int test = 1, tc = 0;     while (test--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LL_INF = 0x3f3f3f3f3f3f3f3f;   const double PI = 3.14159265359;   const int mod = 1000 * 1000 * 1000 + 7;   int dx[4] = {1, -1, 0, 0};   int dy[4] = {0, 0, 1, -1};   string s;   int dp[100005];   int solve(int idx) {     if (idx == s.size()) return 1;     int &ret = dp[idx];     if (~ret) return ret;     ret = 0;     if (s[idx] ==  u  or s[idx] ==  n ) {       if (idx + 1 < s.size() and s[idx + 1] == s[idx]) {         ret = (solve(idx + 1) + solve(idx + 2)) % mod;       } else         ret = solve(idx + 1) % mod;     } else       ret = solve(idx + 1) % mod;     return ret;   }   int main() {     cin >> s;     if (s.find( w ) != -1 or s.find( m ) != -1) return cout << 0 << endl, 0;     memset(dp, -1, sizeof dp);     cout << solve(0) % mod << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> arr[100005];   vector<int> v1[100005];   int visited[100005];   stack<int> s;   int n, m;   set<int> se[100005];   void dfs(int node) {     int i;     visited[node] = 1;     int n = v1[node].size();     for (i = 0; i < n; i++) {       if (visited[v1[node][i]] == 0) {         dfs(v1[node][i]);       }     }     s.push(node);   }   bool check(int mid) {     int i;     bool ans = true;     for (i = 1; i <= mid; i++) {       v1[arr[i].first].push_back(arr[i].second);       se[arr[i].first].insert(arr[i].second);     }     for (i = 1; i <= n; i++) visited[i] = 0;     for (i = 1; i <= n; i++) {       if (visited[i] == 0) {         dfs(i);       }     }     int x;     x = s.top();     s.pop();     while (!s.empty()) {       if (se[x].find(s.top()) == se[x].end()) ans = false;       x = s.top();       s.pop();     }     for (i = 1; i <= n; i++) {       v1[i].clear();       se[i].clear();     }     return ans;   }   int main() {     int i, j, k;     scanf( %d , &n);     scanf( %d , &m);     for (i = 1; i <= m; i++) {       scanf( %d , &arr[i].first);       scanf( %d , &arr[i].second);     }     int l = 0, r = m, mid;     while (l <= r) {       mid = (l + r) / 2;       bool temp1 = check(mid);       if (mid == m) {         if (temp1 == false) {           printf( -1 n );           return 0;         } else {           r = mid - 1;           continue;         }       }       bool temp2 = check(mid + 1);       if (temp1 == false && temp2 == true) {         printf( %d n , mid + 1);         return 0;       } else if (temp2 == true) {         r = mid - 1;       } else {         l = mid + 1;       }     }     printf( -1 n );     return 0;   }
#include <bits/stdc++.h>   const int Maxn = 1000;   const int Inf = 0x3f3f3f3f;   int n, m;   int a[Maxn + 5], b[Maxn + 5];   std::vector<int> edge[Maxn + 5];   int pre[Maxn + 5];   long long dis[Maxn + 5];   bool vis[Maxn + 5];   bool check(int x) {     for (int i = 1; i <= n; i++) {       vis[i] = 0;     }     vis[1] = 1;     long long now = x, tot = 1;     while (tot < n) {       std::queue<int> q;       for (int i = 1; i <= n; i++) {         pre[i] = 0;         dis[i] = 0;         if (vis[i]) {           q.push(i);           dis[i] = now;         }       }       int x = 0, y = 0;       while (!q.empty()) {         int u = q.front();         q.pop();         for (int i = 0; i < (int)edge[u].size(); i++) {           int v = edge[u][i];           if (!vis[v]) {             if (dis[u] <= a[v]) {               continue;             }             if (pre[v] == 0) {               pre[v] = u;               dis[v] = dis[u] + b[v];               q.push(v);             } else if (v != pre[u]) {               x = u, y = v;               goto OUT;             }           } else if (!vis[u] && v != pre[u]) {             x = u, y = v;             goto OUT;           }         }       }     OUT:;       if (x == 0) {         return 0;       }       for (int u = x; u; u = pre[u]) {         if (!vis[u]) {           vis[u] = 1;           now += b[u];           tot++;         }       }       for (int u = y; u; u = pre[u]) {         if (!vis[u]) {           vis[u] = 1;           now += b[u];           tot++;         }       }     }     return 1;   }   void solve() {     int left = Inf, right = 0;     scanf( %d%d , &n, &m);     for (int i = 2; i <= n; i++) {       scanf( %d , &a[i]);       left = std::min(left, a[i]);       right = std::max(right, a[i]);     }     right++;     for (int i = 2; i <= n; i++) {       scanf( %d , &b[i]);     }     for (int i = 1; i <= n; i++) {       edge[i].clear();     }     for (int i = 1; i <= m; i++) {       int u, v;       scanf( %d%d , &u, &v);       edge[u].push_back(v), edge[v].push_back(u);     }     while (left < right) {       int mid = (left + right) >> 1;       if (check(mid)) {         right = mid;       } else {         left = mid + 1;       }     }     printf( %d n , left);   }   int main() {     int T;     scanf( %d , &T);     while (T--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int Maxn = 102;   int n, K;   double dp[2][Maxn][Maxn];   int a[Maxn];   inline int c2(int x) { return x * (x - 1) / 2; }   inline int max(int x, int y) { return x > y ? x : y; }   inline int min(int x, int y) { return x < y ? x : y; }   int main() {     scanf( %d%d , &n, &K);     if (K > 875) K = 875;     for (int i = 1; i <= n; i++) scanf( %d , a + i);     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++) dp[0][i][j] = a[i] > a[j];     int cs = 1;     for (int k = 1; k <= K; k++, cs ^= 1) {       for (int i = 1; i <= n; i++)         for (int j = i + 1; j <= n; j++) {           dp[cs][i][j] = 0;           dp[cs][i][j] += dp[cs ^ 1][i][j] * (c2(i) + c2(n - j + 1) + c2(j - i));           for (int sum = j + 1; sum <= i + n; sum++) {             dp[cs][i][j] += (1.0 - dp[cs ^ 1][sum - j][sum - i]) *                             (min(i, sum - j) - max(1, sum - n) + 1);           }           for (int sum = i + 1; sum <= i + j - 1; sum++)             dp[cs][i][j] += dp[cs ^ 1][sum - i][j] *                             (min(i, sum - i) - max(1, sum - j + 1) + 1);           for (int sum = j + i + 1; sum <= n + j; sum++)             dp[cs][i][j] += dp[cs ^ 1][i][sum - j] *                             (min(j, sum - j) - max(i + 1, sum - n) + 1);           dp[cs][i][j] /= c2(n + 1);         }     }     double ans = 0;     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++) ans += dp[cs ^ 1][i][j];     printf( %.12f n , ans);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, x, y = 0;     cin >> n >> x;     for (int i = 1; i <= n; i++) {       char a;       long long d;       cin >> a >> d;       if (a ==  + )         x += d;       else if (x >= d)         x -= d;       else         y++;     }     cout << x <<     << y << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9;   int a, c;   int arrA[2000];   int arrC[2000];   void sol() {     cin >> c >> a;     for (int i = 0; i < c; i++) {       cin >> arrC[i];     }     for (int i = 0; i < a; i++) {       cin >> arrA[i];     }     int result = 0;     int j = 0;     for (int i = 0; i < a && j < c; i++, j++) {       while (j < c && arrC[j] > arrA[i]) j++;       if (j >= c) break;       result++;     }     cout << result << endl;   }   int main(void) {     sol();     return 0;   }
#include <bits/stdc++.h>   void __check(const char* expr, const char* file, unsigned int line) {     printf( %s:%u: failed check `%s  n , file, line, expr);     abort();   }   using namespace std;   template <typename T>   bool uin(T& a, T b) {     if (b < a) {       a = b;       return true;     }     return false;   }   template <typename T>   bool uax(T& a, T b) {     if (a < b) {       a = b;       return true;     }     return false;   }   template <typename T>   int sz(const T& t) {     return static_cast<int>(t.size());   }   template <typename T>   void shrink(std::vector<T>& v) {     std::vector<T>(v).swap(v);   }   const bool DBG = false;   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     vector<int> v(1 + n);     for (int i = 0; i < (int)(n); ++i) {       cin >> v[i];       v[i]--;     }     v[n] = v[n - 1] ^ 1;     int prev = v[0];     array<int, 2> a;     a.fill(0);     a[prev]++;     int res = 0;     for (int i = 1; i <= n; ++i) {       int value = v[i];       if (value != prev) {         if (a[value] > 0) {           int opposite = value ^ 1;           auto local_res = min(a[opposite], a[value]) * 2;           res = max(local_res, res);           a[value] = 0;         }       }       a[value]++;       prev = value;     }     cout << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Player {     string name;     long int points;     map<int, int> places;   };   Player compSecond(Player a, Player b) {     if (a.points > b.points) {       return a;     } else if (a.points < b.points) {       return b;     }     for (int i = 1; i < 70; i++) {       if (a.places[i] > b.places[i]) {         return a;       }       if (a.places[i] < b.places[i]) {         return b;       }     }   }   Player compFirst(Player a, Player b) {     if (a.places[1] > b.places[1]) {       return a;     } else if (a.places[1] < b.places[1]) {       return b;     }     if (a.points > b.points) {       return a;     } else if (a.points < b.points) {       return b;     }     for (int i = 2; i < 70; i++) {       if (a.places[i] > b.places[i]) {         return a;       }       if (a.places[i] < b.places[i]) {         return b;       }     }   }   map<string, Player> parts;   vector<int> points = {25, 18, 15, 12, 10, 8, 6, 4, 2, 1};   int main() {     int n;     cin >> n;     set<Player> st;     for (int i = 0; i < n; i++) {       int players;       cin >> players;       for (int j = 1; j <= players; j++) {         string name;         cin >> name;         parts[name].points += (j <= 10) ? points[j - 1] : 0;         parts[name].places[j]++;         parts[name].name = name;       }     }     Player min;     Player first;     Player second;     first.points = 0;     second.points = 0;     for (auto i : parts) {       first = compSecond(i.second, first);       second = compFirst(i.second, second);     }     cout << first.name << endl << second.name << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n;   pair<int, int> a[111];   int m;   long long p[111];   long long an;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d%d , &a[i].second, &a[i].first);     sort(a + 1, a + 1 + n);     scanf( %d , &m);     for (int i = 1; i <= m; ++i) scanf( %I64d , &p[i]);     p[++m] = 1e12;     long long cur = 0;     int ps = 1;     for (int i = 1; i <= n; ++i) {       while (a[i].second) {         if (cur + a[i].second > p[ps]) {           a[i].second -= p[ps] - cur;           an += 1ll * ps * (p[ps] - cur) * a[i].first;           cur = p[ps++];         } else {           an += 1ll * a[i].second * ps * a[i].first;           cur += a[i].second;           a[i].second = 0;         }       }     }     printf( %I64d , an);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 4e5 + 5;   struct Edge {     int v, nxt;     Edge() {}     Edge(int _v, int _nxt) : v(_v), nxt(_nxt) {}   };   Edge E[MAXN * 2];   int head[MAXN], ans[MAXN], sub[MAXN], mxsub[MAXN];   int tot = 0, fuc, n, x, y;   void addedge(int u, int v) {     E[tot] = Edge(v, head[u]);     head[u] = tot++;   }   void dfs1(int now, int fa) {     sub[now] = 1;     mxsub[now] = 1;     int tmp = 1;     for (int i = head[now]; ~i; i = E[i].nxt) {       int v = E[i].v;       if (v != fa) {         dfs1(v, now);         sub[now] += sub[v];         mxsub[now] = max(mxsub[now], mxsub[v]);         tmp += sub[v];       }     }     if (tmp <= fuc) {       mxsub[now] = tmp;     }   }   void dfs2(int now, int fa, int subfa, int mxfa) {     int cnt = subfa > fuc;     int id;     if (cnt) {       id = -1;     }     int tmp = n;     int mx1, mx2;     int id1, id2;     mx1 = mx2 = -1;     for (int i = head[now]; ~i; i = E[i].nxt) {       int v = E[i].v;       if (v != fa) {         if (id1 == -1 || mx1 < mxsub[v]) {           mx2 = mx1;           id2 = id1;           mx1 = mxsub[v];           id1 = v;         } else if (id2 == -1 || mx2 < mxsub[v]) {           mx2 = mxsub[v];           id2 = v;         }       }     }     for (int i = head[now]; ~i; i = E[i].nxt) {       int v = E[i].v;       if (v != fa) {         dfs2(v, now, sub[now] - sub[v] + subfa,              max(tmp - sub[v] <= fuc ? tmp - sub[v] : 0,                  max(mxfa, id1 == v ? mx2 : mx1)));         if (sub[v] > fuc) {           cnt++;           id = v;         }       }     }     if (cnt > 1) {       ans[now] = 0;     } else if (cnt == 0) {       ans[now] = 1;     } else if (id != -1) {       ans[now] = (sub[id] - mxsub[id] <= fuc);     } else {       ans[now] = (subfa - mxfa <= fuc);     }   }   int main() {     memset(head, -1, sizeof(head));     scanf( %d , &n);     for (int i = 1; i < n; i++) {       scanf( %d%d , &x, &y);       addedge(x, y);       addedge(y, x);     }     fuc = n / 2;     dfs1(1, 1);     dfs2(1, 1, 0, 0);     for (int i = 1; i <= n; i++) {       printf( %d  , ans[i]);     }   }
#include <bits/stdc++.h>   using namespace std;   const double EPS = 0.0000001;   const double PI = acos(-1);   const long long INFLL = 0x7FFFFFFFFFFFFFFF;   const int INF = 0x7FFFFFFF;   template <typename T>   inline void next(T &num) {     char c;     num = 0;     do {       c = getchar_unlocked();     } while (c != EOF && c ==     && c ==   n  && c ==   t );     int sign = (c ==  -  ? -1 : 1);     if (c !=  - ) num += (c -  0 );     while ((c = getchar_unlocked()) != EOF && c !=   n  && c !=   t  &&            c !=    ) {       num *= 10;       num += (c -  0 );     }     num *= sign;   }   inline string getstr() {     string str;     char k;     while ((k = getchar_unlocked()) ==     || k ==   n ) {       k = getchar_unlocked();       if (k ==     || k ==   n )         continue;       else         break;     }     str.push_back(k);     while ((k = getchar_unlocked()) != EOF && k !=   n  && k !=   t  &&            k !=   v  && k !=   0  && k !=    )       str.push_back(k);     return str;   }   const int M = 50005;   long long n, a[M];   long long ans;   void dfs(long long n, int p, long long now) {     if (now > ans) return;     if (!n) {       ans = min(ans, now);       return;     }     if (!p) return;     int need = n / a[p];     dfs(n - need * a[p], p - 1, now + p * need);     dfs((need + 1) * a[p] - n, p - 1, now + p * (need + 1));   }   int main() {     cin >> n;     for (int i = int(1), _b = int(16); i <= _b; i++) a[i] = a[i - 1] * 10 + 1;     ans = n;     dfs(n, 16, 0);     cout << ans;     return 0;   }