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

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define task "tst"
using namespace std;
const ll N = 1e5 + 1;
const ll inf = 1e8;
typedef pair<ll, ll> LL;

template <typename T> inline void Read(T &x) {
  register char c;
  bool neg = false;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    if (c == '-')
      neg = true;
  for (x = 0; '0' <= c && c <= '9'; c = getchar())
    x = x * 10 + c - '0';
  if (neg)
    x = -x;
}

template <typename T, typename... Args> inline void Read(T &x, Args &...args) {
  Read(x);
  Read(args...);
}

template <typename T> inline void Write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    Write(x / 10);
  putchar(x % 10 + '0');
}

template <typename T> inline void Write(T x, const char &c) {
  if (x < 0)
    putchar('-'), x = -x;
  Write(x);
  putchar(c);
}

template <typename T, typename... Args>
inline void Write(const T &a, const char &c, Args... args) {
  Write(a, c);
  Write(args...);
}

LL a[3 * N];
ll u, v, n, q, i, dp[1024][N];
ll gen(ll x, ll y) {
  if (x < 1024)
    return dp[x][y];
  ll ans = gen(x / 2, y);
  if (a[x].sc <= y)
    ans = max(ans, a[x].fs + gen(x / 2, y - a[x].sc));
  return ans;
}
int main() {
  // ios_base::sync_with_stdio(0);
  // cin.tie(0), cout.tie(0);
  Read(n);
  for (i = 1; i <= n; i++)
    Read(a[i].fs), Read(a[i].sc);
  for (i = 1; i < 1024; i++) {
    for (ll j = 1; j < N; j++) {
      dp[i][j] = dp[i / 2][j];
      if (j >= a[i].sc)
        dp[i][j] = max(dp[i][j], a[i].fs + dp[i / 2][j - a[i].sc]);
    }
  }
  cin >> q;
  while (q--) {
    Read(u);
    Read(v);
    Write(gen(u, v));
    cout << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define task "tst"
using namespace std;
const ll N = 1e5 + 9;
const ll inf = 1e8;
typedef pair<ll, ll> LL;

template <typename T> inline void Read(T &x) {
  register char c;
  bool neg = false;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    if (c == '-')
      neg = true;
  for (x = 0; '0' <= c && c <= '9'; c = getchar())
    x = x * 10 + c - '0';
  if (neg)
    x = -x;
}

template <typename T, typename... Args> inline void Read(T &x, Args &...args) {
  Read(x);
  Read(args...);
}

template <typename T> inline void Write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    Write(x / 10);
  putchar(x % 10 + '0');
}

template <typename T> inline void Write(T x, const char &c) {
  if (x < 0)
    putchar('-'), x = -x;
  Write(x);
  putchar(c);
}

template <typename T, typename... Args>
inline void Write(const T &a, const char &c, Args... args) {
  Write(a, c);
  Write(args...);
}

LL a[3 * N];
ll u, v, n, q, i, dp[1024][N];
ll f(ll x, ll y) {
  if (x <= 0 || y <= 0)
    return 0;
  if (dp[x][y] != -1)
    return dp[x][y];
  ll ans = f(x / 2, y);
  if (a[x].sc <= y)
    ans = max(ans, a[x].fs + f(x / 2, y - a[x].sc));
  return dp[x][y] = ans;
}
ll gen(ll x, ll y) {
  if (x < 1024)
    return f(x, y);
  ll ans = gen(x / 2, y);
  if (a[x].sc <= y)
    ans = max(ans, a[x].fs + gen(x / 2, y - a[x].sc));
  return ans;
}
int main() {
  // ios_base::sync_with_stdio(0);
  // cin.tie(0), cout.tie(0);
  Read(n);
  for (i = 1; i <= n; i++)
    Read(a[i].fs), Read(a[i].sc);
  memset(dp, -1, sizeof(dp));
  cin >> q;
  while (q--) {
    Read(u);
    Read(v);
    Write(gen(u, v));
    cout << "\\n";
  }
}