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

#include <bits/stdc++.h>
#define MAX (ll)(1e5 + 7)
#define INF (ll)(1e18)
#define MOD (ll)(998244353)

using namespace std;
using ll = long long;
using cd = complex<double>;

const int x = sqrt(3 * MAX);
int v[3 * MAX], w[3 * MAX], dp[x][MAX];
int main() {
  int n, q;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  int x = sqrt(n);
  for (int i = 0; i <= x; i++) {
    for (int j = 0; j < MAX; j++) {
      dp[i][j] = -MOD;
    }
  }
  dp[0][0] = 0;
  for (int i = 1; i <= x; i++) {
    for (int j = 0; j < MAX; j++) {
      dp[i][j] = max(dp[i / 2][j], dp[i][j]);
      if (j) {
        dp[i][j] = max(dp[i][j], dp[i][j - 1]);
      }
      if (j - w[i] >= 0) {
        dp[i][j] = max(dp[i / 2][j - w[i]] + v[i], dp[i][j]);
      }
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int vv, l;
    cin >> vv >> l;
    vector<int> ve;
    while (vv > x) {
      ve.push_back(vv);
      vv = vv / 2;
    }
    int y = (1 << ve.size()), ma = 0;
    for (int i = 0; i < y; i++) {
      int ww = 0, lx = 0;
      for (int j = 0; j < ve.size(); j++) {
        if ((i >> j) & 1) {
          ww += w[ve[j]], lx += v[ve[j]];
        }
      }
      if (ww <= l) {
        ma = max(lx + dp[vv][l - ww], ma);
      }
    }
    cout << ma << "\\n";
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define MAX (ll)(2e5 + 7)
#define INF (ll)(1e18)
#define MOD (ll)(1e9 + 7)

using namespace std;
using ll = long long;
using cd = complex<double>;
using p = pair<int, int>;
using pp = pair<pair<int, int>, pair<int, pair<int, int>>>;

int dir[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
int h, w, k, x1, yy, x2, y2;

bool chh(int x, int y) { return (x >= 0 && x < h && y >= 0 && y < w); }

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> h >> w >> k >> x1 >> yy >> x2 >> y2;
  int mm[h][w][4];
  bool ch[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      ch[i][j] = (c == '.');
    }
  }
  x1--, yy--, x2--, y2--;
  if (!ch[x1][yy] || !ch[x2][y2]) {
    cout << "-1\\n";
    return 0;
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int x = 0; x < 4; x++) {
        mm[i][j][x] = MOD;
      }
    }
  }
  deque<pp> q;
  for (int i = 0; i < 4; i++) {
    q.push_front(make_pair(make_pair(x1, yy), make_pair(0, make_pair(i, 0))));
  }
  int mx = -1;
  while (q.size()) {
    int x = q.front().first.first, y = q.front().first.second,
        w = q.front().second.first;
    int r = q.front().second.second.second, di = q.front().second.second.first;
    if (x == x2 && y == y2) {
      if (mx == -1) {
        mx = w;
      } else {
        mx = min(w, mx);
      }
    }
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int xx = x + dir[i][0], yy = y + dir[i][1];
      if (chh(xx, yy) && ch[xx][yy]) {
        int ww, rr;
        if (i == di) {
          ww = w + (!r);
          rr = (r + 1) % k;
        } else {
          ww = w + 1;
          rr = 1 % k;
        }
        if (mm[xx][yy][i] > ww) {
          mm[xx][yy][i] = ww;
          if (ww > w) {
            q.push_back(
                make_pair(make_pair(xx, yy), make_pair(ww, make_pair(i, rr))));
          } else {
            q.push_front(
                make_pair(make_pair(xx, yy), make_pair(ww, make_pair(i, rr))));
          }
        }
      }
    }
  }
  cout << mx << "\\n";
}