alexjercan/bugnet huggingface dataset:
Problem p01526 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
#define rrep(i, x, n) for (int i = (x); i < (n); ++i)
#define rep(i, x) rrep(i, 0, (x))
#define fi first
#define se second
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define pb push_back
#define maxs(a, b) (a) = max(a, b)
#define mins(a, b) (a) = min(a, b)

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

int main() {
  int N;
  double R;
  scanf("%d%lf", &N, &R);
  set<pii> S;
  rep(i, N) {
    double x, y;
    scanf("%lf%lf", &x, &y);
    int ny = (int)floor(x / R);
    int nx = (int)floor(y / R);
    S.insert(pii(ny, nx));
  }

  int ans = 0;

  each(it, S) {
    int y = it->fi, x = it->se;
    rep(i, 8) {
      int ty = y + dy[i];
      int tx = x + dx[i];
      if (S.find(pii(ty, tx)) != S.end()) {
        S.erase(pii(ty, tx));
      }
    }
    ans++;
  }
  printf("%d\\n", ans);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
#define rrep(i, x, n) for (int i = (x); i < (n); ++i)
#define rep(i, x) rrep(i, 0, (x))
#define fi first
#define se second
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define pb push_back
#define maxs(a, b) (a) = max(a, b)
#define mins(a, b) (a) = min(a, b)
const double EPS = 1e-6;
typedef complex<double> Point;
bool comp(const Point &p, const Point &q) {
  return real(p) != real(q) ? real(p) < real(q) : imag(p) < imag(q);
}

int main() {
  int N;
  double R;
  scanf("%d%lf", &N, &R);
  vector<Point> V;
  rep(i, N) {
    double x, y;
    scanf("%lf%lf", &x, &y);
    V.pb(Point(x, y));
  }
  sort(all(V), comp);

  int cnt = 0;
  bool used[200000] = {0};
  rep(i, N) {
    if (used[i])
      continue;

    rrep(j, i + 1, N) {
      if ((real(V[i]) - real(V[j])) * (real(V[i]) - real(V[j])) > R * R + EPS)
        break;
      if (abs(V[i] - V[j]) > R + EPS)
        continue;
      used[j] = true;
    }
    cnt++;
    used[i] = true;
  }

  printf("%d\\n", cnt);
}
Problem p01526 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;

int main() {
  int N;
  double R;
  set<Pi> sets;
  cin >> N >> R;
  for (int i = 0; i < N; i++) {
    double A, B;
    cin >> A >> B;
    sets.insert(make_pair((A + 1e6) / R, (B + 1e6) / R));
  }
  int ret = 0;
  while (!sets.empty()) {
    ++ret;
    set<Pi>::iterator it1 = sets.begin();
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (i == 0 && j == 0)
          continue;
        set<Pi>::iterator it2 =
            sets.find(make_pair(it1->first + i, it1->second + j));
        if (it2 != sets.end()) {
          sets.erase(it2);
        }
      }
    }
    sets.erase(it1);
  }
  cout << ret << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;

int main() {
  int N;
  double R;
  set<Pi> sets;
  cin >> N >> R;
  for (int i = 0; i < N; i++) {
    double A, B;
    cin >> A >> B;
    sets.insert(make_pair((A + 1e7) / R, (B + 1e7) / R));
  }
  int ret = 0;
  while (!sets.empty()) {
    ++ret;
    set<Pi>::iterator it1 = sets.begin();
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= -1; j++) {
        if (i == 0 && j == 0)
          continue;
        set<Pi>::iterator it2 =
            sets.find(make_pair(it1->first + i, it2->first + j));
        if (it2 != sets.end()) {
          sets.erase(it2);
        }
      }
    }
    sets.erase(it1);
  }
  cout << ret << endl;
}
Problem p01526 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#pragma warning(disable : 4996)
using namespace std;
int n;
double r, x, y, cx[200009], cy[200009];
pair<double, double> p[200009];
bool vis[200009];
int main() {
  scanf("%d%lf", &n, &r);
  for (int i = 0; i < n; i++) {
    scanf("%lf%lf", &x, &y);
    double ex = x * 0.28 - y * 0.96;
    double ey = x * 0.96 + y * 0.28;
    p[i] = make_pair(ex, ey);
  }
  sort(p, p + n);
  for (int i = 0; i < n; i++)
    cx[i] = p[i].first, cy[i] = p[i].second;
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (vis[i])
      continue;
    int ptl = lower_bound(cx, cx + n, cx[i] - r - 1.0e-4) - cx;
    int ptr = lower_bound(cx, cx + n, cx[i] + r + 1.0e-4) - cx;
    for (int j = ptl; j < ptr; j++) {
      double dx = cx[i] - cx[j];
      double dy = cy[i] - cy[j];
      if (dx * dx + dy * dy <= 2.0 * r * r)
        vis[j] = true;
    }
    ret++;
  }
  cout << ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <ctime>
