Problem p01829 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<double,int,double> tp;
// typedef vector<tp> vt;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e9;
const ll INF = 2e18;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int in[10][10];
#define it vector<int>::iterator
bool next_combination(it first1, it last1, it first2, it last2) {
  if ((first1 == last1) || (first2 == last2))
    return false;
  it m1 = last1, m2 = last2;
  m2--;
  while (--m1 != first1 && !(*m1 < *m2))
    ;
  bool result = (m1 == first1) && !(*first1 < *m2);
  if (!result) {
    while (first2 != m2 && !(*m1 < *first2))
      first2++;
    first1 = m1;
    iter_swap(first1, first2);
    first1++;
    first2++;
  }
  if ((first1 != last1) && (first2 != last2)) {
    m1 = last1;
    m2 = first2;
    while ((m1 != first1) && (m2 != last2))
      iter_swap(--m1, m2++);
    reverse(first1, m1);
    reverse(first1, last1);
    reverse(m2, last2);
    reverse(first2, last2);
  }
  return !result;
}
bool next_combination(it first, it middle, it last) {
  return next_combination(first, middle, middle, last);
}
/*
n=5,r=3;
a=0 1 1 2 3
011 012 013 023 112 113 123
*/
// combination (begin,begin+r,end)
#define int ll
ll sto_l(string s) {
  ll t = 0;
  rep(i, s.size()) {
    t *= 10;
    t += s[i] - '0';
  }
  return t;
}
signed main() {
  string s;
  cin >> s;
  ll n = s.size();
  ll q = sto_l(s);
  vi a(10);
  rep(i, 10) a[i] = i;
  ll out = -1, ans = -1;
  do {
    do {
      ll t = 0;
      rep(i, n) {
        t *= 10;
        t += a[i];
      }
      ll sa = min(abs(q - t), ll(pow(10, n)) - abs(q - t));
      if (out < sa) {
        out = sa;
        ans = t;
      } else if (out == sa)
        ans = min(ans, t);
    } while (next_permutation(a.begin(), a.begin() + n));
  } while (next_combination(a.begin(), a.begin() + n, a.end()));
  string b = to_string(ans);
  while (b.size() != n)
    b = "0" + b;
  cout << b << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<double,int,double> tp;
// typedef vector<tp> vt;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e9;
const ll INF = 2e18;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int in[10][10];
#define it vector<int>::iterator
bool next_combination(it first1, it last1, it first2, it last2) {
  if ((first1 == last1) || (first2 == last2))
    return false;
  it m1 = last1, m2 = last2;
  m2--;
  while (--m1 != first1 && !(*m1 < *m2))
    ;
  bool result = (m1 == first1) && !(*first1 < *m2);
  if (!result) {
    while (first2 != m2 && !(*m1 < *first2))
      first2++;
    first1 = m1;
    iter_swap(first1, first2);
    first1++;
    first2++;
  }
  if ((first1 != last1) && (first2 != last2)) {
    m1 = last1;
    m2 = first2;
    while ((m1 != first1) && (m2 != last2))
      iter_swap(--m1, m2++);
    reverse(first1, m1);
    reverse(first1, last1);
    reverse(m2, last2);
    reverse(first2, last2);
  }
  return !result;
}
bool next_combination(it first, it middle, it last) {
  return next_combination(first, middle, middle, last);
}
/*
n=5,r=3;
a=0 1 1 2 3
011 012 013 023 112 113 123
*/
// combination (begin,begin+r,end)
int main() {
  string s;
  cin >> s;
  int n = s.size();
  int q = stoi(s);
  vi a(10);
  rep(i, 10) a[i] = i;
  int out = -1, ans = -1;
  do {
    do {
      int t = 0;
      rep(i, n) {
        t *= 10;
        t += a[i];
      }
      int sa = min(abs(q - t), int(pow(10, n)) - abs(q - t));
      if (out < sa) {
        out = sa;
        ans = t;
      } else if (out == sa)
        ans = min(ans, t);
    } while (next_permutation(a.begin(), a.begin() + n));
  } while (next_combination(a.begin(), a.begin() + n, a.end()));
  printf("%0*d\\n", n, ans);
}