alexjercan/bugnet huggingface dataset:
Problem p01812 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define inf 101010;

int n, m, k;
int g[101][101];
int dp[1 << 16];
int a[16];
set<int> s;
map<int, int> ma;
queue<int> que;

void bfs(int cnt) {
  queue<int> q2;
  while (!que.empty()) {
    int bit = que.front();
    que.pop();

    for (int i = 0; i < k; i++) {
      int nbit = 0;
      for (int j = 0; j < m; j++) {
        int b = 1 << j;
        if (bit & b) {
          int t = a[j];
          int u = g[t - 1][i];
          if (s.count(u))
            nbit |= 1 << ma[u];
        }
      }
      if (!nbit) {
        dp[0] = cnt + 1;
        return;
      } else if (dp[nbit] > cnt + 1) {
        dp[nbit] = cnt + 1;
        q2.push(nbit);
      }
    }
  }
  que = q2;
  bfs(cnt + 1);
}

int main(void) {
  cin >> n >> m >> k;
  int bit = 0;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
    s.insert(a[i]);
    ma[a[i]] = i;
    bit |= 1 << i;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      cin >> g[i][j];
    }
  }
  for (int i = 0; i < 1 << m; i++)
    dp[i] = inf;
  que.push(bit);
  bfs(0);
  cout << dp[0] << endl;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
#define inf 101010;

int n, m, k;
int g[101][101];
int dp[1 << 16];
int a[16];
set<int> s;
map<int, int> ma;

void dfs(int bit, int cnt) {
  if (cnt >= dp[bit]) {
    return;
  } else
    dp[bit] = cnt;
  if (!bit) {
    dp[0] = cnt;
    return;
  }

  for (int i = 0; i < k; i++) {
    int nbit = 0;
    for (int j = 0; j < m; j++) {
      int b = 1 << j;
      if (bit & b) {
        int t = a[j];
        int u = g[t - 1][i];
        if (s.count(u))
          nbit |= 1 << ma[u];
      }
    }
    dfs(nbit, cnt + 1);
  }
}

int main(void) {
  cin >> n >> m >> k;
  int bit = 0;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
    s.insert(a[i]);
    ma[a[i]] = i;
    bit |= 1 << i;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      cin >> g[i][j];
    }
  }
  for (int i = 0; i < 1 << m; i++)
    dp[i] = inf;
  dfs(bit, 0);
  cout << dp[0] << endl;
}
Problem p01813 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
using namespace std;

typedef pair<int, int> P;
typedef pair<P, int> P1;

map<P1, int> memo;
string s;

int dfs(int l, int r, int flag) {

  if (memo.count(P1(P(l, r), flag)))
    return memo[P1(P(l, r), flag)];

  int L = l, R = r;

  int cnt = 0;

  for (int i = L; i <= R; i++) {
    if ('0' <= s[i] && s[i] <= '9')
      cnt++;
  }

  if (cnt == 1) {
    if (R - L + 1 == 1)
      return memo[P1(P(L, R), flag)] = s[L] - '0';
    else
      return memo[P1(P(L, R), flag)] = flag == 0 ? -1e9 : 1e9;
  }

  while (s[L] == '(')
    L++;

  while (s[R] == ')')
    R--;

  int res = flag == 0 ? -1e9 : 1e9;

  for (int i = L; i <= R; i++) {

    if (s[i] == '+') {
      int A = dfs(L, i - 1, flag);
      int B = dfs(i + 1, R, flag);
      if (flag == 0)
        res = max(res, A + B);
      else
        res = min(res, A + B);
    }

    if (s[i] == '-') {
      int A = dfs(L, i - 1, flag);
      int B = dfs(i + 1, R, !flag);
      if (flag == 0)
        res = max(res, A - B);
      else
        res = min(res, A - B);
    }
  }

  return memo[P1(P(l, r), flag)] = res;
}

