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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define mp make_pair
#define x first
#define y second
typedef long long int ll;
// typedef tree< ll, null_type, less_equal< ll >, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
#define pii pair<ll, ll>
const ll N = 3e5 + 9;
// const ll m = 1e9 + 7;
const ll inf = 1e16;
const ll mod = 1e9 + 7;

ll dp[3005][3005][2];
ll a[3005];
void rec(ll i, ll j) {
  if (i == j) {
    dp[i][j][0] = a[i];
    dp[i][j][1] = 0;
    // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<'\n';
    return;
  } else if (dp[i][j][0] != -1) {
    // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<'\n';
    return;
  } else {
    ll t = inf, sz;
    for (int k = i; k < j; k++) {
      rec(i, k);
      rec(k + 1, j);
      ll sum = dp[i][k][0] + dp[i][k][1] + dp[k + 1][j][0] + dp[k + 1][j][1];
      if (t > sum) {
        t = sum;
        sz = dp[i][k][0] + dp[k + 1][j][0];
      }
    }
    dp[i][j][0] = sz;
    dp[i][j][1] = t;
    // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<'\n';
    return;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    memset(dp, -1, sizeof(dp));
    rec(0, n - 1);
    cout << dp[0][n - 1][1];
  }
}

Fixed submission:
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define mp make_pair
#define x first
#define y second
typedef long long int ll;
// typedef tree< ll, null_type, less_equal< ll >, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
#define pii pair<ll, ll>
const ll N = 3e5 + 9;
// const ll m = 1e9 + 7;
const ll inf = 1e16;
const ll mod = 1e9 + 7;

ll dp[3005][3005][2];
ll a[3005];
void rec(ll i, ll j) {
  if (i == j) {
    dp[i][j][0] = a[i];
    dp[i][j][1] = 0;
    // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<'\n';
    return;
  } else if (dp[i][j][0] != -1) {
    // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<'\n';
    return;
  } else {
    ll t = inf, sz;
    for (int k = i; k < j; k++) {
      rec(i, k);
      rec(k + 1, j);
      ll sum = dp[i][k][0] + dp[i][k][1] + dp[k + 1][j][0] + dp[k + 1][j][1];
      if (t > sum) {
        t = sum;
        sz = dp[i][k][0] + dp[k + 1][j][0];
      }
    }
    dp[i][j][0] = sz;
    dp[i][j][1] = t;
    // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<'\n';
    return;
  }
}

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

  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    memset(dp, -1, sizeof(dp));
    rec(0, n - 1);
    cout << dp[0][n - 1][1];
  }
}
Problem p03173 (C++)

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

using namespace std;
const long long int maxx = 1e15L;
const int sz = 444;

long long dp[sz][sz], sum[sz][sz];
vector<long long> v;

long long f(int a, int b) {
  if (a == b) {
    return 0;
  }
  long long ans = maxx;
  for (int i = a; i < b; ++i) {
    ans = min(ans, f(a, i) + f(i + 1, b) + sum[a][b]);
  }

  dp[a][b] = ans;
  return ans;
}

int main(void) {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    v.push_back(x);
  }

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

  for (int i = 0; i <= n - 1; ++i) {
    for (int j = i; j <= n - 1; ++j) {
      if (j == i) {
        sum[i][j] = v[i];
        continue;
      }
      sum[i][j] = sum[i][j - 1] + v[j];
    }
  }

  cout << f(0, n - 1);

  // cout<<" table "<<endl;
  // for (int i = 0; i < n; ++i) {
  //     for (int j = 0; j < n; ++j) {
  //         cout<<dp[i][j]<< " ";
  //     }
  //     cout<<endl;
  // }

  return 0;
}


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

using namespace std;
const long long int maxx = 1e15L;
const int sz = 444;

long long dp[sz][sz], sum[sz][sz];
vector<long long> v;

long long f(int a, int b) {
  if (a == b) {
    return 0;
  }
  if (dp[a][b] != maxx) {
    return dp[a][b];
  }
  long long ans = maxx;
  for (int i = a; i < b; ++i) {
    ans = min(ans, f(a, i) + f(i + 1, b) + sum[a][b]);
  }

  dp[a][b] = ans;
  return ans;
}

int main(void) {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    v.push_back(x);
  }

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

  for (int i = 0; i <= n - 1; ++i) {
    for (int j = i; j <= n - 1; ++j) {
      if (j == i) {
        sum[i][j] = v[i];
        continue;
      }
      sum[i][j] = sum[i][j - 1] + v[j];
    }
  }

  cout << f(0, n - 1);

  // cout<<" table "<<endl;
  // for (int i = 0; i < n; ++i) {
  //     for (int j = 0; j < n; ++j) {
  //         cout<<dp[i][j]<< " ";
  //     }
  //     cout<<endl;
  // }

  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define uu first
#define vv second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define INF 1e18
#define fastRead                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;

const int MAX = 1e5 + 5;
const ll MOD = 1e9 + 7;
ll dp[300][300];