#include <iostream>
#include <vector>
using namespace std;
int group[1000000];
vector<int> f[1000000];
void unite(int x, int y) {
  if (group[x] == group[y])
    return;
  x = group[x], y = group[y];
  if (f[y].size() > f[x].size())
    swap(x, y);
  for (int j = f[y].size() - 1; j >= 0; j--) {
    group[f[y][j]] = group[x];
    f[x].push_back(f[y][j]);
    f[y].pop_back();
  }
}
int n;
double r;
pair<double, double> p[200009];
int main() {
  cin >> n >> r;
  srand((unsigned)time(NULL));
  int vt = n / 10000 + 1;
  for (int i = 0; i < n; i++)
    group[i] = i, f[i] = {i};
  for (int i = 0; i < n; i++)
    cin >> p[i].first >> p[i].second;
  sort(p, p + n);
  for (int i = 1; i < n; i++) {
    for (int j = i - 1; j >= 0 && p[i].first - p[j].first <= r + 1e-10;
         j -= rand() % vt) {
      if (abs(p[i].second - p[j].second) <= r + 1e-10)
        unite(i, j);
    }
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (f[i].size())
      ret++;
  }
  cout << ret << endl;
  return 0;
}
Problem p01526 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

typedef pair<int, int> Point;

int diff_x[8] = {-1, 0, 1, -1, 1, -1, 0, 1},
    diff_y[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
set<Point> SET;

int main() {

  int N;
  double R;

  scanf("%d %lf", &N, &R);

  double tmp_x, tmp_y;
  for (int i = 0; i < N; i++) {
    scanf("%lf %lf", &tmp_x, &tmp_y);
    SET.insert(Point((int)(tmp_x / R), (int)(tmp_y / R)));
  }

  int ans = 0;

  while (SET.size() != 0) {
    set<Point>::iterator it = SET.begin();
    Point point = *it;
    SET.erase(it);
    ans++;
    for (int i = 0; i < 8; i++) {
      Point tmp = Point(point.first + diff_x[i], point.second + diff_y[i]);
      it = SET.find(tmp);
      if (it != SET.end())
        SET.erase(it);
    }
  }

  printf("%d\\n", ans);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Point {
  bool operator<(const struct Point &arg) const { return x < arg.x; }
  double x, y;
};

double calc_dist(Point a, Point b) {
  return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

Point point[200000];
int number[200000];

int main() {

  int N;
  double R;
  scanf("%d %lf", &N, &R);

  for (int i = 0; i < N; i++) {
    scanf("%lf %lf", &point[i].x, &point[i].y);
  }

  for (int i = 0; i < N; i++)
    number[i] = -1;

  sort(point, point + N);

  int index = 0;

  int left, right, m, calc_left, calc_right;

  for (int i = 0; i < N; i++) {
    if (number[i] == -1) {
      index++;
      number[i] = index;
    }

    left = 0, right = i - 1, m = (left + right) / 2;
    calc_left = i;

    while (left <= right) {
      if (point[i].x - point[m].x <= R) {
        calc_left = m;
        right = m - 1;
      } else {
        left = m + 1;
      }
      m = (left + right) / 2;
    }

    left = i + 1, right = N - 1;
    m = (left + right) / 2;
    calc_right = i;

    while (left <= right) {
      if (point[m].x - point[i].x <= R) {
        calc_right = m;
        left = m + 1;
      } else {
        right = m - 1;
      }
      m = (left + right) / 2;
    }

    for (int k = calc_left; k <= calc_right; k++) {
      if (calc_dist(point[i], point[k]) <= R) {
        number[k] = number[i];
      }
    }
  }

  printf("%d\\n", index);

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

#include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
using namespace std;

#define MAX 999999999
#define EPS (1e-8)

typedef pair<double, double> P;

int n, ans, pos[1000000];
double r;
P p[1000000];
bool rem[1000000];

void solve() {
  ans = 0;
  memset(rem, true, sizeof(rem));

  for (int i = 0; i < n; i++) {
    if (!rem[i])
      continue;

    int from = lower_bound(p, p + n, P(p[i].first - r - EPS, -MAX)) - p;
    int to = lower_bound(p, p + n, P(p[i].first + r + EPS, MAX)) - p;

    for (int j = from; j < to; j++) {
      int y_from = lower_bound(p + j, p + pos[j] + 1,
                               P(p[j].first, p[i].second - r - EPS)) -
                   p;
      int y_to = lower_bound(p + j, p + pos[j] + 1,
                             P(p[j].first, p[i].second + r + EPS)) -
                 p;

      for (int k = y_from; k < y_to; k++) {
        if (pow(p[i].first - p[k].first, 2) +
                pow(p[i].second - p[k].second, 2) <
            r * r + EPS) {
          rem[k] = false;
        }
      }

      j = pos[j];
    }

    ans++;
  }

  cout << ans << endl;
}

int main() {
  while (cin >> n >> r) {
    for (int i = 0; i < n; i++) {
      cin >> p[i].first >> p[i].second;
    }

    sort(p, p + n);
    pos[n - 1] = n - 1;

    for (int i = n - 2; i >= 0; i--) {
      if (p[i].first == p[pos[i + 1]].first) {
        pos[i] = pos[i + 1];
      } else {
        pos[i] = i;
      }
    }

    solve();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
using namespace std;

#define MAX 999999999
#define EPS (1e-8)

typedef pair<double, double> P;

int n, ans;
double r;
P p[1000000];
bool rem[1000000];

void solve() {
  ans = 0;
  memset(rem, true, sizeof(rem));

  for (int i = 0; i < n; i++) {
    if (!rem[i])
      continue;

    int from = lower_bound(p, p + n, P(p[i].first - r - EPS, -MAX)) - p;
    int to = lower_bound(p, p + n, P(p[i].first + r + EPS, MAX)) - p;

    for (int j = from; j < to; j++) {
      if (pow(p[i].first - p[j].first, 2) + pow(p[i].second - p[j].second, 2) <
          r * r + EPS) {
        rem[j] = false;
      }
    }

    ans++;
  }

  cout << ans << endl;
}

int main() {
  while (cin >> n >> r) {
    for (int i = 0; i < n; i++) {
      cin >> p[i].first >> p[i].second;
    }
    sort(p, p + n);
    solve();
  }
}
Problem p01529 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = numeric_limits<ll>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

ll K[4100][4100], x[4100][4100];
ll sum[4100];

// i~j??????
ll sum_w(int i, int j) { return sum[j] - sum[i - 1]; }

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

  int n;
  cin >> n;
  ll w[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> w[i];
  }
  sum[0] = 0;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + w[i];
  }
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < n + 1; j++) {
      K[i][j] = 0;
    }
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j) {
      x[i][j] = 99999999;
      x[i][i] = 0;
      K[i][i] = i;
    }
  }
  for (int k = 1; k < n; k++) {
    for (int i = 1; i + k <= n; i++) {
      int j = i + k;
      ll tmp = INF;
      for (ll r = K[i][j - 1]; r <= K[i + 1][j]; r++) {
        ll c = x[i][r] + x[r + 1][j] + sum_w(i, j);
        if (c <= tmp) {
          tmp = c;
          K[i][j] = r;
        }
      }
      x[i][j] = tmp;
    }
  }
  cout << x[1][n] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = numeric_limits<ll>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

ll sum[4100];

// i~j??????
ll sum_w(int i, int j) { return sum[j] - sum[i - 1]; }

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

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

  ll K[n + 1][n + 1], x[n + 1][n + 1];
  /*for(int i=0;i<n+1;i++){
    K[i][i]=0;x[i][i]=0;
  }*/
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < n + 1; j++) {
      K[i][j] = 0;
    }
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j) {
      x[i][j] = 99999999;
      x[i][i] = 0;
      K[i][i] = i;
    }
  }
  for (int k = 1; k < n; k++) {
    for (int i = 1; i + k <= n; i++) {
      int j = i + k;
      ll tmp = INF;
      for (ll r = K[i][j - 1]; r <= K[i + 1][j]; r++) {
        ll c = x[i][r] + x[r + 1][j] + sum_w(i, j);
        if (c <= tmp) {
          tmp = c;
          K[i][j] = r;
        }
      }
      x[i][j] = tmp;
    }
  }
  cout << x[1][n] << endl;
}
Problem p01529 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 * 2415.cc: Sashimi
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 4000;

