Problem:
Paper Fortune
If you visit Aizu Akabeko shrine, you will find a unique paper fortune on which a number with more than one digit is written.
Each digit ranges from 1 to 9 (zero is avoided because it is considered a bad omen in this shrine). Using this string of numeric values, you can predict how many years it will take before your dream comes true. Cut up the string into more than one segment and compare their values. The difference between the largest and smallest value will give you the number of years before your wish will be fulfilled. Therefore, the result varies depending on the way you cut up the string. For example, if you are given a string 11121314 and divide it into segments, say, as 1,11,21,3,14, then the difference between the largest and smallest is 21 - 1 = 20. Another division 11,12,13,14 produces 3 (i.e. 14 - 11) years. Any random division produces a game of luck. However, you can search the minimum number of years using a program.
Given a string of numerical characters, write a program to search the minimum years before your wish will be fulfilled.
Input
The input is given in the following format.
n
An integer
n
is given. Its number of digits is from 2 to 100,000, and each digit ranges from 1 to 9.
Output
Output the minimum number of years before your wish will be fulfilled.
Sample Input 1
11121314
Sample Output 1
3
Sample Input 2
123125129
Sample Output 2
6
Sample Input 3
119138
Sample Output 3
5

Submitted solutions:

code_snippet: #include<bits/stdc++.h>

using namespace std;

#define rep(i, l, n) for(int i = int(l); i < int(n); i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template<class T> bool chmin(T &a, T b) {if(a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T &a, T b) {if(a < b) {a = b; return true;} return false;}
template<class T> using spq = priority_queue<T, vector<T>, greater<T>>;

// bool -> Yes/No
string answer(bool b) {return b ? "Yes" : "No";}

void fix(int k) {cout << fixed << setprecision(k);}

const int inf = 2e9;
const long long INF = 2e18;
const long double eps = 1e-12;
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

