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

#include <stdio.h>
int n, q;
long long p = 10000000;
long long v[1024 * 256 + 1];
long long w[1024 * 256 + 1];
long long u[100000];
long long l[100000];
int s[1024];
int ss, uu;
long long e[1024];
long long ee[1024];
long long ef[1024];
long long d[1024][1024];
long long te, ans;
int ar, br;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", v + i, w + i);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%lld%lld", u + i, l + i);
  }
  d[1][0] = 0;
  d[1][1] = w[1] * p + v[1];
  s[1] = 2;
  for (int i = 2; i < 1024; i++) {
    te = w[i] * p + v[i];
    ss = s[i / 2];
    for (int j = 0; j < s[i / 2]; j++) {
      e[j] = d[i / 2][j] + te;
    }
    ar = 0, br = 0;
    d[i / 2][ss] = p * p;
    e[ss] = p * p;
    while (ar < ss || br < ss) {
      if (d[i / 2][ar] < e[br]) {
        d[i][ar + br] = d[i / 2][ar];
        ar++;
      } else {
        d[i][ar + br] = e[br];
        br++;
      }
    }
    s[i] = 2 * ss;
    for (int j = 1; j < s[i]; j++) {
      if (d[i][j - 1] % p > d[i][j] % p) {
        d[i][j] += (d[i][j - 1] % p - d[i][j] % p);
      }
    }
  }
  for (int i = 0; i < q; i++) {
    uu = u[i];
    if (uu >= 1024) {
      ss = 1;
      e[0] = 0;
      while (uu >= 1024) {
        te = w[uu] * p + v[uu];
        for (int k = 0; k < ss; k++) {
          ee[k] = e[k];
          ef[k] = e[k] + te;
        }
        ar = 0, br = 0;
        ee[ss] = p * p;
        ef[ss] = p * p;
        while (ar < ss || br < ss) {
          if (ee[ar] < ef[br]) {
            e[ar + br] = ee[ar];
            ar++;
          } else {
            e[ar + br] = ef[br];
            br++;
          }
        }
        uu /= 2;
        ss *= 2;
      }
      for (int j = 1; j < ss; j++) {
        if (e[j - 1] % p > e[j] % p) {
          e[j] += (e[j - 1] % p - e[j] % p);
        }
      }
      ar = ss - 1;
      ans = 0;
      for (int j = 0; j < s[uu]; j++) {
        while (ar >= 0 && (d[uu][j] + e[ar]) / p > l[i])
          ar--;
        if (ar == -1)
          break;
        if ((d[uu][j] + e[ar]) % p > ans) {
          ans = (d[uu][j] + e[ar]) % p;
        }
      }
      printf("%lld\\n", ans);
    } else {
      ans = 0;
      for (int j = 0; j < s[u[i]]; j++) {
        if (d[u[i]][j] / p <= l[i]) {
          if (d[u[i]][j] % p > ans) {
            ans = d[u[i]][j] % p;
          }
        }
      }
      printf("%lld\\n", ans);
    }
  }
}

Error: 0

Fixed submission:

#include <stdio.h>
int n, q;
long long p = 10000000;
long long v[1024 * 256 + 1];
long long w[1024 * 256 + 1];
long long u[100000];
long long l[100000];
int s[1024];
int ss, uu;
long long e[1024];
long long ee[1024];
long long ef[1024];
long long d[1024][1024];
long long te, ans;
int ar, br;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", v + i, w + i);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%lld%lld", u + i, l + i);
  }
  d[1][0] = 0;
  d[1][1] = w[1] * p + v[1];
  s[1] = 2;
  for (int i = 2; i < 1024; i++) {
    te = w[i] * p + v[i];
    ss = s[i / 2];
    for (int j = 0; j < s[i / 2]; j++) {
      e[j] = d[i / 2][j] + te;
    }
    ar = 0, br = 0;
    d[i / 2][ss] = p * p;
    e[ss] = p * p;
    while (ar < ss || br < ss) {
      if (d[i / 2][ar] < e[br]) {
        d[i][ar + br] = d[i / 2][ar];
        ar++;
      } else {
        d[i][ar + br] = e[br];
        br++;
      }
    }
    s[i] = 2 * ss;
    for (int j = 1; j < s[i]; j++) {
      if (d[i][j - 1] % p > d[i][j] % p) {
        d[i][j] += (d[i][j - 1] % p - d[i][j] % p);
      }
    }
  }
  for (int i = 0; i < q; i++) {
    uu = u[i];
    if (uu >= 1024) {
      ss = 1;
      e[0] = 0;
      while (uu >= 1024) {
        te = w[uu] * p + v[uu];
        for (int k = 0; k < ss; k++) {
          ee[k] = e[k];
          ef[k] = e[k] + te;
        }
        ar = 0, br = 0;
        ee[ss] = p * p;
        ef[ss] = p * p;
        while (ar < ss || br < ss) {
          if (ee[ar] < ef[br]) {
            e[ar + br] = ee[ar];
            ar++;
          } else {
            e[ar + br] = ef[br];
            br++;
          }
        }
        uu /= 2;
        ss *= 2;
      }
      for (int j = 1; j < ss; j++) {
        if (e[j - 1] % p > e[j] % p) {
          e[j] += (e[j - 1] % p - e[j] % p);
        }
      }
      ar = ss - 1;
      ans = 0;
      for (int j = 0; j < s[u[i]]; j++) {
        while (ar >= 0 && (d[u[i]][j] + e[ar]) / p > l[i])
          ar--;
        if (ar == -1)
          break;
        if ((d[u[i]][j] + e[ar]) % p > ans) {
          ans = (d[u[i]][j] + e[ar]) % p;
        }
      }
      printf("%lld\\n", ans);
    } else {
      ans = 0;
      for (int j = 0; j < s[u[i]]; j++) {
        if (d[u[i]][j] / p <= l[i]) {
          if (d[u[i]][j] % p > ans) {
            ans = d[u[i]][j] % p;
          }
        }
      }
      printf("%lld\\n", ans);
    }
  }
}