signed main() {

  cin >> s;

  cout << dfs(0, (int)s.size() - 1, 0) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;

typedef pair<string, int> P;

map<P, int> memo;

int dfs(string s, int flag) {

  if (memo.count(P(s, flag)))
    return memo[P(s, flag)];

  int cnt = 0;

  for (int i = 0; i < (int)s.size(); i++) {
    if ('0' <= s[i] && s[i] <= '9')
      cnt++;
  }

  if (cnt == 1) {
    if ((int)s.size() == 1)
      return memo[P(s, flag)] = s[0] - '0';
    else
      return memo[P(s, flag)] = flag == 0 ? -1e9 : 1e9;
  }

  while (s[0] == '(')
    s = s.substr(1);

  while (s[(int)s.size() - 1] == ')')
    s = s.substr(0, (int)s.size() - 1);

  int res = flag == 0 ? -1e9 : 1e9;

  for (int i = 0; i < (int)s.size(); i++) {

    if (s[i] == '+') {
      int A = dfs(s.substr(0, i), flag);
      int B = dfs(s.substr(i + 1), flag);
      if (flag == 0)
        res = max(res, A + B);
      else
        res = min(res, A + B);
    }

    if (s[i] == '-') {
      int A = dfs(s.substr(0, i), flag);
      int B = dfs(s.substr(i + 1), !flag);
      if (flag == 0)
        res = max(res, A - B);
      else
        res = min(res, A - B);
    }
  }

  return memo[P(s, flag)] = res;
}

signed main() {

  string s;
  cin >> s;

  cout << dfs(s, 0) << endl;

  return 0;
}
Problem p01819 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int d[10010][110];
int sum[10010];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  string s;
  rep(i, 0, m) {
    cin >> s;
    rep(j, 0, n) { d[j][i] = (s[j] == 'W' ? 1 : 0); }
  }
  sum[0] = 0;
  rep(i, 0, n) {
    rep(j, 0, m) { sum[i + 1] += d[i][j]; }
  }
  rep(i, 0, n) { sum[i + 1] += sum[i]; }
  int ans = -1, MIN = INF;
  rep(i, 0, n + 1) {
    int left = m * i - sum[i];
    int right = sum[n] - sum[i];
    int tmp = left + right;
    // cout << i << ' ' << left << ' ' << right << endl;
    if (tmp < MIN) {
      MIN = tmp;
      ans = i;
    }
  }
  cout << ans << " " << ans + 1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int d[110][10010];
int sum[110];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  string s;
  rep(i, 0, m) {
    cin >> s;
    rep(j, 0, n) { d[j][i] = (s[j] == 'W' ? 1 : 0); }
  }
  sum[0] = 0;
  rep(i, 0, n) {
    rep(j, 0, m) { sum[i + 1] += d[i][j]; }
  }
  rep(i, 0, n) { sum[i + 1] += sum[i]; }
  int ans = -1, MIN = INF;
  rep(i, 0, n + 1) {
    int left = m * i - sum[i];
    int right = sum[n] - sum[i];
    int tmp = left + right;
    if (tmp < MIN) {
      MIN = tmp;
      ans = i;
    }
  }
  cout << ans << " " << ans + 1 << endl;
}
Problem p01820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define N 3001
using namespace std;
int w, h;
int dx[] = {0, 1, 0, -1}; //^>v<
int dy[] = {-1, 0, 1, 0};
vector<int> X, Y, d;
map<int, int> x, y;

int compless(vector<int> A, map<int, int> &M) {
  sort(A.begin(), A.end());
  int c = 0;
  for (int i = 0; i < A.size(); i++)
    if (!M.count(A[i]))
      M[A[i]] = c++;
  return c;
}