typedef long long ll;

const ll LINF = 1LL << 60;

/* typedef */

/* global variables */

ll wis[MAX_N], wsums[MAX_N + 1], dp[MAX_N][MAX_N + 1];
int ks[MAX_N][MAX_N + 1];

/* subroutines */

/* main */

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

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n; j++)
      dp[i][j] = LINF, ks[i][j] = -1;
    dp[i][i + 1] = 0;
    ks[i][i + 1] = i;
  }

  for (int l = 2; l <= n; l++)
    for (int i = 0, j = l; j <= n; i++, j++) {
      ll mind = LINF;
      ll wsum = wsums[j] - wsums[i];
      int mink = -1;
      for (int k = ks[i][j - 1]; k <= ks[i + 1][j]; k++) {
        ll d = dp[i][k] + dp[k][j] + wsum;
        if (mind > d)
          mind = d, mink = k;
      }
      dp[i][j] = mind;
      ks[i][j] = mink;
    }

  printf("%lld\\n", dp[0][n]);
  return 0;
}

Error: TLE

Fixed submission:

/*
 * 2415.cc: Sashimi
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 4000;

typedef long long ll;

const ll LINF = 1LL << 60;

/* typedef */

/* global variables */

ll wis[MAX_N], wsums[MAX_N + 1], dp[MAX_N][MAX_N + 1];

