http://whu.contest.codeforces.com/group/LqmZzHyOQN/contest/234192/problem/C
题解:乱搞???
反正先按相同排,不行的话倒回去
goto大法好
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=10000;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
int an[11];
int bn[22];
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
ll a,b;
scanf("%I64d%I64d",&a,&b);
int cnt=0;
while(a!=0){
an[a%10]++;
a/=10;
cnt++;
}
int cntb=0;
while(b!=0){
bn[++cntb]=b%10;
b/=10;
}
ll ans=0;
if(cnt<cntb){
for(int j=cnt;j>=1;j--){
for(int k=9;k>=0;k--){
if(an[k]>0){
ans=ans*10+k;
an[k]--;
break;
}
}
}
cout << ans << endl;
return 0;
}
for(int i=cnt;i>=1;i--){
if(an[bn[i]]>0){
ans=ans*10+bn[i];
an[bn[i]]--;
}else{
int start=i;
int flag=0;
g:
for(int k=bn[start]-1;k>=0;k--){
if(an[k]>0){
ans=ans*10+k;
flag=1;
an[k]--;
break;
}
}
if(!flag){
an[ans%10]++;
ans/=10;
start++;
goto g;
}
for(int j=start-1;j>=1;j--){
for(int k=9;k>=0;k--){
if(an[k]>0){
ans=ans*10+k;
an[k]--;
break;
}
}
}
break;
}
}
cout << ans << endl;
//cout << "Hello world!" << endl;
return 0;
}