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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

int n, m, h, k;
typedef pair<int, int> P;
vector<P> ber;
long long int x[1001], ax[1001];
long long int sc[1001], scn[1001];
long long int yk[100001][2], ys[100001][2];
int resn;
long long int res;
long long int ans;

int main(void) {
  while (1) {
    scanf("%d%d%d%d", &n, &m, &h, &k);
    if (n == 0 && m == 0 && h == 0 && k == 0)
      break;
    memset(x, 0, sizeof(x));
    memset(ax, 0, sizeof(ax));
    memset(sc, 0, sizeof(sc));
    memset(scn, 0, sizeof(scn));
    memset(yk, 0, sizeof(yk));
    memset(ys, 0, sizeof(ys));
    ber.clear();
    res = 0;
    ans = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &sc[i]);
      scn[i] = sc[i];
      x[i] = i;
      ax[i] = i;
    }
    for (int i = 0; i < m; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      ber.push_back(P(b, a));
    }
    sort(ber.begin(), ber.end());
    for (int i = 0; i < m; i++) {
      swap(x[ber[i].second], x[ber[i].second + 1]);
    }
    resn = -1;
    for (int i = m - 1; i >= 0; i--) {
      ys[i][0] = scn[ber[i].second], ys[i][1] = scn[ber[i].second + 1];
      swap(x[ber[i].second], x[ber[i].second + 1]);
      swap(scn[ber[i].second], scn[ber[i].second + 1]);
      yk[i][0] = x[ber[i].second], yk[i][1] = x[ber[i].second + 1];
      if (yk[i][0] <= k && yk[i][1] > k) {
        long long int sa = ys[i][1] - ys[i][0];
        if (sa >= res)
          resn = i, res = sa;
      }
      if (yk[i][1] <= k && yk[i][0] > k) {
        long long int sa = ys[i][0] - ys[i][1];
        if (sa >= res)
          resn = i, res = sa;
      }
    }
    for (int i = 0; i < m; i++) {
      if (i != resn)
        swap(ax[ber[i].second], ax[ber[i].second + 1]);
    }
    for (int i = 1; i <= n; i++) {
      if (ax[i] <= k)
        ans += sc[i];
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

int n, m, h, k;
typedef pair<int, int> P;
vector<P> ber;
long long int x[1001], ax[1001];
long long int sc[1001], scn[1001];
long long int yk[100001][2], ys[100001][2];
int resn;
long long int res;
long long int ans;

int main(void) {
  while (1) {
    scanf("%d%d%d%d", &n, &m, &h, &k);
    if (n == 0 && m == 0 && h == 0 && k == 0)
      break;
    memset(x, 0, sizeof(x));
    memset(ax, 0, sizeof(ax));
    memset(sc, 0, sizeof(sc));
    memset(scn, 0, sizeof(scn));
    memset(yk, 0, sizeof(yk));
    memset(ys, 0, sizeof(ys));
    ber.clear();
    res = 0;
    ans = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%lld", sc[i]);
      scn[i] = sc[i];
      x[i] = i;
      ax[i] = i;
    }
    for (int i = 0; i < m; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      ber.push_back(P(b, a));
    }
    sort(ber.begin(), ber.end());
    for (int i = 0; i < m; i++) {
      swap(x[ber[i].second], x[ber[i].second + 1]);
    }
    resn = -1;
    for (int i = m - 1; i >= 0; i--) {
      ys[i][0] = scn[ber[i].second], ys[i][1] = scn[ber[i].second + 1];
      swap(x[ber[i].second], x[ber[i].second + 1]);
      swap(scn[ber[i].second], scn[ber[i].second + 1]);
      yk[i][0] = x[ber[i].second], yk[i][1] = x[ber[i].second + 1];
      if (yk[i][0] <= k && yk[i][1] > k) {
        long long int sa = ys[i][1] - ys[i][0];
        if (sa >= res)
          resn = i, res = sa;
      }
      if (yk[i][1] <= k && yk[i][0] > k) {
        long long int sa = ys[i][0] - ys[i][1];
        if (sa >= res)
          resn = i, res = sa;
      }
    }
    for (int i = 0; i < m; i++) {
      if (i != resn)
        swap(ax[ber[i].second], ax[ber[i].second + 1]);
    }
    for (int i = 1; i <= n; i++) {
      if (ax[i] <= k)
        ans += sc[i];
    }
    printf("%lld\\n", ans);
  }
  return 0;
}