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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int m, n, a[100][100], memo[100][101][101];

int DFS(int y, int l, int r) {
  if (y == m)
    return 0;
  int &res = memo[y][l][r];
  if (res != -1)
    return res;
  if (l == r)
    return res = DFS(y + 1, 0, n);

  res = max(res, DFS(y, l + 1, r) + (a[y][l] + (y > 0) + (l == 0 ? 0 : 1) +
                                     ((r != n) && (r - l == 1))) %
                                        2);
  res = max(res, DFS(y, l, r - 1) + (a[y][r - 1] + (y > 0) + (r == n ? 0 : 1) +
                                     ((l != 0) && (r - l == 1))) %
                                        2);
  return res;
}

void Solve() {
  cin >> m >> n;
  rep(i, 0, m) rep(j, 0, n) cin >> a[i][j];

  fill_n((int *)memo, 100 * 101 * 101, -1);
  cout << DFS(0, 0, n) << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int m, n, a[100][100], memo[100][100][100];

int DFS(int y, int l, int r) {
  if (y == m)
    return 0;
  int &res = memo[y][l][r];
  if (l == r)
    return res = DFS(y + 1, 0, n);

  res = max(res, DFS(y, l + 1, r) + (a[y][l] + (y > 0) + (l == 0 ? 0 : 1) +
                                     ((r != n) && (r - l == 1))) %
                                        2);
  res = max(res, DFS(y, l, r - 1) + (a[y][r - 1] + (y > 0) + (r == n ? 0 : 1) +
                                     ((l != 0) && (r - l == 1))) %
                                        2);
  return res;
}

void Solve() {
  cin >> m >> n;
  rep(i, 0, m) rep(j, 0, n) cin >> a[i][j];

  cout << DFS(0, 0, n) << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}