Problem p03560 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mkp make_pair
#define nl cout << endl
#define MOD 1000000007
#define loop(i, start, end) for (ull i = start; i < end; i++)
#define N 100001
#define INF 1e12
#define oa(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  nl
#define ov(a)                                                                  \
  for (int i = 0; i < a.size(); i++)                                           \
    cout << a[i] << " ";                                                       \
  nl
ll dp[100][N];
int main() {
  fastio();
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  ll s = 0, d;
  cin >> d;
  ll tem = d;
  while (tem > 0) {
    s += tem % 10;
    tem /= 10;
  }
  loop(i, 0, s + 1) { loop(j, 0, d + 1) dp[i][j] = INF; }
  queue<pair<int, int>> q;
  q.push({0, 0});
  dp[0][0] = 0;
  int ans = s;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 10; i++) {
      int xx = x + i;
      int yy = (y * 10 + i) % d;
      if (xx > ans)
        continue;
      if (dp[xx][yy] <= dp[x][y] + 1)
        continue;
      if (yy == 0)
        ans = min(xx, ans);
      dp[xx][yy] = dp[x][y] + 1;
      q.push({xx, yy});
    }
  }
  cout << ans << endl;

  nl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mkp make_pair
#define nl cout << endl
#define MOD 1000000007
#define loop(i, start, end) for (ull i = start; i < end; i++)
#define N 100001
#define INF 1e12
#define oa(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  nl
#define ov(a)                                                                  \
  for (int i = 0; i < a.size(); i++)                                           \
    cout << a[i] << " ";                                                       \
  nl
ll dp[100][N];
int main() {
  fastio();
  // #ifndef ONLINE_JUDGE
  // 	freopen("in.txt","r",stdin);
  // 	freopen("out.txt","w",stdout);
  // #endif

  ll s = 0, d;
  cin >> d;
  ll tem = d;
  while (tem > 0) {
    s += tem % 10;
    tem /= 10;
  }
  loop(i, 0, s + 1) { loop(j, 0, d + 1) dp[i][j] = INF; }
  queue<pair<int, int>> q;
  q.push({0, 0});
  dp[0][0] = 0;
  int ans = s;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 10; i++) {
      int xx = x + i;
      int yy = (y * 10 + i) % d;
      if (xx > ans)
        continue;
      if (dp[xx][yy] <= dp[x][y] + 1)
        continue;
      if (yy == 0)
        ans = min(xx, ans);
      dp[xx][yy] = dp[x][y] + 1;
      q.push({xx, yy});
    }
  }
  cout << ans << endl;

  nl;

  return 0;
}