/* subroutines */

/* main */

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

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

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

  for (int l = 2; l <= n; l++)
    for (int i = 0, j = l; j <= n; i++, j++) {
      ll mind = LINF;
      ll wsum = wsums[j] - wsums[i];
      for (int k = i + 1; k < j; k++) {
        ll d = dp[i][k] + dp[k][j] + wsum;
        if (mind > d)
          mind = d;
      }
      dp[i][j] = mind;
    }

  printf("%lld\\n", dp[0][n]);
  return 0;
}
Problem p01529 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
ll W[5000];
ll S[4004];
int K[4004][4004];
ll dp[4004][4004];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> W[i];
  for (int i = 0; i < N; i++) {
    S[i + 1] = S[i] + W[i];
    K[i][i] = i;
  }

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

  for (int w = 1; w < N; w++) {
    for (int i = 0, j; (j = i + w) < N; i++) {
      for (int k = K[i][j - 1]; k <= K[i + 1][j]; k++) {
        ll at = dp[i][k] + dp[k + 1][j] + S[j + 1] - S[i];
        if (dp[i][j] > at) {
          dp[i][j] = at;
          K[i][j] = k;
        }
      }
    }
  }
  cout << dp[0][N - 1] << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
ll W[5000];
ll S[4004][4004];
int K[4004][4004];
ll dp[4004][4004];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> W[i];
  for (int i = 0; i < N; i++) {
    S[i][i] = W[i];
    for (int j = i + 1; j < N; j++) {
      S[i][j] = S[i][j - 1] + W[j];
    }
    K[i][i] = i;
  }

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

  for (int w = 1; w < N; w++) {
    for (int i = 0, j; (j = i + w) < N; i++) {
      for (int k = K[i][j - 1]; k <= K[i + 1][j]; k++) {
        ll at = dp[i][k] + dp[k + 1][j] + S[i][j];
        if (dp[i][j] > at) {
          dp[i][j] = at;
          K[i][j] = k;
        }
      }
    }
  }
  cout << dp[0][N - 1] << endl;
}
Problem p01529 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

