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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
#define endl '\n'
#define F first
#define S second
const int MAX_N = 4e2 + 4;

int n;
int a[MAX_N];
ll dp[MAX_N][MAX_N], na[MAX_N][MAX_N];

int main() {
  memset(dp, 0x7F, sizeof(dp));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), dp[i][i] = 0, na[i][i] = a[i];
  for (int d = 1; d < n; d++)
    for (int i = 1, j = i + d; i <= n; i++, j++)
      for (int k = i; k < j; k++) {
        ll tmp = dp[i][k] + dp[k + 1][j] + na[i][k] + na[k + 1][j];
        if (dp[i][j] > tmp) {
          dp[i][j] = tmp;
          na[i][j] = na[i][k] + na[k + 1][j];
        } else if (dp[i][j] == tmp)
          na[i][j] = min(na[i][j], na[i][k] + na[k + 1][j]);
      }
  printf("%lld\n", dp[1][n]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
#define endl '\n'
#define F first
#define S second
const int MAX_N = 4e2 + 4;

int n;
int a[MAX_N];
ll dp[MAX_N][MAX_N], na[MAX_N][MAX_N];

int main() {
  memset(dp, 0x7F, sizeof(dp));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), dp[i][i] = 0, na[i][i] = a[i];
  for (int d = 1; d < n; d++)
    for (int i = 1, j = i + d; j <= n; i++, j++)
      for (int k = i; k < j; k++) {
        ll tmp = dp[i][k] + dp[k + 1][j] + na[i][k] + na[k + 1][j];
        if (dp[i][j] > tmp) {
          dp[i][j] = tmp;
          na[i][j] = na[i][k] + na[k + 1][j];
        } else if (dp[i][j] == tmp)
          na[i][j] = min(na[i][j], na[i][k] + na[k + 1][j]);
      }
  printf("%lld\n", dp[1][n]);
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 400;
int n, a[N];
long long sum[N], mem[N][N];
bool vis[N][N];

long long dp(int i, int j) {
  if (i == j) {
    return 0;
  }
  if (vis[i][j]) {
    return mem[i][j];
  }
  vis[i][j] = true;

  long long ans = LLONG_MAX;
  for (int m = i; m < j; m++) {
    ans = min(ans, dp(i, m) + dp(m + 1, j));
  }

  long long cost = sum[j] - sum[i - 1];
  return mem[i][j] = cost + ans;
}

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

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum[i] = sum[i - 1] + a[i];
  }

  cout << dp(1, n) << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 405;
int n, a[N];
long long sum[N], mem[N][N];
bool vis[N][N];

long long dp(int i, int j) {
  if (i == j) {
    return 0;
  }
  if (vis[i][j]) {
    return mem[i][j];
  }
  vis[i][j] = true;

  long long ans = LLONG_MAX;
  for (int m = i; m < j; m++) {
    ans = min(ans, dp(i, m) + dp(m + 1, j));
  }

  long long cost = sum[j] - sum[i - 1];
  return mem[i][j] = cost + ans;
}

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

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum[i] = sum[i - 1] + a[i];
  }

  cout << dp(1, n) << '\n';

  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[500][500], sum[500][500];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (int j = 0; j <= i; j++)
      sum[j][i] = x;
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sum[i][j] += sum[i][j - 1];
    }
    if (i < n - 1)
      dp[i][i + 1] = sum[i][i + 1];
  }
  for (int len = 2; len < n; len++)
    for (int i = 0; i < n - 1; i++) {
      int j = i + len;
      dp[i][j] = sum[i][i] + sum[i + 1][j] + dp[i + 1][j];
      for (int k = i + 1; k < j; k++) {
        dp[i][j] =
            min(dp[i][j], dp[i][k] + dp[k + 1][j] + sum[i][k] + sum[k + 1][j]);
      }
    }
  cout << dp[0][n - 1];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[500][500], sum[500][500];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (int j = 0; j <= i; j++)
      sum[j][i] = x;
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sum[i][j] += sum[i][j - 1];
    }
    if (i < n - 1)
      dp[i][i + 1] = sum[i][i + 1];
  }
  for (int len = 2; len < n; len++)
    for (int i = 0; i < n - len; i++) {
      int j = i + len;
      dp[i][j] = sum[i][i] + sum[i + 1][j] + dp[i + 1][j];
      for (int k = i + 1; k < j; k++) {
        dp[i][j] =
            min(dp[i][j], dp[i][k] + dp[k + 1][j] + sum[i][k] + sum[k + 1][j]);
      }
    }
  cout << dp[0][n - 1];
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define VAL 100000
#define ll long long
#define INF 10000000000000000LL
#define pll pair<long long, long long>
const int MAX = 1e5 + 10;
const int MOD = 1e9 + 7;
const int TOT_PRIMES = 19;
const int MAX_A = 70;
using namespace std;
vector<int> adj[MAX];
int coun(int x) { return __builtin_popcount(x); }
ll dp[400][400];
int n;
int main() {
  // ios_base::sync_with_stdio(false);
  //  cin.tie(NULL);
  //	#ifndef ONLINE_JUDGE
  //	freopen("inp.txt" ,"r", stdin );
  //	freopen("out.txt", "w", stdout);
  //	#endif
  cin >> n;
  ll arr[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  ll pre[n + 1];
  memset(pre, 0, sizeof(pre));
  for (int i = 1; i <= n; i++)
    pre[i] = pre[i - 1] + arr[i];
  for (int i = n; i >= 1; i--) {
    for (int j = i; j <= n; j++) {
      if (i == j) {
        dp[i][j] = 0;
        continue;
      }
      dp[i][j] = INF;
      for (int k = i; k < j; k++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + pre[j] - pre[i - 1]);
      }
      //		cout<<dp[i][j]<<" "<<i<<" "<<j<<"\n";
    }
  }
  cout << dp[1][n];
}

Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define VAL 100000
#define ll long long
#define INF 10000000000000000LL
#define pll pair<long long, long long>
const int MAX = 1e5 + 10;
const int MOD = 1e9 + 7;
const int TOT_PRIMES = 19;
const int MAX_A = 70;
using namespace std;
vector<int> adj[MAX];
int coun(int x) { return __builtin_popcount(x); }
ll dp[405][405];
int n;
int main() {
  // ios_base::sync_with_stdio(false);
  //  cin.tie(NULL);
  //	#ifndef ONLINE_JUDGE
  //	freopen("inp.txt" ,"r", stdin );
  //	freopen("out.txt", "w", stdout);
  //	#endif
  cin >> n;
  ll arr[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  ll pre[n + 1];
  memset(pre, 0, sizeof(pre));
  for (int i = 1; i <= n; i++)
    pre[i] = pre[i - 1] + arr[i];
  for (int i = n; i >= 1; i--) {
    for (int j = i; j <= n; j++) {
      if (i == j) {
        dp[i][j] = 0;
        continue;
      }
      dp[i][j] = INF;
      for (int k = i; k < j; k++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + pre[j] - pre[i - 1]);
      }
      //		cout<<dp[i][j]<<" "<<i<<" "<<j<<"\n";
    }
  }
  cout << dp[1][n];
}
Problem p03173 (C++)

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

