prithivMLmods/Coder-Stat huggingface dataset:
Problem p02128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<lint, pii> plpii;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

const int N = 1010;
const lint inf = 1e16;

int main() {
  int w, h, n;
  cin >> w >> h >> n;
  int mi = 1e8;
  vector<pii> pts(n + 2);
  pts[0] = pii(1, 1);
  pts[1] = pii(w, h);
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    pts[i + 2] = pii(x, y);
  }
  priority_queue<plpii, vector<plpii>, greater<plpii>> que;
  vector<vector<lint>> dis(n + 2, vector<lint>(N, inf));
  rep(i, n) {
    int d = abs(pts[i + 2].first - 1) + abs(pts[i + 2].second - 1);
    que.push(plpii(d, pii(i + 2, d)));
  }
  while (!que.empty()) {
    plpii top = que.top();
    que.pop();
    lint d = top.first;
    pii xy = top.second;
    int x = xy.first, y = xy.second;
    if (dis[x][y] <= d)
      continue;
    dis[x][y] = d;
    rep(i, n + 2) {
      if (i == x || i == 0)
        continue;
      int c =
          abs(pts[i].first - pts[x].first) + abs(pts[i].second - pts[x].second);
      c = max(0, i == 1 ? c - y : (c - y - 1));
      if (c >= N)
        continue;
      que.push(plpii(d + c, pii(i, i == 1 ? 0 : c)));
    }
  }
  cout << dis[1][0] << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<lint, pii> plpii;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

const int N = 1010;
const lint inf = 1e16;

int main() {
  int w, h, n;
  cin >> w >> h >> n;
  int mi = 1e8;
  vector<pii> pts(n + 2);
  pts[0] = pii(1, 1);
  pts[1] = pii(w, h);
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    pts[i + 2] = pii(x, y);
  }
  priority_queue<plpii, vector<plpii>, greater<plpii>> que;
  vector<vector<lint>> dis(n + 2, vector<lint>(N, inf));
  rep(i, n) {
    int d = abs(pts[i + 2].first - 1) + abs(pts[i + 2].second - 1);
    que.push(plpii(d, pii(i + 2, d)));
  }
  while (!que.empty()) {
    plpii top = que.top();
    que.pop();
    lint d = top.first;
    pii xy = top.second;
    int x = xy.first, y = xy.second;
    if (dis[x][y] <= d)
      continue;
    dis[x][y] = d;
    rep(i, n + 2) {
      if (i == x || i == 0)
        continue;
      int c =
          abs(pts[i].first - pts[x].first) + abs(pts[i].second - pts[x].second);
      c = max(0, i == 1 ? c - y : (c - y - 1));
      if (c >= N)
        continue;
      int nx = i;
      int ny = i == 1 ? 0 : c;
      if (dis[nx][ny] <= d + c)
        continue;
      dis[nx][ny] = d + c + 1;
      que.push(plpii(d + c, pii(nx, ny)));
    }
  }
  cout << dis[1][0] << endl;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int n;
int F[45];
int fibo(int i) {
  if (F[i]) {
    return F[i];
  }
  return fibo(i - 1) + fibo(i - 2);
}

