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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define m_p make_pair
#define rep(i, a, b) for (int i = a; i < b; i++)
#define sz(x) (int)x.size()
#define f first
#define debug cout << "##########3" << endl
#define mod 1000000007
#define ll long long int
#define lMax LLONG_MAX
// queue<int> , prefix_sum(a,a+n) , reverse(a,a+n) , priority_queue <int> max
// heap , priority_queue <int, vector<int>, greater<int> > min heap;
// == precedence gretaer then &
ll n, m;
ll arr[105];
// Prefix sum calculation wala dp !!!!
ll dp[105][100006];
ll prefix[105][100006];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll i, j, k, c = 0, ans = 0, t, z, x = 0, y;
  cin >> n >> k;
  rep(i, 0, n) cin >> arr[i];
  dp[0][0] = 1;
  for (j = 0; j <= k; j++) {
    prefix[0][j] = 1;
  }

  for (i = 1; i <= n; i++) {
    for (j = 0; j <= k; j++) {
      if (j == 0) {
        dp[i][j] = dp[i - 1][j];
        dp[i][j] %= mod;
        prefix[i][j] = 1;
        prefix[i][j] %= mod;
      } else {
        ll l = max(0LL, arr[i - 1] + 1);
        dp[i][j] = prefix[i - 1][j] - prefix[i - 1][j - l] + mod;
        dp[i][j] %= mod;
        prefix[i][j] = prefix[i][j - 1] + dp[i][j];
        prefix[i][j] %= mod;
      }
      // cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
    }
  }

  cout << dp[n][k] % mod;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define m_p make_pair
#define rep(i, a, b) for (int i = a; i < b; i++)
#define sz(x) (int)x.size()
#define f first
#define debug cout << "##########3" << endl
#define mod 1000000007
#define ll long long int
#define lMax LLONG_MAX
// queue<int> , prefix_sum(a,a+n) , reverse(a,a+n) , priority_queue <int> max
// heap , priority_queue <int, vector<int>, greater<int> > min heap;
// == precedence gretaer then &
ll n, m;
ll arr[105];
// Prefix sum calculation wala dp !!!!
ll dp[105][100006];
ll prefix[105][100006];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll i, j, k, c = 0, ans = 0, t, z, x = 0, y;
  cin >> n >> k;
  rep(i, 0, n) cin >> arr[i];
  dp[0][0] = 1;
  for (j = 0; j <= k; j++) {
    prefix[0][j] = 1;
  }

  for (i = 1; i <= n; i++) {
    for (j = 0; j <= k; j++) {
      if (j - arr[i - 1] - 1 >= 0)
        dp[i][j] =
            (prefix[i - 1][j] - prefix[i - 1][j - arr[i - 1] - 1] + mod) % mod;
      else
        dp[i][j] = prefix[i - 1][j];
      prefix[i][j] = (prefix[i][j - 1] % mod + dp[i][j] % mod) % mod;
    }
  }

  cout << dp[n][k] % mod;

  return 0;
}
Problem p03172 (C++)

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

#define MOD 1000000007

typedef long long ll;

