Problem p00497 (C++) — buggy submission, verdict: Memory Limit Exceeded

#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
const int MODULO = 1000000000;
const int INF = 2000000000; // 1e9 * 2
const double EPS = 1e-7;
const double PI = acos(-1.0);

typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef complex<double> Cd;

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define sz(c) ((int)(c).size())

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

// start

int *table[5000];

int main() {
  FOR(i, 5000) table[i] = (int *)calloc((i + 1), sizeof(int));

  int n, m;
  cin >> n >> m;
  FOR(i, m) {
    int a, b, x;
    cin >> a >> b >> x;
    a--;
    b--;
    table[a][b] = x + 1;
  }

  for (int i = 1; i < n; i++)
    FOR(j, i + 1) {
      if (j == 0)
        table[i][j] = max(table[i][j], table[i - 1][j] - 1);
      else if (j == i)
        table[i][j] = max(table[i][j], table[i - 1][j - 1] - 1);
      else
        table[i][j] =
            max(table[i][j], max(table[i - 1][j], table[i - 1][j - 1]) - 1);
    }
  int ans = 0;
  FOR(i, n) FOR(j, i + 1) ans += table[i][j] > 0;
  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
const int MODULO = 1000000000;
const int INF = 2000000000; // 1e9 * 2
const double EPS = 1e-7;
const double PI = acos(-1.0);

typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef complex<double> Cd;

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define sz(c) ((int)(c).size())

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

// start

int table[5000][5000];

int main() {
  int n, m;
  cin >> n >> m;
  FOR(i, m) {
    int a, b, x;
    cin >> a >> b >> x;
    a--;
    b--;
    table[a][b] = x + 1;
  }

  for (int i = 1; i < n; i++)
    FOR(j, i + 1) {
      if (j == 0)
        table[i][j] = max(table[i][j], table[i - 1][j] - 1);
      else if (j == i)
        table[i][j] = max(table[i][j], table[i - 1][j - 1] - 1);
      else
        table[i][j] =
            max(table[i][j], max(table[i - 1][j], table[i - 1][j - 1]) - 1);
    }
  int ans = 0;
  FOR(i, n) FOR(j, n) ans += table[i][j] > 0;
  cout << ans << endl;

  return 0;
}