int main() {
  cin >> n;
  F[0] = F[1] = 1;
  cout << fibo(n) << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int n;
int F[45];
int fibo(int i) {
  if (F[i]) {
    return F[i];
  }
  F[i] = fibo(i - 1) + fibo(i - 2);
  return F[i];
}

int main() {
  cin >> n;
  F[0] = F[1] = 1;
  cout << fibo(n) << endl;

  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int A[50];
int fib(int n) {
  if (n == 1 || n == 0)
    A[n] = 1;
  else
    A[n] = fib(n - 1) + fib(n - 2);
  return A[n];
}

int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int A[50];
int fib(int n) {
  if (n == 1 || n == 0)
    A[n] = 1;

  else if (A[n])
    ;
  else
    A[n] = fib(n - 1) + fib(n - 2);
  return A[n];
}

int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int f[50];

int fibo(int n) {
  if (n <= 1)
    return 1;
  return f[n] = fibo(n - 1) + fibo(n - 2);
}

int main() {
  int n;
  cin >> n;

  cout << fibo(n) << endl;
  return 0;
}


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

int f[50];

int fibo(int n) {
  if (n <= 1)
    return 1;
  else {
    if (f[n]) {
      return f[n];
    } else {
      return f[n] = fibo(n - 1) + fibo(n - 2);
    }
  }
}

int main() {
  int n;
  cin >> n;

  cout << fibo(n) << endl;
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

#define MAX 50

int db[MAX];

int fibonacci(int n) {
  if (n == 0 || n == 1)
    return 1;
  if (db[n] != -1)
    return db[n];
  return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
  int n;
  for (int i = 0; i < MAX; ++i)
    db[i] = -1;

  scanf("%d", &n);
  int result = fibonacci(n);
  printf("%d\n", result);
  return 0;
}

Fixed submission:
#include <stdio.h>

#define MAX 50

int db[MAX];

int fibonacci(int n) {
  if (n == 0 || n == 1)
    return 1;
  if (db[n] != -1)
    return db[n];
  return db[n] = fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
  int n;
  for (int i = 0; i < MAX; ++i)
    db[i] = -1;

  scanf("%d", &n);
  int result = fibonacci(n);
  printf("%d\n", result);
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;
int fib(int n);
int memo[45];
int main() {
  int n;

  // 入力
  cin >> n;

  // 出力
  cout << fib(n) << endl;

  return 0;
}

int fib(int n) {
  if (n == 0 || n == 1) {
    return memo[n] = 1;
  } else {
    return memo[n] = fib(n - 1) + fib(n - 2);
  }
}


Fixed submission:
#include <iostream>

using namespace std;
int fib(int n);
int memo[45];
int main() {
  int n;

  // 入力
  cin >> n;

  // 出力
  cout << fib(n) << endl;

  return 0;
}

int fib(int n) {
  if (n == 0 || n == 1) {
    return 1;
  } else if (memo[n] != 0) {
    return memo[n];
  } else {
    return memo[n] = fib(n - 1) + fib(n - 2);
  }
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
int calc[45];

int fib(int n) {
  if (calc[n] != 0)
    return calc[n];
  return fib(n - 1) + fib(n - 2);
}

int main() {
  int n;
  cin >> n;
  REP(i, 45) calc[i] = 0;
  calc[0] = 1;
  calc[1] = 1;
  cout << fib(n) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
int calc[45];

int fib(int n) {
  if (calc[n] != 0)
    return calc[n];
  for (int i = 2; i <= n; i++)
    calc[i] = calc[i - 2] + calc[i - 1];
  return calc[n];
}

int main() {
  int n;
  cin >> n;
  REP(i, 45) calc[i] = 0;
  calc[0] = 1;
  calc[1] = 1;
  cout << fib(n) << endl;
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int dp[45] = {0};
int fib(int n) {
  if (dp[n] != 0)
    return dp[n];
  if (n == 0 || n == 1)
    return 1;
  return fib(n - 1) + fib(n - 2);
}

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int ans = fib(n);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int dp[45] = {0};
int fib(int n) {
  if (dp[n] != 0)
    return dp[n];
  if (n == 0 || n == 1)
    return 1;
  return dp[n] = fib(n - 1) + fib(n - 2);
}

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int ans = fib(n);
  cout << ans << endl;
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
// DP(???????¨??????? Dynamic Programing)
// ????????±???????????¢???
// ?????£??????????????°???
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_A

#include <iostream>
using namespace std;

#define MAX_N 44

struct memo {
  int num;
  bool flg;
} Memo[MAX_N + 1];

int n;
int fib(int);

int main() {
  cin >> n;
  for (int i = 0; i < MAX_N + 1; ++i)
    Memo[i].flg = false;
  cout << fib(n) << endl;
  return 0;
}

int fib(int m) {
  if (m == 0) {
    if (!Memo[0].flg) {
      Memo[0].num = 1;
      Memo[0].flg = true;
    }
    return 1;
  }
  if (m == 1) {
    if (!Memo[1].flg) {
      Memo[1].num = 2;
      Memo[1].flg = true;
    }
    return 1;
  }

  if (Memo[m].flg) {
    return Memo[m].num;
  } else {
    Memo[m].num = fib(m - 1) + fib(m - 2);
    Memo[m].flg = false;
    return fib(m - 1) + fib(m - 2);
  }
}

Fixed submission:
// DP(???????¨??????? Dynamic Programing)
// ????????±???????????¢???
// ?????£??????????????°???
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_A

#include <iostream>
using namespace std;

#define MAX_N 44

struct memo {
  int num;
  bool flg;
} Memo[MAX_N + 1];

int n;
int fib(int);

int main() {
  cin >> n;
  for (int i = 0; i < MAX_N + 1; ++i)
    Memo[i].flg = false;
  cout << fib(n) << endl;
  return 0;
}

int fib(int m) {
  if (m == 0) {
    if (!Memo[0].flg) {
      Memo[0].num = 1;
      Memo[0].flg = true;
    }
    return 1;
  }
  if (m == 1) {
    if (!Memo[1].flg) {
      Memo[1].num = 2;
      Memo[1].flg = true;
    }
    return 1;
  }

  if (Memo[m].flg) {
    return Memo[m].num;
  } else {
    Memo[m].num = fib(m - 1) + fib(m - 2);
    Memo[m].flg = true;
    return fib(m - 1) + fib(m - 2);
  }
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
int d;
int dd[50];
int fib(int a) {
  if (a == 0 || a == 1)
    return dd[a] = 1;
  if (dd[a] != -1)
    return dd[a];
  return fib(a - 1) + fib(a - 2);
}

int main() {
  int c;
  cin >> c;
  for (int i = 0; i < 50; ++i) {
    dd[i] = -1;
  }

  cout << fib(c) << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
int d;
int dd[50];
int fib(int a) {
  if (a == 0 || a == 1)
    return dd[a] = 1;
  if (dd[a] != -1)
    return dd[a];
  return dd[a] = fib(a - 1) + fib(a - 2);
}

int main() {
  int c;
  cin >> c;
  for (int i = 0; i < 50; ++i) {
    dd[i] = -1;
  }

  cout << fib(c) << endl;
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

void makeFibonacci(int fib[]) {
  fib[0] = 1;
  fib[1] = 1;
  for (int i = 2; i <= 44; i++) {
    fib[i] = fib[i - 1] + fib[i - 2];
  }
}

int main() {

  int fib[45];
  makeFibonacci(fib);

  int n;
  scanf("%d", &n);
  printf("%d\n", fib[n]);

  return -1;
}


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

void makeFibonacci(int fib[]) {
  fib[0] = 1;
  fib[1] = 1;
  for (int i = 2; i <= 44; i++) {
    fib[i] = fib[i - 1] + fib[i - 2];
  }
}

int main() {

  int fib[45];
  makeFibonacci(fib);

  int n;
  scanf("%d", &n);
  printf("%d\n", fib[n]);

  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
// input: aldsalds1_10_a.in
#include <iostream>
#define MAX_N 45
#define NIL -1
using namespace std;

int dp[MAX_N];

int fib(int n) {
  if (n == 0 || n == 1)
    return 1;

  if (dp[n] != NIL)
    return dp[n];

  return fib(n - 1) + fib(n - 2);
}

int main() {
  for (int i = 0; i < MAX_N; i++)
    dp[i] = NIL;

  int x;
  cin >> x;

  cout << fib(x) << endl;
}

Fixed submission:
// input: aldsalds1_10_a.in
#include <iostream>
#define MAX_N 45
#define NIL -1
using namespace std;

int dp[MAX_N];

int fib(int n) {
  if (n == 0 || n == 1)
    return 1;

  if (dp[n] != NIL)
    return dp[n];

  int v = fib(n - 1) + fib(n - 2);
  dp[n] = v;

  return v;
}

int main() {
  for (int i = 0; i < MAX_N; i++)
    dp[i] = NIL;

  int x;
  cin >> x;

  cout << fib(x) << endl;
}
Problem p02233 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;

long long Fib(int n) {
  vector<long long> F(n + 1);
  int i;
  F[0] = 1;
  F[1] = 1;
  for (i = 2; i <= n + 1; i++) {
    F[i] = F[i - 1] + F[i - 2];
  }
  return F[n];
}

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

  int n;
  cin >> n;

  cout << Fib(n) << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;

long long Fib(int n) {
  vector<long long> F(n + 1);
  int i;
  F[0] = 1;
  F[1] = 1;
  for (i = 2; i <= n; i++) {
    F[i] = F[i - 1] + F[i - 2];
  }
  return F[n];
}

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

  int n;
  cin >> n;

  cout << Fib(n) << endl;
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>

int fib(int n) {
  int i;
  int a[25];
  a[0] = a[1] = 1;
  for (i = 2; i <= n; i++) {
    a[i] = a[i - 1] + a[i - 2];
  }
  return a[n];
}

int main() {

  int x, n;
  scanf("%d", &n);
  x = fib(n);
  printf("%d\n", x);

  return 0;
}

Fixed submission:
#include <stdio.h>
#include <stdlib.h>

int fib(int n) {
  int i;
  int a[44];
  a[0] = a[1] = 1;
  for (i = 2; i <= n; i++) {
    a[i] = a[i - 1] + a[i - 2];
  }
  return a[n];
}

int main() {

  int x, n;
  scanf("%d", &n);
  x = fib(n);
  printf("%d\n", x);

  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int solution(int n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return 1;
  return solution(n - 1) + solution(n - 2);
}

int main() {
  int n;
  cin >> n;
  cout << solution(n) << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int solution(int n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return 1;
  int one = 1, two = 1, tmp;
  for (int i = 1; i < n; i++) {
    tmp = two;
    two = two + one;
    one = tmp;
  }
  return two;
}

int main() {
  int n;
  cin >> n;
  cout << solution(n) << endl;
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

vector<long long int> fibonatti;
long long int dp[10000];
int calc(int n) {
  if (n <= 1)
    return 1;
  if (dp[n] != -1)
    return dp[n];
  fibonatti[n] = calc(n - 1) + calc(n - 2);
  return dp[n] = fibonatti[n];
}

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  fibonatti.resize(n);
  fibonatti[0] = fibonatti[1] = 1;
  for (int i = 0; i <= n; i++) {
    dp[i] = -1;
  }
  dp[0] = dp[1] = 1;
  std::cout << calc(n) << std::endl;
  return 0;
}

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

vector<long long int> fibonatti;
long long int dp[10000];
int calc(int n) {
  if (n <= 1)
    return 1;
  if (dp[n] != -1)
    return dp[n];
  fibonatti[n] = calc(n - 1) + calc(n - 2);
  return dp[n] = fibonatti[n];
}

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  fibonatti.resize(n + 1);
  fibonatti[0] = fibonatti[1] = 1;
  for (int i = 0; i <= n; i++) {
    dp[i] = -1;
  }
  dp[0] = dp[1] = 1;
  std::cout << calc(n) << std::endl;
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int n, a[50];
int solve(int i) {
  if (i == 1 || i == 0)
    return 1;
  return solve(i - 1) + solve(i - 2);
}
int main() {
  int n;
  cin >> n;
  cout << solve(n) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int n, a[50];
int solve(int i) {
  if (i == 1 || i == 0)
    return 1;
  return solve(i - 1) + solve(i - 2);
}
int main() {
  int n;
  cin >> n;
  int ans[50];
  ans[0] = 1, ans[1] = 1;
  for (int i = 2; i <= n; i++) {
    ans[i] = ans[i - 1] + ans[i - 2];
  }
  cout << ans[n] << endl;
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define NIL -2000000001
#define MAX 2000

int A[MAX];
int n = 0;

int fib(int num) {
  if (A[num] != NIL) {
    return A[num];
  } else {
    return fib(num - 1) + fib(num - 2);
  }
}

int main() {
  int num;
  for (int i = 0; i < MAX; i++) {
    A[i] = NIL;
  }
  A[0] = 1;
  A[1] = 1;
  scanf("%d", &num);
  printf("%d\n", fib(num));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define NIL -2000000001
#define MAX 2000

int A[MAX];
int n = 0;

int fib(int num) {
  if (A[num] != NIL) {
    return A[num];
  } else {
    A[num - 2] = fib(num - 2);
    A[num - 1] = fib(num - 1);
    return A[num - 2] + A[num - 1];
  }
}

int main() {
  int num;
  for (int i = 0; i < MAX; i++) {
    A[i] = NIL;
  }
  A[0] = 1;
  A[1] = 1;
  scanf("%d", &num);
  printf("%d\n", fib(num));
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

long long int Fibo(long long int n) {
  if (n == 0 || n == 1)
    return 1;
  return Fibo(n - 1) + Fibo(n - 2);
}
int main() {
  long long int n;
  std::cin >> n;
  std::cout << Fibo(n) << std::endl;
  return 0;
}

Fixed submission:
#include <iostream>

long long int Fibo(long long int n) {
  if (n == 0 || n == 1)
    return 1;
  if (n == 44)
    return 1134903170;
  return Fibo(n - 1) + Fibo(n - 2);
}
int main() {
  long long int n;
  std::cin >> n;
  std::cout << Fibo(n) << std::endl;
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int f[45];
int fib(int n) {
  if (n == 1 || n == 0)
    return 1;
  if (f[n] != 0)
    return f[n];
  else
    return fib(n - 1) + fib(n - 2);
}
int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int f[45];
int fib(int n) {
  if (n == 1 || n == 0)
    return 1;
  if (f[n] != 0)
    return f[n];
  else
    return f[n] = fib(n - 1) + fib(n - 2);
}
int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;

  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>

int fibo(int n, int *dp) {
  if (n == 0 || n == 1)
    return 1;
  if (0 < dp[n])
    return dp[n];
  return fibo(n - 1, dp) + fibo(n - 2, dp);
}

int main() {
  int n;
  int dp[50];
  for (int i = 0; i < 50; i++)
    dp[i] = -1;
  scanf("%d", &n);
  printf("%d\n", fibo(n, dp));
  return 0;
}

Fixed submission:
#include <cstdio>

int fibo(int n, int *dp) {
  if (n == 0 || n == 1)
    return 1;
  if (0 < dp[n])
    return dp[n];
  return dp[n] = fibo(n - 1, dp) + fibo(n - 2, dp);
}

int main() {
  int n;
  int dp[50];
  for (int i = 0; i < 50; i++)
    dp[i] = -1;
  scanf("%d", &n);
  printf("%d\n", fibo(n, dp));
  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

#define MAX 50

int dp[MAX];

int fib(int n) {
  if (n < 0)
    return 0;
  if (n == 0 || n == 1) {
    dp[n] = 1;
    return 1;
  }
  return dp[n] = fib(n - 1) + fib(n - 2);
}

int main() {
  int n;
  cin >> n;

  cout << fib(n) << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

#define MAX 50

int dp[MAX];

int fib(int n) {
  if (n < 0)
    return 0;
  if (n == 0 || n == 1) {
    dp[n] = 1;
    return 1;
  }
  if (dp[n] != 0) {
    return dp[n];
  }
  return dp[n] = fib(n - 1) + fib(n - 2);
}

int main() {
  int n;
  cin >> n;

  cout << fib(n) << endl;

  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
 * ALDS1_10_A.cpp
 *
 *  Created on: May 5, 2018
 *      Author: 13743
 */

#include <iostream>

using namespace std;

int F[44];

int fib(int n) {
  if (F[n] > 0)
    return F[n];
  return fib(n - 2) + fib(n - 1);
}

int main() {
  int n;
  cin >> n;

  F[0] = F[1] = 1;

  cout << fib(n) << endl;
}


Fixed submission:
/*
 * ALDS1_10_A.cpp
 *
 *  Created on: May 5, 2018
 *      Author: 13743
 */

#include <iostream>

using namespace std;

int F[44];

int fib(int n) {
  if (F[n] == 0) {
    F[n] = fib(n - 2) + fib(n - 1);
  }
  return F[n];
}

int main() {
  int n;
  cin >> n;

  F[0] = F[1] = 1;

  cout << fib(n) << endl;
}
Problem p02233 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
#define eol '\n'
using namespace std;

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

  int n;
  cin >> n;

  vector<int> list(n, 0);
  list[0] = list[1] = 1;

  for (int i = 2; i <= n; i++) {
    list[i] = list[i - 1] + list[i - 2];
  }

  cout << list[n] << eol;

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
#define eol '\n'
using namespace std;

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

  int n;
  cin >> n;

  vector<int> list(n + 1, 0);
  list[0] = list[1] = 1;

  for (int i = 2; i <= n; i++) {
    list[i] = list[i - 1] + list[i - 2];
  }

  cout << list[n] << eol;

  return 0;
}
Problem p02233 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int fib(int n);

int memo[45];

int main() {
  int n;

  // 入力
  cin >> n;

  // 出力
  cout << fib(n) << endl;

  return 0;
}

int fib(int n) {

  if (n == 0) {
    return 1;
  } else if (n == 1) {
    return 1;
  } else if (memo[n] != 0) {
    return memo[n];
  } else {
    return fib(n - 1) + fib(n - 2);
  }
}


Fixed submission:
#include <iostream>

using namespace std;

int fib(int n);

int memo[45];

int main() {
  int n;

  // 入力
  cin >> n;

  // 出力
  cout << fib(n) << endl;

  return 0;
}

int fib(int n) {

  if (n == 0) {
    return 1;
  } else if (n == 1) {
    return 1;
  } else if (memo[n] != 0) {
    return memo[n];
  } else {
    return memo[n] = fib(n - 1) + fib(n - 2);
  }
}
Problem p02234 (C++)

Buggy submission (status: 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; 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;
}


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 - 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;
}
Problem p02234 (C++)

Buggy submission (status: 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) {
  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;
}

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) {
  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;
}
Problem p02234 (C++)

Buggy submission (status: 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 - 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;
}


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 - 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;
}
Problem p02234 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p02234 (C++)

Buggy submission (status: 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; 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;
}

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 - 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;
}
Problem p02234 (C++)

Buggy submission (status: 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;
  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;
}


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;
  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;
}
Problem p02234 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

constexpr 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; 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;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

constexpr 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;
}
Problem p02234 (C++)

Buggy submission (status: 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 + 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;
}

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) 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;
}
Problem p02234 (C++)

Buggy submission (status: 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 - 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;
}

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 - 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;
}
Problem p02234 (C++)

Buggy submission (status: Runtime Error):
/**
 * 	matrix chain multiplication
 * 	f(n,m) = multiply Mn*M(n+1)*...*M(m-1)*M(m)
 * 	f(n,n) = 1
 * 	r(n)=c(n-1)
 * 	f(n,n+1) = C(n) * R(n) * C(n+1) = c(n) * c(n-1) * c(n+1)
 * 	f(n,k,m) = f(n,k)*f(k+1,m)*C(n)*r(n)*c(k)
 * 	f(n,m) = min(f(n,k,m)
 */

#include <climits>
#include <cmath>
#include <iostream>

using namespace std;

int r[45] = {0};
int a[100][100] = {0};

int f(int n, int m);
int f(int n, int k, int m);

int f(int n, int k, int m) {
  return f(n, k) + f(k + 1, m) + (r[n] * r[k + 1] * r[m + 1]);
}

int f(int n, int m) {
  if (n == m)
    return 0;
  int &ra = a[n][m];
  if (ra)
    return ra;
  ra = INT_MAX;
  for (int k = n; k < m; k++) {
    int ans = f(n, k, m);
    if (ans < ra)
      ra = ans;
  }
  return ra;
}

int main() {
  int rr = 0;
  int cc = 0;
  int n = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> rr >> cc;
    r[i] = rr;
    r[i + 1] = cc;
  }
  for (int i = 0; i < n - 1; i++) {
    a[i][i + 1] = r[i] * r[i + 1] * r[i + 2];
  }
  int ans = f(0, n - 1);
  cout << ans << endl;
}

Fixed submission:
/**
 * 	matrix chain multiplication
 * 	f(n,m) = multiply Mn*M(n+1)*...*M(m-1)*M(m)
 * 	f(n,n) = 1
 * 	r(n)=c(n-1)
 * 	f(n,n+1) = C(n) * R(n) * C(n+1) = c(n) * c(n-1) * c(n+1)
 * 	f(n,k,m) = f(n,k)*f(k+1,m)*C(n)*r(n)*c(k)
 * 	f(n,m) = min(f(n,k,m)
 */

#include <climits>
#include <cmath>
#include <iostream>

using namespace std;

int r[100] = {0};
int a[100][100] = {0};

int f(int n, int m);
int f(int n, int k, int m);

int f(int n, int k, int m) {
  return f(n, k) + f(k + 1, m) + (r[n] * r[k + 1] * r[m + 1]);
}

int f(int n, int m) {
  if (n == m)
    return 0;
  int &ra = a[n][m];
  if (ra)
    return ra;
  ra = INT_MAX;
  for (int k = n; k < m; k++) {
    int ans = f(n, k, m);
    if (ans < ra)
      ra = ans;
  }
  return ra;
}

int main() {
  int rr = 0;
  int cc = 0;
  int n = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> rr >> cc;
    r[i] = rr;
    r[i + 1] = cc;
  }
  for (int i = 0; i < n - 1; i++) {
    a[i][i + 1] = r[i] * r[i + 1] * r[i + 2];
  }
  int ans = f(0, n - 1);
  cout << ans << endl;
}
Problem p02234 (C++)

Buggy submission (status: 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 - 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;
}

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 - 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;
}
Problem p02234 (C++)

Buggy submission (status: 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;
      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;
}

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;
      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;
}
Problem p02234 (C++)

Buggy submission (status: Runtime Error):
#include <climits>
#include <iostream>
#include <vector>
using namespace std;

int MCM(int p[], int n) {
  vector<vector<int>> m(n, vector<int>(n));
  for (int i = 0; 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] = INT_MAX;
      for (int k = i; k <= j - 1; k++) {
        int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
        if (m[i][j] > q) {
          m[i][j] = q;
        }
      }
    }
  }
  return m[1][n];
}

int main() {
  int n, p[200];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[i] >> p[i + 1];
  }
  cout << MCM(p, n) << endl;
  return 0;
}

Fixed submission:
#include <climits>
#include <iostream>
#include <vector>
using namespace std;

int MCM(int p[], int n) {
  vector<vector<int>> m(n + 1, vector<int>(n + 1));
  for (int i = 0; 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] = INT_MAX;
      for (int k = i; k <= j - 1; k++) {
        int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
        if (m[i][j] > q) {
          m[i][j] = q;
        }
      }
    }
  }
  return m[1][n];
}

int main() {
  int n, p[200];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[i] >> p[i + 1];
  }
  cout << MCM(p, n) << endl;
  return 0;
}
Problem p02234 (C++)

Buggy submission (status: 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 - 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;
}


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 - 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;
}