signed main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int dp[n + 1][n + 1];
  int arr[n + 1];
  int prefix[n + 1];
  arr[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    dp[i][i] = 0;
    dp[i - 1][i] = arr[i - 1] + arr[i];
  }
  partial_sum(arr, arr + 1 + n, prefix);
  for (int i = n; i >= 1; i--) {
    for (int j = i + 2; j <= n; j++) {
      dp[i][j] = LLONG_MAX;
      for (int k = i; k <= j; k++) {
        dp[i][j] =
            min(dp[i][j], dp[i][k] + dp[k + 1][j] + prefix[j] - prefix[i - 1]);
      }
    }
  }
  cout << dp[1][n] << endl;
  return 0;
}

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

signed main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int dp[n + 1][n + 1];
  int arr[n + 1];
  int prefix[n + 1];
  arr[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    dp[i][i] = 0;
    dp[i - 1][i] = arr[i - 1] + arr[i];
  }
  partial_sum(arr, arr + 1 + n, prefix);
  for (int i = n; i >= 1; i--) {
    for (int j = i + 2; j <= n; j++) {
      dp[i][j] = LLONG_MAX;
      for (int k = i; k < j; k++) {
        dp[i][j] =
            min(dp[i][j], dp[i][k] + dp[k + 1][j] + prefix[j] - prefix[i - 1]);
      }
    }
  }
  cout << dp[1][n] << endl;
  return 0;
}
Problem p03173 (C++)

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

#define int long long
const int MOD = 10000000000007LL;

int dp[410][410] = {};
int a[410];

//[l, r)
int solve(int l, int r) {

  if (!dp[l][r] == MOD)
    return dp[l][r];
  if (r - l == 1) {
    dp[l][r] = 0;
    return dp[l][r];
  }

  int rec = MOD;
  int sum = 0;
  for (int i = l; i < r; i++) {
    sum += a[i];
  }

  for (int i = l + 1; i < r; i++) {
    rec = min(rec, solve(l, i) + solve(i, r) + sum);
  }

  dp[l][r] = rec;
  return dp[l][r];
}

signed main() {

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = MOD;
    }
  }

  cout << solve(0, n) << endl;

  return 0;
}

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

#define int long long
const int MOD = 10000000000007LL;

int dp[410][410] = {};
int a[410];

//[l, r)
int solve(int l, int r) {

  // cout << l << " " << r << endl;
  if (!(dp[l][r] == MOD))
    return dp[l][r];
  if (r - l == 1) {
    dp[l][r] = 0;
    return dp[l][r];
  }

  int rec = MOD;
  int sum = 0;
  for (int i = l; i < r; i++) {
    sum += a[i];
  }

  for (int i = l + 1; i < r; i++) {
    rec = min(rec, solve(l, i) + solve(i, r) + sum);
  }

  dp[l][r] = rec;
  return dp[l][r];
}

signed main() {

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = MOD;
    }
  }

  cout << solve(0, n) << endl;

  return 0;
}
Problem p03173 (C++)

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

int n, a[401];
long long b[401], dp[401][401], mod = 1e9 + 7;

long long func(int l, int r) {
  if (l == r)
    return dp[l][r] = 0;
  if (l + 1 == r)
    return dp[l][r] = a[l];
  long long res = 1e18;
  for (int i = l + 1; i < r; ++i)
    res = min(res, func(l, i) + func(i, r) - b[l - 1] + b[r - 1]);
  return dp[l][r] = res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i + 1];
    b[i + 1] = a[i + 1];
    b[i + 1] += b[i];
  }
  cout << func(1, n + 1) - b[n] << "\n";
}

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

int n, a[401];
long long b[401], dp[401][401], mod = 1e9 + 7;

long long func(int l, int r) {
  if (l == r)
    return dp[l][r] = 0;
  if (l + 1 == r)
    return dp[l][r] = a[l];
  if (dp[l][r] != 0)
    return dp[l][r];
  long long res = 1e18;
  for (int i = l + 1; i < r; ++i)
    res = min(res, func(l, i) + func(i, r) - b[l - 1] + b[r - 1]);
  return dp[l][r] = res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i + 1];
    b[i + 1] = a[i + 1];
    b[i + 1] += b[i];
  }
  cout << func(1, n + 1) - b[n] << "\n";
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define all(x) (x).begin(), (x).end()

using namespace std;

const int MSIZE = 404;

long long dp[MSIZE][MSIZE];