int calc(int y, int x, vector<vector<int>> &mp) {
  vector<vector<bool>> used(h, vector<bool>(w, 0));
  int res = 0;
  while (1) {
    res++;
    int i = mp[y][x];
    used[y][x] = 1;
    while (1) {
      y += dy[i], x += dx[i];
      if (y < 0 || x < 0 || x >= w || y >= h)
        return res;
      if (mp[y][x] != -1 && !used[y][x])
        break;
    }
  }
  assert(0);
}

int main() {
  int n;
  cin >> n;
  X.resize(n), Y.resize(n), d.resize(n);
  for (int i = 0; i < n; i++) {
    char ch;
    cin >> X[i] >> Y[i] >> ch;
    if (ch == '^')
      d[i] = 0;
    if (ch == '>')
      d[i] = 1;
    if (ch == 'v')
      d[i] = 2;
    if (ch == '<')
      d[i] = 3;
  }
  h = compless(Y, y);
  w = compless(X, x);
  vector<vector<int>> mp(h, vector<int>(w, -1));
  for (int i = 0; i < X.size(); i++)
    mp[y[Y[i]]][x[X[i]]] = d[i];

  int ans = 1;
  for (int i = 0; i < n; i++)
    ans = max(ans, calc(y[Y[i]], x[X[i]], mp));
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 3001
using namespace std;
int w, h;
int dx[] = {0, 1, 0, -1}; //^>v<
int dy[] = {-1, 0, 1, 0};
vector<int> X, Y, d;
map<int, int> x, y;

int compless(vector<int> A, map<int, int> &M) {
  sort(A.begin(), A.end());
  int c = 0;
  for (int i = 0; i < A.size(); i++)
    if (!M.count(A[i]))
      M[A[i]] = c++;
  return c;
}

int calc(int y, int x, vector<vector<int>> mp) {
  int res = 0;
  while (1) {
    res++;
    int i = mp[y][x];
    mp[y][x] = -1;
    while (1) {
      y += dy[i];
      x += dx[i];
      if (y < 0 || x < 0 || x >= w || y >= h)
        return res;
      if (mp[y][x] != -1)
        break;
      ;
    }
  }
  assert(0);
}

int main() {
  int n;
  cin >> n;
  X.resize(n), Y.resize(n), d.resize(n);
  for (int i = 0; i < n; i++) {
    char ch;
    cin >> X[i] >> Y[i] >> ch;
    if (ch == '^')
      d[i] = 0;
    if (ch == '>')
      d[i] = 1;
    if (ch == 'v')
      d[i] = 2;
    if (ch == '<')
      d[i] = 3;
  }
  h = compless(Y, y);
  w = compless(X, x);
  vector<vector<int>> mp(h, vector<int>(w, -1));
  for (int i = 0; i < X.size(); i++)
    mp[y[Y[i]]][x[X[i]]] = d[i];
  int ans = 1;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (mp[i][j] != -1)
        ans = max(ans, calc(i, j, mp));
  cout << ans << endl;

  return 0;
}
Problem p01820 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

map<int, set<int>> xx, yy;
map<pint, char> d;
int x[3001], y[3001];
const int INF = 1000100010;
int main() {
  int n;
  cin >> n;
  char c;
  rep(i, n) {
    cin >> x[i] >> y[i] >> c;
    xx[x[i]].insert(y[i]);
    yy[y[i]].insert(x[i]);
    d[pint(x[i], y[i])] = c;
  }
  for (auto it : xx) {
    it.second.insert(INF);
    it.second.insert(-INF);
    yy[it.first].insert(INF);
    yy[it.first].insert(-INF);
  }
  for (auto it : yy) {
    it.second.insert(INF);
    it.second.insert(-INF);
    xx[it.first].insert(INF);
    xx[it.first].insert(-INF);
  }
  int mx = 0;
  rep(i, n) {
    int cnt = 0;
    int tx = x[i], ty = y[i];
    map<int, set<int>> sx = xx, sy = yy;
    while (1) {
      if (sx[tx].empty() || sy[ty].empty())
        break;
      ++cnt;
      sx[tx].erase(ty), sy[ty].erase(tx);
      char tc = d[pint(tx, ty)];
      if (tc == 'v') {
        ty = *sx[tx].lower_bound(ty);
      } else if (tc == '^') {
        auto p = sx[tx].lower_bound(ty);
        if (p == sx[tx].begin())
          break;
        --p;
        ty = *p;
      } else if (tc == '<') {
        auto p = sy[ty].lower_bound(tx);
        if (p == sy[ty].begin())
          break;
        --p;
        tx = *p;

      } else if (tc == '>') {
        tx = *sy[ty].lower_bound(tx);
      } else
        break;
      if (abs(tx) == INF || abs(ty) == INF)
        break;
    }
    mx = max(mx, cnt);
  }
  cout << mx << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

map<int, set<int>> xx, yy;
map<pint, char> d;
int x[3001], y[3001];
const int INF = 1000100010;
int main() {
  int n;
  cin >> n;
  char c;
  rep(i, n) {
    cin >> x[i] >> y[i] >> c;
    xx[x[i]].insert(y[i]);
    yy[y[i]].insert(x[i]);
    d[pint(x[i], y[i])] = c;
  }
  for (auto it : xx) {
    it.second.insert(INF);
    it.second.insert(-INF);
    yy[it.first].insert(INF);
    yy[it.first].insert(-INF);
  }
  for (auto it : yy) {
    it.second.insert(INF);
    it.second.insert(-INF);
    xx[it.first].insert(INF);
    xx[it.first].insert(-INF);
  }
  int mx = 0;
  rep(i, n) {
    int cnt = 0;
    int tx = x[i], ty = y[i];
    map<int, set<int>> sx = xx, sy = yy;
    while (1) {
      ++cnt;
      sx[tx].erase(ty), sy[ty].erase(tx);
      char tc = d[pint(tx, ty)];
      if (tc == 0)
        break;
      if (tc == 'v') {
        ty = *sx[tx].lower_bound(ty);
      } else if (tc == '^') {
        auto p = sx[tx].lower_bound(ty);
        --p;
        ty = *p;
      } else if (tc == '<') {
        auto p = sy[ty].lower_bound(tx);
        --p;
        tx = *p;

      } else {
        tx = *sy[ty].lower_bound(tx);
      }
      if (abs(tx) == INF || abs(ty) == INF)
        break;
    }
    mx = max(mx, cnt);
  }
  cout << mx << endl;
  return 0;
}
Problem p01824 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <set>
#include <tuple>
using namespace std;

typedef tuple<int, int, int> T;

int dx[] = {1, 0, 0, 0, 0, -1};
int dy[] = {0, 0, 1, 0, -1, 0};
int dz[] = {0, 1, 0, -1, 0, 0};

int main() {
  long long a, b, c;
  int n;
  cin >> a >> b >> c >> n;

  long long ans = 2 * (a * b + b * c + c * a);
  set<T> t;
  for (int i = 0; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    t.emplace(x, y, z);
  }

  for (auto i : t) {
    ans += 6;
    int x, y, z;
    tie(x, y, z) = i;

    int cnt = 0;
    if (x == 0)
      ans -= 2;
    if (x == a - 1)
      ans -= 2;
    if (y == 0)
      ans -= 2;
    if (y == b - 1)
      ans -= 2;
    if (z == 0)
      ans -= 2;
    if (z == c - 1)
      ans -= 2;

    for (int j = 0; j < 6; j++) {
      int nx = x + dx[j];
      int ny = y + dy[j];
      int nz = z + dz[j];
      if (t.count(T(nx, ny, nz)))
        ans--;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

string s;
vector<string> st;
void dfs(string t, vector<bool> use) {
  if (s.size() == t.size()) {
    if (t.find("{") == string::npos)
      st.emplace_back(t);
    return;
  }

  char c = s[t.size()];
  if (c == 'a') {
    t += c;
    dfs(t, use);
    t.pop_back();
  }
  if (not use[c - 'a' + 1]) {
    t += c + 1;
    use[c - 'a' + 1] = true;
    dfs(t, use);
    use[c - 'a' + 1] = false;
    t.pop_back();
  }
  if (use[c - 'a']) {
    t += c;
    use[c - 'a'] = true;
    dfs(t, use);
    use[c - 'a'] = false;
    t.pop_back();
  }
}

int main() {
  while (cin >> s, s[0] != '#') {
    st.clear();
    dfs("", vector<bool>(27, 0));
    sort(all(st));

    cout << st.size() << endl;

    if (st.size() <= 10) {
      for (auto t : st) {
        cout << t << endl;
      }
    } else {
      rep(i, 5) { cout << st[i] << endl; }
      rep(i, 5) { cout << st[st.size() - 5 + i] << endl; }
    }
  }
}
Problem p01828 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
#define ll long long
string a, b;
char t[2000];
int main() {
  cin >> a >> b;
  int as = a.size(), bs = b.size();
  int num = as / 2, pos = 0;
  bool ok = 0, ok2;

  /*for(int k=0;k<2;k++){
  int i=0,j=0;
  string tmp="";
  bool f=k;


  while(true){
          if(f){//aからとる
                  if(a[pos]==a[i]){tmp.push_back(a[i]);pos++;f=!f;}
                  i++;
          }

          else if(!f){
                  if(a[pos]==b[j]){tmp.push_back(b[j]);pos++;f=!f;}
                  j++;
          }

          if(j>=bs)break;
          if(tmp.size()==as)break;
  }

          if(tmp==a)ok=1;
  }*/

  for (int k = 0; k < 2; k++) {
    pos = 0;
    rep(l, 2000) t[l] = ' ';

    for (int i = k; i < as; i += 2) {
      t[pos++] = a[i];
    }

    for (int i = 0; i < bs; i++) {
      int c = 0;
      if (b[i] == t[c]) {
        for (int j = 0; j < bs; j++) {
          if (b[j] == t[c])
            c++;
        }
        if (c == pos)
          ok = 1;
      }
    }
  }

  if (ok || as == 1)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
#define ll long long
string a, b;
char t[2000];
int main() {
  cin >> a >> b;
  int as = a.size(), bs = b.size();
  int num = as / 2, pos = 0;
  bool ok = 0, ok2;

  for (int k = 0; k < 2; k++) {
    int i = 0, j = 0;
    string tmp = "";
    bool f = k;

    while (true) {
      if (f && i < as) { // aからとる
        if (a[pos] == a[i]) {
          tmp.push_back(a[i]);
          pos++;
          f = !f;
        }
        i++;
      }

      else if (!f && j < bs) {
        if (a[pos] == b[j]) {
          tmp.push_back(b[j]);
          pos++;
          f = !f;
        }
        j++;
      }
      if (i >= as && j >= bs)
        break;
      if (tmp.size() == as)
        break;
    }

    // cout<<tmp<<endl;
    if (tmp == a)
      ok = 1;
  }
  /*
  for(int k=0;k<2;k++){
  pos=0;
  rep(l,2000)t[l]=' ';

  for(int i=k;i<as;i+=2){
          //if(i>=bs){t[pos++]='@';}
           t[pos++]=a[i];
  }

  rep(j,pos)cout<<t[j]<<" ";cout<<endl;
  for(int i=0;i<bs;i++){
          int c=0;
          if(b[i]==t[c]){
                  for(int j=i;j<bs;j++){
                          if(b[j]==t[c])c++;
                  }
                  if(c==pos)ok=1;
          }

  }
  }
  */
  // if(abs(as-bs)>1)ok=0;
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}
Problem p01828 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string S, T;
  cin >> S >> T;
  string s[2];
  rep(i, S.size()) s[i & 1] += S[i];
  rep(i, 2) {
    int d = 0;
    rep(j, s[i].size()) {
      d = (int)T.find(s[i][j], d) + 1;
      if (!d)
        goto g;
    }
    puts("Yes");
    return 0;
  g:;
  }
  puts("No");
}

Error: TLE

Fixed submission:

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

string S[2];
string s;
int p[2];
void solve(bool b) {
  if (s.size() == S[0].size()) {
    if (s == S[0]) {
      puts("Yes");
      exit(0);
    }
    return;
  }
  for (int i = p[b]; i < S[b].size(); i++) {
    s += S[b][i];
    int d = p[b];
    p[b] = i + 1;
    solve(!b);
    s.pop_back();
    p[b] = d;
  }
}
int main() {
  cin >> S[0] >> S[1];
  p[0] = p[1] = 0;
  solve(0);
  p[0] = p[1] = 0;
  solve(1);
  puts("No");
}
Problem p01829 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;
using lint = long long;

lint ten = 1;
lint M;
lint maxdiff = 0, ans = 0;

void dfs(int N, int used, lint num) {
  if (N == 0) {
    lint diff = min(abs(num - M), ten - abs(num - M));
    if (diff > maxdiff || (diff == maxdiff && num < ans)) {
      ans = num;
      maxdiff = diff;
    }
  } else {
    for (int d = 0; d < 10; ++d) {
      if ((used >> d) & 1)
        continue;
      dfs(N - 1, used | (1 << d), num * 10 + d);
    }
  }
}

int main() {
  string S;
  cin >> S;
  int N = S.size();
  for (int i = 0; i < N; ++i)
    ten *= 10;
  M = stoll(S);

  dfs(N, 0, 0);
  cout << right << setw(N) << setfill('0') << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;
using lint = long long;

lint ten = 1;
lint M;
lint maxdiff = 0, ans = 0;

void dfs(int N, int used, lint num) {
  if (N == 0) {
    lint diff = min(abs(num - M), ten - abs(num - M));
    if (diff > maxdiff || (diff == maxdiff && num < ans)) {
      ans = num;
      maxdiff = diff;
    }
  } else {
    for (int d = 0; d < 10; ++d) {
      if ((used >> d) & 1)
        continue;
      dfs(N - 1, used | (1 << d), num * 10 + d);
    }
  }
}

int main() {
  string S;
  cin >> S;
  int N = S.size();
  for (int i = 0; i < N; ++i)
    ten *= 10;
  M = stoi(S);

  dfs(N, 0, 0);
  cout << right << setw(N) << setfill('0') << ans << endl;
  return 0;
}
Problem p01829 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int change_to_i(string s) {
  stringstream ss;
  int k;
  ss << s;
  ss >> k;
  return k;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int old = change_to_i(s);
  int n = s.size();
  string ans = "";
  int MAX = -1;
  vector<int> a(10);
  rep(i, 0, 10) a[i] = i;
  do {
    string snxt = "";
    rep(i, 0, n) { snxt += '0' + a[i]; }
    int nxt = change_to_i(snxt);
    int diff = min(abs(nxt - old), (int)pow(10, n) - abs(nxt - old));
    if (diff > MAX) {
      MAX = diff;
      ans = snxt;
    }
  } while (next_permutation(all(a)));
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int old = stoi(s);
  int n = s.size();
  string ans = "";
  int MAX = -1;
  vector<int> a(10);
  rep(i, 0, 10) a[i] = i;
  do {
    string snxt = "";
    rep(i, 0, n) { snxt += '0' + a[i]; }
    int nxt = stoi(snxt);
    int diff = min(abs(nxt - old), (int)pow(10, n) - abs(nxt - old));
    if (diff > MAX) {
      MAX = diff;
      ans = snxt;
    }
  } while (next_permutation(all(a)));
  cout << ans << endl;
}