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


#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;

template <typename T, typename U>
std::enable_if_t<std::rank<T>::value == 0> fill_all(T &arr, const U &v) {
  arr = v;
}
template <typename ARR, typename U>
std::enable_if_t<std::rank<ARR>::value != 0> fill_all(ARR &arr, const U &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}

int64_t V[362244];
int64_t W[362244];
int64_t dp[1 << 9][100100];
int64_t func(int32_t i, int32_t w) {
  if (i >= sizeof(dp) / sizeof(dp[0])) {
    int64_t memo;
    memo = func((i - 1) / 2, w);
    if (W[i] <= w) {
      memo = std::max(memo, func((i - 1) / 2, w - W[i]) + V[i]);
    }
    return memo;
  }

  auto &memo = dp[i][w];
  if (memo != -1) {
    return memo;
  }
  if (i == 0) {
    if (W[i] <= w) {
      return memo = V[i];
    }
    return memo = 0;
  }
  if (w == 0) {
    return memo = 0;
  }

  memo = func((i - 1) / 2, w);
  if (W[i] <= w) {
    memo = std::max(memo, func((i - 1) / 2, w - W[i]) + V[i]);
  }
  return memo;
}

void func_init() {
  for (int32_t w = 0; w < W[0]; w++) {
    dp[0][w] = 0;
  }
  for (int32_t w = W[0]; w < 100100; w++) {
    dp[0][w] = V[0];
  }
  for (int32_t i = 1; i < sizeof(dp) / sizeof(dp[0]); i++) {
    auto p = (i - 1) / 2;
    for (int32_t w = 0; w < W[i]; w++) {
      dp[i][w] = dp[p][w];
    }
    for (int32_t w = W[i]; w < 100100; w++) {
      dp[i][w] = std::max(dp[p][w], dp[p][w - W[i]] + V[i]);
    }
  }
}

int32_t N;

int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);
  fill_all(dp, -1);
  in >> N;
  for (int i = 0; i < N; i++) {
    in >> V[i] >> W[i];
  }
  func_init();
  int Q;
  in >> Q;
  while (Q--) {
    int32_t v;
    int32_t L;
    in >> v >> L;
    --v;
    out << func(v, L) << '\\n';
  }

  return 0;
}
#endif


Error: TLE

Fixed submission:


#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;

template <typename T, typename U>
std::enable_if_t<std::rank<T>::value == 0> fill_all(T &arr, const U &v) {
  arr = v;
}
template <typename ARR, typename U>
std::enable_if_t<std::rank<ARR>::value != 0> fill_all(ARR &arr, const U &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}

int64_t V[262244];
int64_t W[262244];
int64_t dp[1 << 10][100100];
int64_t func(int32_t i, int32_t w) {
  if (i >= (1 << 10)) {
    int64_t memo;
    memo = func((i - 1) / 2, w);
    if (W[i] <= w) {
      memo = std::max(memo, func((i - 1) / 2, w - W[i]) + V[i]);
    }
    return memo;
  }

  auto &memo = dp[i][w];
  if (memo != -1) {
    return memo;
  }
  if (i == 0) {
    if (W[i] <= w) {
      return memo = V[i];
    }
    return memo = 0;
  }
  if (w == 0) {
    return memo = 0;
  }

  memo = func((i - 1) / 2, w);
  if (W[i] <= w) {
    memo = std::max(memo, func((i - 1) / 2, w - W[i]) + V[i]);
  }
  return memo;
}

int32_t N;

int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);
  fill_all(dp, -1);
  in >> N;
  for (int i = 0; i < N; i++) {
    in >> V[i] >> W[i];
  }
  int Q;
  in >> Q;
  while (Q--) {
    int32_t v;
    int32_t L;
    in >> v >> L;
    --v;
    out << func(v, L) << '\\n';
  }

  return 0;
}
#endif