malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   char s[110], ss[110];   int a[210], ans[210], n, k, namlen;   bool boo;   bool check() {     int i, j;     for (i = 1; i <= n; i++)       if (ss[i] ==  0 ) {         bool bk = true;         for (j = 1; j <= namlen; j++)           if (a[i + j - 1] != s[j]) {             bk = false;             break;           }         if (bk) return false;       }     return true;   }   void search(int x) {     if (x == n + 1) {       if (check()) {         for (int i = 1; i <= n; i++) ans[i] = a[i];         boo = true;       }       return;     }     if (ss[x] ==  1 ) {       search(x + 1);       return;     }     if (a[x] != 0) {       search(x + 1);       return;     }     for (int i = 0; i < k; i++) {       a[x] = i +  a ;       search(x + 1);       if (boo) return;     }   }   int main() {     int len, i, j;     scanf( %d%d , &n, &k);     getchar();     gets(s + 1);     gets(ss + 1);     namlen = strlen(s + 1);     len = strlen(ss + 1);     for (i = 1; i <= namlen; i++)       if (s[i] -  a  + 1 > k) {         printf( No solution n );         return 0;       }     bool bk = true;     for (i = 1; i <= n; i++) a[0] = 0;     for (i = 1; i <= len; i++) {       if (ss[i] ==  1 ) {         if (i + namlen - 1 > n) {           bk = false;           break;         }         for (j = 1; j <= namlen; j++) {           if (a[i + j - 1] != 0 && a[i + j - 1] != s[j]) {             bk = false;             break;           }           a[i + j - 1] = s[j];         }         if (!bk) break;       }     }     if (!bk) {       printf( No solution n );       return 0;     }     boo = false;     search(1);     if (!boo)       printf( No solution n );     else {       for (i = 1; i <= n; i++) printf( %c , char(ans[i]));       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     string a;     cin >> a;     string res =   ;     int kolvo = k / 2;     int i = 0;     int s = 0;     int bla = 0;     while (i < kolvo) {       res += a[s];       if (a[s] ==  ( ) {         ++i;       } else {         ++bla;       }       ++s;     }     while (bla < kolvo) {       res +=  ) ;       ++bla;     }     cout << res;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5, M = 1e18 + 5, OO = 0x3f3f3f3f;   const double pi = 3.141592653589793238462643383279502884;   double n, r;   double calc() {     double ang = 180 / n;     ang = (ang / 180) * pi;     double a = (sin(ang / 2) * r) / sin(pi - (1.5 * ang));     double ans = n * a * r * sin(ang);     return ans;   }   int main() {     cin >> n >> r;     printf( %.12lf  n , calc());     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3228;   const int MOD = 1000 * 1000 * 1000 + 7;   long long pw(long long a, int first) {     if (!first) return 1;     if (first % 2) return a * pw(a * a, first / 2);     return pw(a * a, first / 2);   }   void solve() {     int pl[3] = {0};     int n, p1, p2;     cin >> n >> p1 >> p2;     pl[1] -= p1;     pl[2] -= p2;     for (int i = 0; i < n; ++i) {       int nxt;       cin >> nxt;       if (nxt == 2 && pl[2] >= 0)         ++pl[1];       else         ++pl[nxt];     }     cout << max(0, pl[1]) + max(0, pl[2]);   }   int main() {     double tt = clock();     cout.tie(nullptr);     cin.tie(nullptr);     ios_base::sync_with_stdio(false);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const int N = 300005;   const double PI = 4 * atan(1);   const long long MAX = 9223372036854775807;   int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};   int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};   long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }   long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }   bool issquare(long long w) { return trunc(sqrt(w)) * trunc(sqrt(w)) == w; }   bool isprime(long long u) {     for (long long i = 2; i <= (int)sqrt(u); i++) {       if (u % i == 0) return 0;     }     return 1;   }   long long mod(long long to_mod, long long MOD) {     to_mod %= MOD;     while (to_mod < 0) to_mod += MOD;     return to_mod % MOD;   }   long long moduloMultiplication(long long a, long long b, long long mod) {     long long res = 0;     a %= mod;     while (b) {       if (b & 1) res = (res + a) % mod;       a = (2 * a) % mod;       b >>= 1;     }     return res;   }   long long power(long long x, long long y) {     long long res = 1;     x = x;     while (y > 0) {       if (y & 1) res = (res * x);       y = y >> 1;       x = (x * x);     }     return res;   }   long long n, m, x, y, res, t, k, ans = 0, q, z, w, l, a, b;   vector<pair<long long, pair<long long, long long> > > v;   long long dp[N];   vector<long long> vv;   long long curr[N];   int main() {     ios::sync_with_stdio(0);     memset(dp, 0, sizeof(dp));     memset(curr, 0, sizeof(curr));     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> x >> y >> z;       x--;       y--;       v.push_back(make_pair(z, make_pair(x, y)));     }     sort(v.begin(), v.end());     ans = 1;     for (int i = 0; i < m;) {       long long j = i;       vv.clear();       while (j < m && v[j].first == v[i].first) {         curr[v[j].second.second] =             max(curr[v[j].second.second], 1 + dp[v[j].second.first]);         vv.push_back(v[j].second.second);         j++;       }       i = j;       for (auto t : vv) {         dp[t] = max(dp[t], curr[t]);       }     }     for (int i = 0; i < n; i++) {       ans = max(ans, dp[i]);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int max(int a, int b) {     if (a > b)       return a;     else       return b;   }   long long min(long long a, long long b) {     if (a < b)       return a;     else       return b;   }   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   int lcm(int a, int b) { return a * (b / gcd(a, b)); }   vector<bool> isPrime(1000005 + 1, true);   string tos(int n) {     stringstream ss;     ss << n;     return ss.str();   }   void seive() {     isPrime[0] = 0, isPrime[1] = 0;     for (long long i = 2; i * i <= 1000005; i++)       if (isPrime[i])         for (long long j = i * i; j <= 1000005; j += i) isPrime[j] = 0;   }   int dx[] = {1, -1, 0, 0};   int dy[] = {0, 0, 1, -1};   const int val = 1 << 20;   long long n, m, st, e, ans = 1, a[1111111], b[1111111];   int main() {     string s1, s2;     cin >> s1 >> s2;     long long i = 0, j = 0, beg = -1;     while (j < s2.size()) {       if (s1[i] == s2[j]) {         if (beg == -1) {           beg = j;           a[0] = j;         }         i++;       }       if (i == s1.size()) {         b[0] = j;         break;       }       j++;     }     i = s1.size();     j = s2.size();     beg = -1;     while (j >= 0) {       if (s1[i] == s2[j]) {         if (beg == -1) {           beg = j;           b[1] = j;         }         i--;       }       if (i < 0) {         a[1] = j;         break;       }       j--;     }     ans = max(0, a[1] - b[0]);     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   T abs(T x) {     return x < 0 ? -x : x;   }   const double pi = asin(1.0) * 2;   int n;   int m;   long long pl, pr, vl, vr;   int k;   vector<long long> h;   double prob(long long l, long long r, long long L, long long R) {     L = max(l, L);     R = min(R, r);     if (L > R) return 0.0;     return (R - L + 1) / (double)(r - l + 1);   }   int main() {     cin >> pl >> pr >> vl >> vr >> k;     h.push_back(0);     int cur = 0;     for (int i = cur; h[i] * 10 + 4 <= 1e10; i++) {       h.push_back(h[i] * 10 + 4);       h.push_back(h[i] * 10 + 7);     }     sort((h).begin(), (h).end());     double ans = 0;     for (int i = 1; i < (int)((h).size()); i++) {       if (i + k >= (int)((h).size())) break;       long long l1 = h[i - 1] + 1, r1 = h[i];       long long l2 = h[i + k - 1], r2 = h[i + k] - 1;       ans += prob(pl, pr, l1, r1) * prob(vl, vr, l2, r2);       ans += prob(pl, pr, l2, r2) * prob(vl, vr, l1, r1);       ans -= prob(pl, pr, max(l2, l1), min(r1, r2)) *              prob(vl, vr, max(l2, l1), min(r1, r2));     }     printf( %.10lf n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double p, f[220][220];   int x, k;   int main() {     scanf( %d%d%lf , &x, &k, &p);     p /= 100;     for (int i = 0; i <= k; i++)       for (int j = x + i; j % 2 == 0; j /= 2) f[0][i]++;     for (int i = 0; i < k; i++) {       for (int j = 0; j <= k; j++) {         if (j) f[i + 1][j - 1] += (1 - p) * f[i][j];         if (j * 2 <= k) f[i + 1][j * 2] += p * (f[i][j] + 1);       }     }     printf( %.10lf n , f[k][0]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void inout() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;   }   int main() {     inout();     long long n, i, u, v;     cin >> n;     map<long long, long long> prv, nxt, cnt1, cnt2;     vector<long long> ans(n + 5);     for (i = 0; i < n; i++) {       cin >> u >> v;       prv[v] = u;       nxt[u] = v;       cnt1[u]++;       cnt2[v]++;     }     for (auto k : cnt1)       if (cnt1[k.first] > cnt2[k.first]) ans[1] = k.first;     for (auto k : cnt2)       if (cnt1[k.first] < cnt2[k.first]) ans[n] = k.first;     i = 3;     while (i <= n) {       ans[i] = nxt[ans[i - 2]];       i += 2;     }     i = n + n % 2 - 2;     while (i > 0) {       ans[i] = prv[ans[i + 2]];       i -= 2;     }     for (i = 1; i <= n; i++) cout << ans[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 105000;   const int M = 105;   int n, m, p;   long long hill[N];   long long a[N], s[N];   long long dp[N];   int top, tail;   long long duix[N], duiy[N];   int getint() {     char e = getchar();     int res = 0;     while (e <  0  || e >  9 ) e = getchar();     while ( 0  <= e && e <=  9 ) {       res = (res << 3) + (res << 1) + e -  0 ;       e = getchar();     }     return res;   }   int main() {     int i, j;     n = getint();     m = getint();     p = getint();     for (i = 2; i <= n; i++) hill[i] = getint();     for (i = 2; i <= n; i++) hill[i] += hill[i - 1];     for (i = 1; i <= m; i++) {       a[i] = -hill[getint()];       a[i] += getint();     }     sort(a + 1, a + m + 1);     for (i = 1; i <= m; i++) s[i] = a[i] + s[i - 1];     for (i = 1; i <= m; i++) dp[i] = i * a[i];     for (j = 2; j <= p; j++) {       top = 1;       tail = 0;       for (i = 1; i <= m; i++) {         long long nowx = i;         long long nowy = dp[i];         while (tail >= 2) {           double xla = duiy[tail] - duiy[tail - 1];           xla /= duix[tail] - duix[tail - 1];           double xlb = nowy - duiy[tail - 1];           xlb /= nowx - duix[tail - 1];           if (xla - xlb > -1e-10)             tail--;           else             break;         }         tail++;         duix[tail] = nowx;         duiy[tail] = nowy;       }       for (i = 1; i <= m; i++) {         dp[i] = duiy[top] - a[i] * duix[top];         while (top < tail && duix[top + 1] <= i) {           long long now = duiy[top + 1] - a[i] * duix[top + 1];           if (now <= dp[i]) {             top++;             dp[i] = now;           } else             break;         }         dp[i] += i * a[i];       }     }     printf( %I64d n , dp[m] - s[m]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void sprint(int x) { cout << x; }   void sprint(long x) { cout << x; }   void sprint(long long x) { cout << x; }   void sprint(unsigned x) { cout << x; }   void sprint(unsigned long x) { cout << x; }   void sprint(unsigned long long x) { cout << x; }   void sprint(float x) { cout << x; }   void sprint(double x) { cout << x; }   void sprint(long double x) { cout << x; }   void sprint(char x) { cout <<      << x <<     ; }   void sprint(const char *x) { cout <<     << x <<    ; }   void sprint(const string &x) { cout <<     << x <<    ; }   void sprint(bool x) { cout << (x ?  true  :  false ); }   template <typename T, typename V>   void sprint(const pair<T, V> &x);   template <typename T>   void sprint(const vector<T> &x);   template <typename T, typename V>   void sprint(const pair<T, V> &x) {     cout <<  { ;     sprint(x.first);     cout <<  , ;     sprint(x.second);     cout <<  } ;   }   template <typename T>   void sprint(const vector<T> &x) {     cout <<  { ;     for (int u69 = 0; u69 < x.size(); u69++) {       sprint(x[u69]);       cout << (u69 + 1 == x.size() ?    :  , );     };     cout <<  } ;   }   template <typename T>   void sprint(const T &x) {     int f = 0;     cout <<  { ;     for (auto &i : x) cout << (f++ ?  ,  :   ), sprint(i);     cout <<  } ;   }   void sprint() { cout <<  ] n ; }   template <typename T, typename... V>   void sprint(T t, V... v) {     sprint(t);     if (sizeof...(v)) cout <<  ,  ;     sprint(v...);   }   inline long long ceil2(long long a, long long b) { return (a + b - 1) / b; }   inline bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); }   bool isPerfectSquare(long long x) {     long long sr = sqrt(x);     return ((sr * sr) == x);   }   bool isIn(string &s2, string &s1) {     if (s1.find(s2) != string::npos) return true;     return false;   }   bool isSorted(vector<int> &a) {     for (int i = 0; i < (int)a.size() - 1; i++)       if (a[i] > a[i + 1]) return false;     return true;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     char a[n][m];     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) {         cin >> a[i][j];       }     }     int count = 0;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) {         if (a[i][j] ==  W  && ((a[i + 1][j] ==  P  && i + 1 < n) ||                                (a[i - 1][j] ==  P  && i - 1 >= 0) ||                                (a[i][j + 1] ==  P  && j + 1 < m) ||                                (a[i][j - 1] ==  P  && j - 1 >= 0))) {           count++;         }       }     }     cout << count <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int l[100005], r[100005];   int main(int argc, char* argv[]) {     std::ios::sync_with_stdio(false);     int n, k;     scanf( %d%d , &n, &k);     int sum = 0;     for (int i = 0; i < n; i++) {       scanf( %d%d , &l[i], &r[i]);       sum += r[i] - l[i] + 1;     }     int res = 0;     while (sum % k != 0) {       sum++;       res++;     }     printf( %d n , res);     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);   }   template <class T>   inline T lcm(T a, T b) {     a = abs(a);     b = abs(b);     return (a / _gcd(a, b)) * b;   }   template <class T, class X>   inline bool getbit(T a, X i) {     T t = 1;     return ((a & (t << i)) > 0);   }   template <class T, class X>   inline T setbit(T a, X i) {     T t = 1;     return (a | (t << i));   }   template <class T, class X>   inline T resetbit(T a, X i) {     T t = 1;     return (a & (~(t << i)));   }   inline long long power(long long a, long long b) {     long long multiply = 1;     for (int i = (0); i < (b); i++) {       multiply *= a;     }     return multiply;   }   char given[505][505];   long long dp[505][505];   int n, m, modby;   long long calc(int x, int y) {     long long &ret = dp[x][y];     if (x == 2 && y == 0) return 1;     if (x == 0 && y == 1) return 0;     if (ret != -1) return ret;     ret = 0;     long long a;     if (x > 1) {       a = calc(x - 2, y) % modby;       a = (a * ((x * (x - 1)) / 2) % modby) % modby;       ret += a % modby;     }     if (y > 1) {       a = calc(x + 2, y - 2) % modby;       a = (a * ((y * (y - 1)) / 2) % modby) % modby;       ret += a % modby;     }     if (x != 0 && y != 0) {       a = calc(x, y - 1) % modby;       a = (a * (x * y) % modby) % modby;       ret += a % modby;     }     return ret;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m >> modby;     memset(dp, -1, sizeof(dp));     for (int i = (0); i < (m); i++) {       for (int j = (0); j < (n); j++) cin >> given[i][j];     }     int zeros = 0, ones = 0;     for (int i = (0); i < (n); i++) {       int x = 0;       for (int j = (0); j < (m); j++) {         if (given[j][i] ==  1 ) x++;       }       if (x == 0)         zeros++;       else if (x == 1)         ones++;     }     if (n == m)       cout << 1 << endl;     else       cout << calc(ones, zeros) % modby << endl;     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:64000000 )   using namespace std;   const int MAXN = -1;   const int MOD = 1;   const long double EPS = 1e-11;   long double sqr(long double x) { return x * x; }   bool ok(long double x) { return x >= -EPS && x <= 1 + EPS; }   void norm(long double &x) {     if (x < 0) x = 0;     if (x > 1) x = 1;   }   int main() {     int n;     while (scanf( %d , &n) == 1) {       vector<long double> mn(n), mx(n);       for (int i = 0; i < n; i++) {         double o;         scanf( %lf , &o);         mx[i] = o;       }       for (int i = 0; i < n; i++) {         double o;         scanf( %lf , &o);         mn[i] = o;       }       vector<double> a(n), b(n);       long double sa = 0.0, sb = 0.0;       for (int i = 0; i < n; i++) {         long double g = mn[i] + mx[i];         long double A = -1;         long double B = (sb + g - sa);         long double C = g * sa - mx[i];         long double D = B * B - 4 * A * C;         assert(D >= -EPS);         D = fabs(D);         long double na = (-B - sqrt(D)) / (2 * A);         long double nb = g - na;         assert(ok(na));         assert(ok(nb));         a[i] = na;         b[i] = nb;         sa += na;         sb += nb;       }       for (int i = 0; i < n; i++) printf( %.15f%c , a[i],    n [i + 1 == n]);       for (int i = 0; i < n; i++) printf( %.15f%c , b[i],    n [i + 1 == n]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 65;   char g[maxn][maxn];   int r, c, row[maxn], col[maxn];   void solve() {     memset(row, 0, sizeof row);     memset(col, 0, sizeof col);     int sum = 0;     for (int i = 1; i <= r; i++) {       for (int j = 1; j <= c; j++) {         if (g[i][j] ==  A ) col[i]++, row[j]++, sum++;       }     }     if (sum == 0)       printf( MORTAL n );     else if (sum == r * c)       printf( 0 n );     else if (row[1] == r || row[c] == r || col[1] == c || col[r] == c)       printf( 1 n );     else {       int flag = 0;       for (int i = 2; i < r; i++)         if (col[i] == c) flag = 1;       for (int i = 2; i < c; i++)         if (row[i] == r) flag = 1;       if (flag == 1 || g[1][1] ==  A  || g[r][1] ==  A  || g[1][c] ==  A  ||           g[r][c] ==  A )         printf( 2 n );       else if (row[1] > 0 || row[c] > 0 || col[1] > 0 || col[r] > 0)         printf( 3 n );       else         printf( 4 n );     }   }   int main() {     int T;     scanf( %d , &T);     while (T--) {       scanf( %d%d , &r, &c);       for (int i = 1; i <= r; i++) scanf( %s , g[i] + 1);       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[300005], pre[300005], L[300005], R[300005];   int st[300005][19], f[300005], ans;   int Max(int l, int r) {     int s = f[r - l];     return a[st[l][s]] > a[st[r - (1 << s)][s]] ? st[l][s] : st[r - (1 << s)][s];   }   void solve(int l, int r) {     if (l > r) return;     if (l == r) {       ans += a[l] == 1;       return;     }     int k = Max(l, r);     if (k - l < r - k) {       for (int i = l; i <= k; i++)         if (i + a[k] - 1 >= k && i + a[k] - 1 <= r && R[i] >= i + a[k] - 1) ans++;     } else       for (int i = k; i <= r; i++)         if (i - a[k] + 1 <= k && i - a[k] + 1 >= l && L[i] <= i - a[k] + 1) ans++;     solve(l, k - 1), solve(k + 1, r);   }   int main() {     scanf( %d , &n);     for (int i = 2; i <= n; i++) f[i] = f[i >> 1] + 1;     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i < n; i++) st[i][0] = a[i] > a[i + 1] ? i : (i + 1);     for (int i = 1; i <= n; i++)       L[i] = max(L[i - 1], pre[a[i]] + 1), pre[a[i]] = i;     for (int i = 1; i <= n; i++) pre[i] = n + 1;     R[n] = n, pre[a[n]] = n;     for (int i = n - 1; i >= 1; i--)       R[i] = min(R[i + 1], pre[a[i]] - 1), pre[a[i]] = i;     for (int i = 1; 1 << i < n; i++)       for (int j = 1; j <= n - (1 << i); j++)         st[j][i] = a[st[j][i - 1]] > a[st[j + (1 << i - 1)][i - 1]]                        ? st[j][i - 1]                        : st[j + (1 << i - 1)][i - 1];     solve(1, n);     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int c[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};   char a[60][60], color[2510];   int to[60][60], minx[2510], maxx[2510], miny[2510], maxy[2510], f[2510][4][2];   void dfs(int x0, int y0, int value) {     to[x0][y0] = value;     minx[value] = min(minx[value], x0);     miny[value] = min(miny[value], y0);     maxx[value] = max(maxx[value], x0);     maxy[value] = max(maxy[value], y0);     for (int i = 0; i < 4; i++) {       int x = x0 + c[i][0], y = y0 + c[i][1];       if (to[x][y] == 0 && a[x][y] == a[x0][y0]) dfs(x, y, value);     }   }   int calc(int x, int y, int z) {     int x1 = minx[x], x2 = maxx[x], y1 = miny[x], y2 = maxy[x];     if (y == 0)       return (z == 0 ? to[x1][y2 + 1] : to[x2][y2 + 1]);     else if (y == 1)       return (z == 0 ? to[x2 + 1][y2] : to[x2 + 1][y1]);     else if (y == 2)       return (z == 0 ? to[x2][y1 - 1] : to[x1][y1 - 1]);     else       return (z == 0 ? to[x1 - 1][y1] : to[x1 - 1][y2]);   }   int main() {     int n, K;     scanf( %d%d , &n, &K);     for (int i = 1; i <= n; i++) scanf( %s , a[i] + 1);     int m = strlen(a[1] + 1), tot = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] !=  0  && to[i][j] == 0) {           tot++;           color[tot] = a[i][j];           minx[tot] = miny[tot] = 1 << 30;           maxx[tot] = maxy[tot] = 0;           dfs(i, j, tot);         }     for (int i = 1; i <= tot; i++)       for (int j = 0; j < 4; j++)         for (int k = 0; k < 2; k++) f[i][j][k] = calc(i, j, k);     int bp = 1, dp = 0, cp = 0;     for (int i = 1; i <= K; i++) {       int p = f[bp][dp][cp];       if (p == 0)         if (cp == 0)           cp = 1;         else {           cp = 0;           dp = dp + 1 & 3;         }       else         bp = p;     }     printf( %c n , color[bp]);     return (0);   }
#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const unsigned long long MAX = LONG_MAX;   void fasterIO() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   int sumDig(int a) {     int sum = 0;     while (a != 0) {       sum += a % 10;       a /= 10;     }     return sum;   }   int main() {     int test;     cin >> test;     while (test--) {       long long p, k;       long long arr[150];       cin >> p >> k;       for (unsigned long long i = 0; i < (p); i++) cin >> arr[i];       sort(arr, arr + p);       if (abs((arr[0] + k) - arr[p - 1]) > k)         cout <<  -1  << endl;       else         cout << arr[0] + k << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[18] = {0};   long long int get(long long int n, int l) {     if (!n) return 0;     if (l == 1) return n;     long long int t = a[l];     if (n == t) return l;     if (n < t)       return min(get(n, l - 1), l + get(t - n, l - 1));     else       return min((n / t) * l + get(n % t, l - 1),                  ((1 + (n / t)) * l) + get(t - n % t, l - 1));   }   int main() {     long long int n, i, ans;     scanf( %I64d , &n);     for (int i = 1; i < 18; i++) a[i] = a[i - 1] * 10 + 1;     for (i = 1; n > a[i]; i++)       ;     ans = get(n, i);     printf( %I64d n , ans);     return 0;   }