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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e10, EPS = 1e-9;

int n, m, h, k, pt[1000], tmp[1000];
pi bar[100000];
int ln[100000], ls[100000], rn[100000], rs[100000];

int main() {
  while (scanf("%d%d%d%d", &n, &m, &h, &k), n) {
    rep(i, n) scanf("%d", pt + i);
    rep(i, m) scanf("%d%d", &bar[i].second, &bar[i].first);
    sort(bar, bar + m);

    rep(i, n) tmp[i] = i;
    rep(i, m) {
      int a = bar[i].second - 1;
      ln[i] = tmp[a];
      rn[i] = tmp[a + 1];
      swap(tmp[a], tmp[a + 1]);
    }
    int tot = 0;
    rep(i, n) tmp[i] = i;
    for (int i = m - 1; i >= 0; i--) {
      int a = bar[i].second - 1;
      ls[i] = pt[tmp[a]];
      rs[i] = pt[tmp[a + 1]];
      swap(tmp[a], tmp[a + 1]);
    }
    rep(i, k) tot += pt[tmp[i]];

    int ans = 0;
    rep(i, m) if ((ln[i] < k) ^ (rn[i] < k)) {
      ans = min(ans, (ls[i] - rs[i]) * (ln[i] < k ? 1 : -1));
    }
    printf("%d\\n", ans + tot);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e10, EPS = 1e-9;

int n, m, h, k, pt[1000];
pi bar[10000];
int ln[10000], ls[10000], rn[10000], rs[10000];

int main() {
  while (scanf("%d%d%d%d", &n, &m, &h, &k), n) {
    rep(i, n) scanf("%d", pt + i);
    rep(i, m) scanf("%d%d", &bar[i].second, &bar[i].first);
    sort(bar, bar + m);

    int tot = 0, tmp[1000];
    rep(i, n) tmp[i] = i;
    rep(i, m) {
      int a = bar[i].second - 1;
      ln[i] = tmp[a];
      rn[i] = tmp[a + 1];
      swap(tmp[a], tmp[a + 1]);
    }
    rep(i, n) tmp[i] = i;
    for (int i = m - 1; i >= 0; i--) {
      int a = bar[i].second - 1;
      ls[i] = pt[tmp[a]];
      rs[i] = pt[tmp[a + 1]];
      swap(tmp[a], tmp[a + 1]);
    }
    rep(i, k) tot += pt[tmp[i]];

    int ans = 0;
    rep(i, m) if ((ln[i] < k) ^ (rn[i] < k)) {
      ans = min(ans, (ls[i] - rs[i]) * (ln[i] < k ? 1 : -1));
    }
    printf("%d\\n", ans + tot);
  }
  return 0;
}