void normal(ll &a) {
  a %= MOD;
  (a < 0) && (a += MOD);
}
ll modMul(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a * b) % MOD;
}
ll modAdd(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a + b) % MOD;
}
ll modSub(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
ll modInverse(ll a) { return modPow(a, MOD - 2); }
ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

int main() {

#ifdef tajir
  freopen("input.txt", "r", stdin);
#else
  // online submission
#endif

  int n, k;

  cin >> n >> k;

  vector<int> dp(k + 1, 0);

  dp[0] = 1;

  for (int child = 0; child < n; ++child) {
    int up_to;
    cin >> up_to;

    vector<int> dp2(k + 1, 0);

    for (int used = k; used >= 0; --used) {
      int L = used + 1, R = used + min(up_to, k - used);
      if (L > R)
        continue;
      dp2[L] = modAdd(dp2[L], dp[used]);
      dp2[R + 1] = modSub(dp2[R + 1], dp[used]);
    }

    for (int i = 0; i <= k; ++i) {
      dp2[i] = modAdd(dp2[i], dp2[i - 1]);

      dp[i] = modAdd(dp[i], dp2[i]);
    }
  }

  cout << dp[k] << endl;

  return 0;
}

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

#define MOD 1000000007

typedef long long ll;

void normal(ll &a) {
  a %= MOD;
  (a < 0) && (a += MOD);
}
ll modMul(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a * b) % MOD;
}
ll modAdd(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a + b) % MOD;
}
ll modSub(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
ll modInverse(ll a) { return modPow(a, MOD - 2); }
ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

int main() {

#ifdef tajir
  freopen("input.txt", "r", stdin);
#else
  // online submission
#endif

  int n, k;

  cin >> n >> k;

  vector<int> dp(k + 1, 0);

  dp[0] = 1;

  for (int child = 0; child < n; ++child) {
    int up_to;
    cin >> up_to;

    vector<int> dp2(k + 1, 0);

    for (int used = k; used >= 0; --used) {
      int L = used + 1, R = used + min(up_to, k - used);
      if (L > R)
        continue;
      dp2[L] = modAdd(dp2[L], dp[used]);
      if (R + 1 <= k)
        dp2[R + 1] = modSub(dp2[R + 1], dp[used]);
    }

    for (int i = 0; i <= k; ++i) {
      dp2[i] = modAdd(dp2[i], dp2[i - 1]);

      dp[i] = modAdd(dp[i], dp2[i]);
    }
  }

  cout << dp[k] << endl;

  return 0;
}
Problem p03173 (C++)

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

const int MOD = 1000000007;
const long long INF = 1LL << 60;

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// 入力
int N;
long long a[410];
long long b[410];
long long dp[410][410];
bool ischecked[410][410] = {false};

void ruiseki() {
  b[0] = a[0];
  for (int i = 1; i < N; ++i)
    b[i] = b[i - 1] + a[i];
}

long long sum(int L, int R) { return b[R] - b[L - 1]; }

long long rec(int L, int R) {
  if (L == R)
    return 0;
  if (ischecked[L][R])
    return dp[L][R];
  long long ret = INF;
  for (int i = L; i < R; ++i)
    chmin(ret, rec(L, i) + rec(i + 1, R) + sum(L, R));
  return dp[L][R] = ret;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  ruiseki();

  cout << rec(0, N - 1) << endl;
  return 0;
}

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

const int MOD = 1000000007;
const long long INF = 1LL << 60;

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// 入力
int N;
long long a[410];
long long b[410];
long long dp[410][410];
bool ischecked[410][410] = {false};

void ruiseki() {
  b[0] = a[0];
  for (int i = 1; i < N; ++i)
    b[i] = b[i - 1] + a[i];
}

long long sum(int L, int R) { return b[R] - b[L - 1]; }

long long rec(int L, int R) {
  if (L == R)
    return 0;
  if (ischecked[L][R])
    return dp[L][R];
  ischecked[L][R] = true;
  long long ret = INF;
  for (int i = L; i < R; ++i)
    chmin(ret, rec(L, i) + rec(i + 1, R) + sum(L, R));
  return dp[L][R] = ret;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  ruiseki();

  cout << rec(0, N - 1) << endl;
  return 0;
}
Problem p03173 (C++)

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

const int MAXN = 1e2 + 5;
const int MAXNN = 1e5 + 5;
const long long INF = 1e18 + 7;
#define ll long long
#define pb push_back
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define F first
#define S second

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

ll MOD(ll a) { return ((a % INF) + INF) % INF; }

ll inv(ll a) { return poww(a, INF - 2, INF); }

ll a[MAXN], dp[MAXN][MAXN], sums[MAXN];

int main() {
  fast_io;
  // cout << fixed << setprecision(15);

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

  for (int i = 1; i <= n; ++i)
    sums[i] = sums[i - 1] + a[i];

  for (int len = 1; len <= n; ++len) {
    for (int l = 1; l <= n - len + 1; ++l) {

      int r = l + len;
      dp[l][r] = INF;
      for (int f = l; f <= r - 1; ++f) {
        dp[l][r] =
            min(dp[l][r], sums[r] - sums[l - 1] + dp[l][f] + dp[f + 1][r]);
      }
    }
  }

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


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

const int MAXN = 4e2 + 5;
const long long INF = 1e18 + 7;
#define ll long long
#define pb push_back
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define F first
#define S second

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

ll MOD(ll a) { return ((a % INF) + INF) % INF; }

ll inv(ll a) { return poww(a, INF - 2, INF); }

ll a[MAXN], dp[MAXN][MAXN], sums[MAXN];

int main() {
  fast_io;
  // cout << fixed << setprecision(15);

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

  for (int i = 1; i <= n; ++i)
    sums[i] = sums[i - 1] + a[i];

  for (int len = 1; len <= n; ++len) {
    for (int l = 1; l <= n - len + 1; ++l) {

      int r = l + len;
      dp[l][r] = INF;
      for (int f = l; f <= r - 1; ++f) {
        dp[l][r] =
            min(dp[l][r], sums[r] - sums[l - 1] + dp[l][f] + dp[f + 1][r]);
      }
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout << s, 0
#define rcc(s) cout << s, exit(0)
#define er erase
#define in insert
#define pi pair<int, int>
#define sz(x) (int)((x).size())
#define int long long

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

const ll inf = 0x3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int N = 500 + 11;

int dp[N][N][2], a[N], n, x;
// vector<int>v[N];

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);

  cin >> n;

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

  for (int j = 1; j <= n; j++) {
    for (int i = 1; i <= n; i++) {
      if (j == 1) {
        dp[i][i][0] = a[i];
        continue;
      }
      if (j == 2) {
        dp[i][i + 1][0] = a[i] + a[i + 1];
        dp[i][i + 1][1] = a[i] + a[i + 1];
        continue;
      }
      int l = i, r = i + j - 1;
      dp[l][r][1] = 3e18;
      for (int k = i; k < r; k++) {
        int val = dp[i][k][0] + dp[k + 1][r][0];
        int price = dp[i][k][1] + dp[k + 1][r][1] + val;
        if (dp[l][r][1] > price) {
          dp[l][r][1] = price;
          dp[l][r][0] = val;
        }
      }
    }
  }

  rc(dp[1][n][1]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout << s, 0
#define rcc(s) cout << s, exit(0)
#define er erase
#define in insert
#define pi pair<int, int>
#define sz(x) (int)((x).size())
#define int long long

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

const ll inf = 0x3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int N = 500 + 11;

int dp[N][N][2], a[N], n, x;
// vector<int>v[N];

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);

  cin >> n;

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

  for (int j = 1; j <= n; j++) {
    for (int i = 1; i <= n; i++) {
      if (j == 1) {
        dp[i][i][0] = a[i];
        continue;
      }
      if (j == 2) {
        dp[i][i + 1][0] = a[i] + a[i + 1];
        dp[i][i + 1][1] = a[i] + a[i + 1];
        continue;
      }
      int l = i, r = i + j - 1;
      dp[l][r][1] = 3e18;
      if (r > n)
        continue;
      for (int k = i; k < r; k++) {
        int val = dp[i][k][0] + dp[k + 1][r][0];
        int price = dp[i][k][1] + dp[k + 1][r][1] + val;
        if (dp[l][r][1] > price) {
          dp[l][r][1] = price;
          dp[l][r][0] = val;
        }
      }
    }
  }

  rc(dp[1][n][1]);
  return 0;
}
Problem p03173 (C++)

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

int n;
ll DP[303][303];
ll pf[303];
ll A[303];

ll solve(int l, int r) {
  if (l >= r)
    return 0;
  if (l + 1 == r)
    return A[l] + A[r];
  if (DP[l][r] != 0)
    return DP[l][r];

  DP[l][r] = LLONG_MAX;

  for (int k = l; k <= r; k++)
    DP[l][r] = min(DP[l][r], pf[k] - pf[l - 1] + pf[r] - pf[k] + solve(l, k) +
                                 solve(k + 1, r));

  return DP[l][r];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> A[i], pf[i] = pf[i - 1] + A[i];

  cout << solve(1, n);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define oo 666666666
#define ll long long
#define ld long double

int n;
ll DP[404][404];
ll pf[404];
ll A[404];

ll solve(int l, int r) {
  if (l >= r)
    return 0;
  if (l + 1 == r)
    return A[l] + A[r];
  if (DP[l][r] != 0)
    return DP[l][r];

  DP[l][r] = LLONG_MAX;

  for (int k = l; k <= r; k++)
    DP[l][r] = min(DP[l][r], pf[k] - pf[l - 1] + pf[r] - pf[k] + solve(l, k) +
                                 solve(k + 1, r));

  return DP[l][r];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> A[i], pf[i] = pf[i - 1] + A[i];

  cout << solve(1, n);
}
Problem p03173 (C++)

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

const long long oo = 1e16;
long long a[500];
long long s[500];
long long f[500][500];
bool c[500][500];
int n;

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

long long tryyy(int l, int r) {
  if (l == r)
    return 0;
  if (c[l][r])
    return f[l][r];
  long long tmp = oo;
  for (int m = l; m < r; m++)
    tmp = min(tmp, tryyy(l, m) + tryyy(m + 1, r));
  tmp += s[r] - s[l - 1];
  f[l][r] = tmp;
  return tmp;
}

int main() {
  inp();
  cout << tryyy(1, n);
  return 0;
}


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

const long long oo = 1e16;
long long a[500];
long long s[500];
long long f[500][500];
bool c[500][500];
int n;

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

long long tryyy(int l, int r) {
  if (l == r)
    return 0;
  if (c[l][r])
    return f[l][r];
  long long tmp = oo;
  c[l][r] = true;
  for (int m = l; m < r; m++)
    tmp = min(tmp, tryyy(l, m) + tryyy(m + 1, r));
  tmp += s[r] - s[l - 1];
  f[l][r] = tmp;
  return tmp;
}

int main() {
  inp();
  cout << tryyy(1, n);
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// const long double PIL = 3.141592653589793238462643383279502884L;
// const double PI = 3.14159265358979323846;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vii;
#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()

const ll INF = 1e18;
const int N = 401;
int a[N];
ll ps[N], dp[N][N];

int main() {
  //~ ios::sync_with_stdio(0);
  //~ cin.tie(0);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    ps[i] = ps[i - 1] + a[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[i][i] = 0;

  for (int x = 1; x < n; x++) {
    for (int i = 1; i <= n; i++) {
      for (int j = i; j < i + x; j++) {
        ll tmp = dp[i][j] + dp[j + 1][i + x] + ps[i + x] - ps[i - 1];
        dp[i][i + x] = min(dp[i][i + x], tmp);
      }
    }
  }
  printf("%lld\n", dp[1][n]);
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// const long double PIL = 3.141592653589793238462643383279502884L;
// const double PI = 3.14159265358979323846;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vii;
#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()

const ll INF = 1e18;
const int N = 401;
int a[N];
ll ps[N], dp[N][N];

int main() {
  //~ ios::sync_with_stdio(0);
  //~ cin.tie(0);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    ps[i] = ps[i - 1] + a[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[i][i] = 0;

  for (int x = 1; x < n; x++) {
    for (int i = 1; i <= n - x; i++) {
      for (int j = i; j < i + x; j++) {
        ll tmp = dp[i][j] + dp[j + 1][i + x] + ps[i + x] - ps[i - 1];
        dp[i][i + x] = min(dp[i][i + x], tmp);
      }
    }
  }
  printf("%lld\n", dp[1][n]);
}
Problem p03173 (C++)

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

long long b[401][401];
long long suma[401];
int a[401];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  suma[0] = a[0];
  for (int i = 1; i < n; i++) {
    suma[i] = suma[i - 1] + a[i];
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      if (i == 0) {
        b[j][j] = 0;
        continue;
      }
      long long min1 = 9223372036854775807LL;
      for (int k = 0; k < i; k++) {
        long long x = b[j][j + k] + b[j + k + 1][j + i] + suma[j + i] -
                      (j == 0 ? 0 : suma[j - 1]);
        if (x < min1)
          min1 = x;
      }
      b[j][j + i] = min1;
    }
  cout << b[0][n - 1];
  return 0;
}


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

long long b[401][401];
long long suma[401];
int a[401];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  suma[0] = a[0];
  for (int i = 1; i < n; i++) {
    suma[i] = suma[i - 1] + a[i];
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j + i < n; j++) {
      if (i == 0) {
        b[j][j] = 0;
        continue;
      }
      long long min1 = 9223372036854775807LL;
      for (int k = 0; k < i; k++) {
        long long x = b[j][j + k] + b[j + k + 1][j + i] + suma[j + i] -
                      (j == 0 ? 0 : suma[j - 1]);
        if (x < min1)
          min1 = x;
      }
      b[j][j + i] = min1;
    }
  cout << b[0][n - 1];
  return 0;
}
Problem p03173 (C++)

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

using namespace std;

long long dp[405][405];
long long pre[405];

int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> pre[i];
    pre[i] += pre[i - 1];
  }
  for (int len = 2; len <= N; len++) {
    for (int l = 0; l <= N; l++) {
      int r = l + len;
      dp[l][r] = LLONG_MAX / 2 - 5;
      for (int k = l + 1; k < r; k++) {
        dp[l][r] = min(dp[l][k] + dp[k][r], dp[l][r]);
      }
      dp[l][r] += pre[r] - pre[l];
    }
  }
  cout << dp[0][N] << endl;
}


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

using namespace std;

long long dp[405][405];
long long pre[405];

int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> pre[i];
    pre[i] += pre[i - 1];
  }
  for (int len = 2; len <= N; len++) {
    for (int l = 0; l + len <= N; l++) {
      int r = l + len;
      dp[l][r] = LLONG_MAX / 2 - 5;
      for (int k = l + 1; k < r; k++) {
        dp[l][r] = min(dp[l][k] + dp[k][r], dp[l][r]);
      }
      dp[l][r] += pre[r] - pre[l];
    }
  }
  cout << dp[0][N] << endl;
}
Problem p03173 (C++)

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