int main() {
  fastRead;
  int n;
  cin >> n;
  vector<ll> vec(n + 1), sum(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> vec[i], sum[i] = (sum[i - 1] + vec[i]);

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      dp[i][j] = INF;
  }

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

  for (int i = 2; i <= n; i++) {
    for (int j = 1; j + i - 1 <= n; j++) {
      for (int k = 1; k < i; k++) {

        int len1 = k, len2 = i - k;
        int frm1 = j, frm2 = j + len1;

        int to = j + i - 1;

        //                cout<<i<<" -> "<<j<<" "<<i + j-1<<" -- "<<frm1<<"
        //                "<<frm1 + len1-1<<" -- "<<frm2<<" "<<len2 + frm2-1;
        //                cout<<"\t--"<<dp[len1][frm1]<<" "<<dp[len2][frm2]<<"
        //                "<<sum[to] - sum[frm1-1]<<'\n';

        dp[i][j] = min(dp[i][j], dp[len1][frm1] + dp[len2][frm2] + sum[to] -
                                     sum[frm1 - 1]);
      }
    }
  }
  cout << dp[n][1] - sum[n] << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define uu first
#define vv second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define INF 1e18
#define fastRead                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;

const int MAX = 1e5 + 5;
const ll MOD = 1e9 + 7;
ll dp[500][500];

int main() {
  fastRead;
  int n;
  cin >> n;
  vector<ll> vec(n + 1), sum(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> vec[i], sum[i] = (sum[i - 1] + vec[i]);

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      dp[i][j] = INF;
  }

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

  for (int i = 2; i <= n; i++) {
    for (int j = 1; j + i - 1 <= n; j++) {
      for (int k = 1; k < i; k++) {

        int len1 = k, len2 = i - k;
        int frm1 = j, frm2 = j + len1;

        int to = j + i - 1;

        //                cout<<i<<" -> "<<j<<" "<<i + j-1<<" -- "<<frm1<<"
        //                "<<frm1 + len1-1<<" -- "<<frm2<<" "<<len2 + frm2-1;
        //                cout<<"\t--"<<dp[len1][frm1]<<" "<<dp[len2][frm2]<<"
        //                "<<sum[to] - sum[frm1-1]<<'\n';

        dp[i][j] = min(dp[i][j], dp[len1][frm1] + dp[len2][frm2] + sum[to] -
                                     sum[frm1 - 1]);
      }
    }
  }
  cout << dp[n][1] - sum[n] << '\n';
  return 0;
}
Problem p03173 (C++)

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

using namespace std;

#define ff first
#define ss second

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;

const int MOD = 1e9 + 7;
const ll INF = 1e18;
const double EPS = 1e-6;
const int MAX_N = 405;

int N;
ll a[MAX_N];
ll memo[MAX_N][MAX_N];

ll dp(int l, int r) {
  if (l == r)
    return 0;
  if (memo[l][r] != -1)
    return memo[l][r];
  ll total = 0;
  for (int i = l; i <= r; ++i)
    total += a[i];
  ll ret = INF, sum = 0;
  for (int i = l; i < r; ++i) {
    sum += a[i];
    ret = min(ret, dp(l, i) + dp(i + 1, r) + sum + (total - sum));
  }
  return ret;
}

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

  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  memset(memo, -1, sizeof memo);

  cout << dp(0, N - 1) << '\n';

  return 0;
}

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

using namespace std;

#define ff first
#define ss second

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;

const int MOD = 1e9 + 7;
const ll INF = 1e18;
const double EPS = 1e-6;
const int MAX_N = 405;

int N;
ll a[MAX_N];
ll memo[MAX_N][MAX_N];

ll dp(int l, int r) {
  if (l == r)
    return 0;
  if (memo[l][r] != -1)
    return memo[l][r];
  ll total = 0;
  for (int i = l; i <= r; ++i)
    total += a[i];
  ll ret = INF, sum = 0;
  for (int i = l; i < r; ++i) {
    sum += a[i];
    ret = min(ret, dp(l, i) + dp(i + 1, r) + sum + (total - sum));
  }
  return memo[l][r] = ret;
}

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

  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  memset(memo, -1, sizeof memo);

  cout << dp(0, N - 1) << '\n';

  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, a, b) for (long long i = a; i <= b; ++i)
#define REPN(i, a, b) for (long long i = a; i <= b; --i)
#define rep(i, a, b) for (long long i = a; i < b; ++i)
#define rep0(i, b) for (long long i = 0; i < b; ++i)
#define REP0(i, b) for (long long i = 0; i <= b; ++i)
#include <cmath>
#include <iomanip>
using namespace std;

typedef long long ll;

int MODE = 1e9 + 7;
ll prefix[403];
ll dp[403][403];
ll arr[403];
int n;

ll helper(int left, int right) {
  if (left < 0 || right > n - 1 || left > right || left == right) {

    return 0;
  }
  if (dp[left][right] != -1) {
    return dp[left][right];
  }

  ll minimum = 1e18 + 5;

  for (int i = left; i <= right; ++i) {
    minimum = min(minimum, helper(left, i) + helper(i + 1, right));
  }

  return dp[left][right] = minimum + prefix[right] - prefix[left - 1];
}

int main() {

  cin >> n;

  memset(dp, -1, sizeof(dp));

  rep0(i, n) { cin >> arr[i]; }

  prefix[0] = 0;
  rep0(i, n + 1) { prefix[i] = prefix[i - 1] + arr[i]; }

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

  /*for(int i=0; i<n; ++i)
  {
      dp[i][i] = 0;
  }

  for(int i=n-2; i>=0; --i)
  {

      for(int j=i+1; j<n; ++j)
      {
          dp[i][j] = 1e18+5;
          ll s =  prefix[j] - prefix[i-1];
          for(int k = i; k<j; ++k)
          {
              dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + s);
          }
      }
  }*/

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, a, b) for (long long i = a; i <= b; ++i)
#define REPN(i, a, b) for (long long i = a; i <= b; --i)
#define rep(i, a, b) for (long long i = a; i < b; ++i)
#define rep0(i, b) for (long long i = 0; i < b; ++i)
#define REP0(i, b) for (long long i = 0; i <= b; ++i)
#include <cmath>
#include <iomanip>
using namespace std;

