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

#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
char str[700][700];
pair<double, int> ev[500000];
double eps = 1e-9;
int main() {
  int a, b;
  scanf("%d%d", &a, &b);

  for (int i = 0; i < a; i++)
    scanf("%s", str[i]);
  for (int i = 0; i <= a; i++)
    str[i][b] = '.';
  for (int i = 0; i <= b; i++)
    str[a][i] = '.';
  a++;
  b++;
  int sz = 0;
  for (int i = 0; i < a - 1; i++) {
    for (int j = 0; j < b - 1; j++) {
      if (str[i][j] == str[i][j + 1] && str[i][j] == str[i + 1][j] &&
          str[i][j] != str[i + 1][j + 1]) {
        ev[sz++] = make_pair(atan2(a - i - 2, j + 1), -1);
      }
      if (str[i][j] != str[i][j + 1] && str[i][j] != str[i + 1][j] &&
          str[i][j] != str[i + 1][j + 1]) {
        ev[sz++] = make_pair(atan2(a - i - 2, j + 1), 1);
      }
    }
  }
  for (int i = 1; i < b; i++) {
    if (str[0][i] == '#' && str[0][i - 1] == '.') {
      ev[sz++] = make_pair(atan2(a - 1, i), -1);
    }
  }
  for (int i = 1; i < a; i++) {
    if (str[i][0] == '#' && str[i - 1][0] == '.') {
      ev[sz++] = make_pair(atan2(1, 0), -1);
    }
  }
  std::sort(ev, ev + sz);
  // for(int i=0;i<sz;i++)printf("%f %d\\n",ev[i].first,ev[i].second);
  int ret = 1;
  int now = 1;
  for (int i = 0; i < sz; i++) {
    now += ev[i].second;
    if (i < sz - 1 && ev[i + 1].first - ev[i].first < eps)
      continue;
    ret = max(ret, now);
  }
  printf("%d\\n", ret);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
const double EPS = 1e-10;
char str[700][700];
int array[700][700];
int gcd(int a, int b) {
  while (a) {
    b %= a;
    int c = b;
    b = a;
    a = c;
  }
  return b;
}
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  for (int i = 0; i < a; i++)
    scanf("%s", str[a - 1 - i]);
  for (int i = 0; i < a; i++)
    for (int j = 0; j < b; j++)
      array[i][j] = (str[i][j] == '#' ? 1 : 0);
  int ret = 0;

  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      if (gcd(i + 1, j + 1) > 1)
        continue;
      int val = 0;
      double X = i + 1;
      double Y = (1e-8) + j + 1;
      for (int k = 0; k < a; k++) {
        int d = (int)(Y * (k + 1) / X);
        if (d >= b)
          break;
        val += (array[k][d] ^ array[k + 1][d]);
      }
      for (int k = 0; k < b; k++) {
        int d = (int)(X * (k + 1) / Y);
        if (d >= a)
          break;
        val += (array[d][k] ^ array[d][k + 1]);
      }
      //	if(val==18)printf("%d %d\\n",i,j);
      ret = max(ret, val);
      val = 0;
      Y = -(1e-8) + j + 1;
      for (int k = 0; k < a; k++) {
        int d = (int)(Y * (k + 1) / X);
        if (d >= b)
          break;
        val += (array[k][d] ^ array[k + 1][d]);
      }
      for (int k = 0; k < b; k++) {
        int d = (int)(X * (k + 1) / Y);
        if (d >= a)
          break;
        val += (array[d][k] ^ array[d][k + 1]);
      }
      //	if(val==18)printf("%d %d\\n",i,j);
      ret = max(ret, val);
    }
  }
  if (str[0][0] == '#')
    ret++;
  printf("%d\\n", 1 + ret / 2);
}
Problem p01427 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
typedef double D;
int H, W;
string s[601];
bool dx[601][601], dy[601][601];
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int main() {
  cin >> H >> W;
  rep(i, H) cin >> s[H - 1 - i];
  s[H].assign(W, '.');
  rep(i, H + 1) s[i] += '.';
  rep(i, H) rep(j, W + 1) dx[i][j] = (s[i][j] == s[i + 1][j]);
  rep(i, H + 1) rep(j, W) dy[i][j] = (s[i][j] == s[i][j + 1]);
  int mx = 0;
  rep1(a, W) rep1(b, H) {
    if (gcd(a, b) != 1)
      continue;
    int cnt = 0;
    rep1(x, W) {
      int y = (b * x - 1) / a;
      if (y < H && !dy[y][x - 1])
        cnt++;
    }
    rep1(y, H) {
      int x = (a * y) / b;
      if (x < W && !dx[y - 1][x])
        cnt++;
    }
    mx = max(mx, cnt);
    cnt = 0;
    rep1(x, W) {
      int y = (b * x) / a;
      if (y < H && !dy[y][x - 1])
        cnt++;
    }
    rep1(y, H) {
      int x = (a * y - 1) / b;
      if (x < W && !dx[y - 1][x])
        cnt++;
    }
    mx = max(mx, cnt);
  }
  cout << (mx + 3) / 2 << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
typedef double D;
int H, W;
string s[601];
bool dx[601][601], dy[601][601];
int main() {
  cin >> H >> W;
  rep(i, H) cin >> s[H - 1 - i];
  s[H].assign(W, '.');
  rep(i, H + 1) s[i] += '.';
  rep(i, H) rep(j, W + 1) dx[i][j] = (s[i][j] == s[i + 1][j]);
  rep(i, H + 1) rep(j, W) dy[i][j] = (s[i][j] == s[i][j + 1]);
  int mx = 0;
  rep1(a, W) rep1(b, H) {
    int cnt = 0;
    rep1(x, W) {
      int y = (b * x - 1) / a;
      if (y < H && !dy[y][x - 1])
        cnt++;
    }
    rep1(y, H) {
      int x = (a * y) / b;
      if (x < W && !dx[y - 1][x])
        cnt++;
    }
    mx = max(mx, cnt);
    cnt = 0;
    rep1(x, W) {
      int y = (b * x) / a;
      if (y < H && !dy[y][x - 1])
        cnt++;
    }
    rep1(y, H) {
      int x = (a * y - 1) / b;
      if (x < W && !dx[y - 1][x])
        cnt++;
    }
    mx = max(mx, cnt);
  }
  cout << (mx + 3) / 2 << endl;
}
Problem p01429 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
#define range(i, a, b) assert(a <= i && i <= b)
using namespace std;
int N, M, L;
vector<long long> K, S;
long long floorL(long long x) { return (x / L) * L; }
long long sumofS(int a, int b) {
  long long sum = 0;
  for (int i = a; i < b + 1; i++) {
    range(i, 0, M - 1);
    sum += S[i];
  }
  return sum;
}
long long solve(int n) {
  if (n > 0) {
    auto res = solve(n - 1);
    auto sum1 = sumofS(K[n - 1] + 1, K[n]);
    auto sum2 = sumofS(K[n - 1], K[n]);
    range(n, 1, N - 1);
    auto candi1 = floorL(res + sum1) + sum2;
    auto candi2 = res + sum1 + floorL(sum2);
    // cerr << candi1 << " " << candi2 << endl;
    return min(candi1, candi2);
  } else {
    range(K[0], 0, M - 1);
    // cerr << S[K[0]] << endl;
    return S[K[0]];
  }
}
int main() {
  cin >> N >> M >> L;
  K = vector<long long>(N);
  S = vector<long long>(M);
  for (int i = 0; i < N; i++) {
    cin >> K[i];
    K[i]--;
  }
  for (int i = 0; i < M; i++)
    cin >> S[i];
  sort(K.begin(), K.end());
  auto ans = solve(N - 1) / L;
  cout << ans << endl;
}

Error: 0
100
1194 1194
2284 2287
3672 3675


Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
#define range(i, a, b) assert(a <= i && i <= b)
using namespace std;
int N, M, L;
vector<long long> K, S;
long long floorL(long long x) { return (x / L) * L; }
long long sumofS(int a, int b) {
  long long sum = 0;
  for (int i = a; i < b + 1; i++) {
    range(i, 0, M - 1);
    sum += S[i];
  }
  return sum;
}
long long solve(int n) {
  if (n > 0) {
    auto res = solve(n - 1);
    auto sum1 = sumofS(K[n - 1] + 1, K[n]);
    auto sum2 = sumofS(K[n - 1], K[n]);
    range(n, 1, N - 1);
    auto candi1 = floorL(res + sum1) + sum2;
    auto candi2 = res + sum1 + floorL(sum2);
    cerr << candi1 << " " << candi2 << endl;
    return min(candi1, candi2);
  } else {
    range(K[0], 0, M - 1);
    cerr << S[K[0]] << endl;
    return S[K[0]];
  }
}
int main() {
  cin >> N >> M >> L;
  K = vector<long long>(N);
  S = vector<long long>(M);
  for (int i = 0; i < N; i++) {
    cin >> K[i];
    K[i]--;
  }
  for (int i = 0; i < M; i++)
    cin >> S[i];
  sort(K.begin(), K.end());
  auto ans = solve(N - 1) / L;
  cout << ans << endl;
}
Problem p01429 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef long long ll;

int K[2000], S[100000], N, M, L;
ll sum[100000], memo[2000][2000];

ll getCost(int i, int j) {
  ll value = ((K[i] - 1 >= 0) ? sum[K[i] - 1] : 0LL);
  ll cost = sum[K[j]] - value;
  return floor((double)cost / (double)L);
}

ll dfs(int p1, int p2) {
  int v = 1 + max(p1, p2);
  if (v == N - 1)
    return getCost(p1, N - 1) + getCost(p2, N - 1);
  if (memo[p1][p2] != -1)
    return memo[p1][p2];
  return memo[p1][p2] =
             min(getCost(p1, v) + dfs(v, p2), getCost(p2, v) + dfs(p1, v));
}

int main() {

  cin >> N >> M >> L;
  rep(i, N) {
    cin >> K[i];
    --K[i];
  }
  rep(i, M) cin >> S[i];
  sort(K, K + N);
  sum[0] = S[0];
  REP(i, 1, M) sum[i] = sum[i - 1] + S[i];
  rep(i, N) rep(j, N) memo[i][j] = -1;
  cout << dfs(0, 0) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef long long ll;

int K[2000], S[100000], N, M, L;
ll cost[2000][2000], sum[100000], memo[2000][2000];

ll dfs(int p1, int p2) {
  int v = 1 + max(p1, p2);
  if (v == N - 1)
    return cost[p1][N - 1] + cost[p2][N - 1];
  if (memo[p1][p2] != -1)
    return memo[p1][p2];
  return memo[p1][p2] = min(cost[p1][v] + dfs(v, p2), cost[p2][v] + dfs(p1, v));
}

int main() {

  cin >> N >> M >> L;
  rep(i, N) {
    cin >> K[i];
    --K[i];
  }
  rep(i, M) cin >> S[i];
  sort(K, K + N);
  sum[0] = S[0];
  REP(i, 1, M) sum[i] = sum[i - 1] + S[i];
  rep(i, N) {
    REP(j, i, N) {
      ll value = ((K[i] - 1 >= 0) ? sum[K[i] - 1] : 0LL);
      cost[i][j] = sum[K[j]] - value;
    }
  }
  rep(i, N) REP(j, i, N) cost[i][j] = floor((double)cost[i][j] / (double)L);
  rep(i, N) rep(j, N) memo[i][j] = -1;
  cout << dfs(0, 0) << endl;
  return 0;
}
Problem p01429 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <random>
#include <vector>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "sz:" << v.size() << "\\n[";
  for (const auto &p : v) {
    os << p << ",";
  }
  os << "]\\n";
  return os;
}

int main() {
  ll N, M, L;
  cin >> N >> M >> L;
  uniform_int_distribution<ll> kdist{1, M};
  vector<ll> K(N);
  for (int i = 0; i < N; i++) {
    cin >> K[i];
    K[i]--;
  }
  vector<ll> S(M);
  vector<ll> sum(M);
  for (int i = 0; i < M; i++) {
    cin >> S[i];
    sum[i] = ((i > 0) ? sum[i - 1] : 0) + S[i];
  }
  sort(K.begin(), K.end());
  vector<vector<ll>> dist(N, vector<ll>(N, 0));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      ll l = min(K[i], K[j]);
      ll r = max(K[i], K[j]);
      dist[i][j] = (sum[r] - ((l > 0) ? sum[l - 1] : 0)) / L;
    }
  }
  constexpr ll INF = 1LL << 60;

  vector<ll> prev(N, INF);
  prev[0] = 0;
  for (int i = 0; i < N - 1; i++) {
    vector<ll> dp(N, INF);
    for (int j = 0; j <= i; j++) {
      dp[i] = min(dp[i], prev[j] + dist[j][i + 1]);
      dp[j] = min(dp[j], prev[j] + dist[i][i + 1]);
    }
    prev = dp;
  }
  ll mini = INF;
  for (int j = 0; j < N - 1; j++) {
    mini = min(mini, prev[j] + dist[j][N - 1]);
  }
  cout << mini << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <random>
