Language: C++

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);
  string s;
  auto calc=[](Int x){
    if(x <= 9) return x + 1;
    Int res=10;
    Int t = 10;
    Int k = 2;
    while( t * 10 <= x){
      res += k * (t * 9);
      k++;
      t *= 10;
    }
    return res + (x - t + 1) * k;
  };

  //cout<<calc(101)<<endl;
  //cout<<calc(13)<<endl;
  
  auto sub=[](string s,Int k){
    return s.substr(s.size()-k);
  };

  auto inc=[](string s){
    for(Int i=s.size()-1;i>=0;i--){
      if(s[i]=='9'){
	s[i]='0';
	continue;
      }
      s[i]++;
      break;
    }
    return s;
  };
  
  while(cin>>s,s!="#"){
    Int ans=1e18;
    for(Int i=1;i<(Int)min(17u,s.size());i++){
      string t=s.substr(0,i),u=s.substr(i);
      if(u.front()=='0'||u.size()>16) continue; 
      //cout<<t<<"::"<<u<<endl;
      for(Int j=(Int)max(t.size(),u.size());j<17;j++){
	string x=inc(t),y=u;
	//cout<<i<<" "<<j<<":"<<x<<":"<<y<<endl;
	x=string(j-t.size(),'$')+x;
	y=y+string(j-u.size(),'$');
	bool f=0;
	for(Int k=0;k<j;k++){
	  if(k+1!=j){
	    f|=(isdigit(x[k])&&isdigit(y[k])&&x[k]!=y[k]);
	  }
	  f|=(x[k]=='$'&&y[k]=='$');
	}
	if(f) continue;
	if(y.back()!='$'){
	  if((x.back()-'0'+1)%10!=y.back()-'0') continue;
	}
	for(Int k=0;k<j;k++){
	  if(y[k]!='$') continue;
	  y[k]=isdigit(x[k])?x[k]:'0';
	}
	Int tmp=calc(stoll(y)-1)-i;
	//cout<<i<<" "<<j<<":"<<x<<":"<<y<<":"<<tmp<<endl;
	ans=min(ans,tmp);
	
      }
    }
    //cout<<"SPL"<<endl;
    for(Int i=1;i<17;i++){
      for(Int j=0;j<i;j++){
	if(j+i>(Int)s.size()) continue;
	string t=s.substr(j,i);
	if(t.front()=='0') continue;
	Int x=stoll(t);
	//cout<<t<<" "<<x<<endl;
	if(j){
	  if(x==1) continue;
	  string u=to_string(x-1);
	  //cout<<s.substr(0,j)<<":"<<u<<" "<<sub(u,j)<<endl;
	  if(s.substr(0,j)!=sub(u,j)) continue;
	}
	Int p=j+i;
	bool f=1;
	while(p<(Int)s.size()){
	  string u=to_string(++x);
	  string v=s.substr(p,min((Int)u.size(),(Int)s.size()-p));
	  //cout<<u<<":"<<v<<endl;
	  u=u.substr(0,v.size());
	  f&=(u==v);
	  p+=u.size();
	}
	if(!f) continue;
	Int tmp=calc(stoll(t)-1)+j;
	//cout<<i<<" "<<j<<" "<<":"<<t<<" "<<tmp<<endl;
	ans=min(ans,tmp);
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:42:27: error: no matching function for call to 'min(unsigned int, std::__cxx11::basic_string<char>::size_type)'
   42 |     for(Int i=1;i<(Int)min(17u,s.size());i++){
      |                        ~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:42:27: note:   deduced conflicting types for parameter 'const _Tp' ('unsigned int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'})
   42 |     for(Int i=1;i<(Int)min(17u,s.size());i++){
      |                        ~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:42:27: note:   mismatched types 'std::initializer_list<_Tp>' and 'unsigned int'
   42 |     for(Int i=1;i<(Int)min(17u,s.size());i++){
      |                        ~~~^~~~~~~~~~~~~~