Problem:
Change a Password
Password authentication is used in a lot of facilities. The office of JAG also uses password authentication. A password is required to enter their office. A password is a string of $N$ digits '0'-'9'. This password is changed on a regular basis. Taro, a staff of the security division of JAG, decided to use the following rules to generate a new password from an old one.
The new password consists of the same number $N$ of digits to the original one and each digit appears at most once in the new password. It can have a leading zero. (Note that an old password may contain same digits twice or more.)
The new password maximizes the difference from the old password within constraints described above. (Definition of the difference between two passwords is described below.)
If there are two or more candidates, the one which has the minimum value when it is read as an integer will be selected.
The difference between two passwords is defined by
min
($|a - b|,  10^N - |a - b|$), where $a$ and $b$ are the integers represented by the two passwords. For example, the difference between "11" and "42" is 31, and the difference between "987" and "012" is 25.
Taro would like to use a computer to calculate a new password correctly, but he is not good at programming. Therefore, he asked you to write a program. Your task is to write a program that generates a new password from an old password.
Input
The input consists of a single test case. The first line of the input contains a string $S$ which denotes the old password. You can assume that the length of $S$ is no less than 1 and no greater than 10. Note that old password $S$ may contain same digits twice or more, and may have leading zeros.
Output
Print the new password in a line.
Sample Input 1
201
Output for the Sample Input 1
701
Sample Input 2
512
Output for the Sample Input 2
012
Sample Input 3
99999
Output for the Sample Input 3
49876
Sample Input 4
765876346
Output for the Sample Input 4
265874931

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

bool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }

const ll INF = 1e18;
ll mod = 998244353;

ifstream in;
ofstream out;

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    if (argc > 2) {
        in.open(argv[1]);
        cin.rdbuf(in.rdbuf());
        out.open(argv[2]);
        cout.rdbuf(out.rdbuf());
    }

    string inp;
    ll n;
    cin >> inp;
    n = stoll(inp);
    ll siz = inp.size();
    ll ten = 1;
    for (int i = 0; i < siz; i++) ten *= 10;
    ll mx = 0;
    string ans = to_string(n);

    vector<ll> per(10, 0);
    iota(per.begin(), per.end(), 0);
    do {
        ll dig = 0;
        string s;
        for (int i = 0; i < siz; i++) {
            dig *= 10;
            dig += per[i];
            s.push_back(per[i] + '0');
        }
        if (mx < min(abs(dig - n), ten - abs(dig - n))) {
            mx = min(abs(dig - n), ten - abs(dig - n));
            ans = s;
        }
        if (mx == min(abs(dig - n), ten - abs(dig - n)) && stoll(ans) > dig)
            ans = s;
    } while (next_permutation(per.begin(), per.end()));

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3584
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

bool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }

const ll INF = 1e18;
ll mod = 998244353;

ifstream in;
ofstream out;

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    if (argc > 2) {
        in.open(argv[1]);
        cin.rdbuf(in.rdbuf());
        out.open(argv[2]);
        cout.rdbuf(out.rdbuf());
    }

    ll n;
    cin >> n;
    ll siz = to_string(n).size();
    ll ten = 1;
    for (int i = 0; i < siz; i++) ten *= 10;
    ll mx = 0;
    string ans = to_string(n);

    vector<ll> per(10, 0);
    iota(per.begin(), per.end(), 0);
    do {
        ll dig = 0;
        string s;
        for (int i = 0; i < siz; i++) {
            dig *= 10;
            dig += per[i];
            s.push_back(per[i] + '0');
        }
        if (mx < min(abs(dig - n), ten - abs(dig - n))) {
            mx = min(abs(dig - n), ten - abs(dig - n));
            ans = s;
        }
        if (mx == min(abs(dig - n), ten - abs(dig - n)) && stoll(ans) > dig)
            ans = s;
    } while (next_permutation(per.begin(), per.end()));

    cout << ans << endl;
    return 0;
}
accuracy: 0.6065573770491803
time_ms: 70
memory_kb: 3584
final_rank: 17

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)
#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  string s;cin >> s;
  ll S = stol(s),tn = 1;
  rep(i,0,sz(s))tn *= 10;
  auto comp = [&](string &a,string &b)->bool{
    ll A = stol(a),B = stol(b);
    if(min(abs(A-S),tn-abs(A-S)) == min(abs(B-S),tn-abs(B-S)))return A < B;
    return min(abs(A-S),tn-abs(A-S)) > min(abs(B-S),tn-abs(B-S));
  };
  string res = "";
  rep(i,0,sz(s))res += char('0'+i);
  auto dfs = [&](auto &dfs,string &t,int k)->void{
    if(sz(t) == sz(s)){
      string u = t;
      do{
        if(comp(u,res))res = u;
      }while(next_permutation(ALL(u)));
      return ;
    }
    if(10-k < sz(s)-sz(t))return ;
    rep(i,k,10){
      t += char('0'+i);
      dfs(dfs,t,i+1);
      t.pop_back();
    }
  };
  string t = "";
  dfs(dfs,t,0);
  cout << res << endl;
  

  return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 3432