using namespace std;

#define FOR(i, a, b) for (int i = (a), _b = (b); i < (_b); ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i > (_b); --i)
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define endl '\n'

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned char byte;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;

const int MX = 405;

ll dp[MX][MX];

int main(int argc, char *argv[]) {
#ifdef HTRINH_UNIT_TEST
  freopen(argv[1], "r", stdin);
  ifstream cin(argv[1]);
#endif
#if 0
	ofstream cout(argv[2]);
#endif
  ios ::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  ll a[n];
  FOR(i, 0, n) cin >> a[i];
  ll sum[n];
  sum[0] = a[0];
  FOR(i, 1, n) sum[i] = sum[i - 1] + a[i];
  FOR(d, 2, n + 1) FOR(l, 0, n) {
    int r = l + d - 1;
    ll tot = 0;
    if (l - 1 >= 0)
      tot = sum[r] - sum[l - 1];
    else
      tot = sum[r];
    ll best = 1LL << 60;
    FOR(k, l, r) best = min(best, dp[l][k] + dp[k + 1][r]);
    dp[l][r] = best + tot;
  }
  cout << dp[0][n - 1] << endl;
  return 0;
}


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

using namespace std;

#define FOR(i, a, b) for (int i = (a), _b = (b); i < (_b); ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i > (_b); --i)
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define endl '\n'

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned char byte;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;