long long arr[MSIZE];
long long pref[MSIZE];

long long Sum(int l, int r) {
  l = max(l, 0);
  return pref[r] - (l ? pref[l - 1] : 0LL);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  memset(dp, 0, sizeof dp);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  memcpy(pref, arr, sizeof arr);
  for (int i = 1; i < n; i++) {
    pref[i] += pref[i - 1];
  }

  for (int len = 1; len <= n; len++) {
    for (int l = 0; l < n; l++) {
      int r = l + len - 1;
      if (len == 1) {
        dp[l][r] = 0;
      } else {
        dp[l][r] = 1e18;
        for (int m = l; m < r; m++) {
          dp[l][r] = min(dp[l][r], dp[l][m] + dp[m + 1][r] + Sum(l, r));
        }
      }
    }
  }
  cout << dp[0][n - 1] << endl;
}

Fixed submission:
#include "bits/stdc++.h"
#define all(x) (x).begin(), (x).end()

using namespace std;

const int MSIZE = 404;

long long dp[MSIZE][MSIZE];

long long arr[MSIZE];
long long pref[MSIZE];

long long Sum(int l, int r) {
  l = max(l, 0);
  return pref[r] - (l ? pref[l - 1] : 0LL);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  memset(dp, 0, sizeof dp);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  memcpy(pref, arr, sizeof arr);
  for (int i = 1; i < n; i++) {
    pref[i] += pref[i - 1];
  }

  for (int len = 1; len <= n; len++) {
    for (int l = 0; l + len <= n; l++) {
      int r = l + len - 1;
      if (len == 1) {
        dp[l][r] = 0;
      } else {
        dp[l][r] = 1e18;
        for (int m = l; m < r; m++) {
          dp[l][r] = min(dp[l][r], dp[l][m] + dp[m + 1][r] + Sum(l, r));
        }
      }
    }
  }
  cout << dp[0][n - 1] << endl;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf = 1e16;
int n, a[410];
ll f[410][410], s[410];
int main() {
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      f[i][j] = inf;
    }
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    s[i] = s[i - 1] + a[i];
    f[i][i] = 0;
  }
  for (int l = 1; l < n; l++) {
    for (int i = 1; i <= n; i++) {
      int j = i + l;
      for (int k = i; k < j; k++) {
        f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j]);
      }
      f[i][j] += s[j] - s[i - 1];
    }
  }
  /*
  for (int i=1;i<=n;i++)
  {
          for (int j=i;j<=n;j++)
          {
                  cerr<<f[i][j]<<" ";
          }
          cerr<<endl;
  }
  */
  cout << f[1][n] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf = 1e16;
int n, a[410];
ll f[410][410], s[410];
int main() {
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      f[i][j] = inf;
    }
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    s[i] = s[i - 1] + a[i];
    f[i][i] = 0;
  }
  for (int l = 1; l < n; l++) {
    for (int i = 1; i + l <= n; i++) {
      int j = i + l;
      for (int k = i; k < j; k++) {
        f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j]);
      }
      f[i][j] += s[j] - s[i - 1];
    }
  }
  /*
  for (int i=1;i<=n;i++)
  {
          for (int j=i;j<=n;j++)
          {
                  cerr<<f[i][j]<<" ";
          }
          cerr<<endl;
  }
  */
  cout << f[1][n] << endl;
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long int;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll mod = 1e9 + 7;

ll dp[410][410] = {};

int main() {
  int n;
  cin >> n;
  ll a[n];
  rep(i, n) cin >> a[i];

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      ll tmp = 1000000000000000;
      ll sum = 0;
      for (int k = j; k < j + i; k++) {
        tmp = min(tmp, dp[j][k] + dp[k + 1][j + i]);
        sum += a[k];
      }
      sum += a[j + i]; // a[j]~a[j+i]まで足す
      dp[j][j + i] = tmp + sum;
    }
  }
  cout << dp[0][n - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long int;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll mod = 1e9 + 7;

ll dp[410][410] = {};

int main() {
  int n;
  cin >> n;
  ll a[n];
  rep(i, n) cin >> a[i];

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n - i; j++) {
      ll tmp = 1000000000000000;
      ll sum = 0;
      for (int k = j; k < j + i; k++) {
        tmp = min(tmp, dp[j][k] + dp[k + 1][j + i]);
        sum += a[k];
      }
      sum += a[j + i]; // a[j]~a[j+i]まで足す
      dp[j][j + i] = tmp + sum;
    }
  }
  cout << dp[0][n - 1] << endl;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <vector>
#define INF (1LL << 60)
using namespace std;

long long int dp[3001][3001];
long long int sum[3001][3001];

long long int slimes(vector<int> &v, int i, int j) {
  if (i == j)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j]; // Look-Up
  long long int minCost = INF;
  for (int k = i; k < j; k++) {
    minCost =
        min(minCost, sum[i][j] + slimes(v, i, (i + k)) + slimes(v, (k + 1), j));
  }
  return (dp[i][j] = minCost);
}

void preprocess(vector<int> &v, int n) {
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      sum[i][j] = v[j] + ((j == i) ? 0 : sum[i][j - 1]);
    }
  }
}

int main() {
  int n;
  cin >> n;
  memset(dp, -1, sizeof(dp));
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  preprocess(v, n);
  cout << slimes(v, 1, n) << endl;
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <vector>
#define INF (1LL << 60)
using namespace std;

long long int dp[3001][3001];
long long int sum[3001][3001];

long long int slimes(vector<int> &v, int i, int j) {
  if (i == j)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j]; // Look-Up
  long long int minCost = INF;
  for (int k = i; k < j; k++) {
    minCost =
        min(minCost, (sum[i][j] + slimes(v, i, k) + slimes(v, (k + 1), j)));
  }
  return (dp[i][j] = minCost);
}