final_rank: 7

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)
#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  string s;cin >> s;
  ll S = stoi(s),tn = 1;
  rep(i,0,sz(s))tn *= 10;
  auto comp = [&](string &a,string &b)->bool{
    ll A = stoi(a),B = stoi(b);
    if(min(abs(A-S),tn-abs(A-S)) == min(abs(B-S),tn-abs(B-S)))return A < B;
    return min(abs(A-S),tn-abs(A-S)) > min(abs(B-S),tn-abs(B-S));
  };
  string res = "";
  rep(i,0,sz(s))res += char('0'+i);
  auto dfs = [&](auto &dfs,string &t,int k)->void{
    if(sz(t) == sz(s)){
      string u = t;
      do{
        if(comp(u,res))res = u;
      }while(next_permutation(ALL(u)));
      return ;
    }
    if(10-k < sz(s)-sz(t))return ;
    rep(i,k,10){
      t += char('0'+i);
      dfs(dfs,t,i+1);
      t.pop_back();
    }
  };
  string t = "";
  dfs(dfs,t,0);
  cout << res << endl;
  

  return 0;
}
accuracy: 0.21311475409836064
time_ms: 190
memory_kb: 3432
final_rank: 19

code_snippet: #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
char c;
bool flag;

int Cs[10];
int d;
inline void read(int&a)
{
	a=0;do c=getchar();while(c!='-'&&(c<'0'||c>'9'));
	c=c=='-'?flag=true,getchar():c;
	while(c<='9'&&c>='0')d++,a=(a<<3)+(a<<1)+c-'0',c=getchar();
	a=flag?flag=false,-a:a;
}
#define ll long long
inline void read(ll&a)
{
	a=0;do c=getchar();while(c!='-'&&(c<'0'||c>'9'));
	c=c=='-'?flag=true,getchar():c;
	while(c<='9'&&c>='0')d++,a=(a<<3)+(a<<1)+c-'0',c=getchar();
	a=flag?flag=false,-a:a;
}
int t;
int n;
ll Old,Base;
ll Ans;
ll Calc(ll Val)
{
	return min(Base-abs(Val-Old),abs(Val-Old));
}
void Up(ll Val)
{
	if(Calc(Val)>Calc(Ans))Ans=Val;
	if(Ans==0)
		Ans++,Ans--;							
}

void solve(int x,ll Val)
{
	if(d==x-1){return Up(Val);}
	for(int i=0;i<=9;i++)
		if(!Cs[i])
		Cs[i]=true,solve(x+1,Val*10+i),
		Cs[i]=false;
}