typedef long long lli;

const lli MAXN = 4004;
const lli INF = 1LL << 60;
lli N;
lli w[MAXN], dp[MAXN][MAXN], K[MAXN][MAXN];

int main() {
  while (cin >> N) {
    w[0] = 0;
    for (lli i = 0; i < N; ++i) {
      cin >> w[i + 1];
      w[i + 1] += w[i];
    }
    fill(dp[0], dp[MAXN], INF);
    for (lli i = 0; i < N; ++i) {
      dp[i][i + 1] = 0;
      K[i][i + 1] = i;
    }
    for (lli l = 2; l <= N; ++l) {
      for (lli i = 0, j; (j = i + l) <= N; ++i) {
        for (lli s = K[i][j - 1]; s <= K[i + 1][j]; ++s) {
          lli v = dp[i][s] + dp[s][j] + (w[j] - w[i]);
          if (dp[i][j] > v) {
            dp[i][j] = v;
            K[i][j] = s;
          }
        }
      }
    }
    cout << dp[0][N] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long lli;

const lli MAXN = 4004;
const lli INF = 1LL << 60;
lli N;
lli a[MAXN], w[MAXN][MAXN], dp[MAXN][MAXN], K[MAXN][MAXN];

int main() {
  while (cin >> N) {
    for (lli i = 0; i < N; ++i) {
      cin >> a[i];
    }
    for (lli i = 0; i < N; ++i) {
      w[i][i] = 0;
      for (lli j = i; j < N; ++j) {
        w[i][j + 1] = w[i][j] + a[j];
      }
    }
    fill(dp[0], dp[MAXN], INF);
    for (lli i = 0; i < N; ++i) {
      dp[i][i + 1] = 0;
      K[i][i + 1] = i;
    }
    for (lli l = 2; l <= N; ++l) {
      for (lli i = 0, j; (j = i + l) <= N; ++i) {
        for (lli s = K[i][j - 1]; s <= K[i + 1][j]; ++s) {
          lli v = dp[i][s] + dp[s][j] + w[i][j];
          if (dp[i][j] > v) {
            dp[i][j] = v;
            K[i][j] = s;
          }
        }
      }
    }
    cout << dp[0][N] << endl;
  }
  return 0;
}
Problem p01529 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define HUGE_NUM 9999999999999999

int N;
ll W[4001], dp[4000][4000];
int loc[4000][4000];

int main() {

  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    for (int k = 0; k < N; k++)
      dp[i][k] = HUGE_NUM;
  }

  W[0] = 0;
  for (int i = 1; i <= N; i++) {
    scanf("%lld", &W[i]);
    W[i] += W[i - 1];
  }

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

  int right;
  ll tmp;
  for (int length = 2; length <= N; length++) {
    for (int left = 0; left + length - 1 <= N - 1; left++) {
      right = left + length - 1;
      for (int mid = loc[left][right - 1]; mid <= loc[left + 1][right]; mid++) {
        tmp = dp[left][mid] + dp[mid + 1][right] + W[right + 1] - W[left];
        if (tmp < dp[left][right]) {
          dp[left][right] = tmp;
          loc[left][right] = mid;
        }
      }
    }
  }

  printf("%lld\\n", dp[0][N - 1]);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define HUGE_NUM 9999999999999999

int N;
ll W[4001], dp[4000][4000];

int main() {

  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    for (int k = 0; k < N; k++)
      dp[i][k] = HUGE_NUM;
  }

  W[0] = 0;
  for (int i = 1; i <= N; i++) {
    scanf("%lld", &W[i]);
    W[i] += W[i - 1];
  }

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

  int right;
  for (int length = 2; length <= N; length++) {
    for (int left = 0; left + length - 1 <= N - 1; left++) {
      right = left + length - 1;
      for (int mid = left; mid <= right - 1; mid++) {
        dp[left][right] =
            min(dp[left][right],
                dp[left][mid] + dp[mid + 1][right] + W[right + 1] - W[left]);
      }
    }
  }

  printf("%lld\\n", dp[0][N - 1]);
}