const int MX = 405;

ll dp[MX][MX];

int main(int argc, char *argv[]) {
#ifdef HTRINH_UNIT_TEST
  freopen(argv[1], "r", stdin);
  ifstream cin(argv[1]);
#endif
#if 0
	ofstream cout(argv[2]);
#endif
  ios ::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  ll a[n];
  FOR(i, 0, n) cin >> a[i];
  ll sum[n];
  sum[0] = a[0];
  FOR(i, 1, n) sum[i] = sum[i - 1] + a[i];
  FOR(d, 2, n + 1) FOR(l, 0, n) {
    int r = l + d - 1;
    if (r >= n)
      break;
    ll tot = 0;
    if (l - 1 >= 0)
      tot = sum[r] - sum[l - 1];
    else
      tot = sum[r];
    ll best = 1LL << 60;
    FOR(k, l, r) best = min(best, dp[l][k] + dp[k + 1][r]);
    dp[l][r] = best + tot;
  }
  cout << dp[0][n - 1] << endl;
  return 0;
}
Problem p03173 (C++)

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

ll sum[MAX] = {};
vector<vector<ll>> dp(MAX, vector<ll>(MAX, -1));

ll dfs(int i, int j) {
  if (dp[i][j] != -1)
    return dp[i][j];
  if (i == j)
    return dp[i][j] = 0;
  ll d = INF;
  for (int k = i; k < j; k++) {
    d = min(d, dfs(i, k) + dfs(k + 1, j) + sum[j] - sum[i - 1]);
  }
  return d;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    ll a;
    cin >> a;
    sum[i + 1] += sum[i] + a;
  }
  cout << dfs(1, n) << endl;
  return 0;
}

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