int main()
{
	read(Old);
	//if(!Old){cout<<5<<endl;return 0;}
	Base=1;
	Ans=Old;
	int t=0;
	while(t<d)Base*=10,t++;
	//while(Base<=Old)Base*=10,d++;
	solve(1,0);
	while(Base/10>Ans)
	Base/=10,putchar('0');
	cout<<Ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3476
final_rank: 4

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

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
    string S;
    cin >> S;
    int N = S.size();
    vector<int> D(10,-1);
    rep(i,0,N) D[i] = i;
    sort(ALL(D));
    ll Cur = -1;
    string ANS = "";
    ll x = 1;
    rep(i,0,N) x *= 10;
    do {
        string T = "";
        rep(i,0,N) T += '.';
        rep(i,0,10) {
            if (D[i] != -1) T[D[i]] = '0' + i;
        }
        ll X = stoll(S), Y = stoll(T);
        ll Z = min(abs(X-Y),x-abs(X-Y));
        if (Cur < Z) {
            Cur =  Z;
            ANS = T;
        }
        else if (Cur == Z && ANS > T) {
            ANS = T;
        }

    } while(next_permutation(ALL(D)));
    cout << ANS << endl;
}
accuracy: 1
time_ms: 250
memory_kb: 3500
final_rank: 10

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

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;

  ll n = 1;
  for (int i = 0; i < s.size(); i++) n *= 10;

  string t = "0123456789";
  ll dist = 0;
  string ans = "";
  do {
    string t2 = t.substr(0, (int) s.size());
    ll tmp = stoll(t2);
    ll diff = min(abs(stoll(s) - tmp), n - abs(stoll(s) - tmp));
    if (diff > dist) {
      dist = diff;
      ans = t2;
    }
  } while (next_permutation(t.begin(), t.end()));

  cout << ans << endl;
}
accuracy: 1
time_ms: 300
memory_kb: 3456
final_rank: 9

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

using namespace std;

using ll = long long;
#define int long long

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

#define all(c) std::begin(c), std::end(c)

#ifdef LOCAL
#define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__)
template <class T, class ...Args> void debug_impl(string s, T&& f, Args &&...args) {
    cerr << "(" << s << "): " << "(" << forward<T>(f);
    ((cerr << ", " << forward<Args>(args)), ..., (cerr << ")\n"));
}
#else
#define debug(...) void(0)
#endif

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


template <class T> istream& operator>>(istream& in, vector<T>& v) {
    for (auto& e : v) in >> e;
    return in;
}
template <class ...Args> void read(Args&... args) {
    (cin >> ... >> args);
}

template <class T> ostream& operator>>(ostream& out, vector<T>& v) {
    int n = v.size();
    rep(i, n) {
        out << v[i];
        if (i + 1 != n) out << ' ';
    }
    return out;
}

template <class T, class ...Tails> void print(T&& h, Tails &&... tails) {
    cout << h, ((cout << ' ' << forward<Tails>(tails)), ..., (cout << '\n'));
}

int intpow(int a, int b){ int ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
template <class T> using vc = vector<T>;

const int N = 10;
const ll inf = 1LL<<60;

int f(string s) {
    int n = (int)s.size();
    int ret = 0;
    rep(i, n) {
        ret *= 10;
        ret += s[i] - '0';
    }   
    return ret;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s; read(s);
    int n = (int)s.size();
    int val = f(s);
    vc<int> ids(N, 0);
    iota(all(ids), 0);
    int res = -inf;
    string ans = s;
    do {
        int now = 0;
        string t = "";
        rep(i, n) {
            now *= 10;
            now += ids[i];
            t += (char)('0' + ids[i]);
        }
        if(chmax(res, min(abs(now - val), intpow(10, n) - abs(now - val)))) {
            ans = t;
        }
    } while(next_permutation(all(ids)));
    print(ans);
}
accuracy: 1
time_ms: 60
memory_kb: 3456
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#include <chrono>
#include <thread>
////#include <atcoder/all>

//using namespace atcoder;
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

double EPS = 1e-9;
template<class T>
bool chmin(T& p, T q) {
    if (p > q + EPS) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

ll gcd(ll(a), ll(b)) {
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}

vector<ll> fact, factinv, inv;
ll mod = 998244353;
void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact.at(0) = fact.at(1) = 1;
    factinv.at(0) = factinv.at(1) = 1;
    inv.at(1) = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact.at(i) = (fact.at(i - 1) * i) % mod;
        inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
        factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
    }

}
ll nCk(ll n, ll k) {
    if (k < 0)return 0;
    if (n < k) return 0;
    return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}