typedef long long ll;

int MODE = 1e9 + 7;
ll prefix[403];
ll dp[403][403];
ll arr[403];
int n;

ll helper(int left, int right) {
  if (left < 0 || right > n - 1 || left > right || left == right) {

    return 0;
  }
  if (dp[left][right] != -1) {
    return dp[left][right];
  }

  ll minimum = 1e18 + 5;

  for (int i = left; i < right; ++i) {
    minimum = min(minimum, helper(left, i) + helper(i + 1, right));
  }

  return dp[left][right] = minimum + prefix[right] - prefix[left - 1];
}

int main() {

  cin >> n;

  memset(dp, -1, sizeof(dp));

  rep0(i, n) { cin >> arr[i]; }

  prefix[0] = 0;
  rep0(i, n + 1) { prefix[i] = prefix[i - 1] + arr[i]; }

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

  /*for(int i=0; i<n; ++i)
  {
      dp[i][i] = 0;
  }

  for(int i=n-2; i>=0; --i)
  {

      for(int j=i+1; j<n; ++j)
      {
          dp[i][j] = 1e18+5;
          ll s =  prefix[j] - prefix[i-1];
          for(int k = i; k<j; ++k)
          {
              dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + s);
          }
      }
  }*/

  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int M = 998244353;
#define int long long int
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define MAX 1e15 + 1
#define pi pair<int, int>
const int mod = 1e9 + 7;
const int N = 3e5 + 7;

int n, k, a[109];
int dp[109][109], pre[109];

int sum(int aa, int b) {
  if (aa > b)
    return 0;
  if (aa == 0)
    return pre[b];
  return pre[b] - pre[aa - 1];
}
int solve(int l, int r) {
  if (l >= r) {

    return 0;
  }
  if (dp[l][r] != -1)
    return dp[l][r];
  int val = MAX;
  for (int i = l; i < r; i++) {

    int x = solve(l, i);
    int y = solve(i + 1, r);
    int p = x + y + sum(l, i) + sum(i + 1, r);
    val = min(val, p);
  }
  dp[l][r] = val;
  return val;
}

signed main() {
  fast;

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

  memset(dp, -1, sizeof(dp));
  cout << solve(0, n - 1);
}

Fixed submission:
#include <bits/stdc++.h>
const int M = 998244353;
#define int long long int
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define MAX 1e15 + 1
#define pi pair<int, int>
const int mod = 1e9 + 7;
const int N = 3e5 + 7;

int n, k, a[409];
int dp[409][409], pre[409];

int sum(int aa, int b) {
  if (aa > b)
    return 0;
  if (aa == 0)
    return pre[b];
  return pre[b] - pre[aa - 1];
}
int solve(int l, int r) {
  if (l >= r) {

    return 0;
  }
  if (dp[l][r] != -1)
    return dp[l][r];
  int val = MAX;
  for (int i = l; i < r; i++) {

    int x = solve(l, i);
    int y = solve(i + 1, r);
    int p = x + y + sum(l, i) + sum(i + 1, r);
    val = min(val, p);
  }
  dp[l][r] = val;
  return val;
}

signed main() {
  fast;

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

  memset(dp, -1, sizeof(dp));
  cout << solve(0, n - 1);
}
Problem p03173 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
const int64_t INF = 1e18;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &e : A)
    cin >> e;
  using P = pair<int64_t, int64_t>;
  map<int, map<int, P>> dp;
  auto f = [&](auto &&f, int l, int r) -> P {
    if (dp[l][r] != P(0LL, 0LL))
      return dp[l][r];
    if (l + 1 == r)
      return make_pair(0, A[l]);
    P res = make_pair(INF, INF);
    for (int i = l + 1; i < r; ++i) {
      auto left = f(f, l, i);
      auto right = f(f, i, r);
      auto cost = left.first + right.first;
      auto sz = left.second + right.second;
      if (res.first > cost + sz) {
        res.first = cost + sz;
        res.second = sz;
      }
    }
    return dp[l][r] = res;
  };
  cout << f(f, 0, N).first << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
const int64_t INF = 1e18;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &e : A)
    cin >> e;
  using P = pair<int64_t, int64_t>;
  vector<vector<P>> dp(N + 1, vector<P>(N + 1));
  auto f = [&](auto &&f, int l, int r) -> P {
    if (dp[l][r] != P(0LL, 0LL))
      return dp[l][r];
    if (l + 1 == r)
      return make_pair(0, A[l]);
    P res = make_pair(INF, INF);
    for (int i = l + 1; i < r; ++i) {
      auto left = f(f, l, i);
      auto right = f(f, i, r);
      auto cost = left.first + right.first;
      auto sz = left.second + right.second;
      if (res.first > cost + sz) {
        res.first = cost + sz;
        res.second = sz;
      }
    }
    return dp[l][r] = res;
  };
  cout << f(f, 0, N).first << '\n';
}
Problem p03173 (C++)

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

using namespace std;

using int64 = long long;

