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

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

using ll = long long;
using Pair = std::pair<int, int>;

int n, m, h, k;
int hori_bars[101000]; //?????´??????????£?
int stat[1010];        //????£??????\\?¶??????????->??????
int last[1010];        //????£??????\\?¶??????????->??????
int score[1010];       //????£??????\\?¶??????????->????????¢
Pair swapping
    [100010]; //?¨??£?->????£?????????????????????´???(????????§?????????

int main() {
  while (std::cin >> n >> m >> h >> k, n + m + h + k) {

    for (int i = 0; i < n; ++i) {
      std::cin >> score[i];
    }
    std::vector<Pair> bars;
    bars.reserve(m);
    for (int i = 0; i < m; ++i) {
      int a, b;
      std::cin >> a >> b;
      --a;
      bars.emplace_back(Pair{b, a});
    }
    std::sort(bars.begin(), bars.end());

    for (int i = 0; i < bars.size(); ++i)
      hori_bars[i] = bars[i].second;

    for (int i = 0; i < n; ++i)
      stat[i] = i;

    for (int i = 0; i < m; ++i) {
      swapping[i] = {stat[hori_bars[i]], stat[hori_bars[i] + 1]};

      std::swap(stat[hori_bars[i]], stat[hori_bars[i] + 1]);
    }

    for (int i = 0; i < n; ++i) {
      last[stat[i]] = i;
    }

    int ans = 0, base;
    for (int i = 0; i < n; ++i) {
      if (stat[i] < k) {
        ans += score[i];
      }
    }
    base = ans;

    for (int i = 0; i < m; ++i) {
      auto a = swapping[i].first;
      auto b = swapping[i].second;

      if (a < k && b < k || a >= k && b >= k)
        continue;

      if (a < k) {
        ans = std::min(ans, base - score[last[a]] + score[last[b]]);
      }
      std::swap(a, b);
      if (a < k) {
        ans = std::min(ans, base - score[last[a]] + score[last[b]]);
      }
    }

    std::cout << ans << std::endl;
  }
}

Error: 0

Fixed submission:

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

using ll = long long;
using Pair = std::pair<int, int>;

int n, m, h, k;
int hori_bars[1010]; //?????´??????????£?
int stat[1010];      //????£??????\\?¶??????????->??????
int last[1010];      //????£??????\\?¶??????????->??????
ll score[1010];      //????£??????\\?¶??????????->????????¢
Pair swapping
    [101010]; //?¨??£?->????£?????????????????????´???(????????§?????????

int main() {
  while (std::cin >> n >> m >> h >> k, n + m + h + k) {

    for (int i = 0; i < n; ++i) {
      std::cin >> score[i];
    }
    std::vector<Pair> bars;
    bars.reserve(m);
    for (int i = 0; i < m; ++i) {
      int a, b;
      std::cin >> a >> b;
      --a;
      bars.emplace_back(Pair{b, a});
    }
    std::sort(bars.begin(), bars.end());

    for (int i = 0; i < bars.size(); ++i)
      hori_bars[i] = bars[i].second;

    for (int i = 0; i < n; ++i)
      stat[i] = i;

    for (int i = 0; i < m; ++i) {
      swapping[i] = {stat[hori_bars[i]], stat[hori_bars[i] + 1]};

      std::swap(stat[hori_bars[i]], stat[hori_bars[i] + 1]);
    }

    for (int i = 0; i < n; ++i) {
      last[stat[i]] = i;
    }

    ll ans = 0, base;
    for (int i = 0; i < n; ++i) {
      if (stat[i] < k) {
        ans += score[i];
      }
    }
    base = ans;

    for (int i = 0; i < m; ++i) {
      auto a = swapping[i].first;
      auto b = swapping[i].second;

      if (a < k && b < k || a >= k && b >= k)
        continue;

      if (a < k) {
        ans = std::min(ans, base - score[last[a]] + score[last[b]]);
      }
      std::swap(a, b);
      if (a < k) {
        ans = std::min(ans, base - score[last[a]] + score[last[b]]);
      }
    }

    std::cout << ans << std::endl;
  }
}