Problem p00502 (C++)

Buggy submission (status: Time Limit Exceeded):
//
//  main.cpp

#include <cmath>
#include <iostream>

using namespace std;
struct status {
  int toptemp;
  int bottomtemp;
  int fancy;
};
status dress[201];
int dp[201][201]; // i??\???????????????j????????????????????§????´???????
int temp[201];
int D, N;

int MAX(int i, int j) { // i??\?????????j???????????????????????§???????????????
  if (i == 0) {         //?????\????????????????????????
                        //		return dress[j].fancy;
    return 0;
  }

  //	for(int i=0;i<N;i++){
  for (int J = 0; J < N;
       J++) { // i-1??\?????????J(1<D)???????????????????????§???
    if (temp[i - 1] >= dress[J].bottomtemp && temp[i - 1] <= dress[J].toptemp) {
      dp[i][j] =
          max(dp[i][j], MAX(i - 1, J) + abs(dress[J].fancy - dress[j].fancy));
    }
  }
  return dp[i][j];
  //		dp[i-1][j]
  //	}
}

int main() {

  cin >> D >> N;

  for (int i = 0; i < D; i++) {
    cin >> temp[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> dress[i].bottomtemp >> dress[i].toptemp >> dress[i].fancy;
  }

  for (int i = 0; i < 201; i++) {
    for (int j = 0; j < 201; j++) {
      dp[i][j] = -1;
    }
  }
  int answer = 0;
  for (int i = 0; i < N; i++) {
    if (temp[D - 1] >= dress[i].bottomtemp && temp[D - 1] <= dress[i].toptemp) {
      answer = max(answer, MAX(D - 1, i));
    }
  }
  cout << answer << endl;

  /*
  for(int i=0;i<N;i++){//0??\?????????????¨???????
          if(temp[0] >= dress[i].bottomtemp && temp[0] <= dress[i].toptemp){
                  dp[0][i] = dress[i].fancy;
          }
  }

  for(int i=1;i<D;i++){
          for(int j=0;j<N;j++){
                  if(temp[i] >= dress[j].bottomtemp && temp[i] <=
  dress[j].toptemp){//i??\??????j?????????????????? int Maxfancy=-1; for(int
  k=0;k<N;k++){//i-1??\????????¨????????????????????´???????????????????´???????????????§???????????????????????????
                                  if(dp[i-1][k] == -1)continue;
                                  Maxfancy = max(Maxfancy,abs(dress[j].fancy -
  dress[k].fancy));
                          }
                          dp[i][j] = Maxfancy;
                  }
          }
  }

  int answer = 0;
  for(int i=1;i<D;i++){
          int Max = -1;
          for(int j=0;j<N;j++){
                  Max = max(Max,dp[i][j]);
          }
          answer += Max;
  }
  cout << answer << endl;

  vector<int> dresses;
  int matrix[201][2];
  for(int i=0;i<D;i++){
          for(int j=0;j<N;j++){
                  if(temp[i] >= dress[j].bottomtemp && temp[i] <=
  dress[j].toptemp){ dresses.push_back(dress[j].fancy);
                  }
          }
          sort(dresses.begin(),dresses.end());
          matrix[i][0] = dresses[0];
          matrix[i][1] = dresses[dresses.size() - 1];
  }

  int dp[201]={};
  dp[0] = max(matrix[0][0],matrix[0][1]);

  for(int i=1;i<D;i++){
          dp[i] = max(abs(dp[i-1] - matrix[i][0]),abs(dp[i-1] - matrix[i][1]));
  }

  cout << dp[D-1] << endl;
  */
  return 0;
}

Fixed submission:
//
//  main.cpp

#include <cmath>
#include <iostream>

using namespace std;
struct status {
  int toptemp;
  int bottomtemp;
  int fancy;
};
status dress[201];
int dp[201][201]; // i??\???????????????j????????????????????§????´???????
int temp[201];
int D, N;

int MAX(int i, int j) { // i??\?????????j???????????????????????§???????????????
  if (i == 0) {         //?????\????????????????????????
                        //		return dress[j].fancy;
    return 0;
  }
  if (dp[i][j] != -1) {
    return dp[i][j];
  }

  //	for(int i=0;i<N;i++){
  for (int J = 0; J < N;
       J++) { // i-1??\?????????J(1<D)???????????????????????§???
    if (temp[i - 1] >= dress[J].bottomtemp && temp[i - 1] <= dress[J].toptemp) {
      dp[i][j] =
          max(dp[i][j], MAX(i - 1, J) + abs(dress[J].fancy - dress[j].fancy));
    }
  }
  return dp[i][j];
  //		dp[i-1][j]
  //	}
}

int main() {

  cin >> D >> N;

  for (int i = 0; i < D; i++) {
    cin >> temp[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> dress[i].bottomtemp >> dress[i].toptemp >> dress[i].fancy;
  }

  for (int i = 0; i < 201; i++) {
    for (int j = 0; j < 201; j++) {
      dp[i][j] = -1;
    }
  }
  int answer = 0;
  for (int i = 0; i < N; i++) {
    if (temp[D - 1] >= dress[i].bottomtemp && temp[D - 1] <= dress[i].toptemp) {
      answer = max(answer, MAX(D - 1, i));
    }
  }
  cout << answer << endl;

  /*
  for(int i=0;i<N;i++){//0??\?????????????¨???????
          if(temp[0] >= dress[i].bottomtemp && temp[0] <= dress[i].toptemp){
                  dp[0][i] = dress[i].fancy;
          }
  }

  for(int i=1;i<D;i++){
          for(int j=0;j<N;j++){
                  if(temp[i] >= dress[j].bottomtemp && temp[i] <=
  dress[j].toptemp){//i??\??????j?????????????????? int Maxfancy=-1; for(int
  k=0;k<N;k++){//i-1??\????????¨????????????????????´???????????????????´???????????????§???????????????????????????
                                  if(dp[i-1][k] == -1)continue;
                                  Maxfancy = max(Maxfancy,abs(dress[j].fancy -
  dress[k].fancy));
                          }
                          dp[i][j] = Maxfancy;
                  }
          }
  }

  int answer = 0;
  for(int i=1;i<D;i++){
          int Max = -1;
          for(int j=0;j<N;j++){
                  Max = max(Max,dp[i][j]);
          }
          answer += Max;
  }
  cout << answer << endl;

  vector<int> dresses;
  int matrix[201][2];
  for(int i=0;i<D;i++){
          for(int j=0;j<N;j++){
                  if(temp[i] >= dress[j].bottomtemp && temp[i] <=
  dress[j].toptemp){ dresses.push_back(dress[j].fancy);
                  }
          }
          sort(dresses.begin(),dresses.end());
          matrix[i][0] = dresses[0];
          matrix[i][1] = dresses[dresses.size() - 1];
  }

  int dp[201]={};
  dp[0] = max(matrix[0][0],matrix[0][1]);

  for(int i=1;i<D;i++){
          dp[i] = max(abs(dp[i-1] - matrix[i][0]),abs(dp[i-1] - matrix[i][1]));
  }

  cout << dp[D-1] << endl;
  */
  return 0;
}