template <typename T>
vector<vector<T>> Make2DVector(int d1, int d2, T default_value) {
  return vector<vector<T>>(d1, vector<T>(d2, default_value));
}

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

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

  vector<vector<int64>> dp1 = Make2DVector(n + 1, n + 1, -1LL);
  vector<vector<int64>> dp2 = Make2DVector(n + 1, n + 1, -1LL);
  function<void(int, int)> solve = [&](int l, int r) {
    if (r - l == 1) {
      dp1[l][r] = 0;
      dp2[l][r] = xs[l];
      return;
    }
    for (int m = l + 1; m <= r - 1; m++) {
      solve(l, m);
      solve(m, r);
      if (dp2[l][r] < 0) {
        dp2[l][r] = dp2[l][m] + dp2[m][r];
      }
      if (dp1[l][r] < 0) {
        dp1[l][r] = dp1[l][m] + dp1[m][r] + dp2[l][r];
      } else {
        dp1[l][r] = min(dp1[l][r], dp1[l][m] + dp1[m][r] + dp2[l][r]);
      }
    }
  };
  solve(0, n);
  cout << dp1[0][n] << endl;
}


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

using namespace std;

using int64 = long long;

template <typename T>
vector<vector<T>> Make2DVector(int d1, int d2, T default_value) {
  return vector<vector<T>>(d1, vector<T>(d2, default_value));
}

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

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

  vector<vector<int64>> dp1 = Make2DVector(n + 1, n + 1, -1LL);
  vector<vector<int64>> dp2 = Make2DVector(n + 1, n + 1, -1LL);
  function<void(int, int)> solve = [&](int l, int r) {
    if (dp1[l][r] >= 0)
      return;

    if (r - l == 1) {
      dp1[l][r] = 0;
      dp2[l][r] = xs[l];
      return;
    }
    for (int m = l + 1; m <= r - 1; m++) {
      solve(l, m);
      solve(m, r);
      if (dp2[l][r] < 0) {
        dp2[l][r] = dp2[l][m] + dp2[m][r];
      }
      if (dp1[l][r] < 0) {
        dp1[l][r] = dp1[l][m] + dp1[m][r] + dp2[l][r];
      } else {
        dp1[l][r] = min(dp1[l][r], dp1[l][m] + dp1[m][r] + dp2[l][r]);
      }
    }
  };
  solve(0, n);
  cout << dp1[0][n] << endl;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
#define qmin(x, y) (x = min(x, y))
#define qmax(x, y) (x = max(x, y))
using namespace std;

inline char gc() {
  //	static char buf[100000],*p1,*p2;
  //	return
  //(p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
  return getchar();
}

template <class T> int read(T &ans) {
  ans = 0;
  char ch = gc();
  T f = 1;
  while (!isdigit(ch)) {
    if (ch == EOF)
      return -1;
    if (ch == '-')
      f = -1;
    ch = gc();
  }
  while (isdigit(ch))
    ans = ans * 10 + ch - '0', ch = gc();
  ans *= f;
  return 1;
}

template <class T1, class T2> int read(T1 &a, T2 &b) {
  return read(a) != EOF && read(b) != EOF ? 2 : EOF;
}

template <class T1, class T2, class T3> int read(T1 &a, T2 &b, T3 &c) {
  return read(a, b) != EOF && read(c) != EOF ? 3 : EOF;
}

typedef long long ll;
const int Maxn = 310;
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;

int n;
ll f[Maxn][Maxn], a[Maxn];

signed main() {
  //	freopen("test.in","r",stdin);
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]), a[i] += a[i - 1];
  memset(f, 0x3f, sizeof(f));
  for (int i = 1; i <= n; i++)
    f[i][i] = 0;
  for (int i = 2; i <= n; i++)
    for (int j = i; j <= n; j++) {
      int l = j - i + 1, r = j;
      for (int k = l; k < r; k++)
        qmin(f[l][r], f[l][k] + f[k + 1][r] + a[r] - a[l - 1]);
    }
  printf("%lld", f[1][n]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
#define qmin(x, y) (x = min(x, y))
#define qmax(x, y) (x = max(x, y))
using namespace std;

inline char gc() {
  //	static char buf[100000],*p1,*p2;
  //	return
  //(p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
  return getchar();
}

template <class T> int read(T &ans) {
  ans = 0;
  char ch = gc();
  T f = 1;
  while (!isdigit(ch)) {
    if (ch == EOF)
      return -1;
    if (ch == '-')
      f = -1;
    ch = gc();
  }
  while (isdigit(ch))
    ans = ans * 10 + ch - '0', ch = gc();
  ans *= f;
  return 1;
}

template <class T1, class T2> int read(T1 &a, T2 &b) {
  return read(a) != EOF && read(b) != EOF ? 2 : EOF;
}

template <class T1, class T2, class T3> int read(T1 &a, T2 &b, T3 &c) {
  return read(a, b) != EOF && read(c) != EOF ? 3 : EOF;
}

typedef long long ll;
const int Maxn = 410;
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;

int n;
ll f[Maxn][Maxn], a[Maxn];

signed main() {
  //	freopen("test.in","r",stdin);
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]), a[i] += a[i - 1];
  memset(f, 0x3f, sizeof(f));
  for (int i = 1; i <= n; i++)
    f[i][i] = 0;
  for (int i = 2; i <= n; i++)
    for (int j = i; j <= n; j++) {
      int l = j - i + 1, r = j;
      for (int k = l; k < r; k++)
        qmin(f[l][r], f[l][k] + f[k + 1][r] + a[r] - a[l - 1]);
    }
  printf("%lld", f[1][n]);
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

