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[512 + 1];
long long ee[128 + 1];
long long ef[128 + 1];
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[0][0] = 0;
  s[0] = 1;
  for (int i = 1; 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;
    s[i] = 2 * ss;
    while (ar + br < s[i]) {
      if (d[i / 2][ar] < e[br]) {
        d[i][ar + br] = d[i / 2][ar];
        ar++;
      } else {
        d[i][ar + br] = e[br];
        br++;
      }
    }
    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];
      }
    }
  }
  for (int i = 0; i < q; i++) {
    uu = u[i];
    ans = 0;
    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];
        }
      }
      ar = ss - 1;
      for (int j = 0; j < s[uu]; j++) {
        while (ar + 1 && (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;
        }
      }
    } else {
      for (int j = 0; j < s[uu]; j++) {
        if (d[uu][j] / p <= l[i]) {
          if (d[uu][j] % p > ans) {
            ans = d[uu][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[256];
long long ee[128 + 1];
long long ef[128 + 1];
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[0][0] = 0;
  s[0] = 1;
  for (int i = 1; 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;
    s[i] = 2 * ss;
    while (ar + br < s[i]) {
      if (d[i / 2][ar] < e[br]) {
        d[i][ar + br] = d[i / 2][ar];
        ar++;
      } else {
        d[i][ar + br] = e[br];
        br++;
      }
    }
    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];
      }
    }
  }
  for (int i = 0; i < q; i++) {
    uu = u[i];
    ans = 0;
    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];
        }
      }
      ar = ss - 1;
      for (int j = 0; j < s[uu]; j++) {
        while (ar + 1 && (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;
        }
      }
    } else {
      for (int j = 0; j < s[uu]; j++) {
        if (d[uu][j] / p <= l[i]) {
          if (d[uu][j] % p > ans) {
            ans = d[uu][j] % p;
          }
        }
      }
    }
    printf("%lld\\n", ans);
  }
}