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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

using P = pair<int, int>;
s64 k;
vector<vs32> dp(1 << 9, vs32(100001, -1));
vector<P> vw;

int rec(int v, int l, bool mode) {
  if (v == 0)
    return 0;
  if (mode) {
    if ((1 << k) > v) {
      return dp[v][l];
    }
  } else {
    if (dp[v][l] >= 0) {
      return dp[v][l];
    }
  }

  int ret = rec(v / 2, l, mode);
  if (l >= vw[v - 1].second)
    ret = max(ret, rec(v / 2, l - vw[v - 1].second, mode) + vw[v - 1].first);

  if (!mode)
    dp[v][l] = max(dp[v][l], ret);
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vw.resize(n);
  rep(i, n) { cin >> vw[i].first >> vw[i].second; }

  while ((1ll << (2 * k)) < n)
    ++k;
  for (int v = 1; v < (1 << k); ++v) {
    rep(l, 100000 + 1) { rec(v, l, false); }
  }

  int q;
  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    cout << rec(v, l, true) << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

using P = pair<int, int>;
s64 k;
int dp[(1 << 9) + 1][100001];
vector<P> vw;

int rec(int v, int l, bool mode) {
  if (mode) {
    if ((1 << k) > v) {
      return dp[v][l];
    }
  }
  if (v == 0)
    return 0;

  int ret = rec(v / 2, l, mode);
  if (l >= vw[v - 1].second)
    ret = max(ret, rec(v / 2, l - vw[v - 1].second, mode) + vw[v - 1].first);

  if (!mode)
    dp[v][l] = max(dp[v][l], ret);
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vw.resize(n);
  rep(i, n) { cin >> vw[i].first >> vw[i].second; }

  while ((1ll << (2 * k)) < n)
    ++k;
  rep(v, (1 << k) - 1) {
    rep(l, 100000 + 1) { rec(v + 1, l, false); }
  }

  int q;
  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    cout << rec(v, l, true) << "\\n";
  }

  return 0;
}