void main_program() {
    string s; cin >> s;
    int n = s.size();
    // １桁のみに分ける場合
    int mn = inf, mx = 0;
    rep(i, 0, n) {
        int c = s[i] - '0';
        chmin(mn, c);
        chmax(mx, c);
    }
    int ans = mx - mn;
    if(n == 2) {
        cout << ans << "\n";
        return;
    }
    // １桁と２桁のみに分ける場合
    mn = inf, mx = 0;
    rep(i, 0, n) {
        if(s[i] != '1') {
            int c = s[i] - '0';
            chmin(mn, c); chmax(mx, c);
        }
        else {
            if(i < n - 1) {
                int c = s[i + 1] - '0';
                chmin(mn, c + 10); chmax(mx, c + 10);
                i++;
            }
            else {
                chmin(mn, 1); chmax(mx, 1);
            }
        }
    }
    chmin(ans, mx - mn);
    // k >= 2 として、k桁のみに分ける場合
    for(int k = 2; k < n; k++) {
        if(n % k) continue;
        mn = inf, mx = 0;
        rep(i, 0, n) {
            if(i % k == k - 2) {
                assert(i + 1 < n);
                int c = (s[i] - '0') * 10 + (s[i + 1] - '0');
                chmin(mn, c); chmax(mx, c);
                i++;
            }
            else {
                if(s[i] != s[(i + k) % n]) {
                    mn = 0, mx = inf;
                }
            }
        }
        chmin(ans, mx - mn);
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    //cin >> T;
    while(T--) {
        main_program();
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3580
final_rank: 4

code_snippet: #include<bits/stdc++.h>

using namespace std;

#define rep(i, l, n) for(int i = int(l); i < int(n); i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template<class T> bool chmin(T &a, T b) {if(a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T &a, T b) {if(a < b) {a = b; return true;} return false;}
template<class T> using spq = priority_queue<T, vector<T>, greater<T>>;

// bool -> Yes/No
string answer(bool b) {return b ? "Yes" : "No";}

void fix(int k) {cout << fixed << setprecision(k);}

const int inf = 2e9;
const long long INF = 2e18;
const long double eps = 1e-12;
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

void main_program() {
    string s; cin >> s;
    int n = s.size();
    // １桁のみに分ける場合
    int mn = inf, mx = 0;
    rep(i, 0, n) {
        int c = s[i] - '0';
        chmin(mn, c);
        chmax(mx, c);
    }
    int ans = mx - mn;
    // １桁と２桁のみに分ける場合
    mn = inf, mx = 0;
    rep(i, 0, n) {
        if(s[i] != '1') {
            int c = s[i] - '0';
            chmin(mn, c); chmax(mx, c);
        }
        else {
            if(i < n - 1) {
                int c = s[i + 1] - '0';
                chmin(mn, c + 10); chmax(mx, c + 10);
                i++;
            }
            else {
                chmin(mn, 1); chmax(mx, 1);
            }
        }
    }
    chmin(ans, mx - mn);
    // k >= 2 として、k桁のみに分ける場合
    for(int k = 2; k < n; k++) {
        if(n % k) continue;
        mn = inf, mx = 0;
        rep(i, 0, n) {
            if(i % k == k - 2) {
                assert(i + 1 < n);
                int c = (s[i] - '0') * 10 + (s[i + 1] - '0');
                chmin(mn, c); chmax(mx, c);
                i++;
            }
            else {
                if(s[i] != s[(i + k) % n]) {
                    mn = 0, mx = inf;
                }
            }
        }
        chmin(ans, mx - mn);
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    //cin >> T;
    while(T--) {
        main_program();
    }
}
accuracy: 0.3
time_ms: 20
memory_kb: 3472
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int main()
{
    string S;
    cin >> S;
    ll Min = 10, Max = -1;
    for (char ch : S)
    {
        Min = min(Min, (ll)(ch - '0'));
        Max = max(Max, (ll)(ch - '0'));
    }
    ll ans = Max - Min;
    for (ll i = 2; i * i <= S.size(); i++)
    {
        if ((ll)S.size() % i == 0)
        {
            set<ll> A;
            ll num = 0;
            for (ll j = 0; j < S.size(); j++)
            {
                num *= 10;
                num += S[j] - '0';
                num %= 1000000007;
                if (j % i == i - 1)
                {
                    A.insert(num);
                    num = 0;
                }
            }
            ans = min(ans, *prev(A.end(), 1) - *A.begin());
            if (i * i != S.size())
            {
                ll m = (ll)S.size() / i;
                A.clear();
                for (ll j = 0; j < S.size(); j++)
                {
                    num *= 10;
                    num += S[j] - '0';
                    num %= 1000000007;
                    if (j % m == m - 1)
                    {
                        A.insert(num);
                        num = 0;
                    }
                }
                ans = min(ans, *prev(A.end(), 1) - *A.begin());
            }
        }
    }
    set<ll>B;
    ll num = 0;
    for (ll i = 0; i < S.size(); i++) {
        num *= 10;
        num += S[i] - '0';
        num %= 100;
        if (num != 1) {
            B.insert(num); num = 0;
        }
    }
    if (num != 0) B.insert(num);
    if (B.size() > 1) ans = min(ans, *prev(B.end(), 1) - *B.begin());

    cout << ans << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 4228
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int main()
{
    string S;
    cin >> S;
    ll Min = 10, Max = -1;
    for (char ch : S)
    {
        Min = min(Min, (ll)(ch - '0'));
        Max = max(Max, (ll)(ch - '0'));
    }
    ll ans = Max - Min;
    for (ll i = 2; i * i <= S.size(); i++)
    {
        if ((ll)S.size() % i == 0)
        {
            set<ll> A;
            ll num = 0;
            for (ll j = 0; j < S.size(); j++)
            {
                num *= 10;
                num += S[j] - '0';
                num %= 1000000007;
                if (j % i == i - 1)
                {
                    A.insert(num);
                    num = 0;
                }
            }
            ans = min(ans, *prev(A.end(), 1) - *A.begin());
            if (i * i != S.size())
            {
                ll m = (ll)S.size() / i;
                A.clear();
                for (ll j = 0; j < S.size(); j++)
                {
                    num *= 10;
                    num += S[j] - '0';
                    num %= 1000000007;
                    if (j % m == m - 1)
                    {
                        A.insert(num);
                        num = 0;
                    }
                }
                ans = min(ans, *prev(A.end(), 1) - *A.begin());
            }
        }
    }
    set<ll>B;
    ll num = 0;
    for (ll i = 0; i < S.size(); i++) {
        num *= 10;
        num += S[i] - '0';
        num %= 100;
        if (num != 1) {
            B.insert(num); num = 0;
        }
    }
    
    if (B.size() > 1) ans = min(ans, *prev(B.end(), 1) - *B.begin());

    cout << ans << endl;
}
accuracy: 0.475
time_ms: 60
memory_kb: 3428
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int main()
{
    string S;
    cin >> S;
    ll Min = 10, Max = -1;
    for (char ch : S)
    {
        Min = min(Min, (ll)(ch - '0'));
        Max = max(Max, (ll)(ch - '0'));
    }
    ll ans = Max - Min;
    for (ll i = 2; i * i <= S.size(); i++)
    {
        if ((ll)S.size() % i == 0)
        {
            set<ll> A;
            ll num = 0;
            for (ll j = 0; j < S.size(); j++)
            {
                num *= 10;
                num += S[j] - '0';
                num %= 1000000007;
                if (j % i == i - 1)
                {
                    A.insert(num);
                    num = 0;
                }
            }
            ans = min(ans, *prev(A.end(), 1) - *A.begin());
            if (i * i != S.size())
            {
                ll m = (ll)S.size() / i;
                A.clear();
                for (ll j = 0; j < S.size(); j++)
                {
                    num *= 10;
                    num += S[j] - '0';
                    num %= 1000000007;
                    if (j % m == m - 1)
                    {
                        A.insert(num);
                        num = 0;
                    }
                }
                ans = min(ans, *prev(A.end(), 1) - *A.begin());
            }
        }
    }
    set<ll>B;
    ll num = 0;
    for (ll i = 0; i < S.size(); i++) {
        num *= 10;
        num += S[i] - '0';
        num %= 100;
        if (num != 1) {
            B.insert(num); num = 0;
        }
    }
    ans = min(ans, *prev(B.end(), 1) - *B.begin());

    cout << ans << endl;
}
accuracy: 0.3
time_ms: 60
memory_kb: 3352
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int main()
{
    string S;
    cin >> S;
    ll Min = 10, Max = -1;
    for (char ch : S)
    {
        Min = min(Min, (ll)(ch - '0'));
        Max = max(Max, (ll)(ch - '0'));
    }
    ll ans = Max - Min;
    for (ll i = 2; i * i <= S.size(); i++)
    {
        if ((ll)S.size() % i == 0)
        {
            set<ll> A;
            ll num = 0;
            for (ll j = 0; j < S.size(); j++)
            {
                num *= 10;
                num += S[j] - '0';
                if (j % i == i - 1)
                {
                    A.insert(num);
                    num = 0;
                }
            }
            ans = min(ans, *prev(A.end(), 1) - *A.begin());
            if (i * i != S.size())
            {
                ll m = (ll)S.size() / i;
                A.clear();
                for (ll j = 0; j < S.size(); j++)
                {
                    num *= 10;
                    num += S[j] - '0';
                    if (j % m == m - 1)
                    {
                        A.insert(num);
                        num = 0;
                    }
                }
                ans = min(ans, *prev(A.end(), 1) - *A.begin());
            }
        }
    }
    set<ll>B;
    ll num = 0;
    for (ll i = 0; i < S.size(); i++) {
        num *= 10;
        num += S[i] - '0';
        if (num != 1) {
            B.insert(num); num = 0;
        }
    }
    ans = min(ans, *prev(B.end(), 1) - *B.begin());

    cout << ans << endl;
}
accuracy: 0.275
time_ms: 50
memory_kb: 3472
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
using ll=long long;

template<typename T>
bool chmin(T &a,T b){
    return a>b ? a=b,true:false;
}

int main(){
    string s;
    cin >> s;
    int ans=10;

    //全て同じ長さで切る場合(3以上)
    auto all_same=[&](int len){
        assert(s.size()%len==0);
        assert(len>=3);
        string mx=s.substr(0,len);
        string mn=s.substr(0,len);
        for(int i=len;i<int(s.size());i+=len){
            mx=max(mx,s.substr(i,len));
            mn=min(mn,s.substr(i,len));
        }
        //cout << mx << ' ' << mn << endl;
        //cout << stoi(mx.substr(mx.size()-2)) << ' ' << stoi(mn.substr(mn.size()-2)) << endl;
        if(mx.substr(0,mx.size()-2)!=mn.substr(0,mn.size()-2))return 10;
        else return stoi(mx.substr(mx.size()-2))-stoi(mn.substr(mn.size()-2));
    };
    //全て2桁以下 dpがまわる？
    //dp[i][j]=i桁みて、最小値がjの時、そこまでの最大値min
    {
        vector<vector<int>> dp(s.size()+1,vector<int>(100,1000));
        dp[1][s[0]-'0']=s[0]-'0';
        //2桁だったらこれはダメ
        if(s.size()!=2)dp[2][stoi(s.substr(0,2))]=stoi(s.substr(0,2));
        for(int i=1;i<int(s.size());i++)rep(j,100){
            //1だけ伸ばす
            chmin(dp[i+1][min(j,s[i]-'0')],max(dp[i][j],s[i]-'0'));
            //2伸ばす
            if(i+2<=int(s.size()))chmin(dp[i+2][min(j,stoi(s.substr(i,2)))],max(dp[i][j],stoi(s.substr(i,2))));
        }
        rep(j,100)chmin(ans,dp.back()[j]-j);
    }
    //デバッグ出力
    //cout << ans << endl;


    for(int i=1;i*i<=int(s.size());i++)if(s.size()%i==0){
        if(i>2)chmin(ans,all_same(i));
        if(i>1)chmin(ans,all_same(s.size()/i));
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 310
memory_kb: 46116
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const int INF = ((1<<30)-1);
const long long LINF = (1LL<<60);
const double PI = 3.141592653589793238;
template<class T> inline void chmax(T& a, T b) {if (a < b) {a = b;}}
template<class T> inline void chmin(T& a, T b) {if (a > b) {a = b;}}



int solve(){
    string s; cin >> s;
    int n = s.size();
    vi a(s.size());
    rep(i,0,s.size())a[i] = s[i]-'0';
    rep(i,0,8){
        rep(mi,1,99){
            int ma = mi+i;
            int now = 0;
            int ind = 0;
            bool ok = 1;
            int cnt = 0;
            while(ind < n){
                now = 10*now + a[ind];
                if(mi <= now && now <= ma){
                    cnt++;
                    now = 0;
                }
                else if(ma < now){
                    ok = 0;
                    break;
                }
                ind++;
            }
            if(now != 0)ok = 0;
            if(cnt == 1)ok = 0;
            if(ok){
                cout << i << endl;
                return 0;
            }
        }
        rep(k,3,n){
            if(n % k != 0)continue;
            vector<string> vs;
            int ind = 0;
            while(ind < n){
                string tmps;
                rep(j,ind,min(ind+k,(ll)n))tmps.push_back(s[j]);
                vs.push_back(tmps);
                ind += k;
            }
            sort(all(vs));
            string l = vs[0];
            string h = vs.back();
            string nl;
            int tmp = i;
            for(int j = l.size()-1; j >= 0; --j){
                tmp += (l[j]-'0');
                nl.push_back('0'+(tmp%10));
                tmp /= 10;
            }
            while(tmp > 0){
                nl.push_back('0'+(tmp%10));
                tmp /= 10;
            }
            while(nl.size() < h.size())nl.push_back('0');
            reverse(all(nl));
            if(nl >= h){
                cout << i << endl;
                return 0;
            }
        }
    }
    cout << 8 << endl;



    return 0;
}

int main(){
    cout << fixed << setprecision(10);
    ios::sync_with_stdio(0), cin.tie(0);
    int t = 1; 
    //cin >> t;
    for(int i = 0; i < t; i++){
        solve();
    }
	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 5584
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const int INF = ((1<<30)-1);
const long long LINF = (1LL<<60);
const double PI = 3.141592653589793238;
template<class T> inline void chmax(T& a, T b) {if (a < b) {a = b;}}
template<class T> inline void chmin(T& a, T b) {if (a > b) {a = b;}}



int solve(){
    string s; cin >> s;
    int n = s.size();
    vi a(s.size());
    rep(i,0,s.size())a[i] = s[i]-'0';
    rep(i,0,8){
        rep(mi,1,99){
            int ma = mi+i;
            int now = 0;
            int ind = 0;
            bool ok = 1;
            int cnt = 0;
            while(ind < n){
                now = 10*now + a[ind];
                if(mi <= now && now <= ma){
                    cnt++;
                    now = 0;
                }
                else if(ma < now){
                    ok = 0;
                    break;
                }
                ind++;
            }
            if(now != 0)ok = 0;
            if(cnt == 1)ok = 0;
            if(ok){
                cout << i << endl;
                return 0;
            }
        }
        rep(k,3,n){
            vector<string> vs;
            int ind = 0;
            while(ind < n){
                string tmps;
                rep(j,ind,min(ind+k,(ll)n))tmps.push_back(s[j]);
                vs.push_back(tmps);
                ind += k;
            }
            sort(all(vs));
            string l = vs[0];
            string h = vs.back();
            string nl;
            int tmp = i;
            for(int j = l.size()-1; j >= 0; --j){
                tmp += (l[j]-'0');
                nl.push_back('0'+(tmp%10));
                tmp /= 10;
            }
            while(tmp > 0){
                nl.push_back('0'+(tmp%10));
                tmp /= 10;
            }
            reverse(all(nl));
            if(nl >= h){
                cout << i << endl;
                return 0;
            }
        }
    }
    cout << 8 << endl;



    return 0;
}

int main(){
    cout << fixed << setprecision(10);
    ios::sync_with_stdio(0), cin.tie(0);
    int t = 1; 
    //cin >> t;
    for(int i = 0; i < t; i++){
        solve();
    }
	return 0;
}
accuracy: 0.425
time_ms: 920
memory_kb: 5052
final_rank: 15

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0374.cc: Paper Fortune
 */

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef vector<int> vi;

/* global variables */

char s[MAX_N + 4];

/* subroutines */

int subv(const vi &a, const vi &b) {
  int ret = -1, co = 0;
  for (int i = a.size() - 1; i >= 0; i--) {
    int d = a[i] - b[i] - co;
    if (d < 0) d += 10, co = 1;
    else co = 0;
    if (i == a.size() - 1) ret = d;
    else if (d > 0) return -1;
  }
  if (co > 0) return -1;
  return ret;
}

/* main */

int main() {
  scanf("%s", s);
  int n = strlen(s);

  vi ss(n);
  for (int i = 0; i < n; i++) ss[i] = s[i] - '0';

  vi ds(1, 1);
  for (int p = 2; p * p <= n; p++)
    if (n % p == 0) {
      ds.push_back(p);
      int q = n / p;
      if (q != p) ds.push_back(q);
    }
  sort(ds.begin(), ds.end());

  int mind = 10;
  for (auto l: ds) {
    vi minv(ss.begin(), ss.begin() + l);
    vi maxv(minv);
    for (int i = l; i < n; i+= l) {
      vi v(ss.begin() + i, ss.begin() + i + l);
      if (minv > v) minv = v;
      if (maxv < v) maxv = v;
    }
    int d = subv(maxv, minv);
    if (d >= 0) mind = min(mind, d);
  }

  int mint = 10, maxt = 0;
  for (int i = 0; i < n;) {
    if (ss[i] == 1 && i + 1 < n) {
      int t = ss[i] * 10 + ss[i + 1];
      mint = min(mint, t);
      maxt = max(maxt, t);
      i += 2;
    }
    else {
      mint = min(mint, ss[i]);
      maxt = max(maxt, ss[i]);
      i++;
    }
  }
  mind = min(mind, maxt - mint);

  printf("%d\n", mind);
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3848
final_rank: 2

code_snippet: #include "bits/stdc++.h"

using namespace std;
using ll = long long;

#define rep(i, n) for (ll i = 0; (i) < (ll) (n); (i)++)
#define rep2(i, s, n) for(ll i = (ll) (s); (i) < (ll) (n); (i)++)
#define rrep(i, n) for (ll i = (ll) (n) - 1; (i) >= 0; (i)--)

template<class T>
bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
void vecout(const vector<T> &v, char div = '\n') {
    rep(i, v.size()) {
        if (i) cout << div;
        cout << v[i];
    }
    cout << endl;
}

int main() {
    string s;
    cin >> s;

    int n = s.size();

    if (n == 2) {
        cout << abs(s[0] - s[1]) << endl;
        return 0;
    }

    vector<int> divisors;
    rep2(i, 1, n + 1) {
        if (i * i > n) break;
        if (n % i == 0) {
            divisors.push_back(i);
            if (i * i != n) {
                divisors.push_back(n / i);
            }
        }
    }

    sort(divisors.begin(), divisors.end());

    ll ans = 10;

    // 桁数が 3 以上の場合
    for (auto len: divisors) {
        if (len <= 2 || len == n) continue;
//        cerr << len << endl;
        string t = s.substr(0, len - 2);
        int mx = stoi(s.substr(len - 2, 2));
        int mn = mx;
        rep(i, n / len) {
            int start = i * len;
            string t1 = s.substr(start, len - 2);
            if (t != t1) {
                goto CONTINUE;
            }
            int p = stoi(s.substr(start + len - 2, 2));
            chmax(mx, p);
            chmin(mn, p);
        }
        chmin(ans, ll(mx - mn));

        CONTINUE:;
    }

    // 桁数が 2 以下である場合
    {
        // dp[i][j] = s[0..i) まで見て，切り分けた最小値が j であるときの，最大値の最小値
        vector<vector<ll>> dp(n + 1, vector<ll>(100, 10000));
        rep(j, 100) dp[0][j] = 0;
        rep(i, n) {
//            vecout(dp[i], ' ');
            rep(j, 100) {
                // 一桁で切る
                {
                    ll p = stoi(s.substr(i, 1));
                    chmin(dp[i + 1][min(j, p)], max(dp[i][j], p));
                }

                // 二桁で切る
                if (i + 2 <= n) {
                    ll p = stoi(s.substr(i, 2));
                    chmin(dp[i + 2][min(j, p)], max(dp[i][j], p));
                }
            }
        }

        rep(j, 100) {
            chmin(ans, dp[n][j] - j);
        }
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 340
memory_kb: 85184
final_rank: 13

code_snippet: #ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;

/*---------macro---------*/
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define unless(x) if(!(x))
#define until(x) while(!(x))
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define mybit(i,j) (((i)>>(j))&1)

/*---------type/const---------*/
const int big=1000000007;
//const int big=998244353;
const double EPS=1e-8; //適宜変える
typedef long long ll;
typedef unsigned long long ull;
typedef std::string::const_iterator state; //構文解析
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const char newl='\n';
struct{
    constexpr operator int(){return -int(1e9)-10;}
    constexpr operator ll(){return -ll(1e18)-10;}
}neginf;
struct{
    constexpr operator int(){return int(1e9)+10;}
    constexpr operator ll(){return ll(1e18)+10;}
    constexpr auto operator -(){return neginf;}
}inf;

/*---------debug---------*/
#ifdef LOGX
#include <template/debug.hpp>
#else
#define dbg(...) ;
#define dbgnewl ;
#define prt(x) ;
#define _prt(x) ;
#endif

/*---------function---------*/
template<typename T> T max(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}
template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}
template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}
bool valid(int i,int j,int h,int w){return (i>=0 && j>=0 && i<h && j<w);}
template<class T,class U>T expm(T x,U y,const ll mod=big){T res=1;while(y){if(y&1)(res*=x)%=mod;(x*=x)%=mod;y>>=1;}return res;}
template<class T,class U>T exp(T x,U y){T res=1;while(y){if(y&1)res*=x;x*=x;y>>=1;}return res;}

string s;
int sz;
//len>=3
int solve_len(int len){
    if(len<3 || len==sz)return 100;
    vector<string> x(sz/len);
    rep(i,sz/len){
        x[i]=s.substr(i*len,len);
    }
    string l=min(x),r=max(x);
    dbg(len,l,r);
    //下から2桁目より上のいずれかが異なっている => 差は((R)*100+(2桁))-((L)*100+(2桁))>=100+(2桁-2桁)>=12 (111-99が最適)
    if(l.substr(0,len-2)!=r.substr(0,len-2))return 10;
    else return stoi(r.substr(len-2))-stoi(l.substr(len-2));
}
int solve_12(){
    //そもそものlenが2だと切り分けていない場合があるので別処理
    if(sz==2)return abs(s[0]-s[1]);
    //dp[i][j]=i文字処理して、最小がjの時の最大値の最小
    vector<vector<int>> dp(sz+1,vector<int>(100,1000));
    dp[0][99]=0;
    rep(i,sz)rep(j,100)for(int k:{1,2})if(i+k<=sz){
        //dp[i][j]から、s[i,i+k)を追加した時の遷移
        int J=min(j,stoi(s.substr(i,k)));
        int val=max(dp[i][j],stoi(s.substr(i,k)));
        chmin(dp[i+k][J],val);
    }
    dbg(dp);
    int ans=10;
    rep(j,100)chmin(ans,dp[sz][j]-j);
    return ans;
}

int main(){
    //std::ios::sync_with_stdio(false);
    //std::cin.tie(nullptr);
    std::cout.precision(10);
/*------------------------------------*/
    
    cin >> s;
    sz=s.size();
    int ans=solve_12();
    dbg(ans);
    for(int i=1;i*i<=sz;i++)if(sz%i==0){
        chmin(ans,solve_len(i));
        chmin(ans,solve_len(sz/i));
    }
    cout << ans << newl;
}
/*
そもそも答えは8以下なので、答え全探索はできる。一番左の桁数も決め打つ。
一番左が1,2桁：適当にdp 最適な分割方法に3桁以上は現れない(3桁-2桁の最小は12(>8))
3桁以上：n桁-(n-1)桁>=111-99=12>8, (n+1)桁-n桁>8 なので全てn桁でないといけない。
*/
accuracy: 1
time_ms: 610
memory_kb: 46148
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 1000;
vector<int> z_algorithm(string S){
  int N = S.size();
  vector<int> ans(N);
  for (int i = 1, j = 0; i < N; i++){
    if (i + ans[i - j] < j + ans[j]){
      ans[i] = ans[i - j];
    } else {
      int k = max(0, j + ans[j] - i);
      while (i + k < N && S[k] == S[i + k]){
        k++;
      }
      ans[i] = k;
      j = i;
    }
  }
  ans[0] = N;
  return ans;
}
int main(){
  string n;
  cin >> n;
  int L = n.size();
  vector<int> Z = z_algorithm(n);
  int ans = 8;
  for (int i = 1; i < L; i++){
    if (L % i == 0){
      bool ok = true;
      for (int j = 0; j < L; j += i){
        if (Z[j] < i - 1){
          ok = false;
        }
      }
      if (ok){
        int mx = 1, mn = 9;
        for (int j = i - 1; j < L; j += i){
          mx = max(mx, n[j] - '0');
          mn = min(mn, n[j] - '0');
        }
        ans = min(ans, mx - mn);
      }
    }
  }
  for (int i = 1; i <= 99; i++){
    vector<int> dp(L + 1, INF);
    dp[0] = i;
    for (int j = 0; j < L; j++){
      int x = n[j] - '0';
      if (x >= i){
        dp[j + 1] = min(dp[j + 1], max(dp[j], x));
      }
      if (j < L - 1 && !(j == 0 && j + 2 == L)){
        int y = (n[j] - '0') * 10 + (n[j + 1] - '0');
        if (y >= i){
          dp[j + 2] = min(dp[j + 2], max(dp[j], y));
        }
      }
    }
    ans = min(ans, dp[L] - i);
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 4132
final_rank: 5

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)


long long sumvec(vector<int> A){
	long long res = 0;
	for (auto a : A) res += a;
	return res;
}


int solve1(string S, int d){
	int n = S.size();
	int ansl = 0;
	int ansr = 0;
	for (int s = 0; s < n; s += d){
		int tmp = 0;
		rep(i, d){
			tmp *= 10;
			tmp += S[s + i] - S[i];
			if (tmp > 10) tmp = 10;
			if (tmp < -10) tmp = -10;
		}
		ansl = min(ansl, tmp);
		ansr = max(ansr, tmp);
	}
	int ans = ansr - ansl;
	//cout << "solve1(" << d << ") = " << ans << endl;
	return ans;
}

int solve2(string S, int d){
	int p = 0;
	int res = 0;
	for (char c : S){
		p = p * 10 + (c - '0');
		if (p >= d){
			res = max(res, p);
			p = 0;
		}
	}
	int ans = 10;
	if (p == 0) ans = res - d;
	//cout << "solve2(" << d << ") = " << ans << endl;
	return ans;
}


int main(){
	string S; cin >> S;
	int N = S.size();
	int ans = 10;
	for (int i = 2; i < N; i++) if (N%i == 0){
		ans = min(ans, solve1(S, i));
	}
	for (int i = 1; i < 10; i++){
		ans = min(ans, solve2(S, i));
	}
	cout << ans << endl;
	return 0;
}




struct ios_improve_speed{
	ios_improve_speed(){
		ios::sync_with_stdio(0);
		cin.tie(0);
	}
} ios_improve_speed_instance;
accuracy: 1
time_ms: 10
memory_kb: 3624
final_rank: 1

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string N;
int f(int L)
{
	vector<string>A(N.size()/L);
	for(int i=0;i<N.size();i+=L)A[i/L]=N.substr(i,L);
	sort(A.begin(),A.end());
	int ret=0;
	for(int i=0;i<L;i++)
	{
		ret=ret*10+A.back()[i]-A[0][i];
		if(ret>=10)return ret;
	}
	return ret;
}
bool dp[1<<17][20][20];
main()
{
	cin>>N;
	if(N.size()==2)
	{
		cout<<abs(N[0]-N[1])<<endl;
		return 0;
	}
	dp[1][N[0]-'0'][N[0]-'0']=true;
	if(N[0]=='1')
	{
		int t=(N[0]-'0')*10+N[1]-'0';
		dp[2][t][t]=true;
	}
	for(int i=1;i<N.size();i++)for(int j=0;j<20;j++)for(int k=j;k<20;k++)if(dp[i][j][k])
	{
		int t=N[i]-'0';
		dp[i+1][min(j,t)][max(k,t)]=true;
		if(t==1&&i+1<N.size())
		{
			t=t*10+N[i+1]-'0';
			dp[i+2][min(j,t)][max(k,t)]=true;
		}
	}
	int ans=10;
	for(int j=0;j<20;j++)for(int k=j;k<20;k++)if(dp[N.size()][j][k])ans=min(ans,k-j);
	for(int i=1;i*i<=N.size();i++)if(N.size()%i==0)
	{
		ans=min(ans,f(i));
		if(i>1&&i*i<N.size())ans=min(ans,f(N.size()/i));
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 47480
final_rank: 10

code_snippet: #include<bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define eb emplace_back
using namespace std;
const char newl='\n';
template<class T>
using V=vector<T>;
string s;
int f(int st,int len){
    if(len==1){
        return s[st]-'0';
    }else if(len==2){
        return (s[st+1]-'0')+(s[st]-'0')*10;
    }
}
int main(){
    cin>>s;
    int n=s.length();
    V<V<int>> dp(n+1,V<int>(100,1<<30));
    for(int i=0;i<=s[0]-'0';i++){
        dp[0][i]=s[0]-'0';
    }
    if(n>2){
        for(int i=0;i<=f(0,2);i++){
            dp[1][i]=f(0,2);
        }
    }
    for(int i=0;i<=f(1,1);i++){
        dp[1][i]=min(dp[1][i],max(dp[0][i],f(1,1)));
    }
    for(int i=2;i<n;i++){
        for(int j=0;j<=f(i,1);j++){
            dp[i][j]=min(dp[i][j],max(dp[i-1][j],f(i,1)));
        }
        for(int j=0;j<=f(i-1,2);j++){
            dp[i][j]=min(dp[i][j],max(dp[i-2][j],f(i-1,2)));
        }
    }
    int res=1<<30;
    for(int i=0;i<100;i++){
        res=min(res,dp[n-1][i]-i);
    }
    //cout<<res<<newl;
    for(int i=1;i<n;i++){
        if(n%i==0){
            string st=s.substr(0,i-2);
            bool fl=true;
            int mi=f(i-2,2),ma=f(i-2,2);
            for(int j=0;j<n;j+=i){
                if(st!=s.substr(j,i-2)){
                    fl=false;
                    break;
                }
                mi=min(mi,f(j+i-2,2));
                ma=max(ma,f(j+i-2,2));
            }
            if(fl){
                res=min(res,ma-mi);
            }
        }
    }
    cout<<res<<newl;
}
accuracy: 1
time_ms: 60
memory_kb: 46284
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define range(a) (a).begin(),(a).end()
#define print(A,x,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i]x;}cout<<endl;
#define pprint(A,y,m,n) rep(j,0,m){print(A[j],y,n);}
const long mod=1e9+7;
const int siz=1e5;
const int inf=1e9;
int main(){
	string s; cin>>s;
	int n = s.size();
	int ans = 8;
	rep(i,2,10){
		int m = 11;
		for(int j = n-1; j >= 0; j--){
			if(s[j] - '0' < i){
                if(j == 0 || s[j-1] - '0' > 1){ m = inf; break; }
				else{ m = max(m, s[j] - '0' + 10); j--; }
			}
		}ans = min(ans, m - i);
	}
	rep(i,1,n){
		if(n % i != 0) continue;
		string a = "", b = "";
        rep(j,0,i){ a += "9"; b += "1"; }
		rep(j,0,n/i){
            string t = "";
            rep(k,0,i){
			    if(a[k] < s[i*j+k] && s[i*j+k] < b[k]) break;
                else t += s[i*j+k];
            }
            if((int)t.size() == i){
                if(t < a) a = t;
                if(b < t) b = t;
            }
		}
        rep(j,0,i){
            if(a[j] <= b[j]) b[j] = b[j] - a[j] + '0';
            else b[j] = 10*(b[j-1]-'0') + (b[j]-a[j]) + '0';
        }rep(j,0,i){
            if(j == i-1) ans = min(ans, b[j] - '0');
            else if(b[j] > '0') break;
        }
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3340
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define range(a) (a).begin(),(a).end()
#define print(A,x,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i]x;}cout<<endl;
#define pprint(A,y,m,n) rep(j,0,m){print(A[j],y,n);}
const long mod=1e9+7;
const int siz=1e5;
const int inf=1e9;
int main(){
	string s; cin>>s;
	int n = s.size();
	int ans = 8;
	rep(i,2,10){
		int m = 11;
		for(int j = n-1; j >= 0; j--){
			if(s[j] - '0' < i){
                if(j == 0 || s[j-1] - '0' > 1){ m = inf; break; }
				else{ m = max(m, s[j] - '0' + 10); j--; }
			}
		}ans = min(ans, m - i);
	}
	rep(i,1,n){
		if(n % i != 0) continue;
		int t = 0;
		int a = inf, b = 0;
		rep(j,0,n){
			t = 10 * t + (s[j] - '0');
			if(j % i == i - 1){
				a = min(a, t); b = max(b, t);
				t = 0;
			}
		}ans = min(ans, b - a);
	}
	cout<<ans<<endl;
}
accuracy: 0.275
time_ms: 20
memory_kb: 3340
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define range(a) (a).begin(),(a).end()
#define print(A,x,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i]x;}cout<<endl;
#define pprint(A,y,m,n) rep(j,0,m){print(A[j],y,n);}
const long mod=1e9+7;
const int siz=1e5;
const int inf=1e9;
int main(){
	string s; cin>>s;
	int n = s.size();
	int ans = 8;
	rep(i,2,10){
		int m = 11;
		for(int j = n-1; j > 0; j--){
			if(s[j] - '0' < i){
				if(s[j-1] - '0' > 1){ m = inf; break; }
				else{ m = max(m, s[j] - '0' + 10); j--; }
			}
		}ans = min(ans, m - i);
	}
	rep(i,1,n){
		if(n % i != 0) continue;
		int t = 0;
		int a = inf, b = 0;
		rep(j,0,n){
			t = 10 * t + (s[j] - '0');
			if(j % i == i - 1){
				a = min(a, t); b = max(b, t);
				t = 0;
			}
		}ans = min(ans, b - a);
	}
	cout<<ans<<endl;
}
accuracy: 0.275
time_ms: 20
memory_kb: 3192
final_rank: 18

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
#define int long long
#define V vector
#define vel V<int>
#define vvel V<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define sor(s) sort(s.begin(),s.end())
signed main() {
	string s; cin >> s;
	int sz = s.size();
	if (sz == 2) {
		sor(s);
		cout << s[1] - s[0] << endl; return 0;
	}
	int ans = 100;
	for (int i = 2; i < sz; i++) {
		if (sz % i == 0) {
			string mxs = s.substr(0, i);
			string mns = mxs;
			for (int j = 0; j < sz / i; j++) {
				mmax(mxs, s.substr(j * i, i));
				mmin(mns, s.substr(j * i, i));
			}
			if (mxs.substr(0, i - 2) == mns.substr(0, i - 2)) {
				int mxn = mxs[i - 2] * 10 + mxs[i - 1];
				int mnn = mns[i - 2] * 10 + mns[i - 1];
				mmin(ans, mxn - mnn);
			}
		}
	}
	int mx = 0;
	int mn = 100;
	int pot = 0;
	while (pot < sz) {
		int nval;
		if (s[pot] == '1' && pot + 2 <= sz) {
			nval = (s[pot] - '0') * 10 + (s[pot + 1] - '0');
			pot += 2;
		}
		else {
			nval = (s[pot] - '0');
			pot++;
		}
		mmin(mn, nval);
		mmax(mx, nval);
	}
	mmin(ans, mx - mn);
	sor(s);
	mmin(ans, (int)(s[sz-1]-'0') - (s[0]-'0'));
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3300
final_rank: 3