pair<ll, ll> dp[401][401];

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  a[n] = INF;

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = make_pair(0, 0);
      if (i + 1 == j) {
        dp[i][j].second = a[i];
      }
    }
  }

  for (int l = 2; l <= n; l++) {
    for (int i = 0; i < n; i++) {
      int j = i + l;
      ll v = INF;
      ll s = 0;
      for (int k = 1; k < l; k++) {
        if (dp[i + k][j].first + dp[i][i + k].first + dp[i + k][j].second +
                dp[i][i + k].second <
            v) {
          v = dp[i + k][j].first + dp[i][i + k].first + dp[i + k][j].second +
              dp[i][i + k].second;
          s = dp[i + k][j].second + dp[i][i + k].second;
        }
      }
      dp[i][j].first = v;
      dp[i][j].second = s;
    }
  }

  cout << dp[0][n].first << endl;

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

pair<ll, ll> dp[401][401];

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  a[n] = INF;

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = make_pair(0, 0);
      if (i + 1 == j) {
        dp[i][j].second = a[i];
      }
    }
  }

  for (int l = 2; l <= n; l++) {
    for (int i = 0; i + l <= n; i++) {
      int j = i + l;
      ll v = INF;
      ll s = 0;
      for (int k = 1; k < l; k++) {
        if (dp[i + k][j].first + dp[i][i + k].first + dp[i + k][j].second +
                dp[i][i + k].second <
            v) {
          v = dp[i + k][j].first + dp[i][i + k].first + dp[i + k][j].second +
              dp[i][i + k].second;
          s = dp[i + k][j].second + dp[i][i + k].second;
        }
      }
      dp[i][j].first = v;
      dp[i][j].second = s;
    }
  }

  cout << dp[0][n].first << endl;

  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long dp[401][401];
long long a[401];
long long ps[401];

int main() {
  int n;
  cin >> n;
  memset(dp, 0x3f3f3f3f, sizeof dp);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    dp[i][i] = 0;

    if (i > 0) {
      dp[i - 1][i] = a[i - 1] + a[i];
    }
  }
  partial_sum(a, a + n + 1, ps);

  for (int l = 2; l <= n + 1; l++) {
    for (int i = 1; i <= n - l + 1; i++) {
      for (int j = 0; j < l; j++) {
        dp[i][i + l] =
            min(dp[i][i + l], dp[i][i + j] + dp[i + j + 1][i + l] + ps[i + j] -
                                  ps[i - 1] + ps[i + l] - ps[i + j]);
      }
    }
  }
  cout << dp[1][n];
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long dp[402][402];
long long a[402];
long long ps[402];

int main() {
  int n;
  cin >> n;
  memset(dp, 0x3f3f3f3f, sizeof dp);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    dp[i][i] = 0;

    if (i > 0) {
      dp[i - 1][i] = a[i - 1] + a[i];
    }
  }
  partial_sum(a, a + n + 1, ps);

  for (int l = 2; l <= n + 1; l++) {
    for (int i = 1; i <= n - l + 1; i++) {
      for (int j = 0; j < l; j++) {
        dp[i][i + l] =
            min(dp[i][i + l], dp[i][i + j] + dp[i + j + 1][i + l] + ps[i + j] -
                                  ps[i - 1] + ps[i + l] - ps[i + j]);
      }
    }
  }
  cout << dp[1][n];
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long dp[402][402] = {}, sum[402][402] = {};
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j <= n; j++) {
      dp[i][j] = 1LL << 60;
      sum[i][j] = sum[i][j - 1] + a[j - 1];
    }
  for (int i = 0; i < n; i++)
    dp[i][i + 1] = 0;
  for (int w = 2; w <= n; w++)
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < i + w; j++) {
        dp[i][i + w] = min(dp[i][i + w],
                           dp[i][j] + dp[j][i + w] + sum[i][j] + sum[j][i + w]);
      }
    }
  cout << dp[0][n] << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long dp[402][402] = {}, sum[402][402] = {};
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j <= n; j++) {
      dp[i][j] = 1LL << 60;
      sum[i][j] = sum[i][j - 1] + a[j - 1];
    }
  for (int i = 0; i < n; i++)
    dp[i][i + 1] = 0;
  for (int w = 2; w <= n; w++)
    for (int i = 0; i + w <= n; i++) {
      for (int j = i + 1; j < i + w; j++) {
        dp[i][i + w] = min(dp[i][i + w],
                           dp[i][j] + dp[j][i + w] + sum[i][j] + sum[j][i + w]);
      }
    }
  cout << dp[0][n] << endl;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;

const int N = 300;
vector<int> a(N + 1);
vector<int> psum(N + 1);
vector<vector<int>> dp(N + 1, vector<int>(N + 1, -1));
int n;
int solve(int l, int r) {
  if (l == r)
    return 0;
  if (dp[l][r] != -1)
    return dp[l][r];
  dp[l][r] = 1e15;
  for (int i = l; i < r; i++)
    dp[l][r] =
        min(dp[l][r], psum[r] - psum[l - 1] + solve(l, i) + solve(i + 1, r));
  return dp[l][r];
}

int32_t main() {
  IOS cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    psum[i] = psum[i - 1] + a[i];
  }
  cout << solve(1, n) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;

