Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const int maxn = (1 << 18) + 10;
const int INF = 100000;
int n, q, w[maxn], dep[maxn];
int tot, d[100], K;
ll val[maxn], dp[(1 << 9) + 10][INF + 10], ans, s1, s2;
int main() {
  // printf("%d\\n",sizeof(dp)/1024/1024);
  // freopen("1.txt","r",stdin);
  K = 9;
  read(n);
  for (int i = 1; i <= n; i++)
    read(val[i]), read(w[i]), dep[i] = dep[i / 2] + 1;
  for (register int i = 1; i <= n; ++i)
    if (dep[i] <= K) {
      for (register int j = 0; j <= INF; ++j) {
        dp[i][j] = dp[i / 2][j];
        if (j >= w[i])
          dp[i][j] = max(dp[i][j], dp[i / 2][j - w[i]] + val[i]);
      }
      continue;
      for (int j = 0; j + w[i] <= INF; j++)
        dp[i][j + w[i]] = max(dp[i][j + w[i]], dp[i / 2][j] + val[i]);
      // printf("%d\\n",i);
    } else
      break;
  int x, y, z;
  read(q);
  while (q--) {
    read(x);
    read(y);
    if (dep[x] <= K)
      printf("%lld\\n", dp[x][y]);
    else {
      tot = 0;
      ans = 0;
      z = x;
      while (1) {
        d[++tot] = z;
        z /= 2;
        if (dep[z] <= K)
          break;
      }
      for (register int t = 0; t < (1 << tot); ++t) {
        s1 = s2 = 0;
        for (register int i = 1; i <= tot; ++i)
          if (t & (1 << (i - 1))) {
            s1 += val[d[i]], s2 += w[d[i]];
            if (s2 > y)
              break;
          }
        if (s2 > y)
          continue;
        ans = max(ans, s1 + dp[z][y - s2]);
      }
      printf("%lld\\n", ans);
    }
  }
  return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?

  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const int maxn = (1 << 18) + 10;
const int INF = 100000;
int n, q, w[maxn], dep[maxn];
int tot, d[100], K;
ll val[maxn], dp[(1 << 9) + 10][INF + 10], ans, s1, s2;
int main() {
  // printf("%d\\n",sizeof(dp)/1024/1024);
  // freopen("1.txt","r",stdin);
  K = 8;
  read(n);
  for (int i = 1; i <= n; i++)
    read(val[i]), read(w[i]), dep[i] = dep[i / 2] + 1;
  for (int i = 1; i <= n; i++)
    if (dep[i] <= K) {
      for (int j = 0; j <= INF; j++)
        dp[i][j] = dp[i / 2][j];
      for (int j = 0; j + w[i] <= INF; j++)
        dp[i][j + w[i]] = max(dp[i][j + w[i]], dp[i / 2][j] + val[i]);
    }
  int x, y, z;
  read(q);
  while (q--) {
    read(x);
    read(y);
    if (dep[x] <= K)
      printf("%lld\\n", dp[x][y]);
    else {
      tot = 0;
      ans = 0;
      z = x;
      while (1) {
        d[++tot] = z;
        z /= 2;
        if (dep[z] <= K)
          break;
      }
      for (int t = 0; t < (1 << tot); t++) {
        s1 = s2 = 0;
        for (int i = 1; i <= tot; i++)
          if (t & (1 << (i - 1)))
            s1 += val[d[i]], s2 += w[d[i]];
        if (s2 > y)
          continue;
        ans = max(ans, s1 + dp[z][y - s2]);
      }
      printf("%lld\\n", ans);
    }
  }
  return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?

  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/