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

////////////////////////////////////////
// ALDS1_9_A:   Complete Binary Tree
// ALDS1_9_B:   Maximum Heap
// ALDS1_9_C:   Priority Queue
////////////////////////////////////////

#include <algorithm> // next_permutation
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map> //???????????\\??¢??°
#include <fstream> //ifstream, ofstream

// #define NDEBUG //NDEBUG???#include
// <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???
//????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#define din cin
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout                                                                   \\
  dummy.str("");                                                               \\
  dummy.clear(stringstream::goodbit);                                          \\
  dummy // dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§????????????????????????????????????
//http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUTFILE????????????????????¬???????????????????????????????????¢????????????
// #define OUTPUTFILE "output.txt"
// //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout                                                                   \\
  outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath                                                         \\
  "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/"      \\
  "output.txt"
#endif

//?¨??????\\???????????????????????????????????????????????´??????INPUTFROMTEXTFILE????????????????????¬???????????????????????????????????¢????????????
// #define INPUTFILE "input.txt"
// //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif

#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A)                                                                \\
  dout << setw(3) << (A) << " " // << setw(3) ??????????????\\????????????
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)

#define dispAll(A, n)                                                          \\
  dout << #A << " = ";                                                         \\
  rep(j, 0, (n)) { disP(A[j]); }                                               \\
  dout << endl

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef unsigned long long ll;

const int INF = 1e9 - 1;

int N;
const int N_MAX = 100;
int m[N_MAX + 2];
int dp
    [N_MAX]
    [N_MAX]; // dp[i][j] =
             // ??????M[i]??????M[j]?????§????????????????±?????????¨???????????????????????????????°??????°

int main() {
  din >> N;
  int r, c;
  rep(i, 0, N) {
    cin >> r >> c;
    if (i == 0)
      m[i] = r, m[i + 1] = c;
    else
      m[i + 1] = c;
  }

  //    dispAll(m, N+1);

  rep(i, 0, N) {
    rep(j, 0, N) {
      if (i == j)
        dp[i][j] = 0;
      else
        dp[i][j] = INF;
    }
  }

  //    //test display
  //    dout<<"-----------------------------\\n";
  //    dout<<"        j = ";
  //    rep(j,0,N) disP(j);
  //    dout<<endl;
  //    rep(i,0,N) {
  //        disP(i);
  //        dispAll(dp[i], N);
  //    }

  // calc.
  rep(x, 1, N) {
    //        disp(x);
    rep(i, 0, N - x) {
      int j = x + i;
      //            disp(i); disp(j);
      rep(k, i, j) {
        //                disp(k);
        //                disp(m[i]*m[k+1]*m[j+1]);

        assert(dp[i][k] != INF);
        assert(dp[k + 1][j] != INF);

        dp[i][j] =
            min(dp[i][j], dp[i][k] + dp[k + 1][j] + m[i] * m[k + 1] * m[j + 1]);
      }

      //            //test display
      //            dout<<"        j = ";
      //            rep(j,0,N) disP(j);
      //            dout<<endl;
      //            rep(i,0,N) {
      //                disP(i);
      //                dispAll(dp[i], N);
      //            }
      //            dout<<"-----------------------------\\n";
    }
  }

  dout << dp[0][N - 1] << endl;

#ifdef INPUTFILE
  inputfile.close();
#endif

#ifdef OUTPUTFILE
  outputfile.close();
  cout << "\\"" << OutputFilePath << "\\"" << endl;
#endif

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // next_permutation
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map> //???????????\\??¢??°
#include <fstream> //ifstream, ofstream

// #define NDEBUG //NDEBUG???#include
// <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???
//????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#define din cin
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout                                                                   \\
  dummy.str("");                                                               \\
  dummy.clear(stringstream::goodbit);                                          \\
  dummy // dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§????????????????????????????????????
//http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUTFILE????????????????????¬???????????????????????????????????¢????????????
// #define OUTPUTFILE "output.txt"
// //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout                                                                   \\
  outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath                                                         \\
  "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/"      \\
  "output.txt"
#endif

//?¨??????\\???????????????????????????????????????????????´??????INPUTFROMTEXTFILE????????????????????¬???????????????????????????????????¢????????????
// #define INPUTFILE "input.txt"
// //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif

#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A)                                                                \\
  dout << setw(3) << (A) << " " // << setw(3) ??????????????\\????????????
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)

#define dispAll(A, n)                                                          \\
  dout << #A << " = ";                                                         \\
  rep(j, 0, (n)) { disP(A[j]); }                                               \\
  dout << endl

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef unsigned long long ll;

const int INF = 1e9 - 1;

int N;
int R[101];
int C[101];

int solve(int n, int r[], int c[]) {
  if (n == 1)
    return 0;

  if (n == 2) {
    assert(c[0] == r[1]);
    return r[0] * c[0] * c[1];
  }

  int min = INF;
  int tmp;
  rep(i, 0, n - 1) {

    // M[i]*M[i+1]?????????????????¨??????r[],c[]?????????
    int rr[n - 1], cc[n - 1];
    rep(j, 0, n - 1) {
      if (j < i) {
        rr[j] = r[j];
        cc[j] = c[j];
      } else if (j == i) {
        rr[j] = r[i];
        cc[j] = c[i + 1];
      } else {
        rr[j] = r[j + 1];
        cc[j] = c[j + 1];
      }
    }

    // M[i]*M[i+1]?????????????´???°???1????????£???n-1??¨?????£?????????????????°???????§£???
    //????????????ans???M[i]*M[i+1]????¨?????????°??????????????????
    assert(c[i] == r[i + 1]);
    tmp = solve(n - 1, rr, cc) + r[i] * c[i] * c[i + 1];

    if (tmp < min)
      min = tmp;
  }

  assert(min != INF);
  return min;
}

int main() {
  din >> N;
  rep(i, 0, N) { din >> R[i] >> C[i]; }

  dout << solve(N, R, C) << endl;

#ifdef INPUTFILE
  inputfile.close();
#endif

#ifdef OUTPUTFILE
  outputfile.close();
  cout << "\\"" << OutputFilePath << "\\"" << endl;
#endif

  return 0;
}