void preprocess(vector<int> &v, int n) {
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      sum[i][j] = v[j] + ((j == i) ? 0 : sum[i][j - 1]);
    }
  }
}

int main() {
  int n;
  cin >> n;
  memset(dp, -1, sizeof(dp));
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  preprocess(v, n);
  cout << slimes(v, 1, n) << endl;
  return 0;
}
Problem p03173 (C++)

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

const int MXN = 4e0 + 20;

long long n, a[MXN], ps[MXN], dp[MXN][MXN];

void npt() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ps[i] = ps[i - 1] + a[i];
  }
  for (int i = 0; i < MXN; i++)
    for (int j = 0; j < MXN; j++)
      dp[i][j] = 1e15 + 20;
}

void prnt() {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++)
      cerr << dp[i][j] << ' ';
    cerr << endl;
  }
  cerr << "-----------------------\n";
}

void slv() {
  for (int i = 0; i <= n; ++i) {
    for (int l = 0; l <= n - i; ++l) {
      int r = l + i;
      if (r - l < 2) {
        dp[l][r] = 0;
        continue;
      }
      long long tmp = ps[r] - ps[l];
      for (int mid = l; mid <= r; ++mid)
        dp[l][r] = min(dp[l][r], dp[l][mid] + dp[mid][r] + tmp);
    }
  }
  cout << dp[0][n] << endl;
}

int main() {
  npt();
  slv();
  return 0;
}


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

const int MXN = 4e2 + 20;

long long n, a[MXN], ps[MXN], dp[MXN][MXN];

void npt() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ps[i] = ps[i - 1] + a[i];
  }
  for (int i = 0; i < MXN; i++)
    for (int j = 0; j < MXN; j++)
      dp[i][j] = 1e15 + 20;
}

void prnt() {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++)
      cerr << dp[i][j] << ' ';
    cerr << endl;
  }
  cerr << "-----------------------\n";
}

void slv() {
  for (int i = 0; i <= n; ++i) {
    for (int l = 0; l <= n - i; ++l) {
      int r = l + i;
      if (r - l < 2) {
        dp[l][r] = 0;
        continue;
      }
      long long tmp = ps[r] - ps[l];
      for (int mid = l; mid <= r; ++mid)
        dp[l][r] = min(dp[l][r], dp[l][mid] + dp[mid][r] + tmp);
    }
  }
  cout << dp[0][n] << endl;
}

int main() {
  npt();
  slv();
  return 0;
}
Problem p03173 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

ll dp[405][405];
int l[405];
int n;

ll solve(int lhs, int rhs) {
  if (lhs == rhs)
    return 0;
  ll total = 0;
  for (int i = lhs; i <= rhs; i++) {
    total += l[i];
  }
  dp[lhs][rhs] = 1e18;
  for (int i = lhs; i < rhs; i++) {
    dp[lhs][rhs] = min(dp[lhs][rhs], solve(lhs, i) + solve(i + 1, rhs));
  }
  dp[lhs][rhs] += total;
  return dp[lhs][rhs];
}

void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  cout << solve(0, n - 1) << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}


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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

ll dp[405][405];
int l[405];
int n;

ll solve(int lhs, int rhs) {
  if (lhs == rhs)
    return 0;
  if (dp[lhs][rhs])
    return dp[lhs][rhs];
  ll total = 0;
  for (int i = lhs; i <= rhs; i++) {
    total += l[i];
  }
  dp[lhs][rhs] = 1e18;
  for (int i = lhs; i < rhs; i++) {
    dp[lhs][rhs] = min(dp[lhs][rhs], solve(lhs, i) + solve(i + 1, rhs));
  }
  dp[lhs][rhs] += total;
  return dp[lhs][rhs];
}

void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  cout << solve(0, n - 1) << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mxN = 1e9;
const ll mod = 1e9 + 7;

ll pow(ll a, ll b) {
  ll r = 1;
  while (b) {
    if (b % 2 != 0)
      r = (r * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }

  return r;
}

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

  int n;
  cin >> n;

  vector<ll> v(n);

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

  vector<ll> pref(n + 1);
  pref[0] = 0;

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

  ll dp[n][n];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j + i < n; j++) {
      if (i + j == j)
        dp[j][j + i] = 0;
      else {
        dp[j][i + j] = 1e18L + 5;
        for (int k = j; k <= j + i; k++) {
          dp[j][i + j] = min(dp[j][i + j], dp[j][k] + dp[k + 1][i + j] +
                                               (pref[i + j + 1] - pref[j]));
        }
      }
    }
  }

  cout << dp[0][n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mxN = 1e9;
const ll mod = 1e9 + 7;

ll pow(ll a, ll b) {
  ll r = 1;
  while (b) {
    if (b % 2 != 0)
      r = (r * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }

  return r;
}

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

  int n;
  cin >> n;

  vector<ll> v(n);

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

  vector<ll> pref(n + 1);
  pref[0] = 0;

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

  ll dp[n][n];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j + i < n; j++) {
      if (i + j == j)
        dp[j][j + i] = 0;
      else {
        dp[j][i + j] = 1e18L + 5;
        for (int k = j; k <= j + i - 1; k++) {
          dp[j][i + j] = min(dp[j][i + j], dp[j][k] + dp[k + 1][i + j] +
                                               (pref[i + j + 1] - pref[j]));
        }
      }
    }
  }

  cout << dp[0][n - 1];
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;

const ll INF = 1145141919810893;