ll sum[MAX] = {};
vector<vector<ll>> dp(MAX, vector<ll>(MAX, -1));

ll dfs(int i, int j) {
  if (dp[i][j] != -1)
    return dp[i][j];
  if (i == j)
    return dp[i][j] = 0;
  ll d = INF;
  for (int k = i; k < j; k++) {
    d = min(d, dfs(i, k) + dfs(k + 1, j) + sum[j] - sum[i - 1]);
  }
  return dp[i][j] = d;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    ll a;
    cin >> a;
    sum[i + 1] += sum[i] + a;
  }
  cout << dfs(1, n) << endl;
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
typedef long long ll;
using namespace std;

int main(void) {
  ll i, j, k, n;
  scanf("%lld", &n);
  ll a[n], dp[n][n], sum[n];
  for (i = 0; i < n; ++i) {
    scanf("%lld", &a[i]);
    if (i)
      sum[i] = sum[i - 1] + a[i];
    else
      sum[i] = a[i];
  }
  for (i = 0; i < n; ++i)
    for (j = 0; j < n; ++j)
      dp[i][j] = 1e18;
  for (i = 0; i < n; ++i)
    dp[i][0] = 0;
  for (i = 1; i < n; ++i)
    for (j = 0; j < n; ++j) {
      for (k = 0; k < i; ++k)
        if (dp[j][i] > dp[j][k] + dp[j + k + 1][i - k - 1] + sum[j + i] -
                           (j ? sum[j - 1] : 0)) {
          dp[j][i] = dp[j][k] + dp[j + k + 1][i - k - 1] + sum[j + i] -
                     (j ? sum[j - 1] : 0);
        }
    }
  printf("%lld", dp[0][n - 1]);
  return 0;
}

Fixed submission:
#include <stdio.h>
typedef long long ll;
using namespace std;