vector<string>S;
ll dfs(ll L, ll R, ll n, char c = '+') {
    ll res = 0;
    if (c == '*')res = 1;
    for (ll i = L; i <= R; i++) {
        ll d = 0;
        if (S[i].size() != n + 1)continue;
        if (S[i][n] == '+') {
            bool OK = 1;
            for (ll j = i + 1; j < R; j++) {
                if (S[j].size() == n + 1) {
                    d = dfs(i + 1, j - 1, n + 1, '+');
                    OK = 0;
                    break;
                }

            }
            if (OK)d = dfs(i + 1, R, n + 1, '+');
        }
        else if (S[i][n] == '*') {
            bool OK = 1;
            for (ll j = i + 1; j < R; j++) {
                if (S[j].size() == n + 1) {
                    d = dfs(i + 1, j - 1, n + 1, '*');
                    OK = 0;
                    break;
                }

            }
            if (OK)d = dfs(i + 1, R, n + 1, '*');
        }
        else {
            d = S[i][n] - '0';
        }
        if (c == '+')res += d;
        else res *= d;
    }
    return res;
}



int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    string QN;
    vll P = { 0,1,2,3,4,5,6,7,8,9 };
    cin >> QN;
    ll M = -1, an = -1;
    ll K = QN.size();
    ll N = stoll(QN);
    ll p = pow(10, K);
    do {
        ll d = 0;
        rep(q, K) {
            d += P[q];
            d *= 10;
        }
        d /= 10;
        ll S = min(abs(N - d), p - abs(N - d));
        if (chmax(M, S))an = d;
    } while (next_permutation(all(P)));
    string AN = to_string(an);
    while (AN.size() < K)AN = '0' + AN;
    cout << AN << endl;

}
accuracy: 1
time_ms: 20
memory_kb: 3948
final_rank: 13

code_snippet: #include <bits/stdc++.h>
#include <chrono>
#include <thread>
////#include <atcoder/all>

//using namespace atcoder;
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

double EPS = 1e-9;
template<class T>
bool chmin(T& p, T q) {
    if (p > q + EPS) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

ll gcd(ll(a), ll(b)) {
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}

vector<ll> fact, factinv, inv;
ll mod = 998244353;
void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact.at(0) = fact.at(1) = 1;
    factinv.at(0) = factinv.at(1) = 1;
    inv.at(1) = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact.at(i) = (fact.at(i - 1) * i) % mod;
        inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
        factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
    }

}
ll nCk(ll n, ll k) {
    if (k < 0)return 0;
    if (n < k) return 0;
    return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}
vector<string>S;
ll dfs(ll L, ll R, ll n, char c = '+') {
    ll res = 0;
    if (c == '*')res = 1;
    for (ll i = L; i <= R; i++) {
        ll d = 0;
        if (S[i].size() != n + 1)continue;
        if (S[i][n] == '+') {
            bool OK = 1;
            for (ll j = i + 1; j < R; j++) {
                if (S[j].size() == n + 1) {
                    d = dfs(i + 1, j - 1, n + 1, '+');
                    OK = 0;
                    break;
                }

            }
            if (OK)d = dfs(i + 1, R, n + 1, '+');
        }
        else if (S[i][n] == '*') {
            bool OK = 1;
            for (ll j = i + 1; j < R; j++) {
                if (S[j].size() == n + 1) {
                    d = dfs(i + 1, j - 1, n + 1, '*');
                    OK = 0;
                    break;
                }

            }
            if (OK)d = dfs(i + 1, R, n + 1, '*');
        }
        else {
            d = S[i][n] - '0';
        }
        if (c == '+')res += d;
        else res *= d;
    }
    return res;
}



int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N;
    vll P = { 0,1,2,3,4,5,6,7,8,9 };
    cin >> N;
    ll M = -1, an = -1;
    ll K = to_string(N).size();
    ll p = pow(10, K);
    do {
        ll d = 0;
        rep(q, K) {
            d += P[q];
            d *= 10;
        }
        d /= 10;
        ll S = min(abs(N - d), p - abs(N - d));
        if (chmax(M, S))an = d;
    } while (next_permutation(all(P)));
    string AN = to_string(an);
    while (AN.size() < K)AN = '0' + AN;
    cout << AN << endl;

}
accuracy: 0.6065573770491803
time_ms: 20
memory_kb: 3948
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#include <chrono>
#include <thread>
////#include <atcoder/all>

//using namespace atcoder;
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