int N;
ll dp[400][400];
ll cost[400];
ll ruisekiwa[401]; // idx i ~ idx j (i < j)は [i, j)

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> cost[i];
    ruisekiwa[i + 1] = cost[i] + ruisekiwa[i];
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      dp[i][j] = INF;

  for (int i = 0; i < N; i++)
    dp[i][i] = 0;

  for (int i = 1; i < N; i++) {
    for (int j = 0; j + i < N; j++) {
      ll ans = INF;
      if (i == 1) {
        dp[j][j + i] = ruisekiwa[j + i + 1] - ruisekiwa[j];
      } else {
        for (int k = j; k <= j + i; k++) {
          int ll = j, lr = k, rl = k + 1, rr = j + i;
          ans = min(ans, dp[ll][lr] + dp[rl][rr]);
        }
        ans += ruisekiwa[j + i + 1] - ruisekiwa[j];
        dp[j][j + i] = ans;
      }
    }
  }

  cout << dp[0][N - 1] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;

const ll INF = 1145141919810893;

int N;
ll dp[400][400];
ll cost[400];
ll ruisekiwa[401]; // idx i ~ idx j (i < j)は [i, j)

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> cost[i];
    ruisekiwa[i + 1] = cost[i] + ruisekiwa[i];
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      dp[i][j] = INF;

  for (int i = 0; i < N; i++)
    dp[i][i] = 0;

  for (int i = 1; i < N; i++) {
    for (int j = 0; j + i < N; j++) {
      ll ans = INF;
      if (i == 1) {
        dp[j][j + i] = ruisekiwa[j + i + 1] - ruisekiwa[j];
      } else {
        for (int k = j; k <= j + i - 1; k++) {
          int ll = j, lr = k, rl = k + 1, rr = j + i;
          ans = min(ans, dp[ll][lr] + dp[rl][rr]);
        }
        ans += ruisekiwa[j + i + 1] - ruisekiwa[j];
        dp[j][j + i] = ans;
      }
    }
  }

  cout << dp[0][N - 1] << endl;
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define nmax 405
using namespace std;
int n, a[nmax];
long long s[nmax][nmax];
long long dp[nmax][nmax];

int main() {
  int i, j, k, l;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  for (i = 1; i <= n; i++)
    for (j = i; j <= n; j++)
      s[i][j] = s[i][j - 1] + a[j];

  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      dp[i][j] = 1LL << 60;

  for (i = 1; i <= n; i++)
    dp[i][i] = 0;

  for (l = 2; l <= n; l++)
    for (i = 1; i <= n; i++) {
      j = i + l - 1;
      for (k = i; k < j; k++) {
        long long val = dp[i][k] + s[i][k] + dp[k + 1][j] + s[k + 1][j];
        if (dp[i][j] > val)
          dp[i][j] = val;
      }
    }
  cout << dp[1][n] << '\n';

  return 0;
}


Fixed submission:
#include <iostream>
#define nmax 405
using namespace std;
int n, a[nmax];
long long s[nmax][nmax];
long long dp[nmax][nmax];

int main() {
  int i, j, k, l;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  for (i = 1; i <= n; i++)
    for (j = i; j <= n; j++)
      s[i][j] = s[i][j - 1] + a[j];

  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      dp[i][j] = 1LL << 60;

  for (i = 1; i <= n; i++)
    dp[i][i] = 0;

  for (l = 2; l <= n; l++)
    for (i = 1; i + l - 1 <= n; i++) {
      j = i + l - 1;
      for (k = i; k < j; k++) {
        long long val = dp[i][k] + s[i][k] + dp[k + 1][j] + s[k + 1][j];
        if (dp[i][j] > val)
          dp[i][j] = val;
      }
    }
  cout << dp[1][n] << '\n';

  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF (1 << 29)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define uniq(v) v.erase(unique(all(v)), v.end())

int n;
int a[400];
long long dp[400][400];
long long sum[401];

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

  cin >> n;
  rep(i, n) cin >> a[i];

  rep(i, n) sum[i + 1] = sum[i] + a[i];

  for (int len = 2; len <= n; len++) {
    rep(i, n) {
      auto ret = 1LL << 60;
      for (int j = 1; j < len; j++) {
        ret = min(ret, dp[i][i + j - 1] + dp[i + j][i + len - 1]);
      }
      dp[i][i + len - 1] = ret + sum[i + len] - sum[i];
    }
  }
  cout << dp[0][n - 1] << endl;

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF (1 << 29)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define uniq(v) v.erase(unique(all(v)), v.end())

int n;
int a[400];
long long dp[400][400];
long long sum[401];

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

  cin >> n;
  rep(i, n) cin >> a[i];

  rep(i, n) sum[i + 1] = sum[i] + a[i];

  for (int len = 2; len <= n; len++) {
    rep(i, n - len + 1) {
      auto ret = 1LL << 60;
      for (int j = 1; j < len; j++) {
        ret = min(ret, dp[i][i + j - 1] + dp[i + j][i + len - 1]);
      }
      dp[i][i + len - 1] = ret + sum[i + len] - sum[i];
    }
  }
  cout << dp[0][n - 1] << endl;

  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ll long long int
#define P pair<ll, ll>
#define vii vector<P>
#define vvi vector<vector<ll>>
#define P_B push_back

#define MOD 1000000007
#define pi 3.141592653589793238

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // ll T; cin>>T; while(T--)
  {

    ll n;
    cin >> n;

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

    ll DP[n][n];

    for (ll l = n - 1; l >= 0; l--) {

      for (int r = l; r < n; r++) {

        if (r == l) {
          DP[l][r] = 0;
          continue;
        }

        ll sum = 0;
        for (int i = l; i <= r; i++)
          sum += v[i];
        DP[l][r] = 1e18L;

        for (int p = l; p <= r; p++) {
          DP[l][r] = min(DP[l][r], DP[l][p] + DP[p + 1][r] + sum);
        }
      }
    }

    cout << DP[0][n - 1];
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ll long long int
#define P pair<ll, ll>
#define vii vector<P>
#define vvi vector<vector<ll>>
#define P_B push_back

#define MOD 1000000007
#define pi 3.141592653589793238

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // ll T; cin>>T; while(T--)
  {

    ll n;
    cin >> n;

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

    ll DP[n][n];

    for (ll l = n - 1; l >= 0; l--) {

      for (int r = l; r < n; r++) {

        if (r == l) {
          DP[l][r] = 0;
          continue;
        }

        ll sum = 0;
        for (int i = l; i <= r; i++)
          sum += v[i];
        DP[l][r] = 1e18L;

        for (int p = l; p < r; p++) {
          DP[l][r] = min(DP[l][r], DP[l][p] + DP[p + 1][r] + sum);
        }
      }
    }

    cout << DP[0][n - 1];
  }
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 401;
int dp[maxn][maxn];
int pref[maxn];
signed main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  pref[0] = 0;
  for (int i = 1; i <= n; i++)
    pref[i] = pref[i - 1] + a[i];
  for (int i = 0; i < maxn; i++) {
    for (int j = 0; j < maxn; j++)
      dp[i][j] = LLONG_MAX;
  }
  for (int i = 0; i < maxn; i++)
    dp[i][i] = 0;
  for (int i = 1; i <= n - 1; i++) {
    for (int l = 1; l <= n; l++) {
      int r = l + i;
      for (int m = l; m < r; m++) {
        dp[l][r] =
            min(dp[l][r], dp[l][m] + dp[m + 1][r] + pref[r] - pref[l - 1]);
      }
    }
  }
  cout << dp[1][n];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 401;
int dp[maxn][maxn];
int pref[maxn];
signed main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  pref[0] = 0;
  for (int i = 1; i <= n; i++)
    pref[i] = pref[i - 1] + a[i];
  for (int i = 0; i < maxn; i++) {
    for (int j = 0; j < maxn; j++)
      dp[i][j] = LLONG_MAX;
  }
  for (int i = 0; i < maxn; i++)
    dp[i][i] = 0;
  for (int i = 1; i <= n - 1; i++) {
    for (int l = 1; l <= n; l++) {
      if (l + i >= maxn)
        continue;
      int r = l + i;
      for (int m = l; m < r; m++) {
        dp[l][r] =
            min(dp[l][r], dp[l][m] + dp[m + 1][r] + pref[r] - pref[l - 1]);
      }
    }
  }
  cout << dp[1][n];
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 4e2 + 5;
const int INF = 1e16 + 7;
int dp[MAXN][MAXN];
int sum[MAXN];
int arr[MAXN];
int32_t main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    sum[i] = sum[i - 1] + arr[i];
  }
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n; i++) {
      dp[i][l] = INF;
      int j = i + l - 1;
      for (int idx = i; idx < j; idx++) {
        dp[i][l] = min(dp[i][l], dp[i][idx - i + 1] + dp[idx + 1][j - idx] +
                                     (sum[j] - sum[i - 1]));
      }
    }
  }
  cout << dp[1][n] << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 4e2 + 5;