#include <vector>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "sz:" << v.size() << "\\n[";
  for (const auto &p : v) {
    os << p << ",";
  }
  os << "]\\n";
  return os;
}

int main() {
  ll N, M, L;
  cin >> N >> M >> L;
  uniform_int_distribution<ll> kdist{1, M};
  vector<ll> K(N);
  for (int i = 0; i < N; i++) {
    cin >> K[i];
    K[i]--;
  }
  vector<ll> S(M);
  vector<ll> sum(M);
  for (int i = 0; i < M; i++) {
    cin >> S[i];
    sum[i] = ((i > 0) ? sum[i - 1] : 0) + S[i];
  }
  sort(K.begin(), K.end());
  vector<vector<ll>> dist(N, vector<ll>(N, 0));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      ll l = min(K[i], K[j]);
      ll r = max(K[i], K[j]);
      dist[i][j] = (sum[r] - ((l > 0) ? sum[l - 1] : 0)) / L;
    }
  }
  constexpr ll INF = 1LL << 60;

  vector<vector<ll>> dp(N, vector<ll>(N, INF));
  dp[0][0] = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j <= i; j++) {
      dp[i + 1][i] = min(dp[i + 1][i], dp[i][j] + dist[j][i + 1]);
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + dist[i][i + 1]);
    }
  }
  for (int j = 0; j < N - 1; j++) {
    dp[N - 1][N - 1] = min(dp[N - 1][N - 1], dp[N - 1][j] + dist[j][N - 1]);
  }
  cout << dp[N - 1][N - 1] << endl;

  return 0;
}
Problem p01429 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
static const double EPS = 1e-8;
typedef long long ll;