int main(void) {
  ll i, j, k, n;
  scanf("%lld", &n);
  ll a[n], dp[n][n], sum[n];
  for (i = 0; i < n; ++i) {
    scanf("%lld", &a[i]);
    if (i)
      sum[i] = sum[i - 1] + a[i];
    else
      sum[i] = a[i];
  }
  for (i = 0; i < n; ++i)
    for (j = 0; j < n; ++j)
      dp[i][j] = 1e18;
  for (i = 0; i < n; ++i)
    dp[i][0] = 0;
  for (i = 1; i < n; ++i)
    for (j = 0; j < n - i; ++j) {
      for (k = 0; k < i; ++k)
        if (dp[j][i] > dp[j][k] + dp[j + k + 1][i - k - 1] + sum[j + i] -
                           (j ? sum[j - 1] : 0)) {
          dp[j][i] = dp[j][k] + dp[j + k + 1][i - k - 1] + sum[j + i] -
                     (j ? sum[j - 1] : 0);
        }
    }
  printf("%lld", dp[0][n - 1]);
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
long long memo[405][405];
long long pre[405];
long long dp(int i, int j) {
  if (i == j)
    return 0;
  else if (memo[i][j] != -1)
    return memo[i][j];
  long long ans = 1000000000000000000;
  for (int x = i; x < j; x++) {
    ans = min(ans, dp(i, x) + dp(x + 1, j) + pre[j] - pre[i - 1]);
  }
  return ans;
}
int main() {
  // freopen("input.txt","r",stdin);
  memset(memo, -1, sizeof(memo));
  scanf("%d", &n);
  for (int x = 1; x <= n; x++) {
    scanf("%lld", &pre[x]);
    pre[x] += pre[x - 1];
  }
  printf("%lld\n", dp(1, n));
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
long long memo[405][405];
long long pre[405];
long long dp(int i, int j) {
  if (i == j)
    return 0;
  else if (memo[i][j] != -1)
    return memo[i][j];
  long long ans = 1000000000000000000;
  for (int x = i; x < j; x++) {
    ans = min(ans, dp(i, x) + dp(x + 1, j) + pre[j] - pre[i - 1]);
  }
  return memo[i][j] = ans;
}
int main() {
  // freopen("input.txt","r",stdin);
  memset(memo, -1, sizeof(memo));
  scanf("%d", &n);
  for (int x = 1; x <= n; x++) {
    scanf("%lld", &pre[x]);
    pre[x] += pre[x - 1];
  }
  printf("%lld\n", dp(1, n));
}
Problem p03173 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 400

ll dp[MAXN + 1][MAXN + 1];
bool used[MAXN + 1][MAXN + 1];
vector<ll> a(MAXN + 1), suma(MAXN + 2);

ll f(int l, int r) {
  if (used[l][r])
    return dp[l][r];
  used[l][r] = true;

  if (l == r)
    return 0;

  ll sum = suma[r] - suma[l - 1];
  ll fans = INF;
  for (int m = l; m <= r; m++) {
    fans = min<ll>(fans, f(l, m) + f(m + 1, r));
  }
  // cout<<l<<' '<<r<<' '<<sum<<' '<<fans<<endl;
  return dp[l][r] = fans + sum;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  ll sum = 0;
  rep(i, n) sum += a[i];
  rep(i, n) suma[i + 1] += suma[i] + a[i];

  cout << f(1, n) << endl;

  return 0;

  rep(i, n + 1) {
    rep(j, n + 1) cout << dp[i][j] << ' ';
    cout << endl;
  }

  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 400

ll dp[MAXN + 2][MAXN + 2];
bool used[MAXN + 2][MAXN + 2];
vector<ll> a(MAXN + 1), suma(MAXN + 2);

ll f(int l, int r) {
  if (used[l][r])
    return dp[l][r];
  used[l][r] = true;

  if (l == r)
    return 0;

  ll sum = suma[r] - suma[l - 1];
  ll fans = INF;
  for (int m = l; m <= r; m++) {
    fans = min<ll>(fans, f(l, m) + f(m + 1, r));
  }
  // cout<<l<<' '<<r<<' '<<sum<<' '<<fans<<endl;
  return dp[l][r] = fans + sum;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  ll sum = 0;
  rep(i, n) sum += a[i];
  rep(i, n) suma[i + 1] += suma[i] + a[i];

  cout << f(1, n) << endl;

  return 0;

  rep(i, n + 1) {
    rep(j, n + 1) cout << dp[i][j] << ' ';
    cout << 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++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 400

ll dp[MAXN + 1][MAXN + 1];
bool used[MAXN + 1][MAXN + 1];
vector<ll> a(MAXN + 1), suma(MAXN + 1);

ll f(int l, int r) {
  if (used[l][r])
    return dp[l][r];
  used[l][r] = true;

  if (l == r)
    return 0;

  ll sum = suma[r] - suma[l - 1];
  ll fans = INF;
  for (int m = l; m <= r; m++) {
    fans = min<ll>(fans, f(l, m) + f(m + 1, r));
  }
  // cout<<l<<' '<<r<<' '<<sum<<' '<<fans<<endl;
  return dp[l][r] = fans + sum;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  ll sum = 0;
  rep(i, n) sum += a[i];
  rep(i, n) suma[i + 1] += suma[i] + a[i];

  cout << f(1, n) << endl;

  return 0;

  rep(i, n + 1) {
    rep(j, n + 1) cout << dp[i][j] << ' ';
    cout << endl;
  }

  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 400

ll dp[MAXN + 10][MAXN + 10];
bool used[MAXN + 10][MAXN + 10];
vector<ll> a(MAXN + 10), suma(MAXN + 10);

ll f(int l, int r) {
  if (used[l][r])
    return dp[l][r];
  used[l][r] = true;

  if (l == r)
    return 0;

  ll sum = suma[r] - suma[l - 1];
  ll fans = INF;
  for (int m = l; m <= r; m++) {
    fans = min<ll>(fans, f(l, m) + f(m + 1, r));
  }
  // cout<<l<<' '<<r<<' '<<sum<<' '<<fans<<endl;
  return dp[l][r] = fans + sum;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  ll sum = 0;
  rep(i, n) sum += a[i];
  rep(i, n) suma[i + 1] += suma[i] + a[i];

  cout << f(1, n) << endl;

  return 0;

  rep(i, n + 1) {
    rep(j, n + 1) cout << dp[i][j] << ' ';
    cout << endl;
  }

  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define el '\n'
#define sz(v) ((int)((v).size()))
#define all(v) ((v).begin()), ((v).end())
#define clr(v, d) memset(v, d, sizeof(v))

double const EPS = 1e-8, PI = acos(-1);
const int N = 405 + 9, M = 100 + 9, OO = (int)1e9 + 1;
const long long MOD = 1e9 + 7, INF = 1e18 + 9;

typedef long long ll;

void OUTPUT() {
  cout << fixed << setprecision(12);
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}

void INPUT() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif
}
int n, arr[N];
ll dp[N][N], cum[N];
int main() {
  INPUT();
  OUTPUT();
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    cum[i] = arr[i] + cum[i - 1] * (i != 0);
  }
  for (int L = n - 1; L >= 0; L--) {
    for (int R = L; R < n; R++) {
      if (L == R)
        dp[L][R] = 0;
      else {
        dp[L][R] = INF;
        ll s = cum[R];
        if (L != 0)
          s -= cum[L - 1];
        for (int i = L; i < R; i++) {
          dp[L][R] = min(dp[L][R], dp[L][i] + dp[i + 1][R] + s);
        }
      }
    }
  }
  cout << dp[0][n - 1];

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define el '\n'
#define sz(v) ((int)((v).size()))
#define all(v) ((v).begin()), ((v).end())
#define clr(v, d) memset(v, d, sizeof(v))

double const EPS = 1e-8, PI = acos(-1);
const int N = 405 + 9, M = 100 + 9, OO = (int)1e9 + 1;
const long long MOD = 1e9 + 7, INF = 1e18 + 9;

typedef long long ll;

void OUTPUT() {
  cout << fixed << setprecision(12);
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}

void INPUT() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif
}
int n, arr[N];
ll dp[N][N], cum[N];
int main() {
  //	INPUT();
  OUTPUT();
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    cum[i] = arr[i] + cum[i - 1] * (i != 0);
  }
  for (int L = n - 1; L >= 0; L--) {
    for (int R = L; R < n; R++) {
      if (L == R)
        dp[L][R] = 0;
      else {
        dp[L][R] = INF;
        ll s = cum[R];
        if (L != 0)
          s -= cum[L - 1];
        for (int i = L; i < R; i++) {
          dp[L][R] = min(dp[L][R], dp[L][i] + dp[i + 1][R] + s);
        }
      }
    }
  }
  cout << dp[0][n - 1];

  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 450
using namespace std;
typedef long long LL;
LL a[maxn];
LL f[maxn][maxn];
int main() {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  for (int i = 1; i <= N; i += 1)
    cin >> a[i];
  for (int L = 2; L <= N; L += 1)
    for (int i = 1; i <= N; i += 1) {
      f[i][L] = 1E18;
      for (int j = 1; j < L; j += 1)
        f[i][L] = min(f[i][L], f[i][j] + f[i + j][L - j]);
      for (int j = i; j < i + L; j += 1)
        f[i][L] += a[j];
    }
  cout << f[1][N];
}

Fixed submission:
#include <bits/stdc++.h>
#define maxn 450
using namespace std;
typedef long long LL;
LL a[maxn];
LL f[maxn][maxn];
int main() {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  for (int i = 1; i <= N; i += 1)
    cin >> a[i];
  for (int L = 2; L <= N; L += 1)
    for (int i = 1; i + L <= N + 1; i += 1) {
      f[i][L] = 1E18;
      for (int j = 1; j < L; j += 1)
        f[i][L] = min(f[i][L], f[i][j] + f[i + j][L - j]);
      for (int j = i; j < i + L; j += 1)
        f[i][L] += a[j];
    }
  cout << f[1][N];
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define nyan "(=^･ω･^=)"
#define mkp make_pair
#define lP pair<ll, ll>
#define iP pair<int, int>
typedef long long ll;
using namespace std;

int N, a[400];
ll DP[400][400], s[401];
int main() {
  cin >> N;
  for (int i = 0; i != N; ++i)
    cin >> a[i];
  for (int i = 0; i != N; ++i)
    s[i + 1] = s[i] + a[i];
  for (int i = 1; i != N; ++i) {
    for (int j = 0; i + j != N; ++j) {
      DP[j][i + j] = llmax;
      for (int k = j; k != i + j + 1; ++k) {
        DP[j][i + j] = min(DP[j][i + j],
                           DP[j][k] + DP[k + 1][i + j] + s[i + j + 1] - s[j]);
      }
    }
  }
  cout << DP[0][N - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define nyan "(=^･ω･^=)"
#define mkp make_pair
#define lP pair<ll, ll>
#define iP pair<int, int>
typedef long long ll;
using namespace std;

int N, a[400];
ll DP[400][400], s[401];
int main() {
  cin >> N;
  for (int i = 0; i != N; ++i)
    cin >> a[i];
  for (int i = 0; i != N; ++i)
    s[i + 1] = s[i] + a[i];
  for (int i = 1; i != N; ++i) {
    for (int j = 0; i + j != N; ++j) {
      DP[j][i + j] = llmax;
      for (int k = j; k != i + j; ++k) {
        DP[j][i + j] = min(DP[j][i + j],
                           DP[j][k] + DP[k + 1][i + j] + s[i + j + 1] - s[j]);
      }
    }
  }
  cout << DP[0][N - 1] << endl;
  return 0;
}
Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define FORR(i, m, n) for (ll i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> a(n), sm(n + 1);
  REP(i, n) {
    cin >> a[i];
    sm[i + 1] += sm[i] + a[i];
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1));
  REP(i, n + 1) { dp[i][i + 2] = sm[i + 2] - sm[i]; }
  FOR(i, 2, n + 1) {
    for (int j = 0; j + i <= n; j++) {
      ll tmp = INF;
      for (int k = j + 1; k < j + i; k++) {
        chmin(tmp, dp[j][k] + dp[k][j + i] + sm[j + i] - sm[j]);
      }
      dp[j][j + i] = tmp;
    }
  }
  cout << dp[0][n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define FORR(i, m, n) for (ll i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> a(n), sm(n + 1);
  REP(i, n) {
    cin >> a[i];
    sm[i + 1] += sm[i] + a[i];
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1));
  REP(i, n - 1) { dp[i][i + 2] = sm[i + 2] - sm[i]; }
  FOR(i, 2, n + 1) {
    for (int j = 0; j + i <= n; j++) {
      ll tmp = INF;
      for (int k = j + 1; k < j + i; k++) {
        chmin(tmp, dp[j][k] + dp[k][j + i] + sm[j + i] - sm[j]);
      }
      dp[j][j + i] = tmp;
    }
  }
  cout << dp[0][n] << endl;
}
Problem p03173 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long

int bs_upper_bound(int a[], int n, int x) {
  int l = 0;
  int h = n; // Not n - 1
  while (l < h) {
    int mid = l + (h - l) / 2;
    if (x >= a[mid]) {
      l = mid + 1;
    } else {
      h = mid;
    }
  }
  return l;
}

int bs_lower_bound(int a[], int n, int x) {
  int l = 0;
  int h = n; // Not n - 1
  while (l < h) {
    int mid = l + (h - l) / 2;
    if (x <= a[mid]) {
      h = mid;
    } else {
      l = mid + 1;
    }
  }
  return l;
}

bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

int main() {
  IOS;
  int n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll dp[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      dp[i][j] = LONG_MAX;
  }
  for (int i = 0; i < n; i++)
    dp[i][i] = 0;
  for (int k = 2; k <= n; k++) {
    for (int i = 0; i < n; i++) {
      ll s = a[i];
      for (int j = i + 1; j < i + k; j++) {
        s += a[j];
        for (int l = i; l < j; l++)
          dp[i][j] = min(dp[i][j], s + dp[i][l] + dp[l + 1][j]);
      }
    }
  }
  cout << dp[0][n - 1];
}


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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long

int bs_upper_bound(int a[], int n, int x) {
  int l = 0;
  int h = n; // Not n - 1
  while (l < h) {
    int mid = l + (h - l) / 2;
    if (x >= a[mid]) {
      l = mid + 1;
    } else {
      h = mid;
    }
  }
  return l;
}

int bs_lower_bound(int a[], int n, int x) {
  int l = 0;
  int h = n; // Not n - 1
  while (l < h) {
    int mid = l + (h - l) / 2;
    if (x <= a[mid]) {
      h = mid;
    } else {
      l = mid + 1;
    }
  }
  return l;
}

bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

int main() {
  IOS;
  int n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll dp[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      dp[i][j] = LONG_MAX;
  }
  for (int i = 0; i < n; i++)
    dp[i][i] = 0;
  for (int k = 2; k <= n; k++) {
    for (int i = 0; i < n - k + 1; i++) {
      ll s = a[i];
      for (int j = i + 1; j < i + k; j++) {
        s += a[j];
        for (int l = i; l < j; l++)
          dp[i][j] = min(dp[i][j], s + dp[i][l] + dp[l + 1][j]);
      }
    }
  }
  cout << dp[0][n - 1];
}
Problem p03173 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define st first
#define nd second
#define turbo                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define vi vector<int>
#define vvi vector<vi>
#define qi queue<int>
#define ld long double

using namespace std;

/*---------------------------------------------------------///CODE///---------------------------------------------------------*/

ll dp[402][402];
ll T[402];
ll pref[402];

ll count_dp(int a, int b) {
  if (dp[a][b] != 1e18)
    return dp[a][b];
  if (a == b)
    return 0;

  for (int i = a; i <= b; i++)
    for (int j = i; j <= b; j++)
      dp[a][b] = min(dp[a][b], count_dp(a, j) + count_dp(j + 1, b) + pref[b] -
                                   pref[j] + pref[j] - pref[a - 1]);

  return dp[a][b];
}

int main() {
  turbo

      int n;
  cin >> n;

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

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

  for (int j = 1; j <= n; j++)
    pref[j] = pref[j - 1] + T[j];

  return cout << count_dp(1, n), 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define st first
#define nd second
#define turbo                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define vi vector<int>
#define vvi vector<vi>
#define qi queue<int>
#define ld long double

using namespace std;

/*---------------------------------------------------------///CODE///---------------------------------------------------------*/

ll dp[402][402];
ll T[402];
ll pref[402];

ll count_dp(int a, int b) {
  if (dp[a][b] != 1e18)
    return dp[a][b];
  if (a == b)
    return 0;

  for (int i = a; i <= b; i++)
    dp[a][b] = min(dp[a][b], count_dp(a, i) + count_dp(i + 1, b) + pref[b] -
                                 pref[i] + pref[i] - pref[a - 1]);

  return dp[a][b];
}

int main() {
  turbo

      int n;
  cin >> n;

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

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

  for (int j = 1; j <= n; j++)
    pref[j] = pref[j - 1] + T[j];

  return cout << count_dp(1, n), 0;
}
Problem p03173 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; i++)
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

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

ll rec(ll l, ll r) {
  if (l == r)
    return 0;
  if (dp[l][r] != -1)
    return dp[l][r];
  ll res = 1LL << 60;
  for (ll i = l + 1; i <= r; i++) {
    chmin(res, rec(l, i - 1) + rec(i, r) + s[r] - s[l - 1]);
  }
  return res;
}

int main() {
  cin >> n;
  rep(i, n) {
    ll tmp;
    cin >> tmp;
    s[i + 1] = s[i] + tmp;
  }
  memset(dp, -1, sizeof(dp));
  cout << rec(1, n) << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; i++)
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

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

ll rec(ll l, ll r) {
  if (l == r)
    return 0;
  if (dp[l][r] != -1)
    return dp[l][r];
  ll res = 1LL << 60;
  for (ll i = l + 1; i <= r; i++) {
    chmin(res, rec(l, i - 1) + rec(i, r) + s[r] - s[l - 1]);
  }
  return dp[l][r] = res;
}

int main() {
  cin >> n;
  rep(i, n) {
    ll tmp;
    cin >> tmp;
    s[i + 1] = s[i] + tmp;
  }
  memset(dp, -1, sizeof(dp));
  cout << rec(1, n) << endl;
}