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

#ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var)                                                            \\
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

constexpr auto no_value = numeric_limits<uint64_t>::max();

uint64_t solve(const vector<pair<int, int>> &mat) {
  if (mat.size() < 2) {
    return 0ULL;
  }
  auto n = mat.size();
  vector<vector<uint64_t>> cache(n - 1, vector<uint64_t>(n, no_value));
  for (size_t range = 2; range <= n; ++range) {
    for (size_t first = 0;; ++first) {
      auto last = first + range - 1;
      if (last == n) {
        break;
      }
      for (size_t mid = first; mid != last; ++mid) {
        auto lhs = first == mid ? 0 : cache[first][mid];
        auto rhs = (mid + 1) == last ? 0 : cache[mid + 1][last];
        auto mul = mat[first].first * mat[last].second * mat[mid].second;
        cache[first][last] = min(cache[first][last], lhs + rhs + mul);
      }
    }
  }
  return cache[0][n - 1];
}

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> mat;
  for (int i = 0; i < n; ++i) {
    int row, col;
    cin >> row >> col;
    mat.push_back(make_pair(row, col));
  }
  uint64_t result = solve(mat);
  cout << result << endl;
}

Error: 0

Fixed submission:

#ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var)                                                            \\
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

constexpr auto no_value = numeric_limits<uint64_t>::max();

uint64_t solve(const vector<pair<int, int>> &mat) {
  auto n = mat.size();
  vector<vector<uint64_t>> cache(n - 1, vector<uint64_t>(n, no_value));
  for (size_t range = 2; range <= n; ++range) {
    for (size_t first = 0;; ++first) {
      auto last = first + range - 1;
      if (last == n) {
        break;
      }
      for (size_t mid = first; mid != last; ++mid) {
        auto lhs = first == mid ? 0 : cache[first][mid];
        auto rhs = (mid + 1) == last ? 0 : cache[mid + 1][last];
        auto mul = mat[first].first * mat[last].second * mat[mid].second;
        cache[first][last] = min(cache[first][last], lhs + rhs + mul);
      }
    }
  }
  return cache[0][n - 1];
}

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> mat;
  for (int i = 0; i < n; ++i) {
    int row, col;
    cin >> row >> col;
    mat.push_back(make_pair(row, col));
  }
  uint64_t result = solve(mat);
  cout << result << endl;
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define N 110
using namespace std;
int A[N];
int dp[N][N];