const int INF = 1e16 + 7;
int dp[MAXN][MAXN];
int sum[MAXN];
int arr[MAXN];
int32_t main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    sum[i] = sum[i - 1] + arr[i];
  }
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n; i++) {
      dp[i][l] = INF;
      if (i + l - 1 > n)
        continue;
      int j = i + l - 1;
      for (int idx = i; idx < j; idx++) {
        dp[i][l] = min(dp[i][l], dp[i][idx - i + 1] + dp[idx + 1][j - idx] +
                                     (sum[j] - sum[i - 1]));
      }
    }
  }
  cout << dp[1][n] << '\n';
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long f[102][102];
long long a[102];
long long sum[102];
int main() {
  int n, i, j, k;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  for (j = 1; j <= n - 1; j++) {
    for (i = 1; i + j <= n; i++) {
      f[i][i + j] = std::numeric_limits<long long>::max();
      for (k = i; k < i + j; k++) {
        f[i][i + j] = min(f[i][i + j],
                          f[i][k] + f[k + 1][i + j] + sum[i + j] - sum[i - 1]);
      }
    }
  }
  printf("%lld", f[1][n]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long f[402][402];
long long a[402];
long long sum[402];
int main() {
  int n, i, j, k;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  for (j = 1; j <= n - 1; j++) {
    for (i = 1; i + j <= n; i++) {
      f[i][i + j] = std::numeric_limits<long long>::max();
      for (k = i; k < i + j; k++) {
        f[i][i + j] = min(f[i][i + j],
                          f[i][k] + f[k + 1][i + j] + sum[i + j] - sum[i - 1]);
      }
    }
  }
  printf("%lld", f[1][n]);
  return 0;
}
Problem p03173 (C++)

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

using namespace std;
typedef long long ll;
ll inf = (ll)1 << 60;

ll n;
vector<ll> v;

vector<vector<ll>> memo;
vector<vector<ll>> size;

// Find the cheapest cost to combine the range [lo,hi]
ll solve(ll lo, ll hi) {
  if (memo[lo][hi] != inf)
    return memo[lo][hi];

  if (lo == hi) {
    memo[lo][hi] = 0;
    return 0;
  }

  ll best = inf;
  for (ll i = lo; i < hi; i++) {
    ll here = solve(lo, i) + solve(i + 1, hi);
    best = min(best, here);
  }
  best += size[lo][hi];

  memo[lo][hi] = best;
  return best;
}

void sizes(ll lo, ll hi) {
  if (lo == hi) {
    size[lo][hi] = v[lo];
    return;
  }

  ll best = inf;
  for (int i = lo; i < hi; i++) {
    sizes(lo, i);
    sizes(i + 1, hi);
    ll here = size[lo][i] + size[i + 1][hi];
    best = min(best, here);
  }

  size[lo][hi] = best;
}

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

  v.resize(n);
  for (auto &i : v) {
    cin >> i;
  }

  memo.resize(n + 1, vector<ll>(n + 1, inf));
  size.resize(n + 1, vector<ll>(n + 1, inf));

  sizes(0, n - 1);
  cout << solve(0, n - 1) << endl;
}


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

using namespace std;
typedef long long ll;
ll inf = (ll)1 << 60;

ll n;
vector<ll> v;

vector<vector<ll>> memo;
vector<vector<ll>> size;

// Find the cheapest cost to combine the range [lo,hi]
ll solve(ll lo, ll hi) {
  if (memo[lo][hi] != inf)
    return memo[lo][hi];

  if (lo == hi) {
    memo[lo][hi] = 0;
    return 0;
  }

  ll best = inf;
  for (ll i = lo; i < hi; i++) {
    ll here = solve(lo, i) + solve(i + 1, hi);
    best = min(best, here);
  }
  best += size[lo][hi];

  memo[lo][hi] = best;
  return best;
}

void sizes(ll lo, ll hi) {
  if (size[lo][hi] != inf)
    return;

  if (lo == hi) {
    size[lo][hi] = v[lo];
    return;
  }

  ll best = inf;
  for (int i = lo; i < hi; i++) {
    sizes(lo, i);
    sizes(i + 1, hi);
    ll here = size[lo][i] + size[i + 1][hi];
    best = min(best, here);
  }

  size[lo][hi] = best;
}

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

  v.resize(n);
  for (auto &i : v) {
    cin >> i;
  }

  memo.resize(n + 1, vector<ll>(n + 1, inf));
  size.resize(n + 1, vector<ll>(n + 1, inf));

  sizes(0, n - 1);
  cout << solve(0, n - 1) << endl;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int MAX = 305;
const int NIL = -1;
const ll LINF = 1LL << 50;
const int mod = 1e+9 + 7;
/* global variables */
/* funciton */
/* main */
int main() {
  int N;
  cin >> N;
  vector<ll> a(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];

  // sum(a[i], ... , a[j]) = cum[j+1] - cum[i];
  vector<ll> cum(N + 2);
  for (int i = 0; i <= N; i++)
    cum[i + 1] = cum[i] + a[i];

  // dp[x][y] = a[x] ~ a[y-1]までを合体させる時の最小コスト
  vector<vector<ll>> dp(N + 1, vector<ll>(N + 1, LINF));
  for (int i = 0; i <= N; i++) {
    dp[i][i + 1] = 0;
  }
  for (int len = 1; len <= N; len++) {
    for (int x = 0; x <= N - len; x++) {
      int y = x + len;
      for (int z = x + 1; z < y; z++) {
        ll cost = dp[x][z] + dp[z][y] + (cum[z] - cum[x]) + (cum[y] - cum[z]);
        dp[x][y] = min(dp[x][y], cost);
      }
    }
  }

  cout << dp[0][N] << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int MAX = 305;
const int NIL = -1;
const ll LINF = 1LL << 50;
const int mod = 1e+9 + 7;
/* global variables */
/* funciton */
/* main */
int main() {
  int N;
  cin >> N;
  vector<ll> a(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];

  // sum(a[i], ... , a[j]) = cum[j+1] - cum[i];
  vector<ll> cum(N + 2);
  for (int i = 0; i <= N; i++)
    cum[i + 1] = cum[i] + a[i];

  // dp[x][y] = a[x] ~ a[y-1]までを合体させる時の最小コスト
  vector<vector<ll>> dp(N + 1, vector<ll>(N + 1, LINF));
  for (int i = 0; i < N; i++) {
    dp[i][i + 1] = 0;
  }
  for (int len = 1; len <= N; len++) {
    for (int x = 0; x <= N - len; x++) {
      int y = x + len;
      for (int z = x + 1; z < y; z++) {
        ll cost = dp[x][z] + dp[z][y] + (cum[z] - cum[x]) + (cum[y] - cum[z]);
        dp[x][y] = min(dp[x][y], cost);
      }
    }
  }

  cout << dp[0][N] << '\n';
}
Problem p03173 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ffor(n) for (int i = 0; i < n; i++)
#define fffor(n) for (int j = 0; j < n; j++)
int INF = 1e9 + 7;
long long INF2 = 1e16;

long long f(int l, int r, vector<long long> &v, vector<vector<long long>> &dp,
            vector<vector<long long>> &range_sum) {
  if (l == r) {
    range_sum[l][r] = v[l];
    dp[l][r] = 0;
    return dp[l][r];
  }
  if (l + 1 == r) {
    range_sum[l][r] = v[l] + v[r];
    dp[l][r] = range_sum[l][r];
    return dp[l][r];
  }
  if (l > r) {
    return 0;
  }
  long long ans = INF2;
  for (int i = l; i < r; i++) {
    ans = min(ans, f(l, i, v, dp, range_sum) + f(i + 1, r, v, dp, range_sum) +
                       range_sum[l][i] + range_sum[i + 1][r]);
  }
  dp[l][r] = ans;
  range_sum[l][r] = range_sum[l][l + 1] + range_sum[l + 2][r];
  return ans;
}

int main(void) {
  int n;
  cin >> n;
  vector<long long> v(n);
  ffor(n) cin >> v[i];

  vector<vector<long long>> dp(n, vector<long long>(n, -1));
  vector<vector<long long>> range_sum(n, vector<long long>(n, -1));

  cout << f(0, n - 1, v, dp, range_sum) << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ffor(n) for (int i = 0; i < n; i++)
#define fffor(n) for (int j = 0; j < n; j++)
int INF = 1e9 + 7;
long long INF2 = 1e16;

long long f(int l, int r, vector<long long> &v, vector<vector<long long>> &dp,
            vector<vector<long long>> &range_sum) {
  if (dp[l][r] != -1)
    return dp[l][r];
  if (l == r) {
    range_sum[l][r] = v[l];
    dp[l][r] = 0;
    return dp[l][r];
  }
  if (l + 1 == r) {
    range_sum[l][r] = v[l] + v[r];
    dp[l][r] = range_sum[l][r];
    return dp[l][r];
  }
  if (l > r) {
    return 0;
  }
  long long ans = INF2;
  for (int i = l; i < r; i++) {
    ans = min(ans, f(l, i, v, dp, range_sum) + f(i + 1, r, v, dp, range_sum) +
                       range_sum[l][i] + range_sum[i + 1][r]);
  }
  dp[l][r] = ans;
  range_sum[l][r] = range_sum[l][l + 1] + range_sum[l + 2][r];
  return ans;
}

int main(void) {
  int n;
  cin >> n;
  vector<long long> v(n);
  ffor(n) cin >> v[i];

  vector<vector<long long>> dp(n, vector<long long>(n, -1));
  vector<vector<long long>> range_sum(n, vector<long long>(n, -1));

  cout << f(0, n - 1, v, dp, range_sum) << '\n';
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define si short int
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pill pair<ll, ll>
#define f first
#define s second

using namespace std;

const ll N = 181;
const ll big = 1e18;
const ll block = 100;
const ll mod = 1e6;

ll n;
ll a[N];
ll dp[N][N];
bool was[N][N];

ll rec(ll l, ll r) {
  if (was[l][r])
    return dp[l][r];
  if (l == r)
    return a[l];
  ll ans = big;
  ll ls = a[r];

  for (int j = l; j < r; j++) {
    ls += a[j];
    ll x = rec(l, j) + rec(j + 1, r);
    ans = min(x, ans);
  }
  was[l][r] = 1;
  dp[l][r] = ans + ls;
  return dp[l][r];
}

int main() {
  speed;
  cin >> n;
  ll ls = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i], ls += a[i];
  cout << rec(0, n - 1) - ls;
}

/*
        Code written by
        Codeforces : I_Love_Trott
        mail : dzhon.snoudin@mail.ru
*/


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define si short int
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pill pair<ll, ll>
#define f first
#define s second

using namespace std;

const ll N = 481;
const ll big = 1e18;
const ll block = 100;
const ll mod = 1e6;

ll n;
ll a[N];
ll dp[N][N];
bool was[N][N];

ll rec(ll l, ll r) {
  if (was[l][r])
    return dp[l][r];
  if (l == r)
    return a[l];
  ll ans = big;
  ll ls = a[r];

  for (int j = l; j < r; j++) {
    ls += a[j];
    ll x = rec(l, j) + rec(j + 1, r);
    ans = min(x, ans);
  }
  was[l][r] = 1;
  dp[l][r] = ans + ls;
  return dp[l][r];
}

int main() {
  speed;
  cin >> n;
  ll ls = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i], ls += a[i];
  cout << rec(0, n - 1) - ls;
}

/*
        Code written by
        Codeforces : I_Love_Trott
        mail : dzhon.snoudin@mail.ru
*/
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
using namespace std;
using lli = long long int;
#define int long long int
void YESNO(bool), YesNo(bool);
template <class T1, class T2> bool chmin(T1 &l, const T2 &r);
template <class T1, class T2> bool chmax(T1 &l, const T2 &r);
constexpr int M = 405;
lli dp[M][M] = {};
lli sum[M][M] = {};
void solve(long long N, std::vector<long long> a) {
  rep(i, M) rep(j, M) dp[i][j] = 1e18;
  rep(i, N) dp[i][i + 1] = 0;
  rep(i, N) for (int j = i + 1; j <= N; j++) {
    // sum of a[j, i)
    sum[i][j] = sum[i][j - 1] + a[j - 1];
  }
  for (int r = 2; r <= N; r++) {
    rep(i, N) {
      // [i, i+r)
      for (int k = i + 1; k < i + r; k++)
        chmin(dp[i][i + r],
              dp[i][k] + dp[k][i + r] + sum[i][k] + sum[k][i + r]);
    }
  }
  cout << dp[0][N] << endl;
}

signed main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> a(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }
  solve(N, std::move(a));
  return 0;
}

// -- lib
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }

template <class T1, class T2> bool chmin(T1 &l, const T2 &r) {
  return (l > r) ? (l = r, true) : false;
}

template <class T1, class T2> bool chmax(T1 &l, const T2 &r) {
  return (l < r) ? (l = r, true) : false;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
using namespace std;
using lli = long long int;
#define int long long int
void YESNO(bool), YesNo(bool);
template <class T1, class T2> bool chmin(T1 &l, const T2 &r);
template <class T1, class T2> bool chmax(T1 &l, const T2 &r);
constexpr int M = 405;
lli dp[M][M] = {};
lli sum[M][M] = {};
void solve(long long N, std::vector<long long> a) {
  rep(i, M) rep(j, M) dp[i][j] = 1e18;
  rep(i, N) dp[i][i + 1] = 0;
  rep(i, N) for (int j = i + 1; j <= N; j++) {
    // sum of a[j, i)
    sum[i][j] = sum[i][j - 1] + a[j - 1];
  }
  for (int r = 2; r <= N; r++) {
    rep(i, N) {
      // [i, i+r)
      for (int k = i + 1; k < i + r && i + r <= N; k++)
        chmin(dp[i][i + r],
              dp[i][k] + dp[k][i + r] + sum[i][k] + sum[k][i + r]);
    }
  }
  cout << dp[0][N] << endl;
}

signed main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> a(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }
  solve(N, std::move(a));
  return 0;
}

// -- lib
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }

template <class T1, class T2> bool chmin(T1 &l, const T2 &r) {
  return (l > r) ? (l = r, true) : false;
}

template <class T1, class T2> bool chmax(T1 &l, const T2 &r) {
  return (l < r) ? (l = r, true) : false;
}