Problem p01429 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for (ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n)&1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

int K[201000];
int S[201000];
ll dp[2010][2010];
ll SUM[201000];

ll sum(int a, int b, int L) {
  int tmp = a;
  a = min(a, b);
  b = max(tmp, b);

  return (SUM[b] - SUM[a - 1]) / L;
}

int main() {
  int N, M, L;
  cin >> N >> M >> L;
  REP(i, N) cin >> K[i];
  REP(i, M) cin >> S[i];
  REP(i, M) SUM[i + 1] = SUM[i] + S[i];
  sort(K, K + N);

  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }

  memset(dp, -1, sizeof(dp));
  dp[1][0] = sum(K[0], K[1], L);

  REP2(i, 1, N - 1) {
    REP(j, N) {
      if (dp[i][j] == -1)
        continue;
      ll t1 = dp[i][j] + sum(K[i + 1], K[i], L);
      ll t2 = dp[i][j] + sum(K[i + 1], K[j], L);

      if (dp[i + 1][j] == -1 || dp[i + 1][j] > t1) {
        dp[i + 1][j] = t1;
      }
      if (dp[i + 1][i] == -1 || dp[i + 1][i] > t2) {
        dp[i + 1][i] = t2;
      }
    }
  }

  ll ans = 1e15;
  REP(i, N - 1) if (dp[N - 1][i] != -1) {
    ans = min(dp[N - 1][i] + sum(K[N - 1], K[i], L), ans);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for (ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n)&1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

int K[2010];
int S[2010];
ll dp[2010][2010];
ll SUM[201000];

ll sum(int a, int b, int L) {
  int tmp = a;
  a = min(a, b);
  b = max(tmp, b);

  if (a == 0) {
    cerr << "a < 0" << endl;
    assert(false);
  }
  return (SUM[b] - SUM[a - 1]) / L;
}

int main() {
  int N, M, L;
  cin >> N >> M >> L;
  REP(i, N) cin >> K[i];
  REP(i, M) cin >> S[i];
  REP(i, M) SUM[i + 1] = SUM[i] + S[i];
  sort(K, K + N);

  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }

  memset(dp, -1, sizeof(dp));
  dp[1][0] = sum(K[0], K[1], L);

  REP2(i, 1, N - 1) {
    REP(j, N) {
      if (dp[i][j] == -1)
        continue;
      ll t1 = dp[i][j] + sum(K[i + 1], K[i], L);
      ll t2 = dp[i][j] + sum(K[i + 1], K[j], L);

      if (dp[i + 1][j] == -1 || dp[i + 1][j] > t1) {
        dp[i + 1][j] = t1;
      }
      if (dp[i + 1][i] == -1 || dp[i + 1][i] > t2) {
        dp[i + 1][i] = t2;
      }
    }
  }

  ll ans = 1e15;
  REP(i, N - 1) if (dp[N - 1][i] != -1) {
    ans = min(dp[N - 1][i] + sum(K[N - 1], K[i], L), ans);
  }
  cout << ans << endl;
  return 0;
}