const int N = 400;
vector<int> a(N + 1);
vector<int> psum(N + 1);
vector<vector<int>> dp(N + 1, vector<int>(N + 1, -1));
int n;
int solve(int l, int r) {
  if (l == r)
    return 0;
  if (dp[l][r] != -1)
    return dp[l][r];
  dp[l][r] = 1e15;
  for (int i = l; i < r; i++)
    dp[l][r] =
        min(dp[l][r], psum[r] - psum[l - 1] + solve(l, i) + solve(i + 1, r));
  return dp[l][r];
}

int32_t main() {
  IOS cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    psum[i] = psum[i - 1] + a[i];
  }
  cout << solve(1, n) << endl;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll pref_sum[404][404], dp[404][404];
int a[3000];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (n == 1) {
    cout << a[0] << endl;
    return 0;
  }
  memset(pref_sum, 0, sizeof(pref_sum));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    pref_sum[i][i] = a[i];
    dp[i][i] = a[i];
    dp[i][i + 1] = a[i] + a[i + 1];
    for (int j = i + 1; j < n; j++)
      pref_sum[i][j] = pref_sum[i][j - 1] + a[j];
  }
  int i = 0, j = 2;
  int jstart = 1;
  while (true) {
    ll val;
    ll minval = LONG_LONG_MAX;
    for (int z = i; z < j; z++) {
      val = pref_sum[i][j] + dp[i][z] + dp[z + 1][j];
      if (z == i)
        val -= a[i];
      if (z + 1 == j)
        val -= a[j];
      minval = min(val, minval);
    }
    dp[i][j] = minval;
    if ((i == 0 && j == n - 1))
      break;
    i++;
    j++;
    if (j == n) {
      i = 0;
      jstart++;
      j = jstart;
    }
  }
  cout << dp[0][n - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll pref_sum[404][404], dp[404][404];
int a[3000];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (n == 1) {
    cout << a[0] << endl;
    return 0;
  }
  if (n == 2) {
    cout << a[0] + a[1] << endl;
    return 0;
  }
  memset(pref_sum, 0, sizeof(pref_sum));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    pref_sum[i][i] = a[i];
    dp[i][i] = a[i];
    dp[i][i + 1] = a[i] + a[i + 1];
    for (int j = i + 1; j < n; j++)
      pref_sum[i][j] = pref_sum[i][j - 1] + a[j];
  }
  int i = 0, j = 2;
  int jstart = 1;
  while (true) {
    ll val;
    ll minval = LONG_LONG_MAX;
    for (int z = i; z < j; z++) {
      val = pref_sum[i][j] + dp[i][z] + dp[z + 1][j];
      if (z == i)
        val -= a[i];
      if (z + 1 == j)
        val -= a[j];
      minval = min(val, minval);
    }
    dp[i][j] = minval;
    if ((i == 0 && j == n - 1))
      break;
    i++;
    j++;
    if (j == n) {
      i = 0;
      jstart++;
      j = jstart;
    }
  }
  cout << dp[0][n - 1] << endl;
}
Problem p03173 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

std::vector<i64> a;
std::vector<std::vector<i64>> dp;
i64 solve(i64 L, i64 R) {
  if (L >= R)
    return 0LL;
  if (L + 1 == R)
    return a[L];

  i64 A = 0, B = 0, ret = 1LL << 60;
  for (int i = L; i < R; i++)
    A += a[i];
  for (int i = L; i < R - 1; i++) {
    B += a[i];
    A -= a[i];

    ret = std::min(ret, solve(L, i + 1) + solve(i + 1, R) + (A + B));
  }
  return dp[L][R] = ret;
}

