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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 100000000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

ll dp[2010];
ll dst[100100];
ll v[2010];
int main() {
  ll n, m, l;
  cin >> n >> m >> l;
  vector<ll> k(n), s(m);
  rep(i, n) {
    cin >> k[i];
    k[i]--;
  }
  rep(i, m) { cin >> s[i]; }
  sort(k.begin(), k.end());
  dst[0] = 0;
  for (int i = 1; i <= m; i++) {
    dst[i] = dst[i - 1] + s[i - 1];
  }
  v[1] = (dst[k[1] + 1] - dst[k[0]]) / l;
  for (int i = 2; i < n; i++) {
    v[i] = v[i - 1] + (dst[k[i] + 1] - dst[k[i - 1]]) / l;
  }

  dp[n - 1] = (dst[k[n - 1] + 1] - dst[k[n - 2]]) / l;
  for (int i = n - 2; i > 0; i--) {
    dp[i] = inf;
    for (int j = i + 1; j < n; j++) {
      if (j == (i + 1)) {
        dp[i] = min(dp[i], (dst[k[j] + 1] - dst[k[i - 1]]) / l + dp[j]);
        if (j == n - 1) {
          dp[i] = min(dp[i], (dst[k[j] + 1] - dst[k[i]]) / l +
                                 (dst[k[j] + 1] - dst[k[i - 1]]) / l);
        }
      } else {
        dp[i] = min(dp[i], (dst[k[j] + 1] - dst[k[i - 1]]) / l + v[j - 1] -
                               v[i] + dp[j]);
        dp[i] = min(dp[i], (dst[k[j] + 1] - dst[k[i]]) / l +
                               (dst[k[i + 1] + 1] - dst[k[i - 1]]) / l +
                               v[j - 1] - v[i + 1] + dp[j]);
      }
    }
  }

  ll mi = inf;
  for (int i = 1; i < n; i++) {
    mi = min(mi, dp[i] + (dst[k[i] + 1] - dst[k[0]]) / l + v[i - 1]);
  }
  cout << mi << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 10000000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

ll dp[2010];
ll dst[20100];
ll v[2010];
int main() {
  ll n, m, l;
  cin >> n >> m >> l;
  vector<ll> k(n), s(m);
  rep(i, n) {
    cin >> k[i];
    k[i]--;
  }
  rep(i, m) { cin >> s[i]; }
  sort(k.begin(), k.end());
  dst[0] = 0;
  for (int i = 1; i <= m; i++) {
    dst[i] = dst[i - 1] + s[i - 1];
  }
  v[1] = (dst[k[1] + 1] - dst[k[0]]) / l;
  for (int i = 2; i < n; i++) {
    v[i] = v[i - 1] + (dst[k[i] + 1] - dst[k[i - 1]]) / l;
  }

  dp[n - 1] = (dst[k[n - 1] + 1] - dst[k[n - 2]]) / l;
  for (int i = n - 2; i > 0; i--) {
    dp[i] = inf;
    for (int j = i + 1; j < n; j++) {
      if (j == i + 1) {
        dp[i] = min(dp[i], (dst[k[j] + 1] - dst[k[i - 1]]) / l + dp[j]);
      } else {
        dp[i] = min(dp[i],
                    (dst[k[j] + 1] - dst[k[i - 1]]) / l + v[j] - v[i] + dp[j]);
        dp[i] = min(dp[i], (dst[k[j] + 1] - dst[k[i]]) / l +
                               (dst[k[i + 1] + 1] - dst[k[i - 1]]) / l +
                               v[j - 1] - v[i + 1] + dp[j]);
      }
    }
  }
  ll mi = inf;
  for (int i = 1; i < n; i++) {
    mi = min(mi, dp[i] + (dst[k[i] + 1] - dst[k[0]]) / l + v[i - 1]);
  }
  cout << mi << endl;
  return 0;
}