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

#include <bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int, int>
#define ll long long
#define y1 shinkle
#define fi first
#define se second
#define bg begin
cs int RLEN = 1 << 20 | 1;
inline char gc() {
  static char ibuf[RLEN], *ib, *ob;
  (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));
  return (ib == ob) ? EOF : *ib++;
}
#define gc getchar
inline int read() {
  char ch = gc();
  int res = 0;
  bool f = 1;
  while (!isdigit(ch))
    f ^= ch == '-', ch = gc();
  while (isdigit(ch))
    res = (res * 10) + (ch ^ 48), ch = gc();
  return f ? res : -res;
}
inline ll readll() {
  char ch = gc();
  ll res = 0;
  bool f = 1;
  while (!isdigit(ch))
    f ^= ch == '-', ch = gc();
  while (isdigit(ch))
    res = (res * 10) + (ch ^ 48), ch = gc();
  return f ? res : -res;
}
inline char readchar() {
  char ch = gc();
  while (isspace(ch))
    ch = gc();
  return ch;
}
inline int readstring(char *s) {
  int top = 0;
  char ch = gc();
  while (isspace(ch))
    ch = gc();
  while (!isspace(ch) && ch != EOF)
    s[++top] = ch, ch = gc();
  s[top + 1] = '\\0';
  return top;
}
template <typename tp> inline void chemx(tp &a, tp b) { a = max(a, b); }
template <typename tp> inline void chemn(tp &a, tp b) { a = min(a, b); }
ll a, b, c, d;
map<ll, ll> mp;
ll dfs(ll n) {
  if (n == 0)
    return 0;
  if (mp.count(n))
    return mp[n];
  ll res = 6e18;
  if (res / n >= d)
    chemn(res, n * d);
  if (n == 1)
    return res;
  ll k = n / 2 * 2;
  chemn(res, dfs(n / 2) + a + d * (n - k));
  if (n > 2)
    chemn(res, dfs(n / 2 + 1) + a + d * (k + 2 - n));
  k = n / 3 * 3;
  chemn(res, dfs(n / 3) + b + d * (n - k));
  chemn(res, dfs(n / 3 + 1) + b + d * (k + 3 - n));
  k = n / 5 * 5;
  chemn(res, dfs(n / 5) + c + d * (n - k));
  chemn(res, dfs(n / 5 + 1) + c + d * (k + 5 - n));
  return mp[n] = res;
}
void solve() {
  ll x = readll();
  mp.clear();
  a = readll(), b = readll(), c = readll(), d = readll();
  cout << dfs(x) << '\\n';
}
int main() {
  int T = read();
  while (T--)
    solve();
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int, int>
#define ll long long
#define y1 shinkle
#define fi first
#define se second
#define bg begin
cs int RLEN = 1 << 20 | 1;
inline char gc() {
  static char ibuf[RLEN], *ib, *ob;
  (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));
  return (ib == ob) ? EOF : *ib++;
}
inline int read() {
  char ch = gc();
  int res = 0;
  bool f = 1;
  while (!isdigit(ch))
    f ^= ch == '-', ch = gc();
  while (isdigit(ch))
    res = (res * 10) + (ch ^ 48), ch = gc();
  return f ? res : -res;
}
inline ll readll() {
  char ch = gc();
  ll res = 0;
  bool f = 1;
  while (!isdigit(ch))
    f ^= ch == '-', ch = gc();
  while (isdigit(ch))
    res = (res * 10) + (ch ^ 48), ch = gc();
  return f ? res : -res;
}
inline char readchar() {
  char ch = gc();
  while (isspace(ch))
    ch = gc();
  return ch;
}
inline int readstring(char *s) {
  int top = 0;
  char ch = gc();
  while (isspace(ch))
    ch = gc();
  while (!isspace(ch) && ch != EOF)
    s[++top] = ch, ch = gc();
  s[top + 1] = '\\0';
  return top;
}
template <typename tp> inline void chemx(tp &a, tp b) { a = max(a, b); }
template <typename tp> inline void chemn(tp &a, tp b) { a = min(a, b); }
cs int N = 505;
int d[N][N], v[N][N];
int p[N * N], n;
queue<pii> q;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
inline void update() {
  while (q.size()) {
    int x = q.front().fi, y = q.front().se;
    q.pop();
    for (int t = 0; t < 4; t++) {
      int px = x + dx[t], py = y + dy[t];
      if (px < 1 || px > n || py < 1 || py > n)
        continue;
      if (d[px][py] > d[x][y] + v[px][py]) {
        d[px][py] = d[x][y] + v[px][py];
        q.push(pii(px, py));
      }
    }
  }
}
int main() {
#ifdef Stargazer
  freopen("lx.in", "r", stdin);
#endif
  n = read();
  int res = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      d[i][j] = min(i, min(n - i + 1, min(j, n - j + 1))), v[i][j] = 1;
  for (int tt = 1; tt <= n * n; tt++) {
    int id = read(), x = (id - 1) / n + 1, y = id - (x - 1) * n;
    res += d[x][y] - 1;
    v[x][y] = 0;
    //	cout<<x<<" "<<y<<" "<<res<<'\\n';
    d[x][y]--;
    q.push(pii(x, y));
    update();
  }
  cout << res << '\\n';
}