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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

int main() {
  while (1) {
    static int n, m, h, k;
    static int s[1002];
    static int a[100010], b[100010];

    rep(i, 1002) s[i] = 0;
    rep(i, 100010) {
      a[i] = 0;
      b[i] = 0;
    }

    scanf("%d%d%d%d", &n, &m, &h, &k);
    if (n == 0)
      break;
    rep1(i, n) { scanf("%d", &s[i]); }
    rep(i, m) { scanf("%d%d", &a[i], &b[i]); }

    vector<P> vec;
    rep(i, m) { vec.pb(P(b[i], a[i])); }
    sor(vec);
    rep(i, m) {
      a[i] = vec[i].sc;
      b[i] = vec[i].sc + 1;
    }

    static int ln[100010], rn[100010];
    static int num[1002] = {};
    rep1(i, k) num[i] = 1;

    rep(i, 100010) {
      ln[i] = 0;
      rn[i] = 0;
    }
    rep(i, 1002) { num[i] = 0; }
    rep1(i, k) { num[i] = 1; }

    rep(i, m) {
      ln[i] = num[a[i]];
      rn[i] = num[b[i]];
      swap(num[a[i]], num[b[i]]);
    }

    static int ls[100010], rs[100010];

    rep(i, 100010) {
      ls[i] = 0;
      rs[i] = 0;
    }

    rrep(i, m) {
      swap(s[a[i]], s[b[i]]);
      ls[i] = s[a[i]];
      rs[i] = s[b[i]];
    }

    int sum = 0;
    rep1(i, k) { sum += s[i]; }

    int ret = 0;
    rep(i, m) {
      if (ln[i] > rn[i]) {
        ret = min(ret, rs[i] - ls[i]);
      }
      if (ln[i] < rn[i]) {
        ret = min(ret, ls[i] - rs[i]);
      }
    }

    printf("%d\\n", sum + ret);
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

pair<int, int> make_pair(int a, int b) {
  pair<int, int> ret;
  ret.first = a;
  ret.second = b;
  return ret;
}

bool f(pair<int, int> a, pair<int, int> b) { return a.second <= b.second; }

int main() {
  while (1) {
    int n, m, h, k, sum = 0, ret = 2000000001;
    int s[1002];
    char a[100000 + 2], b[100000 + 2];
    char t[1005], u[1005];
    vector<pair<int, int>> vp, lis;
    scanf("%d%d%d%d", &n, &m, &h, &k);
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &s[i]);
      // t[i]=i;
      // u[i]=i;
    }
    for (int i = 1; i <= m; i++) {
      scanf("%d%d", &a[i], &b[i]);
      vp.push_back(make_pair(a[i], b[i]));
    }
    sort(vp.begin(), vp.end(), f);
    /*for(int i=0;i<m;i++){
            lis.push_back(make_pair(u[vp[i].first],u[vp[i].first+1]));
            int c=t[u[vp[i].first]]; t[u[vp[i].first]]=t[u[vp[i].first+1]];
    t[u[vp[i].first+1]]=c; c=u[vp[i].first]; u[vp[i].first]=u[vp[i].first+1];
    u[vp[i].first+1]=c;
    }
    for(int i=1;i<=k;i++)sum+=s[t[i]];
    ret=sum;
    //cout<<ret<<endl;
    for(int i=0;i<m;i++){
            if(lis[i].first<=k&&lis[i].second>k)ret=min(ret,sum+s[t[lis[i].second]]-s[t[lis[i].first]]);
            if(lis[i].first>k&&lis[i].second<=k)ret=min(ret,sum-s[t[lis[i].second]]+s[t[lis[i].first]]);
    }*/
    printf("%d\\n", ret);
  }
  return 0;
}