double EPS = 1e-9;
template<class T>
bool chmin(T& p, T q) {
    if (p > q + EPS) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

ll gcd(ll(a), ll(b)) {
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}

vector<ll> fact, factinv, inv;
ll mod = 998244353;
void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact.at(0) = fact.at(1) = 1;
    factinv.at(0) = factinv.at(1) = 1;
    inv.at(1) = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact.at(i) = (fact.at(i - 1) * i) % mod;
        inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
        factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
    }

}
ll nCk(ll n, ll k) {
    if (k < 0)return 0;
    if (n < k) return 0;
    return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}
vector<string>S;
ll dfs(ll L, ll R, ll n, char c = '+') {
    ll res = 0;
    if (c == '*')res = 1;
    for (ll i = L; i <= R; i++) {
        ll d = 0;
        if (S[i].size() != n + 1)continue;
        if (S[i][n] == '+') {
            bool OK = 1;
            for (ll j = i + 1; j < R; j++) {
                if (S[j].size() == n + 1) {
                    d = dfs(i + 1, j - 1, n + 1, '+');
                    OK = 0;
                    break;
                }

            }
            if (OK)d = dfs(i + 1, R, n + 1, '+');
        }
        else if (S[i][n] == '*') {
            bool OK = 1;
            for (ll j = i + 1; j < R; j++) {
                if (S[j].size() == n + 1) {
                    d = dfs(i + 1, j - 1, n + 1, '*');
                    OK = 0;
                    break;
                }

            }
            if (OK)d = dfs(i + 1, R, n + 1, '*');
        }
        else {
            d = S[i][n] - '0';
        }
        if (c == '+')res += d;
        else res *= d;
    }
    return res;
}



int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N;
    vll P = { 0,1,2,3,4,5,6,7,8,9 };
    cin >> N;
    ll M = -1, an = -1;
    ll K = to_string(N).size();
    ll p = pow(10, K);
    do {
        ll d = 0;
        rep(q, K) {
            d += P[q];
            d *= 10;
        }
        d /= 10;
        ll S = min(abs(N - d), p - abs(N - d));
        if (chmax(M, S))an = d;
    } while (next_permutation(all(P)));
    cout << an << endl;

}
accuracy: 0.01639344262295082
time_ms: 10
memory_kb: 3860
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcountll(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define vector4d(type,name,h,w,d,...) vector<vector<vector<vector<type>>>>name(h,vector<vector<vector<type>>>(w,vector<vector<type>>(d,vector<type>(__VA_ARGS__))))
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=1LL<<60;
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<'\n';}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){return (a+b-1)/b;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    string s;
    cin>>s;
    int n = s.size();
    ll v2 = stoll(s);
    ll d = intpow(10,n), maxv = 0;
    string t = "0123456789", ans;
    do{
        ll v = stoll(t.substr(0,n));
        ll c = min(abs(v - v2),d - abs(v - v2));
        if(c > maxv){
            maxv = c;
            ans = t.substr(0, n);
        }
    }while(next_permutation(all(t)));
    out(ans);
}
accuracy: 1
time_ms: 130
memory_kb: 3444
final_rank: 5

code_snippet: #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <random>
#include <complex>
#include <bitset>
#include <iomanip>
#include <memory>
#include <functional>

#define rep(i, n, s) for (int i = (s); i < int(n); i++)
#define per(i, n, s) for (int i = (n) - 1; i >= int(s); i--)
#define MM << " " <<
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
using MaxHeap = std::priority_queue<T>;

using ll = long long;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
using Pdd = std::pair<double, double>;

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

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

template <class T>
void vdeb(const std::vector<T> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        if (i == n - 1)
            std::cout << da[i];
        else
            std::cout << da[i] << " ";
    }
    std::cout << std::endl;
}
template<class T>
void vdeb(const std::vector<std::vector<T>> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        // std::cout << i << " : ";
        vdeb(da[i]);
    }
    std::cout << std::endl;
}

using namespace std;

