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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MAX = 1e6;

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int N;
  while (cin >> N, N) {
    vector<int> hp(N);
    for (int i = 0; i < N; i++) {
      cin >> hp[i];
    }
    sort(hp.begin(), hp.end());
    int M;
    cin >> M;
    vector<ll> dp_s(MAX + 1, 1e17);
    vector<ll> dp_a(MAX + 1, 1e17);
    dp_s[0] = dp_a[0] = 0;
    for (int i = 0; i < M; i++) {
      string sp, ta;
      int mp, dm;
      cin >> sp >> mp >> ta >> dm;
      if (ta == "Single") {
        for (int i = 0; i <= MAX; i++) {
          int t = min(MAX, i + dm);
          dp_s[t] = min(dp_s[t], dp_s[i] + mp);
        }
        for (int i = MAX - 1; i >= 0; i--) {
          dp_s[i] = min(dp_s[i + 1], dp_s[i]);
        }
      } else {
        for (int i = 0; i <= MAX; i++) {
          int t = min(MAX, i + dm);
          dp_a[t] = min(dp_a[t], dp_a[i] + mp);
        }
        for (int i = MAX - 1; i >= 0; i--) {
          dp_a[i] = min(dp_a[i + 1], dp_a[i]);
        }
      }
    }
    ll res = LLONG_MAX;
    for (int i = 0; i <= MAX; i++) {
      ll val = dp_a[i];
      for (int j = 0; j < N; j++) {
        val += dp_s[max(0, hp[j] - i)];
      }
      res = min(res, val);
    }
    cout << res << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MAX = 2e6;

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int N;
  while (cin >> N, N) {
    vector<int> hp(N);
    for (int i = 0; i < N; i++) {
      cin >> hp[i];
    }
    sort(hp.begin(), hp.end());
    int M;
    cin >> M;
    vector<ll> dp_s(MAX + 1, 1e17);
    vector<ll> dp_a(MAX + 1, 1e17);
    dp_s[0] = dp_a[0] = 0;
    for (int i = 0; i < M; i++) {
      string sp, ta;
      int mp, dm;
      cin >> sp >> mp >> ta >> dm;
      if (ta == "Single") {
        for (int i = dm; i <= MAX; i++) {
          dp_s[i] = min(dp_s[i], dp_s[i - dm] + mp);
        }
        for (int i = MAX - 1; i >= 0; i--) {
          dp_s[i] = min(dp_s[i + 1], dp_s[i]);
        }
      } else {
        for (int i = dm; i <= MAX; i++) {
          dp_a[i] = min(dp_a[i], dp_a[i - dm] + mp);
        }
        for (int i = MAX - 1; i >= 0; i--) {
          dp_a[i] = min(dp_a[i + 1], dp_a[i]);
        }
      }
    }
    ll res = LLONG_MAX;
    for (int i = 0; i <= MAX; i++) {
      ll val = dp_a[i];
      for (int j = 0; j < N; j++) {
        val += dp_s[max(0, hp[j] - i)];
      }
      res = min(res, val);
    }
    cout << res << endl;
  }
  return 0;
}
Problem p01274 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

const int inf = 1e9;

typedef long long ll;

int n;
int hp[100];
int ms, ma;
int mps[100];
int mpa[100];
int dmgs[100];
int dmga[100];
int dps[2][100001];
int dpa[2][100001];

void calc(int m, int mp[], int dmg[], int dp[][100001]) {
  dp[0][0] = 0;
  fill_n(dp[0] + 1, 100000, inf);
  for (int i = 1; i <= m; ++i) {
    rep(j, 100001) {
      dp[i & 1][j] = dp[(i - 1) & 1][j];
      if (dmg[i - 1]) {
        if (j < dmg[i - 1]) {
          dp[i & 1][j] = min(mp[i - 1], dp[i & 1][j]);
        } else {
          dp[i & 1][j] =
              min(dp[i & 1][j - dmg[i - 1]] + mp[i - 1], dp[i & 1][j]);
        }
      }
    }
  }
}

