malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   template <typename T>   void print(T t) {     cout << t << endl;   }   long long n = 0, t = 0;   long long small = -1e18, big = 1e18;   const long long mod = 1e9 + 7;   map<long long, long long> occ;   map<pair<long long, long long>, long long> occP;   void print2dVec(vector<vector<long long>> v) {     for (int i = 0; i < v.size(); ++i) {       cout << i <<   :  ;       for (int j = 0; j < v[i].size(); ++j) {         cout << v[i][j] <<    ;       }       print(  );     }   }   void printSet(set<int> s) {     for (auto itr = s.begin(); itr != s.end(); ++itr) {       cout << *itr <<    ;     }   }   long long factorial(int n) {     long long ans = 1;     for (int i = 1; i <= n; ++i) {       ans *= i;     }     return ans;   }   long long findGCD(long long a, long long b) {     long long ans = 1;     for (int i = 1; i <= min(a, b); ++i) {       if (a % i == 0 && b % i == 0) {         ans = i;       }     }     return ans;   }   long long findLCM(long long a, long long b) {     for (long long i = min(a, b); i <= a * b; ++i) {       if (i % a == 0 && i % b == 0) {         return i;       }     }     return -1;   }   bool isPrime(int num) {     if (num <= 1) return false;     if (num <= 3) return true;     int range = sqrt(num);     if (num % 2 == 0 || num % 3 == 0) return false;     for (int i = 5; i <= range; i += 6)       if (num % i == 0 || num % (i + 2) == 0) return false;     return true;   }   vector<long long> primeFactors(long long x) {     vector<long long> fac;     for (long long i = 2; i * i <= x; i++) {       if (x % i == 0) {         while (x % i == 0) {           fac.push_back(i);           x /= i;         }       }     }     return fac;   }   bool sortVec(const vector<long long>& v1, const vector<long long>& v2) {     return v1[0] < v2[0];   }   bool check(long long val) { return false; }   long long binsearch(long long low, long long high) {     while (low < high) {       long long mid = (low + high) / 2;       if (check(mid)) {         high = mid;       } else {         low = mid + 1;       }     }     return low;   }   unordered_map<long long, long long> d;   long long rib(long long n, long long a, long long b, long long c) {     if (n == 0) return 0;     if (n < 0) return small;     if (d.count(n))       return d[n];     else {       d[n] = 1 + max(rib(n - a, a, b, c),                      max(rib(n - b, a, b, c), rib(n - c, a, b, c)));       return d[n];     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout << fixed;     cout << setprecision(10);     ;     long long n, a, b, c;     cin >> n >> a >> b >> c;     cout << rib(n, a, b, c);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     if (n == 1) {       cout <<  -1 n ;       return 0;     }     if (n & 1) {       cout <<  -1 n ;       return 0;     }     for (int i = 1; i <= n; i++) {       if (i & 1) {         cout << (i + 1) <<    ;       } else {         cout << (i - 1) <<    ;       }     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100030;   const long long MODD = 1000000009;   int ctr[128];   void do_case() {     int n, k;     cin >> n >> k;     string s;     cin >> s;     for (int i = 0; i < s.size(); i++) ctr[s[i]]++;     sort(ctr, ctr + 128);     reverse(ctr, ctr + 128);     long long ans = 0;     for (int i = 0; i < 128; i++) {       long long v = min(k, ctr[i]);       ans += v * v;       k -= v;     }     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     do_case();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<vector<long long> > g;   vector<bool> used;   void dfs(long long v, long long &h, long long &e) {     used[v] = true;     h++;     e += g[v].size();     for (int i = 0; i < g[v].size(); ++i) {       if (!used[g[v][i]]) dfs(g[v][i], h, e);     }   }   int main() {     long long n, m;     cin >> n >> m;     g.resize(n);     used.resize(n);     for (int i = 0; i < m; ++i) {       long long u, v;       cin >> u >> v;       g[u - 1].push_back(v - 1);       g[v - 1].push_back(u - 1);     }     for (int i = 0; i < n; ++i) {       if (!used[i]) {         long long h = 0, e = 0;         dfs(i, h, e);         if (h * (h - 1) != e) {           cout <<  NO ;           return 0;         }       }     }     cout <<  YES ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     vector<int> res;     for (int i = n + 1;; i++) {       if (res.size() == n) {         break;       }       res.push_back(i);     }     for (auto x : res) {       cout << x <<    ;     }   }   int main() {     long long t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long labs(long long a) { return a < 0 ? (-a) : a; }   long long max(long long a, long long b) { return a > b ? a : b; }   long long min(long long a, long long b) { return a < b ? a : b; }   int main() {     int n;     cin >> n;     int h = 1;     vector<vector<int> > ans;     for (int i = 0; ans.size() + (h - 1) <= n; i++) {       ans.push_back(vector<int>());       for (int(k) = 0; (k) < ((int)ans.size() - 1); (k)++) {         ans[i].push_back(h);         ans[k].push_back(h++);       }     }     cout << ans.size() << endl;     for (int(i) = 0; (i) < (ans.size()); (i)++) {       for (int(j) = 0; (j) < (ans[i].size()); (j)++) {         cout << ans[i][j] <<    ;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int infinity = 1e9;   int N, M;   int A[509][509];   int P[509][509];   int squaresum(int cx, int cy, int k) {     int ax = cx - k / 2;     int ay = cy - k / 2;     int bx = cx + k / 2;     int by = cy + k / 2;     return P[bx][by] - P[ax - 1][by] - P[bx][ay - 1] + P[ax - 1][ay - 1];   }   bool goodk(int i, int j, int k) {     k /= 2;     return ((i - k >= 1) && (i + k <= N) && (j - k >= 1) && (j + k <= M));   }   int main() {     scanf( %d %d  , &N, &M);     for (int i = 1; i <= N; i++)       for (int j = 1; j <= M; j++) scanf( %d , &A[i][j]);     for (int i = 1; i <= N; i++)       for (int j = 1; j <= M; j++)         P[i][j] = P[i - 1][j] + P[i][j - 1] - P[i - 1][j - 1] + A[i][j];     int ans = -infinity;     for (int i = 2; i <= N - 1; i++)       for (int j = 2; j <= M - 1; j++) {         int spiral = A[i][j];         for (int k = 3; goodk(i, j, k); k += 2) {           spiral = squaresum(i, j, k) - spiral - A[i - k / 2 + 1][j - k / 2];           ans = max(ans, spiral);         }       }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   int cnt[maxn], num[maxn], t[maxn], r[maxn], last[maxn];   char ans[maxn];   int main() {     int T, m, k;     scanf( %d , &T);     while (T--) {       scanf( %d%d , &m, &k);       for (int i = 1; i <= k; i++) {         scanf( %d , &num[i]);         ans[i] =  N ;         cnt[i] = 0;         last[i] = 0;       }       int unknown = 0;       bool flag = 0;       for (int i = 1; i <= m - 1; i++) {         scanf( %d%d , &t[i], &r[i]);         last[t[i]] = i;       }       for (int i = 1; i <= m - 1; i++) {         if (r[i] == 0) {           if (t[i])             cnt[t[i]]++;           else             unknown++;         } else {           if (flag == 0) {             int tmp = 0x3fffffff;             for (int j = 1; j <= k; j++) {               if (last[j] < i && cnt[j] + unknown >= num[j]) {                 tmp = min(tmp, num[j] - cnt[j]);                 ans[j] =  Y ;               }             }             unknown -= tmp;             flag = 1;           }           if (t[i])             cnt[t[i]]++;           else             unknown++;         }         if (t[i] != 0 && cnt[t[i]] == num[t[i]]) {           flag = 1;         }       }       for (int i = 1; i <= k; i++) {         if (num[i] - cnt[i] - unknown <= 0) {           ans[i] =  Y ;         }       }       ans[k + 1] = 0;       printf( %s n , ans + 1);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int Maxn = 1000;   int n, m;   char B[Maxn][Maxn];   bool takeHor(int r, int c1, int c2) {     if (c2 >= m) return false;     if (B[r][c1] !=  W  || B[r][c1 + 1] !=  B  || B[r][c2] !=  W ) return false;     bool ctake[4];     fill(ctake, ctake + 4, true);     if (r - 1 >= 0)       for (int j = c1; j <= c2; j++)         if ( a  <= B[r - 1][j] && B[r - 1][j] <=  d )           ctake[B[r - 1][j] -  a ] = false;     if (r + 1 < n)       for (int j = c1; j <= c2; j++)         if ( a  <= B[r + 1][j] && B[r + 1][j] <=  d )           ctake[B[r + 1][j] -  a ] = false;     if (c1 - 1 >= 0 &&  a  <= B[r][c1 - 1] && B[r][c1 - 1] <=  d )       ctake[B[r][c1 - 1] -  a ] = false;     if (c2 + 1 < m &&  a  <= B[r][c2 + 1] && B[r][c2 + 1] <=  d )       ctake[B[r][c2 + 1] -  a ] = false;     for (int i = 0; i < 4; i++)       if (ctake[i]) {         for (int j = c1; j <= c2; j++) B[r][j] =  a  + i;         break;       }     return true;   }   bool takeVer(int c, int r1, int r2) {     if (r2 >= n) return false;     if (B[r1][c] !=  W  || B[r1 + 1][c] !=  B  || B[r2][c] !=  W ) return false;     bool ctake[4];     fill(ctake, ctake + 4, true);     if (c - 1 >= 0)       for (int i = r1; i <= r2; i++)         if ( a  <= B[i][c - 1] && B[i][c - 1] <=  d )           ctake[B[i][c - 1] -  a ] = false;     if (c + 1 < m)       for (int i = r1; i <= r2; i++)         if ( a  <= B[i][c + 1] && B[i][c + 1] <=  d )           ctake[B[i][c + 1] -  a ] = false;     if (r1 - 1 >= 0 &&  a  <= B[r1 - 1][c] && B[r1 - 1][c] <=  d )       ctake[B[r1 - 1][c] -  a ] = false;     if (r2 + 1 < m &&  a  <= B[r2 + 1][c] && B[r2 + 1][c] <=  d )       ctake[B[r2 + 1][c] -  a ] = false;     for (int i = 0; i < 4; i++)       if (ctake[i]) {         for (int j = r1; j <= r2; j++) B[j][c] =  a  + i;         break;       }     return true;   }   bool canFill(int r, int c) {     if (r >= n) return true;     if (c >= m) return canFill(r + 1, 0);     if (B[r][c] !=  W  && B[r][c] !=  B ) return canFill(r, c + 1);     if (B[r][c] ==  B ) return false;     if (takeHor(r, c, c + 2)) return canFill(r, c + 3);     if (takeVer(c, r, r + 2)) return canFill(r, c + 1);     return false;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> B[i][j];         B[i][j] = toupper(B[i][j]);       }     if (canFill(0, 0)) {       cout <<  YES n ;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) cout << B[i][j];         cout << endl;       }     } else       cout <<  NO n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100005, P = 1e9 + 7;   int n, s, ans, c[N], f[N];   inline int pw(int a, int b) {     int r = 1;     for (; b; b >>= 1, a = 1ll * a * a % P)       if (b & 1) r = 1ll * r * a % P;     return r;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &c[i]), s += c[i];     f[1] = 1ll * (s - 1) * (s - 1) % P * pw(s, P - 2) % P;     for (int i = 1; i < 100000; i++)       f[i + 1] = ((2 * f[i] % P - f[i - 1] + P) % P -                   1ll * (s - 1) * pw(s - i, P - 2) % P + P) %                  P;     for (int i = 1; i <= n; i++) ans = (ans + f[c[i]]) % P;     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int LIMIT = 1e7;   int prime[LIMIT], palin[LIMIT];   int pr[LIMIT], pa[LIMIT];   int cnt_dig(int x) {     if (x == 0)       return 1;     else {       int ans = 0;       while (x > 0) {         x /= 10;         ans++;       }       return ans;     }   }   int main() {     int n = 4 * 1e6, dg, fl, t;     stack<int> st;     for (int i = 0; i < n; i++) prime[i] = true;     prime[0] = prime[1] = false;     prime[2] = true;     for (long long i = 2; i < n; i++) {       if (prime[i] == true) {         for (long long j = i * i; j < n; j += i) prime[j] = false;       }     }     for (int i = 0; i < n; i++) {       dg = cnt_dig(i);       t = i;       if (dg == 1)         palin[i] = true;       else {         fl = 0;         if (dg & 1) {           for (int j = 0; j < dg / 2; j++) {             st.push(t % 10);             t = t / 10;           }           t = t / 10;           for (int j = 0; j < dg / 2; j++) {             if (t % 10 == st.top()) {               t = t / 10;               st.pop();             } else {               fl = 1;               break;             }           }           st.empty();         } else {           for (int j = 0; j < dg / 2; j++) {             st.push(t % 10);             t = t / 10;           }           for (int j = 0; j < dg / 2; j++) {             if (t % 10 == st.top()) {               t = t / 10;               st.pop();             } else {               fl = 1;               break;             }           }           st.empty();         }         if (fl == 0)           palin[i] = true;         else           palin[i] = false;       }     }     for (int i = 1; i < n; i++) {       pr[i] = prime[i] + pr[i - 1];       pa[i] = palin[i] + pa[i - 1];     }     double p, q;     cin >> p >> q;     int l = 1, r = n - 1;     t = 0;     int ans = 0;     for (int i = 1; i < n; i++) {       if (pr[i] <= double(pa[i] * (p / q))) ans = i;     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = INT_MAX / 2;   const int Maxn = 305;   int a[Maxn + 5][Maxn + 5], f[Maxn + 5][Maxn + 5], g[Maxn + 5][Maxn + 5], n;   int solve() {     for (int(i) = (1); (i) <= (n); ++(i))       for (int(j) = (1); (j) <= (n); ++(j)) f[i][j] = -inf;     f[1][1] = a[1][1];     for (int(k) = (2); (k) <= (n * 2 - 1); ++(k)) {       for (int(i) = (1); (i) <= (n); ++(i))         for (int(j) = (1); (j) <= (n); ++(j)) g[i][j] = -inf;       for (int(i) = (1); (i) <= (n); ++(i))         for (int(j) = (i); (j) <= (n); ++(j)) {           if (f[i][j] <= -inf) continue;           for (int(si) = (i); (si) <= (i + 1); ++(si))             for (int(sj) = (j); (sj) <= (j + 1); ++(sj)) {               if (si > sj || si > n || sj > n || k + 1 - si > n || k + 1 - sj > n)                 continue;               int bonus;               if (si == sj)                 bonus = a[k + 1 - si][si];               else                 bonus = a[k + 1 - si][si] + a[k + 1 - sj][sj];               g[si][sj] = max(g[si][sj], f[i][j] + bonus);             }         }       for (int(i) = (1); (i) <= (n); ++(i))         for (int(j) = (1); (j) <= (n); ++(j)) f[i][j] = g[i][j];     }     return f[n][n];   }   int main() {     iostream::sync_with_stdio(false);     cin >> n;     for (int(i) = (1); (i) <= (n); ++(i))       for (int(j) = (1); (j) <= (n); ++(j)) cin >> a[i][j];     cout << solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int msb(long long x) {     union {       double a;       int32_t b[2];     };     a = x;     return (b[1] >> 20) - 1023;   }   void sol() {     int n, x, y;     vector<int> a(4), b(4);     for (int i = (0); i < (4); i++) {       cin >> a[i];     }     if ((a[0] + a[1]) & 1) {       if (a[0] || a[3]) b[0]++;       if (a[1] || a[2]) b[1]++;     } else {       if (a[2] || a[1]) b[2]++;       if (a[3] || a[0]) b[3]++;     }     for (int i = (0); i < (4); i++) cout << (b[i] ?  Ya   :  Tidak  );     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     cin >> t;     while (t--) sol();   }
#include <bits/stdc++.h>   int main() {     int n, k;     scanf( %d %d , &n, &k);     char a[n];     scanf( %s , a);     int i, start, end;     for (i = 0; i < n; i++) {       if (a[i] ==  G ) start = i;       if (a[i] ==  T ) end = i;     }     int sign = start - end;     if (start > end) k = -k;     while ((sign * (start - end)) > 0) {       start = start + k;       if (a[start] ==  # ) {         printf( NO );         break;       }       if (a[start] ==  T ) {         printf( YES );         break;       }     }     if (((sign * (start - end)) < 0) && (a[start] !=  # )) printf( NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 1e3 + 10, inf = 1e18, mod = 1e9 + 7;   struct pair_hash {     template <class T1, class T2>     std::size_t operator()(const std::pair<T1, T2> &p) const {       auto h1 = std::hash<T1>{}(p.first);       auto h2 = std::hash<T2>{}(p.second);       return h1 ^ h2;     }   };   long long pw(long long te, long long tee) {     if (tee == 0) return 1;     long long res = pw(te, tee / 2);     return (tee % 2 ? te * res * res : res * res);   }   long long n;   char a[MAXN][MAXN], ans[MAXN][MAXN];   vector<pair<long long, long long>> v, t, p;   bool flag = true;   void Read_input() {     cin >> n;     for (long long i = 1; i <= n; i++)       for (long long j = 1; j <= n; j++) {         cin >> a[i][j];         if (a[i][j] ==  o ) v.push_back({i, j});         if (a[i][j] ==  x ) p.push_back({i, j});       }   }   void Solve() {     for (long long d1 = 0; d1 <= n; d1++) {       for (long long d2 = 0; d2 <= n; d2++) {         if (d1 == 0 and d2 == 0) continue;         bool f[4] = {true, true, true, true};         for (auto i : v) {           long long x = i.first + d1;           long long y = i.second + d2;           if (x <= n and y <= n and a[x][y] ==  . ) f[0] = false;           x = i.first + d1;           y = i.second - d2;           if (x <= n and y >= 1 and a[x][y] ==  . ) f[1] = false;           x = i.first - d1;           if (x >= 1 and y >= 1 and a[x][y] ==  . ) f[2] = false;           y = i.second + d2;           if (x >= 1 and y <= n and a[x][y] ==  . ) f[3] = false;         }         if (f[0]) t.push_back({d1, d2});         if (f[1]) t.push_back({d1, -d2});         if (f[2]) t.push_back({-d1, -d2});         if (f[3]) t.push_back({-d1, d2});       }     }     for (long long i = 1; i <= 2 * n - 1; i++)       for (long long j = 1; j <= 2 * n - 1; j++) ans[i][j] =  . ;     for (auto i : p) {       bool k = false;       for (auto j : t)         if (i.first - j.first >= 1 and i.second - j.second >= 1 and             a[i.first - j.first][i.second - j.second] ==  o ) {           k = true;           break;         }       if (k == false) flag = false;       if (!flag) break;     }     for (auto i : v) {       bool k = true;       for (auto j : t)         if (i.first + j.first <= n and i.second + j.second <= n and             a[i.first + j.first][i.second + j.second] ==  . ) {           k = false;           break;         }       if (k == false) flag = false;       if (!flag) break;     }     if (!flag) {       cout <<  NO ;       exit(0);     }     cout <<  YES  << endl;     ans[n][n] =  o ;     for (auto i : t)       if (n + i.first <= 2 * n - 1 and n + i.first >= 1 and           n + i.second <= 2 * n - 1 and n + i.second >= 1)         ans[n + i.first][n + i.second] =  x ;     for (long long i = 1; i <= 2 * n - 1; i++) {       for (long long j = 1; j <= 2 * n - 1; j++) cout << ans[i][j];       cout << endl;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     Read_input(), Solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long t, i, n, res, mx, a[300009];   map<int, int> mp;   int main() {     cin >> n;     for (i = 1; i <= 2 * n; i++) {       cin >> a[i];     }     for (i = 1; i <= 2 * n; i++) {       mp[a[i]]++;       if (mp[a[i]] == 1) {         res++;       } else {         res--;       }       if (res >= mx) {         mx = res;       }     }     cout << mx;   }
#include <bits/stdc++.h>   #pragma warning(disable : 4996)   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   #pragma GCC optimize( -ffloat-store )   using namespace std;   clock_t time_p = clock();   void aryanc403() {     time_p = clock() - time_p;     cerr <<  Time Taken :   << (float)(time_p) / CLOCKS_PER_SEC <<   n ;   }   const long long int INF = 0xFFFFFFFFFFFFFFFL;   long long int seed;   mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count());   inline long long int rnd(long long int l = 0, long long int r = INF) {     return uniform_int_distribution<long long int>(l, r)(rng);   }   class CMP {    public:     bool operator()(pair<long long int, long long int> a,                     pair<long long int, long long int> b) {       return !(a.second < b.second ||                (a.second == b.second && a.first >= b.first));     }   };   void add(map<long long int, long long int> &m, long long int x,            long long int cnt = 1) {     auto jt = m.find(x);     if (jt == m.end())       m.insert({x, cnt});     else       jt->second += cnt;   }   void del(map<long long int, long long int> &m, long long int x,            long long int cnt = 1) {     auto jt = m.find(x);     if (jt->second <= cnt)       m.erase(jt);     else       jt->second -= cnt;   }   bool cmp(const pair<long long int, long long int> &a,            const pair<long long int, long long int> &b) {     return a.first < b.first || (a.first == b.first && a.second < b.second);   }   const long long int mod = 1000000007L;   const long long int maxN = 100000L;   long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y, ans;   long long int m;   string s;   vector<pair<long long int, long long int> > a, e[maxN + 5];   long long int d[maxN + 5];   priority_queue<pair<long long int, long long int>,                  vector<pair<long long int, long long int> >, CMP>       pq;   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     {       cin >> n >> m >> k;       a.clear();       a.reserve(k);       for (i = 0; i < (m); ++i) {         cin >> x >> y >> in;         e[x].push_back({y, in});         e[y].push_back({x, in});       }       for (i = (1); i <= (n); ++i) d[i] = INF;       d[1] = 0;       for (i = 0; i < (k); ++i) {         cin >> x >> in;         a.push_back({x, in});         pq.push({-x, in});       }       for (auto x : e[1]) pq.push({x.first, x.second});       while (!pq.empty()) {         auto z = pq.top();         pq.pop();         long long int u = abs(z.first);         if (d[u] <= z.second) {           ans += (z.first < 0);           if (z.first < 0)             ;           continue;         }         d[u] = z.second;         for (auto x : e[u]) pq.push({x.first, x.second + z.second});       }       cout << ans <<   n ;     }     aryanc403();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> adj[100005];   int n;   long long sz[100005];   long double E[100005];   void dfs1(int u, int p) {     sz[u] = 1;     for (auto v : adj[u]) {       if (v == p) continue;       dfs1(v, u);       sz[u] += sz[v];     }   }   void dfs2(int u, int p) {     if (u) {       E[u] = E[p] + 1 + 1.0 * (sz[p] - sz[u] - 1) / 2.0;     } else       E[u] = 1;     for (auto v : adj[u]) {       dfs2(v, u);     }   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL);     ;     cin >> n;     for (int i = 1; i < n; i++) {       int p;       cin >> p;       p--;       adj[p].push_back(i);     }     dfs1(0, -1);     dfs2(0, -1);     cout << fixed << setprecision(7);     for (int i = 0; i < n; i++) {       cout << E[i];       if (i < n) cout <<    ;     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long E[50];   int get_len(long long s) {     int cnt(0);     while (s) cnt++, s /= 10;     return cnt;   }   int find(long long n, int l) {     if (n == 0) return 0;     int len = get_len(n);     if (n < E[l]) {       return min(find(n, l - 1), find(E[l] - n, l - 1) + l);     }     int k;     for (k = 1;; k++)       if (E[l] * k > n) break;     k--;     return find(n - E[l] * k, l) + k * l;   }   int main() {     E[1] = 1;     for (int i = 2; i < 20; i++) E[i] = E[i - 1] * 10 + 1;     long long n;     while (cin >> n) {       int len = get_len(n);       cout << min(find(n, len), find(E[len + 1] - n, len) + len + 1) << endl;     }   }