alexjercan/bugnet huggingface dataset:
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

const int N = 100;

int main() {
  int n, p[N + 1], m[N + 1][N + 1];
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> p[i - 1] >> p[i];
  for (int i = 1; i <= n; ++i)
    m[i][i] = 0;
  for (int l = 2; l <= n; ++l) {
    for (int i = 1; i <= n - l + 1; ++i) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      for (int k = i; k <= j - 1; ++k) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

  cout << m[1][n] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
const int N = 100;

int main() {
  int n, p[N + 1], m[N + 1][N + 1];
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> p[i - 1] >> p[i];
  }

  for (int i = 1; i <= n; ++i)
    m[i][i] = 0;
  for (int l = 2; l <= n; ++l) {
    for (int i = 1; i <= n - 1; ++i) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      for (int k = i; k <= j - 1; ++k) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

  cout << m[1][n] << endl;
  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

static const int N = 100;

int main() {
  int n, p[N + 1], m[N + 1][N + 1];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i - 1] >> p[i];
  }

  for (int i = 1; i <= n; i++)
    m[i][i] = 0;
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n - l + 1; i++) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      for (int k = i; k <= j - 1; k++) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

  cout << m[1][n] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

static const int N = 100;

int main() {
  int n, p[N + 1], m[N + 1][N + 1];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i - 1] >> p[i];
  }

  for (int i = 1; i <= n; i++)
    m[i][i] = 0;
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n - 1 + 1; i++) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      for (int k = i; k <= j - 1; k++) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

  cout << m[1][n] << endl;

  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define INT_MIN -2147483646
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;

int memo[105][105];
int r[105], c[105];

int func(int a, int b) {
  if (memo[a][b] != INT_MIN)
    return memo[a][b];
  if (b - a == 0) {
    memo[a][b] = 0;
  } else {
    int m = INT_MAX;
    for (int k = a; k < b; k++) {
      m = min(m, func(a, k) + func(k + 1, b) + r[a] * c[k] * c[b]);
    }
    memo[a][b] = m;
  }
  return memo[a][b];
}

