Problem p00502 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

// #define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \
  { cerr << #o << " " << o << endl; }
#define dumpc(o)                                                               \
  {                                                                            \
    cerr << #o;                                                                \
    for (auto &e : (o))                                                        \
      cerr << " " << e;                                                        \
    cerr << endl;                                                              \
  }
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

#define MAX 10

signed main() {
  //??????dp A<=??????<=B?????¨??? dp[i??\???][j????????????] ?????????
  int D, N;
  cin >> D >> N;
  int T[MAX] = {};
  rep(i, 0, D) cin >> T[i];
  int A[MAX], B[MAX], C[MAX];
  rep(j, 0, N) cin >> A[j] >> B[j] >> C[j];
  int dp[MAX][MAX] = {};
  fill(dp[0], dp[MAX], -1);
  rep(j, 0, N) {
    if (T[0] < A[j] || B[j] < T[0])
      continue;
    dp[1][j] = 0;
  }
  rep(i, 1, D) {
    rep(j, 0, N) {
      if (T[i] < A[j] || B[j] < T[i])
        continue;
      rep(k, 0, N) {
        if (dp[i][k] == -1)
          continue;
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] + abs(C[j] - C[k]));
      }
    }
  }
  cout << *max_element(dp[D], dp[D] + MAX) << endl;
  ;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

// #define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \
  { cerr << #o << " " << o << endl; }
#define dumpc(o)                                                               \
  {                                                                            \
    cerr << #o;                                                                \
    for (auto &e : (o))                                                        \
      cerr << " " << e;                                                        \
    cerr << endl;                                                              \
  }
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

#define MAX 210

signed main() {
  //??????dp A<=??????<=B?????¨??? dp[i??\???][j????????????] ?????????
  int D, N;
  cin >> D >> N;
  int T[MAX] = {};
  rep(i, 0, D) cin >> T[i];
  int A[MAX], B[MAX], C[MAX];
  rep(j, 0, N) cin >> A[j] >> B[j] >> C[j];
  int dp[MAX][MAX] = {};
  fill(dp[0], dp[MAX], -1);
  rep(j, 0, N) {
    if (T[0] < A[j] || B[j] < T[0])
      continue;
    dp[1][j] = 0;
  }
  rep(i, 1, D) {
    rep(j, 0, N) {
      if (T[i] < A[j] || B[j] < T[i])
        continue;
      rep(k, 0, N) {
        if (dp[i][k] == -1)
          continue;
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] + abs(C[j] - C[k]));
      }
    }
  }
  cout << *max_element(dp[D], dp[D] + MAX) << endl;
  ;
  return 0;
}