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

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(), x.end()
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T> T gcd(T a, T b) { return !b ? a : gcd(b, a % b); }
template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; }
template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; }
mt19937 R(chrono::system_clock().now().time_since_epoch().count());

const int N = 1 << 18 | 3, B = 11;
struct node {
  int id, u, l;
};
int n, v[N], w[N], q, ans[N], sz, len, a[N];
vector<node> que[N];
Pii b[N];

void dfs2(int id, int dep, int x, int y) {
  if (x < 0)
    return;
  if (dep > sz) {
    umax(ans[id], y);
    return;
  }
  dfs2(id, dep + 1, x, y);
  dfs2(id, dep + 1, x - w[a[dep]], y + v[a[dep]]);
}
void dfs1(int id, int dep, int x, int y) {
  if (x < b[1].fi)
    return;
  if (dep > sz) {
    umax(ans[id],
         y + b[upper_bound(b + 1, b + len + 1, mp(x, 0x3f3f3f3f)) - b - 1].se);
    return;
  }
  dfs1(id, dep + 1, x, y);
  dfs1(id, dep + 1, x - w[a[dep]], y + v[a[dep]]);
}
void dfs(int dep, int x, int y) {
  if (dep > sz) {
    b[++len] = mp(x, y);
    return;
  }
  dfs(dep + 1, x, y);
  dfs(dep + 1, x + w[a[dep]], y + v[a[dep]]);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i] >> w[i];
  }
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    int u, l;
    cin >> u >> l;
    if (u < (1 << B)) {
      sz = 0;
      for (; u; u >>= 1) {
        a[++sz] = u;
        // dfs2(i, 1, l, 0);
      }
      dfs2(i, 1, l, 0);
    } else {
      int x = u;
      while (x >= (1 << B)) {
        x >>= 1;
        // que[x].pb((node){i,u,l});
      }
      que[x].pb((node){i, u, l});
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (SZ(que[i])) {
      sz = len = 0;
      for (int u = i; u; u >>= 1) {
        a[++sz] = u;
        // dfs(1, 0, 0);
        // sort(b+1,b+len+1);
      }
      dfs(1, 0, 0);
      sort(b + 1, b + len + 1);
      for (int j = 2; j <= len; ++j) {
        umax(b[j].se, b[j - 1].se);
      }
      for (auto q : que[i]) {
        sz = 0;
        for (int u = q.u; u > i; u >>= 1) {
          a[++sz] = u;
          // dfs1(q.id, 1, q.l, 0);
        }
        dfs1(q.id, 1, q.l, 0);
      }
    }
  }
  for (int i = 1; i <= q; ++i) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
const int maxSize = 4000;
int amount, queryNumbers;
vector<vector<int>> tree(maxSize, vector<int>(maxSize));
vector<tuple<int, int, int>> values(maxSize);
vector<tuple<int, int>> weights(maxSize);
vector<int> sumValues(maxSize);
vector<int> maxValues(maxSize);
void fillingValuesAndWeights() {
  for (int i = 0; i < (int)values.size(); ++i) {
    values[i] = make_tuple(0, 0, 0);
  }
  for (int i = 0; i < (int)weights.size(); ++i) {
    weights[i] = make_tuple(0, 0);
  }
  return;
}
void fillingValues() {
  int pointValue, pointWeight;
  cin >> amount;
  for (int point = 1; point <= amount; ++point) {
    cin >> pointValue >> pointWeight;
    values[point - 1] = make_tuple(point, pointValue, pointWeight);
  }
  return;
}
void fillingWeights() {
  int borderPoint, borderWeight;
  cin >> queryNumbers;
  for (int i = 1; i <= queryNumbers; ++i) {
    cin >> borderPoint >> borderWeight;
    weights[i - 1] = make_tuple(borderPoint, borderWeight);
  }
  return;
}
/*
void creatingTree(int root)
{
        if(root>amount)
        {
                return;
        }
        tree[root].push_back(2*root);
        tree[root].push_back(2*root+1);
        tree[2*root].push_back(root);
        tree[2*root+1].push_back(root);
        creatingTree(2*root);
        creatingTree(2*root+1);
        return;
}
*/
int rootWeight = -1, rootValue = -1;
void dfs(int root, int borderWeight) {
  if (root == 0) {
    maxValues.push_back(*max_element(sumValues.begin(), sumValues.end()));
    /*
    cout<<maxValues[index]<<endl;
    cout<<"maxValues <- ";
    for(int i=0; i<maxValues.size(); ++i)
    {
            if(maxValues[i]>0)
            {
                    cout<<maxValues[i]<<endl;
            }
    }
    cout<<endl;
    cout<<"sumValues <- ";
    for(int i=0; i<sumValues.size(); ++i)
    {
            if(sumValues[i]>200)
            {
                    cout<<sumValues[i]<<", ";
            }
    }
    cout<<endl;
    */
    for (int i = 0; i < (int)sumValues.size(); ++i) {
      sumValues[i] = 0;
    }
    return;
  }
  for (int i = 0; i < (int)values.size(); ++i) {
    if ((get<0>(values[i])) == root) {
      rootValue = get<1>(values[i]);
      rootWeight = get<2>(values[i]);
    }
  }
  int rootCopy = root;
  while (rootCopy != 0) {
    for (int i = 0; i < (int)values.size(); ++i) {
      if (get<0>(values[i]) == rootCopy / 2) {
        if ((rootWeight + get<2>(values[i])) <= borderWeight) {
          sumValues.insert(sumValues.begin() + i,
                           (rootValue + get<1>(values[i])));
        }
      }
    }
    rootCopy = (rootCopy / 2);
  }
  dfs(root / 2, borderWeight);
  return;
}
void usingDfs() {
  for (int i = 0; i < (int)weights.size(); ++i) {
    // cout<<"Before command-if <- "<<i<<", ";
    if (get<0>(weights[i]) > 0 && get<1>(weights[i]) > 0) {
      // cout<<"After command-if <- "<<i<<", ";
      dfs(get<0>(weights[i]), get<1>(weights[i]));
    } else {
      break;
    }
  }
  /*
  dfs(11, 227, 0);
  dfs(12, 240, 0);
  dfs(13, 237, 0);
  dfs(14, 206, 0);
  */
  for (int i = 0; i < (int)maxValues.size(); ++i) {
    cout << maxValues[i] << endl;
  }
  return;
}
template <typename Type> void showContentTuple2(Type input) {
  for (int i = 0; i < (int)input.size(); ++i) {
    if ((get<0>(input[i])) > 0 || (get<1>(input[i])) > 0) {
      cout << get<0>(input[i]) << " " << get<1>(input[i]) << endl;
    }
  }
  return;
}
int main() {
  fillingValuesAndWeights();
  fillingValues();
  fillingWeights();
  // creatingTree(1);
  usingDfs();
  cout << endl;
  // showContentTuple2(weights);
  return 0;
}