int main() {
  int n;
  cin >> n;
  Loop(i, n) {
    Loop(j, n) { memo[i][j] = INT_MIN; }
  }
  Loop(i, n) { cin >> r[i] >> c[i]; }
  cout << func(0, n - 1) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define INT_MIN -2147483646
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;

ll memo[105][105];
int r[105], c[105];

ll func(int a, int b) {
  if (b - a == 0) {
    memo[a][b] = 0;
  } else {
    ll m = INT_MAX;
    for (int k = a; k < b; k++) {
      m = min(m, func(a, k) + func(k + 1, b) + r[a] * c[k] * c[b]);
    }
    memo[a][b] = m;
  }
  return memo[a][b];
}

int main() {
  int n;
  cin >> n;
  Loop(i, n) { cin >> r[i] >> c[i]; }
  cout << func(0, n - 1) << endl;
  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <limits>
#include <vector>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define clr(a, v) memset((a), (v), sizeof(a))

const int MAX_N = 100, INF = std::numeric_limits<int>::max() >> 2;

int n;
int r[MAX_N], c[MAX_N];
std::vector<std::vector<int>> dp(MAX_N, std::vector<int>(MAX_N, INF));

/*int rec( int b, int e )
{
    if( b == e )
        return dp[b][e] = 0;

    if( ~dp[b][e] )
        return dp[b][e];

    int ret = INF;

    rep( i, e-b )
        ret = std::min( ret, rec( b, b+i )+rec( b+i+1, e )+r[b]*c[b+i]*c[e] );

    return dp[b][e] = ret;
}*/

int main() {
  scanf("%d", &n);
  rep(i, n) scanf("%d%d", r + i, c + i);

  rep(i, n) dp[i][i] = 0;

  repi(w, 1, n) rep(i, n) if (i + w < n) repi(j, i, i + w) dp[i][i + w] =
      std::min(dp[i][i + w],
               dp[i][j] + dp[j + 1][i + w] + r[i] * c[j] * c[i + w]);

  printf("%d\\n", dp[0][n - 1]);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <limits>
#include <vector>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define clr(a, v) memset((a), (v), sizeof(a))

const int MAX_N = 100, INF = std::numeric_limits<int>::max() >> 2;

int n;
int r[MAX_N], c[MAX_N];
std::vector<std::vector<int>> dp(MAX_N, std::vector<int>(MAX_N, INF));

/*int rec( int b, int e )
{
    if( b == e )
        return dp[b][e] = 0;

    if( ~dp[b][e] )
        return dp[b][e];

    int ret = INF;

    rep( i, e-b )
        ret = std::min( ret, rec( b, b+i )+rec( b+i+1, e )+r[b]*c[b+i]*c[e] );

    return dp[b][e] = ret;
}*/

int main() {
  scanf("%d", &n);
  rep(i, n) scanf("%d%d", r + i, c + i);

  rep(i, n) dp[i][i] = 0;

  repi(w, 1, n + 1) rep(i, n) repi(j, i, i + w) dp[i][i + w] = std::min(
      dp[i][i + w], dp[i][j] + dp[j + 1][i + w] + r[i] * c[j] * c[i + w]);

  printf("%d\\n", dp[0][n - 1]);

  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
const int MAX_N = 100;
const int INF = 999999999;
using namespace std;

int dp[MAX_N + 1][MAX_N + 1];
int p[MAX_N + 1];

int main(void) {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    dp[i][i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    cin >> p[i - 1] >> p[i];
  }

  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n - l + 1; i++) {
      int j = i + l - 1;
      dp[i][j] = INF;
      for (int k = i; k <= j - 1; k++) {
        dp[i][j] =
            min(dp[i][j], dp[i][k] + dp[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

  cout << dp[1][n] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
const int MAX_N = 100;
const int INF = 999999999;
using namespace std;

int dp[MAX_N + 1][MAX_N + 1];
int p[MAX_N + 1];

int main(void) {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    dp[i][i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    cin >> p[i - 1] >> p[i];
  }

  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n; i++) {
      int j = i + l - 1;
      dp[i][j] = INF;
      for (int k = i; k <= j - 1; k++) {
        dp[i][j] =
            min(dp[i][j], dp[i][k] + dp[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

  cout << dp[1][n] << endl;

  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

#define pb push_back
#define SORT(v, n) sort(v, v + n)
#define ALL(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << '\\n'
#define elif else if
#define itn ll
#define int ll
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
///                →,↑,←,↓,↗,↖,↙,↘
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

static const int N = 100;

signed main() {
  ios::sync_with_stdio(false);
  int n, p[N + 1], m[N + 1][N + 1];
  cin >> n;
  int i;
  FOR(i, 1, n + 1) { cin >> p[i - 1] >> p[i]; }
  FOR(i, 1, n + 1) m[i][i] = 0;
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n - l + 1; i++) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      for (int k = i; k <= j - 1; k++) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }
  cout << m[1][n] << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

#define pb push_back
#define SORT(v, n) sort(v, v + n)
#define ALL(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << '\\n'
#define elif else if
#define itn ll
#define int ll
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
///                →,↑,←,↓,↗,↖,↙,↘
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

static const int N = 100;

signed main() {
  ios::sync_with_stdio(false);
  int n, p[N + 1], m[N + 1][N + 1];
  cin >> n;
  int i;
  FOR(i, 1, n + 1) { cin >> p[i - 1] >> p[i]; }
  FOR(i, 1, n + 1) m[i][i] = 0;
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n - 1; i++) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      for (int k = i; k <= j - 1; k++) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }
  cout << m[1][n] << '\\n';
  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

int mcm(vector<int> &p) {
  vector<vector<int>> m(p.size() - 1, vector<int>(p.size() - 1));
  for (int i = 0; i < m.size(); i++) {
    m[i][i] = 0;
  }
  /*
   * l == number of targets
   * i == left most of targets
   */
  for (int l = 2; l <= m.size(); l++) {
    for (int i = 0; i < m.size() - l + 1; i++) {
      int j = i + l - 1;
      m[i][j] = INT_MAX;
      for (int k = i; k < j; k++) {
        m[i][j] =
            min(m[i][j], m[i][k] + m[k + 1][j] + p[i] * p[k + 1] * p[j + 1]);
      }
    }
  }
  return m[0][m.size() - 1];
}

int main() {
  int n;
  cin >> n;
  vector<int> p(n + 1);
  for (int i = 1; i < n + 1; i++) {
    cin >> p.at(i - 1) >> p.at(i);
  }
  cout << mcm(p) << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

int mcm(vector<int> &p) {
  vector<vector<int>> m(p.size() - 1, vector<int>(p.size() - 1));
  for (int i = 0; i < m.size(); i++) {
    m[i][i] = 0;
  }
  /*
   * l == number of targets
   * i == left most of targets
   */
  for (int l = 2; l <= m.size(); l++) {
    for (int i = 0; i < m.size() - l + 1; i++) {
      int j = i + l - 1;
      m[i][j] = INT_MAX;
      for (int k = i; k < j; k++) {
        m[i][j] =
            min(m[i][j], m[i][k] + m[k + 1][j] + p[i] * p[k + 1] * p[j + 1]);
      }
    }
  }
  return m[0][m.size() - 1];
}

int main() {
  int n, x, y;
  cin >> n;
  vector<int> p(n + 1);
  for (int i = i; i < n + 1; i++) {
    cin >> p.at(i - 1) >> p.at(i);
  }
  cout << mcm(p) << endl;
  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <limits.h>
using namespace std;

int m[101][101]; // m[i][j]: from i to j (columns)
int p[101];      //
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int r, c;
    scanf("%d %d", &r, &c);
    p[i] = r;
    p[i + 1] = c;
  }
  // initialize m[][]
  for (int i = 0; i <= 100; ++i) {
    for (int j = 0; j <= 100; ++j)
      m[i][j] = INT_MAX;
  }
  for (int i = 0; i <= 100; ++i)
    m[i][i] = 0;
  //
  for (int l = 2; l <= n; ++l) {
    for (int i = 1; i <= n - l + 1; ++i) {
      int j = i + l - 1;
      for (int k = i; k < j; ++k) {
        if (m[i][k] == INT_MAX || m[k + 1][j] == INT_MAX)
          continue;
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }
  cout << m[1][n] << '\\n';
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <limits.h>
using namespace std;

int m[101][101]; // m[i][j]: i ?????????j ????????§
int p[101];      //
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int r, c;
    scanf("%d %d", &r, &c);
    p[i] = r;
    p[i + 1] = c;
  }
  // initialize m[][]
  for (int i = 0; i <= 100; ++i) {
    for (int j = 0; j <= 100; ++j)
      m[i][j] = INT_MAX;
  }
  for (int i = 0; i <= 100; ++i)
    m[i][i] = 0;
  //
  for (int l = 2; l <= n; ++l) {
    for (int i = 1; i <= n; ++i) {
      int j = i + l - 1;
      for (int k = i; k < j; ++k) {
        if (m[i][k] == INT_MAX || m[k + 1][j] == INT_MAX)
          continue;
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }
  cout << m[1][n] << '\\n';
}
Problem p02234 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
// https://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/
// return the cost
/*int cal(int a[],int i, int j){
    if(i==j) return 0;
    int min = 1<<30;
    //printf("%d\\n",min);
    for(int k = i; k<j; k++){
        int count = cal(a, i, k)+cal(a,k+1, j)+a[i-1]*a[k]*a[j];
        if(count<min) min = count;
    }

    return min;
}*/

int main() {
  int n;
  scanf("%d", &n);
  int x[n + 2], y[n + 2];
  int dp[n + 2][n + 2];
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &x[i], &y[i]);
    dp[i][i] = 0;
  }
  x[n + 1] = y[n];
  dp[n + 1][n + 1] = 0;
  int min = 1 << 30;
  for (int m = 2; m <= n + 1; m++) {
    for (int i = 2; i <= n + 2 - m; i++) {
      int j = i + m - 1;
      dp[i][j] = 1 << 30;
      for (int k = i; k < j; k++) {
        int count = dp[i][k] + dp[k + 1][j] + x[i - 1] * x[k] * x[j];
        if (count < dp[i][j])
          dp[i][j] = count;
      }
    }
  }

  // printf("%d\\n",cal(x, 2,n+1));
  printf("%d\\n", dp[2][n + 1]);
  return 0;
}


Error: TLE

Fixed submission:

/******************************************************************************

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>

// return the cost
int cal(int a[], int i, int j) {
  if (i == j)
    return 0;
  int min = 1 << 30;
  // printf("%d\\n",min);
  for (int k = i; k < j; k++) {
    int count = cal(a, i, k) + cal(a, k + 1, j) + a[i - 1] * a[k] * a[j];
    if (count < min)
      min = count;
  }

  return min;
}

int main() {
  int n;
  scanf("%d", &n);
  int x[n + 2], y[n + 2];
  int dp[n + 1][n + 1];
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &x[i], &y[i]);
  }
  x[n + 1] = y[n];

  printf("%d\\n", cal(x, 2, n + 1));
  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <map>
#define MAX_NUM 100
#define INFTY 1000000000
#define NIL -1

using namespace std;

// think easy make easy

int n; // num of input

pair<int, int> MatrixChain[MAX_NUM + 1];
map<pair<int, int>, int> mapMC;

int calc(int s, int e) { // s~eの連鎖行列積の最少計算回数を出力
  int min_time = INFTY;
  int min_tmp;

  if (mapMC[pair<int, int>(s, e)] != NIL)
    return mapMC[pair<int, int>(s, e)];

  if (s == e) {
    return 0;
  }
  for (int i = s; i < e; i++) {
    min_tmp =
        calc(s, i) + calc(i + 1, e) +
        MatrixChain[s].first * MatrixChain[i].second * MatrixChain[e].second;
    if (min_tmp < min_time)
      min_time = min_tmp;
  }
  mapMC[pair<int, int>(s, e)] = min_time; // メモ
  return min_time;
}

int main() {
  scanf("%d", &n);
  int r, c; // row,column

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &r, &c);
    MatrixChain[i] = pair<int, int>(r, c); // i:1~n-1
  }
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      mapMC[pair<int, int>(i, j)] = NIL;
    }
  }

  cout << calc(0, n - 1) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#define MAX_NUM 100
#define INFTY 1000000000

using namespace std;

// think easy make easy

int n; // num of input

pair<int, int> MatrixChain[MAX_NUM + 1];

int calc(int s, int e) { // s~eの連鎖行列積の最少計算回数を出力
  int min_time = INFTY;

  int min_tmp;
  if (s == e) {
    return 0;
  }
  for (int i = s; i < e; i++) {
    min_tmp =
        calc(s, i) + calc(i + 1, e) +
        MatrixChain[s].first * MatrixChain[i].second * MatrixChain[e].second;
    if (min_tmp < min_time)
      min_time = min_tmp;
  }
  return min_time;
}

int main() {
  scanf("%d", &n);
  int r, c; // row,column

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &r, &c);
    MatrixChain[i] = pair<int, int>(r, c); // i:1~n-1
  }

  cout << calc(0, n - 1) << endl;

  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
#define range(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repR(i, n) for (int i = (n)-1; i > -1; i--)
#define rep1(i, n) for (int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for (int i = (n); i > 0; i--)
using namespace std;

int n;
int m[110][110];
int p[110];

int matrixChainMultiplication() {
  rep1(i, n) m[i][i] = 0;

  range(l, 2, n) {
    rep1(i, n - l + 1) {
      int j = i + l - 1;
      m[i][j] = INT_MAX;
      range(k, i, j - 1) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }
  return m[1][n];
}

int main() {
  cin >> n;
  rep1(i, n) { cin >> p[i - 1] >> p[i]; }
  cout << matrixChainMultiplication() << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
#define range(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repR(i, n) for (int i = (n)-1; i > -1; i--)
#define rep1(i, n) for (int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for (int i = (n); i > 0; i--)
using namespace std;

int n;
int m[110][110];
int p[110];

int matrixChainMultiplication() {
  rep1(i, n) m[i][i] = 0;

  range(l, 2, n) {
    rep1(i, n - 1 + 1) {
      int j = i + l - 1;
      m[i][j] = INT_MAX;
      range(k, i, j - 1) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }
  return m[1][n];
}

int main() {
  cin >> n;
  rep1(i, n) { cin >> p[i - 1] >> p[i]; }
  cout << matrixChainMultiplication() << endl;
  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int calc(const vector<pair<int, int>> &M, int idx_b, int idx_e,
         vector<vector<int>> &Memo) {
  if (idx_b == idx_e)
    return 0;
  if (Memo[idx_b][idx_e])
    return Memo[idx_b][idx_e];
  int min = 2e9;
  for (int i = idx_b; i < idx_e; i++) {
    int val = calc(M, idx_b, i, Memo) + calc(M, i + 1, idx_e, Memo) +
              M[idx_b].first * M[i].second * M[idx_e].second;
    if (val < min)
      min = val;
  }
  return Memo[idx_b][idx_e] = min;
}

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> M;

  for (int i = 0; i < n; i++) {
    pair<int, int> input;
    cin >> input.first >> input.second;
    M.push_back(input);
  }
  vector<int> tmp(n, 0);
  vector<vector<int>> Memo(n, tmp);
  cout << calc(M, 0, M.size() - 1, Memo) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int calc(const vector<pair<int, int>> &M, int idx_b, int idx_e) {
  if (idx_b == idx_e)
    return 0;
  int min = 2e9;
  for (int i = idx_b; i < idx_e; i++) {
    int val = 0;
    val = calc(M, idx_b, i);
    val += calc(M, i + 1, idx_e);
    val += M[idx_b].first * M[i].second * M[idx_e].second;
    if (val < min)
      min = val;
  }
  return min;
}

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> M;

  for (int i = 0; i < n; i++) {
    pair<int, int> input;
    cin >> input.first >> input.second;
    M.push_back(input);
  }
  cout << calc(M, 0, M.size() - 1) << endl;

  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
#define inf 1e9 + 7

int n;
ll dp[102][102];
ll p[102];

void init() {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = inf;
    }
    dp[i][i] = 0;
  }
}

void cal(int l, int r) {
  if (l >= r)
    return;
  if (dp[l][r] < inf)
    return;
  for (int k = l; k < r; k++) {
    cal(l, k);
    cal(k + 1, r);
    dp[l][r] =
        min(dp[l][r], dp[l][k] + dp[k + 1][r] + p[l] * p[k + 1] * p[r + 1]);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  init();
  cin >> p[0] >> p[1];
  ll a, b;
  for (int i = 1; i < n; i++) {
    cin >> a >> b;
    p[i + 1] = b;
  }
  cal(0, n - 1);
  cout << dp[0][n - 1] << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
#define inf 1e9 + 7

int n;
ll dp[102][102];
ll p[102];

void init() {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = inf;
    }
    dp[i][i] = 0;
  }
}

void cal(int l, int r) {
  if (l >= r)
    return;
  for (int k = l; k < r; k++) {
    cal(l, k);
    cal(k + 1, r);
    dp[l][r] =
        min(dp[l][r], dp[l][k] + dp[k + 1][r] + p[l] * p[k + 1] * p[r + 1]);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  init();
  cin >> p[0] >> p[1];
  ll a, b;
  for (int i = 1; i < n; i++) {
    cin >> a >> b;
    p[i + 1] = b;
  }
  cal(0, n - 1);
  cout << dp[0][n - 1] << endl;
  return 0;
}