int main() {
  int n;
  scanf("%d", &n);
  a.resize(n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);

  dp = make_v<i64>(n + 1, n + 1);
  solve(0, n);

  for (auto v : a)
    dp[0][n] -= v;
  printf("%lld\n", dp[0][n]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

std::vector<i64> a;
std::vector<std::vector<i64>> dp;
i64 solve(i64 L, i64 R) {
  if (dp[L][R])
    return dp[L][R];
  if (L >= R)
    return 0LL;
  if (L + 1 == R)
    return a[L];

  i64 A = 0, B = 0, ret = 1LL << 60;
  for (int i = L; i < R; i++)
    A += a[i];
  for (int i = L; i < R - 1; i++) {
    B += a[i];
    A -= a[i];

    ret = std::min(ret, solve(L, i + 1) + solve(i + 1, R) + (A + B));
  }
  return dp[L][R] = ret;
}

int main() {
  int n;
  scanf("%d", &n);
  a.resize(n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);

  dp = make_v<i64>(n + 1, n + 1);
  solve(0, n);

  for (auto v : a)
    dp[0][n] -= v;
  printf("%lld\n", dp[0][n]);
  return 0;
}
Problem p03173 (C++)

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

using namespace std;
long long MOD = 1000000007;
#define IOS()                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
typedef long long ll;
#define pb push_back
typedef pair<ll, ll> pi;
ll dp[401][401];
ll sum[401][401];
ll a[500];
int n;

ll find(int l, int r) {
  ll ans = LONG_LONG_MAX;
  // cout<<"ans is "<<ans<<endl;
  if (l == r)
    return dp[l][r] = 0ll;

  else if (dp[l][r] != -1)
    return dp[l][r];

  else {
    for (int i = l; i < r; i++) {
      ans = std::min(ans, sum[l][r] + find(l, i) + find(i + 1, r));
    }
    return ans;
  }
}
void preprocess() {
  for (int i = 1; i <= n; i++) {
    sum[i][i] = a[i];

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

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

  memset(dp, -1ll, sizeof dp);

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

  preprocess();

  ll ans = find(1, n);
  cout << ans << endl;

  return 0;
}

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

using namespace std;
long long MOD = 1000000007;
#define IOS()                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
typedef long long ll;
#define pb push_back
typedef pair<ll, ll> pi;
ll dp[401][401];
ll sum[401][401];
ll a[500];
int n;

ll find(int l, int r) {
  ll ans = LONG_LONG_MAX;
  // cout<<"ans is "<<ans<<endl;
  if (l == r)
    return dp[l][r] = 0ll;

  else if (dp[l][r] != -1)
    return dp[l][r];

  else {
    for (int i = l; i < r; i++) {
      ans = std::min(ans, sum[l][r] + find(l, i) + find(i + 1, r));
    }
    return dp[l][r] = ans;
  }
}
void preprocess() {
  for (int i = 1; i <= n; i++) {
    sum[i][i] = a[i];

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

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

  memset(dp, -1ll, sizeof dp);

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

  preprocess();

  ll ans = find(1, n);
  cout << ans << endl;

  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
using namespace std;
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1e18;

int N;
vector<ll> a;
vector<vector<P>>
    dp; // first: コストの合計の最小値、second: 1つにまとめた時の値

P rec(int l = 0, int r = N) { // メモ化再帰
  if (r - l == 1)
    return make_pair(0, a[l]);
  P &ret = dp[l][r];

  for (int i = l + 1; i < r; i++) {
    P L = rec(l, i);
    P R = rec(i, r);
    ll sum = L.second + R.second;
    ll cost = L.first + R.first + sum;
    chmin(ret, make_pair(cost, sum));
  }

  return ret;
}

int main() {
  cin >> N;
  a.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }

  dp.assign(N + 2, vector<P>(N + 2, make_pair(INF, 0))); // 初期化

  cout << rec().first << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
using namespace std;
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1e18;

int N;
vector<ll> a;
vector<vector<P>>
    dp; // first: コストの合計の最小値、second: 1つにまとめた時の値

P rec(int l = 0, int r = N) { // メモ化再帰
  if (r - l == 1)
    return make_pair(0, a[l]);
  P &ret = dp[l][r];
  if (ret.first != INF)
    return ret;

  for (int i = l + 1; i < r; i++) {
    P L = rec(l, i);
    P R = rec(i, r);
    ll sum = L.second + R.second;
    ll cost = L.first + R.first + sum;
    chmin(ret, make_pair(cost, sum));
  }

  return ret;
}

int main() {
  cin >> N;
  a.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }

  dp.assign(N + 2, vector<P>(N + 2, make_pair(INF, 0))); // 初期化

  cout << rec().first << endl;

  return 0;
}
Problem p03173 (C++)

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

using namespace std;
#define ll long long
#define mod 1000000007

ll dp[401][401];
vector<ll> v, pre;

ll get(ll l, ll r) { return pre[r] - (l > 0 ? pre[l - 1] : 0); }

ll f(ll l, ll r) {
  if (l == r)
    return 0;
  if (r - l == 1)
    return v[l] + v[r];

  if (dp[l][r] != -1)
    dp[l][r];

  ll ans = -1;
  for (ll i = l; i < r; i++) {
    if (ans == -1) {
      ans = f(l, i) + f(i + 1, r) + get(l, i) + get(i + 1, r);
    } else {
      ans = min(ans, f(l, i) + f(i + 1, r) + get(l, i) + get(i + 1, r));
    }
  }
  dp[l][r] = ans;
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  memset(dp, -1, sizeof(dp));

  ll n;
  cin >> n;
  v.resize(n);
  pre.resize(n);

  for (ll &i : v) {
    cin >> i;
  }

  pre[0] = v[0];
  for (ll i = 1; i < n; i++) {
    pre[i] = v[i];
    pre[i] += (i > 0) ? pre[i - 1] : 0;
  }

  cout << f(0, n - 1);
}


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

using namespace std;
#define ll long long
#define mod 1000000007

ll dp[401][401];
vector<ll> v, pre;

ll get(ll l, ll r) { return pre[r] - (l > 0 ? pre[l - 1] : 0); }

ll f(ll l, ll r) {
  if (l == r)
    return 0;
  if (r - l == 1)
    return v[l] + v[r];

  if (dp[l][r] != -1)
    return dp[l][r];

  ll ans = -1;
  for (ll i = l; i < r; i++) {
    if (ans == -1) {
      ans = f(l, i) + f(i + 1, r) + get(l, i) + get(i + 1, r);
    } else {
      ans = min(ans, f(l, i) + f(i + 1, r) + get(l, i) + get(i + 1, r));
    }
  }
  dp[l][r] = ans;
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  memset(dp, -1, sizeof(dp));

  ll n;
  cin >> n;
  v.resize(n);
  pre.resize(n);

  for (ll &i : v) {
    cin >> i;
  }

  pre[0] = v[0];
  for (ll i = 1; i < n; i++) {
    pre[i] = v[i];
    pre[i] += (i > 0) ? pre[i - 1] : 0;
  }

  cout << f(0, n - 1);
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

typedef long long ll;
const ll mod = 1e9 + 7;

const ll INF = 1e18 + 7;
ll sum(int i, int j, int *a) {
  ll res = 0;
  for (auto k = i; k <= j; ++k)
    res += a[k];
  return res;
}
int main(int argc, const char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  int n, i, j, k;
  cin >> n;
  int a[n];
  for (i = 0; i < n; ++i)
    cin >> a[i];

  ll dp[n][n];

  for (i = n - 1; i >= 0; --i) {
    for (j = i; j < n; ++j) {
      if (i == j)
        dp[i][j] = 0;
      else {
        dp[i][j] = INF;
        ll s = sum(i, j, a);
        for (k = i; k < j; ++k) {
          dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + s);
        }
      }
    }
  }
  cout << dp[0][n - 1] << "\n";
#ifndef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1e1 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif // LOCAL_DEFINE
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

typedef long long ll;
const ll mod = 1e9 + 7;

const ll INF = 1e18 + 7;
ll sum(int i, int j, int *a) {
  ll res = 0;
  for (auto k = i; k <= j; ++k)
    res += a[k];
  return res;
}
int main(int argc, const char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;

  int n, i, j, k;
  cin >> n;
  int a[n];
  for (i = 0; i < n; ++i)
    cin >> a[i];

  ll dp[n][n];

  for (i = n - 1; i >= 0; --i) {
    for (j = i; j < n; ++j) {
      if (i == j)
        dp[i][j] = 0;
      else {
        dp[i][j] = INF;
        ll s = sum(i, j, a);
        for (k = i; k < j; ++k) {
          dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + s);
        }
      }
    }
  }
  cout << dp[0][n - 1] << "\n";
#ifndef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1e1 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif // LOCAL_DEFINE
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using namespace std;

#define INF ((1 << 30) - 1)
#define LLINF (1LL << 60)
#define EPS (1e-10)

// dp[i][r] := l以上r未満のスライムを1体にするときのコストの最小値
// dp[i][i+1] = 0
// dp[l][r] = min(dp[l][m], dp[m][r] + [l, m)スライムと[m,
// r)スライムを合致させるコスト) (r-l)の小さい順からやればいい

ll dp[310][310];
ll sum[310][310];

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  rep(l, n) {
    sum[l][l + 1] = a[l];
    for (int r = l + 2; r <= n; ++r) {
      sum[l][r] = sum[l][r - 1] + a[r - 1];
    }
  }

  for (int len = 2; len <= n; ++len) {
    for (int l = 0; l + len <= n; ++l) {
      int r = l + len;
      dp[l][r] = LLINF;
      for (int m = l + 1; m < r; ++m) {
        dp[l][r] = min(dp[l][r], dp[l][m] + dp[m][r] + sum[l][r]);
      }
    }
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using namespace std;

#define INF ((1 << 30) - 1)
#define LLINF (1LL << 60)
#define EPS (1e-10)

// dp[i][r] := l以上r未満のスライムを1体にするときのコストの最小値
// dp[i][i+1] = 0
// dp[l][r] = min(dp[l][m], dp[m][r] + [l, m)スライムと[m,
// r)スライムを合致させるコスト) (r-l)の小さい順からやればいい

ll dp[410][410];
ll sum[410][410];

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  rep(l, n) {
    sum[l][l + 1] = a[l];
    for (int r = l + 2; r <= n; ++r) {
      sum[l][r] = sum[l][r - 1] + a[r - 1];
    }
  }

  for (int len = 2; len <= n; ++len) {
    for (int l = 0; l + len <= n; ++l) {
      int r = l + len;
      dp[l][r] = LLINF;
      for (int m = l + 1; m < r; ++m) {
        dp[l][r] = min(dp[l][r], dp[l][m] + dp[m][r] + sum[l][r]);
      }
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
long long helper(vector<long long> &vec, vector<vector<long long>> &dp,
                 long long l, long long r) {
  if (l == r)
    return 0;
  if (l + 1 == r)
    return dp[l][r] = vec[l] + vec[r];
  long long ans = LLONG_MAX, sum = 0;
  for (long long i = l; i <= r; ++i) {
    sum += vec[i];
  }
  for (long long i = l; i < r; ++i) {
    ans = min(ans, sum + helper(vec, dp, l, i) + helper(vec, dp, i + 1, r));
  }
  return dp[l][r] = ans;
}

int main() {
  long long n;
  cin >> n;
  vector<long long> vec(n, 0);
  for (long long i = 0; i < n; i++)
    cin >> vec[i];
  vector<vector<long long>> dp(n, vector<long long>(n, -1));

  cout << helper(vec, dp, 0, n - 1);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
long long helper(vector<long long> &vec, vector<vector<long long>> &dp,
                 long long l, long long r) {
  if (l == r)
    return 0;
  if (l + 1 == r)
    return dp[l][r] = vec[l] + vec[r];
  if (dp[l][r] != -1)
    return dp[l][r];
  long long ans = LLONG_MAX, sum = 0;
  for (long long i = l; i <= r; ++i) {
    sum += vec[i];
  }
  for (long long i = l; i < r; ++i) {
    ans = min(ans, sum + helper(vec, dp, l, i) + helper(vec, dp, i + 1, r));
  }
  return dp[l][r] = ans;
}

int main() {
  long long n;
  cin >> n;
  vector<long long> vec(n, 0);
  for (long long i = 0; i < n; i++)
    cin >> vec[i];
  vector<vector<long long>> dp(n, vector<long long>(n, -1));

  cout << helper(vec, dp, 0, n - 1);
}