int main() {
    string s; cin >> s;
    int n = s.size();
    ll m = 0;
    ll p = 10;
    rep(i,n-1,0) p *= 10;
    for(auto &i : s) m = m * 10 + i - '0';
    string t = "0123456789";
    string ans;
    ll score = 0;
    do {
        ll k = 0;
        rep(i,n,0) k = k * 10 + t[i] - '0';
        if(chmax(score, min(abs(m-k), p - abs(m-k)))) {
            ans = t.substr(0, n);
        }
    } while(next_permutation(all(t)));
    cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3444
final_rank: 2

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

#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) begin(x), end(x)
template <class T> using V = vector<T>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string S;
    cin >> S;
    int N = S.size();
    string T = "", ans = "";
    V<int> used(10, 0);
    ll TEN = 1;
    rep(i, N) TEN *= 10;
    auto rec = [&](auto self, int id) -> int {
        if (id == N) {
            if (ans.size() == 0) {
                ans = T;
            } else {
                ll Tn = stoll(T), Sn = stoll(S), ansn = stoll(ans);
                ll d = min(abs(Sn - Tn), TEN - abs(Sn - Tn));
                ll d2 = min(abs(Sn - ansn), TEN - abs(Sn - ansn));
                if (d > d2 || (d == d2 && ansn > Tn)) ans = T;
            }
            return 0;
        }
        for (int i = 0; i <= 9; i++) {
            if (used[i] == 0) {
                T += '0' + i;
                used[i] = 1;
                self(self, id + 1);
                used[i] = 0;
                T.pop_back();
            }
        }
        return 0;
    };
    rec(rec, 0);
    cout << ans << '\n';
    return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 3504
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i, n) for(ll i = 0; i < (ll)n; i++)
#define ALL(v) (v).begin(), (v).end()

ll N = 0;
ll sz;
ll P = 1;
ll ans = 0;
ll diff = 0;
set<ll> st;
void dfs(ll C, ll sz) {
  //cout << C << '\n';
  if(sz == 0) {
    if(diff < min(abs(N-C), P-abs(N-C))) {
      diff = min(abs(N-C), P-abs(N-C));
      ans = C;
    }else if(diff == min(abs(N-C), P-abs(N-C))) {
      if(ans > C) ans = C;
    }
    return;
  }
  for(auto c: st) {
    st.erase(c);
    dfs(C*10+c, sz-1);
    st.insert(c);
  }
  return;
}

int main() {
  string S; cin >> S;
  ll sz = S.size();
  rep(i, sz) {
    N = N*10+(S[i]-'0');
  }
  rep(i, sz) P *= 10;
  vector<ll> V = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  do {
    ll tmp = 0;
    rep(i, sz) tmp = tmp*10+V[i];
    if(diff < min(abs(N-tmp), P-abs(N-tmp))) {
      diff = min(abs(N-tmp), P-abs(N-tmp));
      ans = tmp;
    }else if(diff == min(abs(N-tmp), P-abs(N-tmp))) {
      if(ans > tmp) ans = tmp;
    }
  } while(next_permutation(ALL(V)));
  string T = to_string(ans);
  rep(i, sz-T.size()) cout << '0';
  cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3544
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i, n) for(ll i = 0; i < (ll)n; i++)
#define ALL(v) (v).begin(), (v).end()

ll N;
ll sz;
ll P = 1;
ll ans = 0;
ll diff = 0;
set<ll> st;
void dfs(ll C, ll sz) {
  //cout << C << '\n';
  if(sz == 0) {
    if(diff < min(abs(N-C), P-abs(N-C))) {
      diff = min(abs(N-C), P-abs(N-C));
      ans = C;
    }else if(diff == min(abs(N-C), P-abs(N-C))) {
      if(ans > C) ans = C;
    }
    return;
  }
  for(auto c: st) {
    st.erase(c);
    dfs(C*10+c, sz-1);
    st.insert(c);
  }
  return;
}

int main() {
  cin >> N;
  string S = to_string(N);
  ll sz = S.size();
  rep(i, sz) P *= 10;
  vector<ll> V = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  do {
    ll tmp = 0;
    rep(i, sz) tmp = tmp*10+V[i];
    if(diff < min(abs(N-tmp), P-abs(N-tmp))) {
      diff = min(abs(N-tmp), P-abs(N-tmp));
      ans = tmp;
    }else if(diff == min(abs(N-tmp), P-abs(N-tmp))) {
      if(ans > tmp) ans = tmp;
    }
  } while(next_permutation(ALL(V)));
  string T = to_string(ans);
  rep(i, sz-T.size()) cout << '0';
  cout << ans << endl;
}
accuracy: 0.6065573770491803
time_ms: 30
memory_kb: 3508
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define inc(i, a, b) for (int i = (a); i <= (b); ++i)
#define dec(i, a, b) for (int i = (a); i >= (b); --i)