int Min(int &a, int b) { return a = min(a, b); }
int main() {
  int n;
  cin >> n;

  for (int i = 0, cnt = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    if (i == 0)
      A[cnt++] = a;
    A[cnt++] = b;
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      dp[i][j] = 1e9;
    dp[i][i] = 0;
  }

  for (int len = 0; len < n; len++)
    for (int l = 1; l < n; l++) {
      int r = l + len;
      if (l + len >= N)
        continue;
      for (int m = l; m < r; m++) {
        Min(dp[l][r], dp[l][m] + dp[m + 1][r] + A[l - 1] * A[m] * A[r]);
      }
    }

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 110
using namespace std;
int A[N];
int dp[N][N];

int Min(int &a, int b) { return a = min(a, b); }
int main() {
  int n;
  cin >> n;

  for (int i = 0, cnt = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    if (i == 0)
      A[cnt++] = a;
    A[cnt++] = b;
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      dp[i][j] = 1e9;
    dp[i][i] = 0;
  }

  for (int len = 0; len < n; len++)
    for (int l = 1; l < n; l++) {
      int r = l + len;
      for (int m = l; m < r; m++) {
        Min(dp[l][r], dp[l][m] + dp[m + 1][r] + A[l - 1] * A[m] * A[r]);
      }
    }

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int MAX_L = 10000;
int N;

struct Matrix {
  int R, W;
};
Matrix M[MAX_N];

int mem[MAX_L][MAX_L];
Matrix memM[MAX_L][MAX_L];

void eval(Matrix M[], int s, int e) {

  if (s == e) {
    mem[s][e] = 0;
    memM[s][e] = M[s];
    return;
  }

  int minval = 1000000000;
  for (int mid = s; mid < e; mid++) {
    Matrix m1 = memM[s][mid];
    Matrix m2 = memM[mid + 1][e];
    int val = mem[s][mid] + mem[mid + 1][e] + (m1.W * m1.R * m2.W);

    if (val < minval) {
      minval = val;
      mem[s][e] = val;
      Matrix m;
      m.R = m1.R;
      m.W = m2.W;
      memM[s][e] = m;
    }
  }
}

int solve(Matrix M[], int N) {

  for (int d = 0; d <= N; d++)
    for (int s = 0; s + d <= N; s++) {
      eval(M, s, s + d);
    }

  return mem[0][N - 1];
}

int main() {

  cin >> N;
  rep(i, N) {
    Matrix m;
    cin >> m.R;
    cin >> m.W;
    M[i] = m;
  }

  cout << solve(M, N) << endl;

#ifdef DEBUGRUN
  rep(i, N) {
    rep(j, N) { cerr << mem[i][j] << ' '; }
    cerr << endl;
  }
#endif
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int MAX_L = 10000;
int N;

struct Matrix {
  int R, W;
};
Matrix M[MAX_N];

int mem[MAX_L][MAX_L];
Matrix memM[MAX_L][MAX_L];

void eval(Matrix M[], int s, int e) {

  if (s == e) {
    mem[s][e] = 0;
    memM[s][e] = M[s];
    return;
  }

  int minval = 1000000000;
  for (int mid = s; mid < e; mid++) {
    Matrix m1 = memM[s][mid];
    Matrix m2 = memM[mid + 1][e];
    int val = mem[s][mid] + mem[mid + 1][e] + (m1.W * m1.R * m2.W);

    if (val < minval) {
      minval = val;
      mem[s][e] = val;
      Matrix m;
      m.R = m1.R;
      m.W = m2.W;
      memM[s][e] = m;
    }
  }
}

int solve(Matrix M[], int N) {

  for (int d = 0; d <= N; d++)
    for (int s = 0; s + d <= N; s++) {
      eval(M, s, s + d);
    }

  return mem[0][N];
}

int main() {

  cin >> N;
  rep(i, N) {
    Matrix m;
    cin >> m.R;
    cin >> m.W;
    M[i] = m;
  }

  cout << solve(M, N) << endl;

  // debug
  rep(i, N) {
    rep(j, N) { cerr << mem[i][j] << ' '; }
    cerr << endl;
  }
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <memory.h>
#include <sstream>
#include <string>
#include <vector>
typedef unsigned long long ull;
typedef unsigned long ul;
#define rep(i, n) for (unsigned long long i = 0; i < n; i++)
#define clear(c) memset(c, 0, sizeof(c))
#define clearm(c) memset(c, -1, sizeof(c))
using namespace std;

int main() {
  int n;

  cin >> n;
  ull dp[100 + 1][100 + 1];
  clearm(dp);
  ull r[n], c[n];
  rep(i, n) {
    int rr, cc;
    cin >> rr >> cc;
    r[i] = rr;
    c[i] = cc;
  }
  rep(i, n - 1) { dp[i][i + 1] = r[i] * c[i + 1] * r[i + 1]; }
  rep(i, n) { dp[i][i] = 0; }
  for (int dif = 2; dif < n; dif++) {
    for (int i = 0; i + dif < n; i++) {
      for (int j = i + 1; j <= i + dif - 1; j++) {
        dp[i][i + dif] = min(dp[i][i + dif], dp[i][j] + dp[j + 1][i + dif] +
                                                 r[i] * c[j] * c[i + dif]);
      }
    }
  }
  cout << dp[0][n - 1] << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <memory.h>
#include <sstream>
#include <string>
#include <vector>
typedef unsigned long long ull;
typedef unsigned long ul;
#define rep(i, n) for (unsigned long long i = 0; i < n; i++)
#define clear(c) memset(c, 0, sizeof(c))
#define clearm(c) memset(c, -1, sizeof(c))
using namespace std;

int main() {
  int n;

  cin >> n;
  ull dp[10 + 1][10 + 1];
  clearm(dp);
  ull r[n], c[n];
  rep(i, n) {
    int rr, cc;
    cin >> rr >> cc;
    r[i] = rr;
    c[i] = cc;
  }
  rep(i, n - 1) { dp[i][i + 1] = r[i] * c[i + 1] * r[i + 1]; }
  rep(i, n) { dp[i][i] = 0; }
  for (int dif = 2; dif < n; dif++) {
    for (int i = 0; i + dif < n; i++) {
      for (int j = i + 1; j <= i + dif - 1; j++) {
        dp[i][i + dif] = min(dp[i][i + dif], dp[i][j] + dp[j + 1][i + dif] +
                                                 r[i] * c[j] * c[i + dif]);
      }
    }
  }
  cout << dp[0][n - 1] << endl;
  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
#define INF (99999999)
int main() {

  //-------データの入力------------
  int n;
  cin >> n;
  int row[n];
  int column[n];
  for (int i = 0; i < n; i++) {
    cin >> row[i] >> column[i];
  }
  //-----------------------------

  //[始点][終点（始点+幅）]の最小コスト
  int dp[n][n];

  // INFで初期化
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = INF;
    }
  }

  // 幅0はコスト0
  // dp[i][i+0] = 0
  for (int i = 0; i < n; i++) {
    dp[i][i] = 0;
  }

  // 幅:j = 0 から探索開始
  for (int j = 0; j < n; j++) {
    // 始点: i
    for (int i = 0; i < n - j; i++) {
      // 仕切りを置く位置: k
      //  (M_0, M_1, M_2, M_3) | (M_4, M_5)
      for (int k = i + 1; k <= i + j; k++) {
        // 現在のdp[i][i+j]より小さければ更新
        dp[i][i + j] =
            min(dp[i][i + j], dp[i][k - 1] + dp[k][i + j] +
                                  row[i] * column[k - 1] * column[i + j]);
      }
    }
  }
  /*
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  */
  // 始点０、幅n-1の最小コスト=>
  // n個の行列の連鎖の計算に最低限必要な乗算の回数　を出力
  cout << dp[0][n - 1] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define INF (1 << 21)
int main() {

  //-------データの入力------------
  int n;
  cin >> n;
  int row[n];
  int column[n];
  for (int i = 0; i < n; i++) {
    cin >> row[i] >> column[i];
  }
  //-----------------------------

  //[始点][終点（始点+幅）]の最小コスト
  int dp[n + 1][n + 1];

  // INFで初期化
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = INF;
    }
  }

  // 幅0はコスト0
  // dp[i][i+0] = 0
  for (int i = 0; i < n; i++) {
    dp[i][i] = 0;
  }

  // 幅:j = 0 から探索開始
  for (int j = 0; j < n; j++) {
    // 始点: i
    for (int i = 0; i < n; i++) {
      // 仕切りを置く位置: k
      //  (M_0, M_1, M_2, M_3) | (M_4, M_5)
      for (int k = i + 1; k <= i + j; k++) {
        // 現在のdp[i][i+j]より小さければ更新
        dp[i][i + j] =
            min(dp[i][i + j], dp[i][k - 1] + dp[k][i + j] +
                                  row[i] * column[k - 1] * column[i + j]);
      }
    }
  }
  /*
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  */
  // 始点０、幅n-1の最小コスト=>
  // n個の行列の連鎖の計算に最低限必要な乗算の回数　を出力
  cout << dp[0][n - 1] << endl;
  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = a; i < (n); i++)
#define mod (ll)(1e9 + 7)
#define int ll

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

signed main() {
  int n, p[101], m[101][101];
  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 <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = a; i < (n); i++)
#define mod (ll)(1e9 + 7)
#define int ll

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

signed main() {
  int n, p[101], m[101][101];
  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; 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 <iostream>
using namespace std;

int main() {
  int n;
  int v[101];
  long m[100][100];
  const long nil = 999999999;

  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> v[i] >> v[i + 1];

  for (int i = 0; i < 100; i++)
    for (int j = 0; j < 100; j++)
      if (i == j)
        m[i][j] = 0;
      else
        m[i][j] = nil;

  for (int i = n - 2; i > -1; i--)
    for (int j = i + 1; j < n; j++)
      for (int k = i; k < n; k++)
        m[i][j] =
            min(m[i][j], m[i][k] + m[k + 1][j] + v[i] * v[k + 1] * v[j + 1]);

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

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string.h>
using namespace std;

int n, a[100], v[101];
long sum, m = 1000000000;
bool f[100], g[100];

void permsum(int p) {
  if (p == n - 1) {
    sum = 0;
    memset(g, 0, 100);
    for (int i = 0; i < n - 1; i++) {
      g[a[i]] = 1;
      int u = 1, w = 1;
      while (g[a[i] - u])
        u++;
      while (g[a[i] + w])
        w++;
      sum += v[a[i] - u] * v[a[i]] * v[a[i] + w];
    }
    if (m > sum)
      m = sum;
  } else {
    for (int j = 0; j < n - 1; j++) {
      if (!f[j]) {
        a[p] = j + 1;
        f[j] = 1;
        permsum(p + 1);
        f[j] = 0;
      }
    }
  }
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> v[i] >> v[i + 1];

  permsum(0);

  cout << m << endl;

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

#include <algorithm>
#include <iostream>

int main() {
  int n, p[101], m[101][101];
  std::cin >> n;
  for (int i = 1; i <= n; i++) {
    std::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] =
            std::min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }
  std::cout << m[1][n] << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

int main() {
  int n, p[101], m[101][101];
  std::cin >> n;
  for (int i = 1; i <= n; i++) {
    std::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] =
            std::min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }
  std::cout << m[1][n] << std::endl;
  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

///
// File:  alds1_10_b.cpp
// Author: ymiyamoto
//
// Created on Tue Oct 17 21:37:26 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

static vector<uint32_t> matrix(101, 0);
static uint64_t dp[101][101];

int32_t main() {
  uint32_t n;
  cin >> n;

  for (uint32_t i = 1; i <= n; i++) {
    cin >> matrix[i - 1] >> matrix[i];
  }

  for (uint32_t l = 2; l <= n; l++) {
    for (uint32_t i = 1; i <= n - l + 1; i++) {
      uint32_t j = i + l - 1;
      uint64_t min_val = UINT64_MAX;
      for (uint32_t k = i; k <= j - 1; k++) {
        min_val = min(min_val, dp[i][k] + dp[k + 1][j] +
                                   matrix[i - 1] * matrix[k] * matrix[j]);
      }
      dp[i][j] = min_val;
    }
  }

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

  return 0;
}

Error: -11

Fixed submission:

#include <cstdint>
#include <vector>

using namespace std;

#define MAX_MEMO_SIZE 101

static size_t memo[MAX_MEMO_SIZE][MAX_MEMO_SIZE];

size_t matrix_chain_multiplication(size_t num, vector<uint32_t> matrix) {
  for (uint32_t i = 0; i <= num; i++) {
    for (uint32_t j = i; j <= num; j++) {
      if (i == j) {
        memo[i][j] = 0;
      } else {
        size_t size = SIZE_MAX;

        for (uint32_t k = i; k <= j; j++) {
          size = min(size, memo[i][k] + memo[k + 1][j] +
                               matrix[i - 1] * matrix[k] * matrix[j]);
        }
        memo[i][j] = size;
      }
    }
  }

  return memo[num][num];
}

#include <iostream>

static vector<uint32_t> matrix;

int32_t main() {
  uint32_t num;
  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    uint32_t num1, num2;
    if (i == 0) {
      cin >> num1 >> num2;
      matrix.push_back(num1);
      matrix.push_back(num2);
    } else {
      cin >> num1 >> num2;
      matrix.push_back(num2);
    }
  }
  cout << matrix_chain_multiplication(num, matrix);

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

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

struct Matrix {
  int r, c;
};

long f(vector<Matrix> &m, int begin, int end, vector<vector<long>> &memo) {
  if (m.size() < 2 || (end - begin) < 2)
    return 0;
  if (memo[begin][end] != -1) {
    return memo[begin][end];
  }
  if ((end - begin) == 2) {
    return memo[begin][end] = m[begin].r * m[begin].c * m[begin + 1].c;
  }
  long common = m[begin].r * m[end - 1].c;
  long res = common * m[begin + 1].r + f(m, begin + 1, end, memo);
  for (int i = 2; i < (end - begin); ++i) {
    long t = f(m, begin, begin + i, memo) + f(m, begin + i, end, memo) +
             common * m[begin + i].r;
    res = min(res, t);
  }
  return memo[begin][end] = res;
}

int main() {
  int n;
  cin >> n;
  vector<Matrix> M(n);
  vector<vector<long>> memo(n);
  for (int i = 0; i < n; ++i) {
    int r, c;
    cin >> r >> c;
    M[i].r = r;
    M[i].c = c;
    memo[i].resize(n + 1);
    for (int j = 0; j < memo[i].size(); ++j)
      memo[i][j] = -1;
  }
  cout << f(M, 0, M.size(), memo) << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

struct Matrix {
  int r, c;
};

long f(vector<Matrix> &m, int begin, int end) {
  if (m.size() < 2 || (end - begin) < 2)
    return 0;
  if ((end - begin) == 2) {
    return m[begin].r * m[begin].c * m[begin + 1].c;
  }
  long common = m[begin].r * m[end - 1].c;
  long res = common * m[begin + 1].r + f(m, begin + 1, end);
  for (int i = 2; i < (end - begin); ++i) {
    long t =
        f(m, begin, begin + i) + f(m, begin + i, end) + common * m[begin + i].r;
    res = min(res, t);
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<Matrix> M(n);
  for (int i = 0; i < n; ++i) {
    int r, c;
    cin >> r >> c;
    M[i].r = r;
    M[i].c = c;
  }
  cout << f(M, 0, M.size()) << endl;
  return 0;
}
Problem p02234 (C++) — buggy submission, verdict: Runtime Error

#include <cassert>
// #include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

struct TSize {
  int r;
  int c;
  TSize() : r(0), c(0) {}
  TSize(int Ar, int Ac) : r(Ar), c(Ac) {}
};

class TMcmSolver {
  typedef std::vector<std::vector<int>> TMatrix;
  TMatrix FMemo;
  const std::vector<TSize> &FChain;

  static int Count(const TSize &Left, const TSize &Right) {
    return Left.r * Right.c * Left.c;
  }
  int Solve(int s, int l) {
    assert(s <= l);
    if (s == l)
      return 0;
    if (FMemo[s][l] != 0)
      return FMemo[s][l];
    if (s + 1 == l) {
      FMemo[s][l] = Count(FChain[s], FChain[l]);
    } else {
      int min = INT_MAX;
      for (int i = s + 1; i < l; ++i) {
        int c = Solve(s, i) +
                Count(TSize(FChain[s].r, FChain[i].c),
                      TSize(FChain[i + 1].r, FChain[l].c)) +
                Solve(i + 1, l);
        min = std::min(min, c);
      }
      FMemo[s][l] = min;
    }
    return FMemo[s][l];
  }

public:
  TMcmSolver(const std::vector<TSize> &Chain) : FChain(Chain) {
    FMemo.resize(Chain.size());
    for (std::size_t i = 0; i < FMemo.size(); ++i) {
      FMemo[i].resize(Chain.size(), 0);
    }
  }
  int Answer() { return Solve(0, FMemo.size() - 1); }
  void Print() const {
    for (std::size_t s = 0; s < FMemo.size(); ++s) {
      for (std::size_t l = 0; l < FMemo[0].size(); ++l) {
        std::cout << FMemo[s][l] << '\\t';
      }
      std::cout << '\\n';
    }
  }
};

int main() {
  int n;
  std::cin >> n;
  if (n == 1) {
    std::cout << 0 << std::endl;
    return 0;
  }
  std::vector<TSize> Chain(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> Chain[i].r >> Chain[i].c;
  }
  TMcmSolver Solver(Chain);

  std::cout << Solver.Answer() << std::endl;
  // Solver.Print();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>

struct Matrix {
  int r;
  int c;
};
int Multiple(std::list<Matrix>::iterator Left) {
  std::list<Matrix>::iterator Right = Left;
  Right++;
  int Count = Left->r * Right->c * Left->c;
  Right->r = Left->r;
  return Count;
}
bool CompareR(const Matrix &Left, const Matrix &Right) {
  return Left.r < Right.r;
}
bool CompareC(const Matrix &Left, const Matrix &Right) {
  return Left.c < Right.c;
}
int Mcm(const std::list<Matrix> &AList) {
  std::list<Matrix> List = AList;
  const std::list<Matrix>::iterator MinR =
      std::min_element(List.begin(), List.end(), CompareR);
  const std::list<Matrix>::iterator MinC =
      std::min_element(List.begin(), List.end(), CompareC);
  int Count = 0;
  for (std::list<Matrix>::iterator It = MinC; It != List.begin(); It) {
    --It;
    Count += Multiple(It);
    It = List.erase(It);
  }
  for (std::list<Matrix>::iterator It = MinR; It != --List.end(); It) {
    Count += Multiple(It);
    It = List.erase(It);
  }
  Count += Multiple(List.begin());
  return Count;
}

int main() {
  std::list<Matrix> List;
  int n;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    Matrix m;
    std::cin >> m.r >> m.c;
    List.push_back(m);
  }

  std::cout << Mcm(List) << std::endl;
  return 0;
}