Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define bogo_sort std::sort
#define bozo_sort std::stable_sort
#define alles(obj) obj.begin(), obj.end()
#define elif else if
#define unless(flg) if (!(flg))
#define elless(flg) else if (!(flg))
#define echo std::cout <<
#define read std::cin >>
#define endl std::endl
#define fin << '\n'
#define bash push_back
#define makePair std::make_pair
#define _ << ' ' <<
// type-define
#define Stack std::stack
#define Queue std::queue
#define Set std::set
#define PQueue std::priority_queue
#define Vector std::vector
#define Pair std::pair
#define Map std::map
#define Greater std::greater
using String = std::string;
using llong = long long;
using boolean = bool;
using Pii = Pair<int, int>;
using Vi = Vector<int>;
using Vii = Vector<Pii>;
// utils
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
constexpr int INF = 0x3f3f3f3f;
constexpr llong LINF = 0x3f3f3f3f3f3f3f3fLL;

namespace {
llong power(llong x, llong n, llong mod) {
  llong ans = 1;
  while (n > 0) {
    if (n & 1)
      ans = (ans * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return ans;
}
llong power(llong x, llong n) { return power(x, n, 1000000007); }
llong gcd(llong x, llong y) { return x % y ? gcd(y, x % y) : y; }
llong lcm(llong x, llong y) { return (x / gcd(x, y) * y); }
llong abs(llong n) { return (n < 0) ? -n : n; }
boolean isMovable(int x, int y, int w, int h) {
  return (x >= 0 && y >= 0 && x < w && y < h);
}
} // namespace

#define int long long

namespace Rlyeh {

int N, W;
int w[110], v[110];
int dp[110][100100];

int dfs(int pos, int left) {
  if (pos == N)
    return 0;
  if (~dp[pos][left])
    return dp[pos][left];
  if (left - w[pos] < 0) {
    return dp[pos][left] = dfs(pos + 1, left);
  } else {
    return std::max(dfs(pos + 1, left), dfs(pos + 1, left - w[pos]) + v[pos]);
  }
}

signed call_of_Cthulhu(signed datum) {

  memset(dp, -1, sizeof(dp));
  read N >> W;
  for (int i = 0; i < N; i++) {
    read w[i] >> v[i];
  }
  echo dfs(0, W) fin;

  return 0;
}
} // namespace Rlyeh

signed main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int main_result = Rlyeh::call_of_Cthulhu(114514);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define bogo_sort std::sort
#define bozo_sort std::stable_sort
#define alles(obj) obj.begin(), obj.end()
#define elif else if
#define unless(flg) if (!(flg))
#define elless(flg) else if (!(flg))
#define echo std::cout <<
#define read std::cin >>
#define endl std::endl
#define fin << '\n'
#define bash push_back
#define makePair std::make_pair
#define _ << ' ' <<
// type-define
#define Stack std::stack
#define Queue std::queue
#define Set std::set
#define PQueue std::priority_queue
#define Vector std::vector
#define Pair std::pair
#define Map std::map
#define Greater std::greater
using String = std::string;
using llong = long long;
using boolean = bool;
using Pii = Pair<int, int>;
using Vi = Vector<int>;
using Vii = Vector<Pii>;
// utils
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
constexpr int INF = 0x3f3f3f3f;
constexpr llong LINF = 0x3f3f3f3f3f3f3f3fLL;

namespace {
llong power(llong x, llong n, llong mod) {
  llong ans = 1;
  while (n > 0) {
    if (n & 1)
      ans = (ans * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return ans;
}
llong power(llong x, llong n) { return power(x, n, 1000000007); }
llong gcd(llong x, llong y) { return x % y ? gcd(y, x % y) : y; }
llong lcm(llong x, llong y) { return (x / gcd(x, y) * y); }
llong abs(llong n) { return (n < 0) ? -n : n; }
boolean isMovable(int x, int y, int w, int h) {
  return (x >= 0 && y >= 0 && x < w && y < h);
}
} // namespace

#define int long long

namespace Rlyeh {

int N, W;
int w[110], v[110];
int dp[110][100100];

int dfs(int pos, int left) {
  if (pos == N)
    return 0;
  if (~dp[pos][left])
    return dp[pos][left];
  if (left - w[pos] < 0) {
    return dp[pos][left] = dfs(pos + 1, left);
  } else {
    return dp[pos][left] = std::max(dfs(pos + 1, left),
                                    dfs(pos + 1, left - w[pos]) + v[pos]);
  }
}

signed call_of_Cthulhu(signed datum) {

  memset(dp, -1, sizeof(dp));
  read N >> W;
  for (int i = 0; i < N; i++) {
    read w[i] >> v[i];
  }
  echo dfs(0, W) fin;

  return 0;
}
} // namespace Rlyeh

signed main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int main_result = Rlyeh::call_of_Cthulhu(114514);
  return 0;
}