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

#include <algorithm>
#include <assert.h>
#include <bit>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

typedef long double ld;
typedef long long ll;

using namespace std;

#define D 10
#define MAXN (1 << 18)
#define MAXW (100010)

ll dp[(1 << D)][MAXW];
ll v[MAXN];
ll w[MAXN];

int n, q;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 1; i < (1 << D); i++) {
    ll prevdp[MAXW];
    memcpy(prevdp, dp[i / 2], sizeof(dp[0]));
    for (int j = 0; j + w[i] < MAXW; j++) {
      dp[i][j + w[i]] = prevdp[j] + v[i];
    }
    for (int j = 0; j < MAXW; j++) {
      dp[i][j] = max(prevdp[j], dp[i][j]);
    }
    for (int j = 1; j < MAXW; j++) {
      dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int cur, cap;
    cin >> cur >> cap;
    if (cur < (1 << D)) {
      cout << dp[cur][cap] << endl;
    } else {
      vector<int> items;
      while (cur >= (1 << D)) {
        items.push_back(cur);
        cur >>= 1;
      }
      int m = items.size();
      ll bestval = 0;
      for (int mask = 0; mask < (1 << m); mask++) {
        ll curv = 0;
        ll curw = 0;
        for (int j = 0; j < m; j++) {
          if (mask & (1 << j)) {
            curv += v[items[j]];
            curw += w[items[j]];
          }
        }
        if (curw > cap) {
          continue;
        }
        curv += dp[cur][cap - curw];
        bestval = max(curv, bestval);
      }
      cout << bestval << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bit>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

typedef long double ld;
typedef long long ll;

using namespace std;

#define D 8
#define MAXN (1 << 18)
#define MAXW (100010)

ll dp[(1 << D)][2 * MAXW];
ll v[MAXN];
ll w[MAXN];

int n, q;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 1; i < (1 << D); i++) {
    ll prevdp[MAXN];
    memcpy(prevdp, dp[i / 2], sizeof(dp[0]));
    for (int j = 0; j < MAXW; j++) {
      dp[i][j + w[i]] = prevdp[j] + v[i];
    }
    for (int j = 0; j < MAXW; j++) {
      dp[i][j] = max(prevdp[j], dp[i][j]);
    }
    for (int j = 1; j < MAXW; j++) {
      dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int cur, cap;
    cin >> cur >> cap;
    if (cur < (1 << D)) {
      cout << dp[cur][cap] << endl;
    } else {
      vector<int> items;
      while (cur >= (1 << D)) {
        items.push_back(cur);
        cur >>= 1;
      }
      int m = items.size();
      ll bestval = 0;
      for (int mask = 0; mask < (1 << m); mask++) {
        ll curv = 0;
        ll curw = 0;
        for (int j = 0; j < m; j++) {
          if (mask & (1 << j)) {
            curv += v[items[j]];
            curw += w[items[j]];
          }
        }
        if (curw > cap) {
          continue;
        }
        curv += dp[cur][cap - curw];
        bestval = max(curv, bestval);
      }
      cout << bestval << endl;
    }
  }
  return 0;
}