int main() {
  while (scanf("%d", &n), n) {
    rep(i, n) { scanf("%d", &hp[i]); }
    int m;
    scanf("%d", &m);
    ms = ma = 0;
    rep(i, m) {
      char nm[17];
      int mp;
      char trgt[7];
      int dmg;
      scanf("%s%d%s%d", nm, &mp, trgt, &dmg);
      if (trgt[0] == 'S') {
        mps[ms] = mp;
        dmgs[ms++] = dmg;
      } else {
        mpa[ma] = mp;
        dmga[ma++] = dmg;
      }
    }

    calc(ms, mps, dmgs, dps);
    calc(ma, mpa, dmga, dpa);

    ll ans = inf;
    rep(i, 100001) {
      ll p = dpa[ma & 1][i];
      rep(j, n) {
        if (hp[j] > i) {
          p += dps[ms & 1][hp[j] - i];
        }
      }
      ans = min(p, ans);
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

const int inf = 1e9;

typedef long long ll;

int n;
int hp[100];
int ms, ma;
int mps[100];
int mpa[100];
int dmgs[100];
int dmga[100];
int dps[101][100001];
int dpa[101][100001];

void calc(int m, int mp[], int dmg[], int dp[][100001]) {
  dp[0][0] = 0;
  fill_n(dp[0] + 1, 100000, inf);
  for (int i = 1; i <= m; ++i) {
    rep(j, 100001) {
      dp[i][j] = dp[i - 1][j];
      if (dmg[i - 1]) {
        if (j < dmg[i - 1]) {
          dp[i][j] = min(mp[i - 1], dp[i][j]);
        } else {
          dp[i][j] = min(dp[i][j - dmg[i - 1]] + mp[i - 1], dp[i][j]);
        }
      }
    }
  }
}

int main() {
  while (scanf("%d", &n), n) {
    rep(i, n) { scanf("%d", &hp[i]); }
    int m;
    scanf("%d", &m);
    ms = ma = 0;
    rep(i, m) {
      char nm[17];
      int mp;
      char trgt[7];
      int dmg;
      scanf("%s%d%s%d", nm, &mp, trgt, &dmg);
      if (trgt[0] == 'S') {
        mps[ms] = mp;
        dmgs[ms++] = dmg;
      } else {
        mpa[ma] = mp;
        dmga[ma++] = dmg;
      }
    }

    calc(ms, mps, dmgs, dps);
    calc(ma, mpa, dmga, dpa);

    ll ans = inf;
    rep(i, 100001) {
      ll p = dpa[ma][i];
      rep(j, n) {
        if (hp[j] > i) {
          p += dps[ms][hp[j] - i];
        }
      }
      ans = min(p, ans);
    }
    printf("%lld\\n", ans);
  }
  return 0;
}
Problem p01274 (C++) — buggy submission, verdict: Memory Limit Exceeded

// 19
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    int h[100];
    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    int m;
    cin >> m;
    vector<pair<int, int>> s[2];
    bool f = false;
    while (m--) {
      int m, d;
      char t[7];
      cin.ignore(17, ' ') >> m >> t >> d;
      s[t[0] == 'S'].push_back(make_pair(m, d));
      f |= m == 0 && d;
    }
    long long as[2][100001] = {};
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < s[i].size(); j++) {
        for (int k = 0; k + s[i][j].first <= 100000; k++) {
          as[i][k + s[i][j].first] =
              max(as[i][k + s[i][j].first], as[i][k] + s[i][j].second);
        }
      }
    }
    long long mp = 1LL << 62;
    for (int i = 0; i < mp; i++) {
      long long cm = i;
      for (int j = 0; j < n; j++) {
        cm += lower_bound(as[1], as[2], h[j] - as[0][i]) - as[1];
      }
      mp = min(mp, cm);
    }
    cout << (f ? 0 : mp) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

// 19
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    int h[100];
    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    int m;
    cin >> m;
    vector<pair<int, int>> s[2];
    while (m--) {
      int m, d;
      char t[7];
      cin.ignore(16, ' ') >> m >> t >> d;
      s[t[0] == 'S'].push_back(make_pair(m, d));
    }
    static int as[2][100001];
    fill(as[0], as[2], 0);
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < s[i].size(); j++) {
        for (int k = 0; k + s[i][j].first <= 100000; k++) {
          as[i][k + s[i][j].first] =
              max(as[i][k + s[i][j].first], as[i][k] + s[i][j].second);
        }
      }
    }
    int mp = 1 << 30;
    for (int i = 0; i < mp; i++) {
      int cm = i;
      for (int j = 0; j < n; j++) {
        cm += lower_bound(as[1], as[2], h[j] - as[0][i]) - as[1];
      }
      mp = min(mp, cm);
    }
    cout << mp << endl;
  }
  return 0;
}
Problem p01274 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define INF 1e9
#define f first
#define s second
using namespace std;
typedef pair<int, int> P;
int dp[2][100010];

