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

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>

using namespace std;

const long long inf = 1e16;

typedef pair<int, int> P;

P ps[4040];
int N;

vector<int> p1, n1, p2;

vector<int> xs;
vector<int> nearest;
vector<vector<long long>> dp;
vector<long long> tmp;

void init() {
  p1.clear();
  n1.clear();
  p2.clear();
  xs.clear();
  nearest.clear();
  dp.clear();
}

int main() {
  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;
    init();
    for (int i = 0; i < N; i++) {
      int p, d;
      scanf("%d%d", &p, &d);
      ps[i] = P(p, d);
    }
    sort(ps, ps + N);
    if (N == 2) {
      printf("%d\\r\\n", ps[1].first - ps[0].first);
      continue;
    }
    for (int i = 0; i < N; i++) {
      if (ps[i].second == 1) {
        p2.push_back(ps[i].first);
      } else {
        p1.push_back(ps[i].first);
        n1.push_back(ps[i].second);
      }
    }
    if (n1.size() == 0) {
      printf("-1\\r\\n");
      continue;
    } else if (n1.size() == 1) {
      if (n1[0] < p2.size()) {
        printf("-1\\r\\n");
      } else {
        long long ans = 0;
        for (int i = 0; i < p2.size(); i++) {
          int tmp = abs(p2[i] - p1[0]);
          ans += tmp;
        }
        printf("%lld\\r\\n", ans);
      }
      continue;
    } else if (p2.size() == 0) {
      printf("%d\\r\\n", p1[p1.size() - 1] - p1[0]);
      continue;
    }
    int cnt = 0;
    for (int i = 0; i < n1.size(); i++) {
      if (i == 0 || i + 1 == n1.size()) {
        n1[i]--;
      } else {
        n1[i] -= 2;
      }
      cnt += n1[i];
    }
    if (cnt < p2.size()) {
      printf("-1\\r\\n");
      continue;
    }
    for (int i = 0; i < p1.size(); i++) {
      for (int j = 0; j < n1[i]; j++) {
        xs.push_back(p1[i]);
      }
    }
    for (int i = 0; i < p2.size(); i++) {
      int id = distance(xs.begin(), lower_bound(xs.begin(), xs.end(), p2[i]));
      if (id != xs.size() && id != 0) {
        int r = abs(xs[id] - p2[i]);
        int l = abs(xs[id - 1] - p2[i]);
        if (r > l)
          id--;
      }
      nearest.push_back(id);
    }
    int l = p2.size();
    tmp.clear();
    for (int i = 0; i < 2 * l + 1; i++)
      tmp.push_back(inf);
    for (int i = 0; i < l; i++)
      dp.push_back(tmp);
    for (int i = 0; i < 2 * l + 1; i++) {
      int k = nearest[0] + i - l;
      if (k >= 0 && k < xs.size()) {
        dp[0][i] = abs(xs[k] - p2[0]);
      }
      if (i > 0)
        dp[0][i] = min(dp[0][i], dp[0][i - 1]);
    }
    for (int i = 1; i < l; i++) {
      for (int j = 0; j < 2 * l + 1; j++) {
        int k = nearest[i] + j - l - 1;
        if (k + 1 < 0 || k + 1 >= xs.size()) {
          if (j > 0) {
            dp[i][j] = min(dp[i][j], dp[i][j - 1]);
          }
          continue;
        }
        int d = k - nearest[i - 1];
        int prev = d + l;
        if (prev > l * 2)
          prev = l * 2;
        if (prev < 0) {
          if (j > 0) {
            dp[i][j] = min(dp[i][j], dp[i][j - 1]);
          }
          continue;
        }
        if (dp[i - 1][prev] == inf) {
          if (j > 0) {
            dp[i][j] = min(dp[i][j], dp[i][j - 1]);
            continue;
          }
        }
        dp[i][j] = dp[i - 1][prev] + abs(xs[k + 1] - p2[i]);
        if (j > 0)
          dp[i][j] = min(dp[i][j], dp[i][j - 1]);
      }
    }
    printf("%lld\\r\\n", dp[l - 1][2 * l] + p1[p1.size() - 1] - p1[0]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>

using namespace std;

const long long inf = 1e16;

typedef pair<int, int> P;

P ps[4040];
int N;

vector<int> p1, n1, p2;

vector<int> xs;
vector<int> nearest;
vector<vector<long long>> dp;
vector<long long> tmp;

void init() {
  p1.clear();
  n1.clear();
  p2.clear();
  xs.clear();
  nearest.clear();
  dp.clear();
}

int main() {
  int datano = 0;
  while (true) {
    datano++;
    scanf("%d", &N);
    if (N == 0)
      break;
    init();
    for (int i = 0; i < N; i++) {
      int p, d;
      scanf("%d%d", &p, &d);
      ps[i] = P(p, d);
    }
    sort(ps, ps + N);
    if (N == 2) {
      printf("%d\\n", ps[1].first - ps[0].first);
      continue;
    }
    for (int i = 0; i < N; i++) {
      if (ps[i].second == 1) {
        p2.push_back(ps[i].first);
      } else {
        p1.push_back(ps[i].first);
        n1.push_back(ps[i].second);
      }
    }
    if (n1.size() == 0) {
      printf("-1\\n");
      continue;
    } else if (n1.size() == 1) {
      if (n1[0] < p2.size()) {
        printf("-1\\n");
      } else {
        long long ans = 0;
        for (int i = 0; i < p2.size(); i++) {
          int tmp = abs(p2[i] - p1[0]);
          ans += tmp;
        }
        printf("%lld\\n", ans);
      }
      continue;
    } else if (p2.size() == 0) {
      printf("%d\\n", p1[p1.size() - 1] - p1[0]);
      continue;
    }
    int cnt = 0;
    for (int i = 0; i < n1.size(); i++) {
      if (i == 0 || i + 1 == n1.size()) {
        n1[i]--;
      } else {
        n1[i] -= 2;
      }
      cnt += n1[i];
    }
    if (cnt < p2.size()) {
      printf("-1\\n");
      continue;
    }
    for (int i = 0; i < p1.size(); i++) {
      for (int j = 0; j < n1[i]; j++) {
        xs.push_back(p1[i]);
      }
    }
    for (int i = 0; i < p2.size(); i++) {
      int id = distance(xs.begin(), lower_bound(xs.begin(), xs.end(), p2[i]));
      if (id != xs.size() && id != 0) {
        int r = abs(xs[id] - p2[i]);
        int l = abs(xs[id - 1] - p2[i]);
        if (r > l)
          id--;
      }
      nearest.push_back(id);
    }
    int l = p2.size();
    tmp.clear();
    for (int i = 0; i < 2 * l + 1; i++)
      tmp.push_back(inf);
    for (int i = 0; i < l; i++)
      dp.push_back(tmp);
    for (int i = 0; i < 2 * l + 1; i++) {
      int k = nearest[0] + i - l;
      if (k >= 0 && k < xs.size()) {
        dp[0][i] = abs(xs[k] - p2[0]);
      }
      if (i > 0)
        dp[0][i] = min(dp[0][i], dp[0][i - 1]);
    }
    for (int i = 1; i < l; i++) {
      for (int j = 0; j < 2 * l + 1; j++) {
        int k = nearest[i] + j - l - 1;
        if (k + 1 < 0 || k + 1 > xs.size()) {
          if (j > 0) {
            dp[i][j] = min(dp[i][j], dp[i][j - 1]);
            continue;
          }
        }
        int d = k - nearest[i - 1];
        int prev = d + l;
        if (prev > l * 2)
          prev = l * 2;
        if (dp[i - 1][prev] == inf) {
          if (j > 0) {
            dp[i][j] = min(dp[i][j], dp[i][j - 1]);
            continue;
          }
        }
        dp[i][j] = dp[i - 1][prev] + abs(xs[k + 1] - p2[i]);
        if (j > 0)
          dp[i][j] = min(dp[i][j], dp[i][j - 1]);
      }
    }
    printf("%lld\\n", dp[l - 1][2 * l] + p1[p1.size() - 1] - p1[0]);
  }
  return 0;
}