int main() {
    vector<ll> p = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    string s;
    ll res = 1e18, cost = -1e18;
    cin >> s;
    ll n = stoll(s), k = s.size(), r = 1;
    rep(i, k) r *= 10;
    do {
        ll m = 0;
        rep(i, k) m = m * 10 + p[i];
        ll c = min(llabs(n - m), r - llabs(n - m));
        if (c > cost) {
            cost = c;
            res = m;
        } else if (c == cost) {
            res = min(res, m);
        }
    } while (next_permutation(p.begin(), p.end()));
    cout << setw(k) << setfill('0') << res << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3432
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define inc(i, a, b) for (int i = (a); i <= (b); ++i)
#define dec(i, a, b) for (int i = (a); i >= (b); --i)

int main() {
    vector<ll> p = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    ll n, res = 1e18, cost = -1e18;
    cin >> n;
    ll k = to_string(n).size(), r = 1;
    rep(i, k) r *= 10;
    do {
        ll m = 0;
        rep(i, k) m = m * 10 + p[i];
        ll c = min(llabs(n - m), r - llabs(n - m));
        if (c > cost) {
            cost = c;
            res = m;
        } else if (c == cost) {
            res = min(res, m);
        }
    } while (next_permutation(p.begin(), p.end()));
    cout << setw(k) << setfill('0') << res << endl;
}
accuracy: 0.6065573770491803
time_ms: 20
memory_kb: 3500
final_rank: 15

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

#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) begin(x), end(x)
template <class T> using V = vector<T>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string S;
    cin >> S;
    int N = S.size();
    string T = "", ans = "";
    V<int> used(10, 0);
    ll TEN = 1;
    rep(i, N) TEN *= 10;
    show(TEN);
    auto rec = [&](auto self, int id) -> int {
        if (id == N) {
            if (ans.size() == 0) {
                ans = T;
            } else {
                ll Tn = stoll(T), Sn = stoll(S), ansn = stoll(ans);
                ll d = min(abs(Sn - Tn), TEN - abs(Sn - Tn));
                ll d2 = min(abs(Sn - ansn), TEN - abs(Sn - ansn));
                if (d > d2 || (d == d2 && ansn > Tn)) ans = T;
                // show(T, Tn, ansn, d, d2);
            }
            return 0;
        }
        for (int i = 0; i <= 9; i++) {
            if (used[i] == 0) {
                T += '0' + i;
                used[i] = 1;
                self(self, id + 1);
                used[i] = 0;
                T.pop_back();
            }
        }
        return 0;
    };
    rec(rec, 0);
    cout << ans << '\n';
    return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 3472
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    string s; cin >> s;
    int N = s.size();

    auto f = [&](string t) {
        ll res = 0;
        for(char c : t) res = res * 10 + c - '0';
        return res;
    };

    ll p10 = 1; for(int i = 0; i < N; i++) p10 *= 10;
    string ans = "";
    for(char c = '0'; c <= '9'; c++) {
        ans += c; if(ans.size() == N) break;
    }
    string t = "";
    set<char> se;
    ll MAX = -1;
    function<void(void)> dfs = [&](void) -> void {
        if(t.size() == N) {
            ll a = f(s), b = f(t);
            ll diff = min(abs(a - b), p10 - abs(a - b));
            if(diff > MAX) {
                MAX = diff;
                ans = t;
            } else if(diff == MAX) {
                if(b < f(ans)) ans = t;
            }
        } else {
            for(char c = '0'; c <= '9'; c++) {
                if(!se.count(c)) {
                    t.push_back(c); se.insert(c);
                    dfs();
                    t.pop_back(); se.erase(c);
                }
            }
        }
    }; dfs();
    cout << ans << endl;
}
accuracy: 1
time_ms: 720
memory_kb: 3468
final_rank: 14