int N, M, L, K[2000], S[100001];
const ll INF = 1000000000000000000LL;
ll sum[100001];
static ll dp[2000][2001];

int main() {
  scanf("%d%d%d", &N, &M, &L);
  for (int i = 0; i < N; ++i)
    scanf("%d", K + i);
  for (int i = 0; i < M; ++i)
    scanf("%d", S + i);
  sort(K, K + N);

  sum[0] = 0;
  for (int i = 1; i <= M; ++i)
    sum[i] = sum[i - 1] + S[i - 1];

  for (int i = 0; i < 2000; ++i)
    for (int j = i; j < 2001; ++j)
      dp[i][j] = INF;

#define seg_sum(a, b) ((sum[b] - sum[a - 1]) / L)
  dp[0][0] = 0;
  for (int i = 0; i < N - 1; ++i) {
    for (int j = i + 1; j < N; ++j)
      dp[i][j] = min(dp[i][j], dp[i][j - 1] + seg_sum(K[j - 1], K[j]));
    for (int j = i + 1; j < N - 1; ++j)
      dp[j][j + 1] = min(dp[j][j + 1], dp[i][j] + seg_sum(K[i], K[j + 1]));
  }
  ll ans = INF;
  for (int i = 0; i < N - 1; ++i)
    ans = min(ans, dp[i][N - 1] + seg_sum(K[i], K[N - 1]));
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
static const double EPS = 1e-8;
typedef long long ll;

int main() {
  int N, M, L, K[2000], S[10001];
  scanf("%d%d%d", &N, &M, &L);
  for (int i = 0; i < N; ++i)
    scanf("%d", K + i);
  for (int i = 1; i <= M; ++i)
    scanf("%d", S + i);
  sort(K, K + N);

  static ll sum[10001];
  sum[0] = 0;
  for (int i = 1; i <= M; ++i)
    sum[i] = sum[i - 1] + S[i];

  const ll INF = 100000000000000ll;
  static ll dp[2000][2001];
  for (int i = 0; i < 2000; ++i)
    for (int j = i; j < 2001; ++j)
      dp[i][j] = INF;

#define seg_sum(a, b) ((sum[b] - sum[a - 1]) / L)
  dp[0][0] = 0;
  for (int i = 0; i < N - 1; ++i) {
    for (int j = i + 1; j < N; ++j)
      dp[i][j] = min(dp[i][j], dp[i][j - 1] + seg_sum(K[j - 1], K[j]));
    for (int j = i + 1; j < N - 1; ++j)
      dp[j][j + 1] = min(dp[j][j + 1], dp[i][j] + seg_sum(K[i], K[j + 1]));
  }
  ll ans = INF;
  for (int i = 0; i < N - 1; ++i)
    ans = min(ans, dp[i][N - 1] + seg_sum(K[i], K[N - 1]));
  printf("%lld\\n", ans);

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define mz(x, y) x = min(x, y)
typedef long long ll;
int K[2000], S[100000];
ll inf = 1e17;
ll dp[2][2001];
ll sum[100001];
vector<ll> ash;
int N, M, L;
ll dis(int a, int b) {
  if (a > b)
    return inf;
  return (sum[ash[b] + 1] - sum[ash[a]]) / L;
}
int main() {
  cin >> N >> M >> L;
  rep(i, N) cin >> K[i];
  rep(i, N) K[i]--;
  rep(i, N) ash.pb(K[i]);
  sort(all(ash));
  ash.erase(unique(all(ash)), ash.end());
  rep(i, N) K[i] = lower_bound(all(ash), K[i]) - ash.begin();
  rep(i, M) cin >> S[i];
  rep(i, M) sum[i + 1] = sum[i] + S[i];
  sort(K, K + N);
  M = ash.size();
  rep(j, M + 1) dp[0][j] = inf;
  dp[0][0] = 0;
  rep(i, N - 1) {
    rep(j, M + 1) dp[1 - i % 2][j] = inf;
    rep(j, M) {
      mz(dp[1 - i % 2][j], dp[i % 2][j] + dis(K[i], K[i + 1]));
      mz(dp[1 - i % 2][K[i]], dp[i % 2][j] + dis(j, K[i + 1]));
    }
  }
  ll ans = inf;
  rep(j, M) mz(ans, dp[1 - N % 2][j] + dis(j, K[N - 1]));
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define mz(x, y) x = min(x, y)
typedef long long ll;
int K[2000], S[100000];
ll inf = 1e17;
ll dp[2][100001];
ll sum[100001];
int N, M, L;
ll dis(int a, int b) {
  if (a > b)
    return inf;
  return (sum[b + 1] - sum[a]) / L;
}
int main() {
  cin >> N >> M >> L;
  rep(i, N) cin >> K[i];
  rep(i, N) K[i]--;
  rep(i, M) cin >> S[i];
  rep(i, M) sum[i + 1] = sum[i] + S[i];
  sort(K, K + N);
  rep(j, M + 1) dp[0][j] = inf;
  dp[0][K[0]] = 0;
  rep(i, N - 1) {
    rep(j, M + 1) dp[1 - i % 2][j] = inf;
    rep(j, M) {
      mz(dp[1 - i % 2][j], dp[i % 2][j] + dis(K[i], K[i + 1]));
      mz(dp[1 - i % 2][K[i]], dp[i % 2][j] + dis(j, K[i + 1]));
    }
  }
  ll ans = inf;
  rep(j, M) mz(ans, dp[1 - N % 2][j] + dis(j, K[N - 1]));
  cout << ans << endl;
}
Problem p01429 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;

int64_t F[100001];
int N, M;
int64_t L;

int64_t f(int a, int b) { return (F[b + 1] - F[a]) / L; }

int main() {
  cin >> N >> M >> L;
  vector<int64_t> K(N), S(M);
  for (auto &k : K)
    cin >> k;
  for (auto &s : S)
    cin >> s;
  for (int i = 0; i < M; i++) {
    F[i + 1] = F[i] + S[i]; //(a,b)????????????=(F[b+1]-F[a])/L
  }
  sort(K.begin(), K.end());
  for (auto &k : K)
    k--;
  int64_t DP[2][N];
  fill(DP[0], DP[2], 1000000000000);
  DP[0][0] = 0;
  DP[1][0] = f(K[0], K[1]);
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < i - 1; j++) {
      DP[i & 1][j] = DP[i & 1 ^ 1][j] + f(K[i - 1], K[i]);
    }
    for (int k = 0; k < i - 1; k++) {
      DP[i & 1][i - 1] =
          min(DP[i & 1][i - 1], DP[i & 1 ^ 1][k] + f(K[k], K[i]));
    }
  }
  int64_t ans = 1000000000000;
  for (int i = 0; i < N - 1; i++) {
    ans = min(ans, DP[N & 1 ^ 1][i] + f(K[i], K[N - 1]));
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;

int64_t F[100001];
int N, M;
int64_t L;

int64_t f(int a, int b) { return (F[b + 1] - F[a]) / L; }

int main() {
  cin >> N >> M >> L;
  vector<int64_t> K(N), S(M);
  for (auto &k : K)
    cin >> k;
  for (auto &s : S)
    cin >> s;
  for (int i = 0; i < M; i++) {
    F[i + 1] = F[i] + S[i]; //(a,b)????????????=(F[b+1]-F[a])/L
  }
  sort(K.begin(), K.end());
  for (auto &k : K)
    k--;
  int64_t DP[N][N];
  fill(DP[0], DP[N], 100000000);
  DP[0][0] = 0;
  DP[1][0] = f(K[0], K[1]);
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < i - 1; j++) {
      DP[i][j] = DP[i - 1][j] + f(K[i - 1], K[i]);
    }
    for (int k = 0; k < i - 1; k++) {
      DP[i][i - 1] = min(DP[i][i - 1], DP[i - 1][k] + f(K[k], K[i]));
    }
  }
  int64_t ans = 100000000;
  for (int i = 0; i < N - 1; i++) {
    ans = min(ans, DP[N - 1][i] + f(K[i], K[N - 1]));
  }
  cout << ans << endl;

  return 0;
}
Problem p01429 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

using ll = long long;
constexpr int MAX = 2010;

int N, M, L;
ll memo[MAX][MAX];
vector<ll> K, sum;

ll calc(int l, int r) { return (sum[K[r] + 1] - sum[K[l]]) / L; }

ll solve(int l, int r) {
  ll &res = memo[l][r];
  if (res != -1)
    return res;

  int n = max(l, r) + 1;
  if (n == N - 1) {
    return calc(l, n) + calc(r, n);
  }
  res = min(solve(n, r) + calc(l, n), solve(l, n) + calc(r, n));
  return res;
}

int main() {
  cin >> N >> M >> L;
  K.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> K[i];
    K[i]--;
  }
  sort(K.begin(), K.end());

  vector<int> S(M);
  for (int i = 0; i < M; i++) {
    cin >> S[i];
  }

  sum.resize(M + 1);
  for (int i = 0; i < M; i++) {
    sum[i + 1] = sum[i] + S[i];
  }

  memset(memo, -1, sizeof(memo));
  cout << solve(0, 0) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

using ll = long long;
constexpr int MAX = 1011;

int N, M, L;
ll memo[MAX][MAX];
vector<ll> K, sum;

ll calc(int l, int r) { return (sum[K[r] + 1] - sum[K[l]]) / L; }

ll solve(int l, int r) {
  ll &res = memo[l][r];
  if (res != -1)
    return res;

  int n = max(l, r) + 1;
  if (n == N - 1) {
    return calc(l, n) + calc(r, n);
  }
  res = min(solve(n, r) + calc(l, n), solve(l, n) + calc(r, n));
  return res;
}

int main() {
  cin >> N >> M >> L;
  K.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> K[i];
    K[i]--;
  }
  sort(K.begin(), K.end());

  vector<int> S(M);
  for (int i = 0; i < M; i++) {
    cin >> S[i];
  }

  sum.resize(M + 1);
  for (int i = 0; i < M; i++) {
    sum[i + 1] = sum[i] + S[i];
  }

  memset(memo, -1, sizeof(memo));
  cout << solve(0, 0) << endl;
  return 0;
}