Problem p00463 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
struct Border {
  int position_height{0}, position_width{0};
};

void solve() {
  int width, height, h, k;
  std::cin >> width >> height >> h >> k;
  while (width != 0 && height != 0) {
    std::vector<int> start_pos(width);
    std::vector<int> point(width);
    for (auto i = 0; i < width; ++i)
      start_pos[i] = i;
    for (auto &p : point)
      std::cin >> p;
    std::vector<Border> border(height);
    for (auto &b : border)
      std::cin >> b.position_width >> b.position_height;
    std::sort(border.begin(), border.end(),
              [](const Border &a, const Border &b) {
                return a.position_height < b.position_height;
              });
    int sum{0};
    for (auto b = border.rbegin(); b != border.rend(); ++b) {
      std::swap(point[b->position_width], point[b->position_width - 1]);
    }
    for (auto &b : border) {
      std::swap(start_pos[b.position_width], start_pos[b.position_width - 1]);
      if (start_pos[b.position_width] >= k &&
          start_pos[b.position_width - 1] < k) {
        if (point[start_pos[b.position_width]] -
                point[start_pos[b.position_width - 1]] <
            sum)
          sum = point[start_pos[b.position_width]] -
                point[start_pos[b.position_width - 1]];
      }
      if (start_pos[b.position_width - 1] >= k &&
          start_pos[b.position_width] < k) {
        if (point[start_pos[b.position_width - 1]] -
                point[start_pos[b.position_width]] <
            sum)
          sum = point[start_pos[b.position_width - 1]] -
                point[start_pos[b.position_width]];
      }
    }
    for (auto i = 0; i < k; ++i) {
      sum += point[i];
    }
    std::cout << sum << std::endl;
    std::cin >> height >> width >> h >> k;
  }
}
int main() { solve(); }


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
struct Border {
  int position_height{0}, position_width{0};
};

void solve() {
  int width, height, h, k;
  std::cin >> width >> height >> h >> k;
  while (width != 0 && height != 0) {
    std::vector<int> start_pos(width);
    std::vector<int> point(width);
    for (auto i = 0; i < width; ++i)
      start_pos[i] = i;
    for (auto &p : point)
      std::cin >> p;
    std::vector<Border> border(height);
    for (auto &b : border)
      std::cin >> b.position_width >> b.position_height;
    std::sort(border.begin(), border.end(),
              [](const Border &a, const Border &b) {
                return a.position_height < b.position_height;
              });
    int sum{0};
    for (auto b = border.rbegin(); b != border.rend(); ++b) {
      std::swap(point[b->position_width], point[b->position_width - 1]);
    }
    for (auto &b : border) {
      std::swap(start_pos[b.position_width], start_pos[b.position_width - 1]);
      if (start_pos[b.position_width] >= k &&
          start_pos[b.position_width - 1] < k) {
        if (point[start_pos[b.position_width]] -
                point[start_pos[b.position_width - 1]] <
            sum)
          sum = point[start_pos[b.position_width]] -
                point[start_pos[b.position_width - 1]];
      }
      if (start_pos[b.position_width - 1] >= k &&
          start_pos[b.position_width] < k) {
        if (point[start_pos[b.position_width - 1]] -
                point[start_pos[b.position_width]] <
            sum)
          sum = point[start_pos[b.position_width - 1]] -
                point[start_pos[b.position_width]];
      }
    }
    for (auto i = 0; i < k; ++i) {
      sum += point[i];
    }
    std::cout << sum << std::endl;
    std::cin >> width >> height >> h >> k;
  }
}
int main() { solve(); }