int main() {
  int n, m, hp[101];

  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> hp[i];

    vector<P> mg[2];
    cin >> m;
    for (int i = 0, cost, dam; i < m; i++) {
      string name, type;
      cin >> name >> cost >> type >> dam;
      mg[type == "All"].push_back(P(dam, cost));
    }

    for (int i = 0; i < 2; i++)
      for (int j = 1; j < 100010; j++)
        dp[i][j] = INF;

    for (int k = 0; k < 2; k++)
      for (int i = 0; i < mg[k].size(); i++)
        for (int j = mg[k][i].f; j < 100000 + mg[k][i].f; j++)
          dp[k][min(100000, j)] =
              min(dp[k][min(100000, j)],
                  dp[k][min(100000, j - mg[k][i].f)] + mg[k][i].s);

    for (int i = 0; i < 2; i++)
      for (int j = 100000; j >= 0; j--)
        dp[i][j] = min(dp[i][j], dp[i][j + 1]);

    long long ans = INF;
    for (int i = 0; i <= 100000; i++) {
      long long cost = dp[1][i];
      for (int j = 0; j < n; j++)
        cost += dp[0][max(0, hp[j] - i)];
      ans = min(ans, cost);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 1e9
#define f first
#define s second
using namespace std;
typedef pair<int, int> P;
int dp[2][100010];

int main() {

  int n, m, hp[101];
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> hp[i];

    vector<P> mg[2];
    cin >> m;
    for (int i = 0, cost, dam; i < m; i++) {
      string name, type;
      cin >> name >> cost >> type >> dam;
      mg[type == "All"].push_back(P(dam, cost));
    }

    for (int i = 0; i < 2; i++)
      for (int j = 1; j < 100010; j++)
        dp[i][j] = INF;

    for (int k = 0; k < 2; k++)
      for (int i = 0; i < (int)mg[k].size(); i++)
        for (int j = mg[k][i].f; j <= 100000 + mg[k][i].f; j++) {
          dp[k][min(100000, j)] =
              min(dp[k][min(100000, j)],
                  dp[k][min(100000, j - mg[k][i].f)] + mg[k][i].s);
        }

    for (int i = 0; i < 2; i++)
      for (int j = 100000; j >= 0; j--)
        dp[i][j] = min(dp[i][j], dp[i][j + 1]);

    int ans = INF;
    for (int i = 0; i < 100000; i++) {
      int cost = dp[1][i];
      for (int j = 0; j < n; j++) {
        assert(dp[0][max(0, hp[j] - i)] != INF);
        cost += dp[0][max(0, hp[j] - i)];
      }
      ans = min(ans, cost);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF 1e9
#define f first
#define s second
#define N 100000
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

int main() {
  int n, m, hp[101];

  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> hp[i];

    vector<P> mg[2];
    cin >> m;
    for (int i = 0, cost, dam; i < m; i++) {
      string name, type;
      cin >> name >> cost >> type >> dam;
      if (dam)
        mg[type == "All"].push_back(P(dam, cost));
    }

    int dp[2][N + 10] = {};
    for (int i = 0; i < 2; i++)
      for (int j = 1; j < N + 10; j++)
        dp[i][j] = INF;

    for (int k = 0; k < 2; k++)
      for (int i = 0; i < mg[k].size(); i++)
        for (int j = mg[k][i].f; j <= (N / mg[k][i].f + 1) * mg[k][i].f; j++)
          dp[k][min(N, j)] =
              min(dp[k][min(N, j)], dp[k][j - mg[k][i].f] + mg[k][i].s);

    for (int i = 0; i < 2; i++)
      for (int j = N; j >= 0; j--)
        dp[i][j] = min(dp[i][j], dp[i][j + 1]);

    ll ans = INF;
    for (int i = 0; i <= N; i++) {
      ll cost = dp[1][i];
      for (int j = 0; j < n; j++)
        cost += dp[0][max(0, hp[j] - i)];
      ans = min(ans, cost);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1e9
#define f first
#define s second
#define N 100000
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

int main() {
  int n, m, hp[101];

  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> hp[i];

    vector<P> mg[2];
    cin >> m;
    for (int i = 0, cost, dam; i < m; i++) {
      string name, type;
      cin >> name >> cost >> type >> dam;
      if (dam)
        mg[type == "All"].push_back(P(dam, cost));
    }

    int dp[2][N + 10] = {};
    for (int i = 0; i < 2; i++)
      for (int j = 1; j < N + 10; j++)
        dp[i][j] = INF;

    for (int k = 0; k < 2; k++)
      for (int i = 0; i < mg[k].size(); i++)
        for (int j = mg[k][i].f; j <= (N / mg[k][i].f + 1) * mg[k][i].f; j++) {
          dp[k][min(N, j)] =
              min(dp[k][min(N, j)], dp[k][j - mg[k][i].f] + mg[k][i].s);
          if (N < j)
            cout << j << endl;
        }
    for (int i = 0; i < 2; i++)
      for (int j = N; j >= 0; j--)
        dp[i][j] = min(dp[i][j], dp[i][j + 1]);

    ll ans = INF;
    for (int i = 0; i <= N; i++) {
      ll cost = dp[1][i];
      for (int j = 0; j < n; j++)
        cost += dp[0][max(0, hp[j] - i)];
      ans = min(ans, cost);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01275 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define INF INT_MAX / 2

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef string Node;
typedef pair<Node, int> pni;

struct Comp {
  bool operator()(pni a, pni b) { return a.second > b.second; }
};

string rot(string a, int from, int to, int n) {
  rep(i, from, to + 1) {
    int j = a[i] - '0';
    j = (j + n + 10) % 10;
    a[i] = '0' + j;
  }

  return a;
}

int solve(int k) {
  string start, end;
  cin >> start >> end;

  priority_queue<pni, vector<pni>, Comp> que;
  map<Node, int> dist;
  set<Node> done;

  que.push(pni(start, 0));
  while (!que.empty()) {
    Node n = que.top().first;
    int c = que.top().second;
    que.pop();

    if (done.find(n) != done.end())
      continue;
    done.insert(n);

    if (n == end)
      return c;

    int from = 0;
    while (n[from] == end[from])
      from++;
    int delta = (end[from] - '0') - (n[from] - '0');
    rep(to, from, k) {
      Node nnode = rot(n, from, to, delta);
      int nc = c + 1;
      if (dist.find(nnode) == dist.end())
        dist[nnode] = INF;
      if (nc < dist[nnode]) {
        dist[nnode] = nc;
        que.push(pni(nnode, nc));
      }
    }
  }
}

int main() {
  while (1) {
    int k;
    cin >> k;
    if (k == 0)
      return 0;
    cout << solve(k) << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define INF INT_MAX / 2

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef string Node;
typedef pair<Node, int> pni;

struct Comp {
  bool operator()(pni a, pni b) { return a.second > b.second; }
};

string rot(string a, int from, int to, int n) {
  rep(i, from, to + 1) {
    int j = a[i] - '0';
    j = (j + n + 10) % 10;
    a[i] = '0' + j;
  }

  return a;
}

int solve(int k) {
  string start, end;
  cin >> start >> end;

  priority_queue<pni, vector<pni>, Comp> que;
  map<Node, int> dist;
  set<Node> done;

  que.push(pni(start, 0));
  while (!que.empty()) {
    Node n = que.top().first;
    int c = que.top().second;
    que.pop();

    if (done.find(n) != done.end())
      continue;
    done.insert(n);

    if (n == end)
      return c;

    int from = 0;
    while (n[from] == end[from])
      from++;

    rep(i, from, k) rep(j, i, k) rep(jj, -9, 10) {
      if (jj == 0)
        continue;
      string nnode = rot(n, i, j, jj);
      int nc = c + 1;
      if (dist.find(nnode) == dist.end())
        dist[nnode] = INF;
      if (nc < dist[nnode]) {
        dist[nnode] = nc;
        que.push(pni(nnode, nc));
      }
    }
  }
}

int main() {
  while (1) {
    int k;
    cin >> k;
    if (k == 0)
      return 0;
    cout << solve(k) << endl;
  }
}
Problem p01275 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int bfs(string &S, string &T) {
  queue<string> que;
  map<string, int> v;
  que.push(S);
  v[S] = 0;
  while (!que.empty()) {
    string &p = que.front();
    int now = v[p];
    if (p == T)
      return (now);
    int match = 0;
    while (p[match] == T[match])
      ++match;
    int ptrr = (T[match] - p[match] + 10) % 10;
    for (int j = match; j < p.size(); j++) {
      (p[j] += ptrr) %= 10;
      if (v.find(p) == v.end()) {
        v[p] = now + 1;
        que.push(p);
      }
    }
    que.pop();
  }
}
int main() {
  int K;
  while (cin >> K, K) {
    string S, T;
    cin >> S >> T;
    for (int i = 0; i < K; i++) {
      S[i] -= '0';
      T[i] -= '0';
    }
    cout << bfs(S, T) << endl;
  }
}

Error: MLE

Fixed submission:

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

int bfs(string &S, string &T) {
  queue<string> que;
  map<string, int> v;
  que.push(S);
  v[S] = 0;
  while (!que.empty()) {
    auto &p = que.front();
    if (p == T)
      return (v[p]);
    for (int i = 0; i < p.size(); i++) {
      for (int j = i; j < p.size(); j++) {
        string q = p;
        for (int l = 1; l < 10; l++) {
          for (int k = i; k <= j; k++)
            (q[k] += 1) %= 10;
          if (v.find(q) == v.end()) {
            v[q] = v[p] + 1;
            que.push(q);
          }
        }
      }
    }
    que.pop();
  }
}
int main() {
  int K;
  while (cin >> K, K) {
    string S, T;
    cin >> S >> T;
    for (int i = 0; i < K; i++) {
      S[i] -= '0';
      T[i] -= '0';
    }
    cout << bfs(S, T) << endl;
  }
}
Problem p01275 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define for_(i, a, b) for (int i = a; i < b; ++i)

int k;
string S, T;

char add(char c, int x) {
  int y = c - '0';
  return char('0' + (y + x) % 10);
}

void solve() {
  map<string, int> cst;
  cst[S] = 0;

  queue<string> q;
  q.push(S);

  while (!q.empty()) {
    string dial = q.front();
    q.pop();
    int cnt = cst[dial];

    if (dial == T) {
      cout << cnt << endl;
      break;
    }

    for_(i, 0, k) if (dial[i] != T[i]) {
      int dif = (T[i] - dial[i] + 10) % 10;

      for_(j, i, k) {
        dial[j] = add(dial[j], dif);
        if (!cst.count(dial)) {
          q.push(dial);
          cst[dial] = cnt + 1;
        }
      }

      break;
    }
  }
}

int main() {
  while (cin >> k, k) {
    cin >> S >> T;
    solve();
  }

  return 0;
}

Error: MLE

Fixed submission:

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

#define for_(i, a, b) for (int i = a; i < b; ++i)

struct State {
  string dial;
  int cnt;
  State(string _d, int _c) : dial(_d), cnt(_c) {}
};

int k;
string S, T;

char add(char c, int x) {
  int y = c - '0';
  return char('0' + (y + x) % 10);
}

void solve() {
  set<string> vis;
  vis.insert(S);

  queue<State> q;
  q.push(State(S, 0));

  while (!q.empty()) {
    State s = q.front();
    q.pop();

    if (s.dial == T) {
      cout << s.cnt << endl;
      break;
    }

    for_(i, 0, k) if (s.dial[i] != T[i]) {
      int dif = (T[i] - s.dial[i] + 10) % 10;

      for_(j, i, k) {
        s.dial[j] = add(s.dial[j], dif);
        if (vis.find(s.dial) == vis.end())
          q.push(State(s.dial, s.cnt + 1));
      }

      break;
    }
  }
}

int main() {
  while (cin >> k, k) {
    cin >> S >> T;
    solve();
  }

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

#include <iostream>
#include <vector>
using namespace std;

int solve(vector<int> D, vector<int> imos, int i, int d) {
  int n = D.size();
  if (i == n)
    return 0;
  d += imos[i];
  D[i] += 10 - d;
  D[i] %= 10;
  int ret = 1e9;
  if (D[i]) {
    for (int j = i + 1; j <= n; ++j) {
      imos[j] -= D[i];
      ret = min(ret, 1 + solve(D, imos, i + 1, (d + D[i]) % 10));
      imos[j] += D[i];
    }
  } else {
    ret = min(ret, solve(D, imos, i + 1, d));
  }
  return ret;
}

int main() {
  int N;
  while (cin >> N, N) {
    string S, T;
    cin >> S >> T;
    vector<int> D(N);
    for (int i = 0; i < N; ++i) {
      D[i] = (T[i] - S[i] + 10) % 10;
    }
    cout << solve(D, vector<int>(N + 1, 0), 0, 0) << endl;
  }
  return 0;
}


Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int solve(vector<int> D, vector<int> imos, int i, int d) {
  int n = D.size();
  if (i == n)
    return 0;
  d += imos[i];
  D[i] += 10 - d;
  D[i] %= 10;
  int ret = 1e9;
  if (D[i]) {
    for (int j = i + 1; j <= n; ++j) {
      imos[j] -= D[i];
      ret = min(ret, 1 + solve(D, imos, i + 1, (d + D[i]) % 10));
      imos[j] += D[i];
    }
  } else {
    ret = min(ret, solve(D, imos, i + 1, d));
  }
  return ret;
}

int main() {
  int N;
  while (cin >> N, N) {
    string S, T;
    cin >> S >> T;
    vector<int> D(N);
    for (int i = 0; i < N; ++i) {
      D[i] = (T[i] - S[i] + 10) % 10;
    }
    cout << solve(D, vector<int>(N, 0), 0, 0) << endl;
  }
  return 0;
}
Problem p01275 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int k;
string cur, unlocking;
int minCnt;

void dfs(int idx, int cnt) {
  if (idx == k) {
    minCnt = min(minCnt, cnt);
    return;
  }
  if (minCnt <= cnt)
    return;
  string tmp = cur;
  // ツづつアツづ慊づ連ツ督ョツつウツつケツづゥツつゥツづ湘ェツ債づュツつッ
  for (int j = idx; j < k; j++) {
    bool isDec = false;
    // diffツ閉ェツつセツつッツ嘉アツ転
    int diff = cur[idx] - unlocking[idx];
    if (abs(diff) != 0) {
      // ツ嘉アツ転ツつウツつケツづゥ
      for (int i = idx; i <= j; i++)
        cur[i] = (cur[i] - '0' - diff + 10) % 10 + '0';
      // rec
      dfs(idx + 1, cnt + 1);
      cur = tmp;
    } else
      dfs(idx + 1, cnt);
  }
}

int main() {
  while (cin >> k && k != 0) {
    minCnt = 10000;
    cin >> cur >> unlocking;
    dfs(0, 0);
    cout << minCnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int k;
string cur, unlocking;
int minCnt;

void dfs(int idx, int cnt) {
  if (idx == k) {
    minCnt = min(minCnt, cnt);
    return;
  }
  string tmp = cur;
  // ツづつアツづ慊づ連ツ督ョツつウツつケツづゥツつゥツづ湘ェツ債づュツつッ
  for (int j = idx; j < k; j++) {
    bool isDec = false;
    // diffツ閉ェツつセツつッツ嘉アツ転
    int diff = cur[idx] - unlocking[idx];
    if (abs(diff) != 0) {
      // ツ嘉アツ転ツつウツつケツづゥ
      for (int i = idx; i <= j; i++)
        cur[i] = (cur[i] - '0' - diff) % 10 + '0';
      // rec
      dfs(idx + 1, cnt + 1);
      cur = tmp;
    } else {
      dfs(idx + 1, cnt);
    }
  }
}

int main() {
  while (cin >> k && k != 0) {
    minCnt = 10000;
    cin >> cur >> unlocking;
    dfs(0, 0);
    cout << minCnt << endl;
  }

  return 0;
}