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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> ppiii;

constexpr int INF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MAX_N = 1005;
constexpr int MAX_M = 100005;
constexpr int dx[] = {-1, 0, 1, 0};
constexpr int dy[] = {0, -1, 0, 1};

int n, m, h, k, s[MAX_N], goal[MAX_N], a[MAX_M], in[MAX_M][2];
vector<pii> tempa;

int Goal(int st) {
  int pos = st;
  for (int i = 0; i < m; ++i) {
    if (a[i] == pos) {
      in[i][0] = st;
      pos = a[i] + 1;
    } else if (a[i] + 1 == pos) {
      in[i][1] = st;
      pos = a[i];
    }
  }

  return pos;
}

bool Solve() {
  cin >> n >> m >> h >> k;
  if (!n)
    return false;

  tempa.clear();

  for (int i = 1; i <= n; ++i)
    cin >> s[i];

  for (int i = 0; i < m; ++i) {
    int p, d;
    cin >> p >> d;
    tempa.PB({d, p});
  }
  sort(ALL(tempa));

  for (int i = 0; i < m; ++i)
    a[i] = tempa[i].second;

  for (int i = 1; i <= n; ++i) {
    goal[i] = Goal(i);
  }

  int sum = 0;
  for (int i = 1; i <= k; ++i) {
    sum += s[goal[i]];
  }

  int ans = sum;
  for (int i = 0; i < m; ++i) {
    int temp = 0;
    for (int j = 0; j < 2; ++j) {
      if (k < in[i][j])
        continue;
      temp += s[goal[in[i][(j + 1) % 2]]] - s[goal[in[i][j]]];
    }
    ans = min(ans, sum + temp);
  }

  cout << ans << endl;

  return true;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  while (Solve())
    ;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> ppiii;

constexpr int INF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MAX_N = 10005;
constexpr int MAX_M = 10005;
constexpr int dx[] = {-1, 0, 1, 0};
constexpr int dy[] = {0, -1, 0, 1};

int n, m, h, k, s[MAX_N], goal[MAX_N], a[MAX_M], in[MAX_M][2];
vector<pii> tempa;

int Goal(int st) {
  int pos = st;
  for (int i = 0; i < m; ++i) {
    if (a[i] == pos) {
      in[i][0] = st;
      pos = a[i] + 1;
    } else if (a[i] + 1 == pos) {
      in[i][1] = st;
      pos = a[i];
    }
  }

  return pos;
}

bool Solve() {
  cin >> n >> m >> h >> k;
  if (!n)
    return false;

  tempa.clear();

  for (int i = 1; i <= n; ++i)
    cin >> s[i];

  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    tempa.PB({b, a});
  }
  sort(ALL(tempa));

  for (int i = 0; i < m; ++i)
    a[i] = tempa[i].second;

  for (int i = 1; i <= n; ++i) {
    goal[i] = Goal(i);
  }

  int sum = 0;
  for (int i = 1; i <= k; ++i) {
    sum += s[goal[i]];
  }

  int ans = sum;
  for (int i = 0; i < m; ++i) {
    int temp = 0;
    for (int j = 0; j < 2; ++j) {
      if (k < in[i][j])
        continue;
      temp += s[goal[in[i][(j + 1) % 2]]] - s[goal[in[i][j]]];
    }
    ans = min(ans, sum + temp);
  }

  cout << ans << endl;

  return true;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  while (Solve())
    ;
}