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

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
ll a[1001];
struct B {
  ll h;      // high
  ll a;      // number(left)
  ll lb, rb; //?????£???????????????
  ll lc, rc; //?????£??????????????????
  ll lv, rv; //?????´???vector?????????????????´
};
B b[100001];
struct C {
  ll h; // high
  ll b; // number
};
vector<C> c[1001];
ll n, m, h, k;
struct U {
  ll ai, bi;
};
U u[100001];
bool operator<(U a, U b) { return a.bi < b.bi; }
int main() {
  while (cin >> n >> m >> h >> k && n + m + h + k > 0) {
    for (ll i = 1; i <= n; i++) {
      cin >> a[i];
      c[i].clear();
    }
    for (ll i = 1; i <= m; i++)
      cin >> u[i].ai >> u[i].bi;
    sort(u + 1, u + m + 1);
    for (ll i = 1; i <= m; i++) {
      ll ai = u[i].ai, bi = u[i].bi;
      ll io = (ll)c[ai].size(), oi = (ll)c[ai + 1].size();
      b[i] = B{bi, ai, -1, -1, -1, -1, io, oi};
      c[ai].push_back(C{bi, i});
      c[ai + 1].push_back(C{bi, i});
    }
    ll sum = 0;
    for (ll i = 1; i <= n; i++) {
      ll d = i;
      ll e = 0;
      while (e < c[d].size()) {
        if (b[c[d][e].b].a == d) {
          b[c[d][e].b].lb = i;
          e = b[c[d][e].b].rv;
          d++;
        } else {
          b[c[d][e].b].rb = i;
          e = b[c[d][e].b].lv;
          d--;
        }
        e++;
      }
      if (i <= k)
        sum += a[d];
    }
    for (ll i = 1; i <= n; i++) {
      ll d = i;
      ll e = c[d].size() - 1;
      while (e >= 0) {
        if (b[c[d][e].b].a == d) {
          b[c[d][e].b].lc = i;
          e = b[c[d][e].b].rv;
          d++;
        } else {
          b[c[d][e].b].rc = i;
          e = b[c[d][e].b].lv;
          d--;
        }
        e--;
      }
    }
    ll ans = sum;
    for (ll i = 1; i <= m; i++) {
      ll sum2 = sum;
      if (b[i].lb <= k) {
        sum2 -= a[b[i].rc];
        if (b[i].lv == (ll)c[b[i].a].size() - 1) {
          sum2 += a[b[i].a];
        } else {
          if (b[c[b[i].a][b[i].lv + 1].b].a == b[i].a) {
            sum2 += a[b[c[b[i].a][b[i].lv + 1].b].rc];
          } else {
            sum2 += a[b[c[b[i].a][b[i].lv + 1].b].lc];
          }
        }
      }
      if (b[i].rb <= k) {
        sum2 -= a[b[i].lc];
        if (b[i].rv == (ll)c[b[i].a + 1].size() - 1) {
          sum2 += a[b[i].a + 1];
        } else {
          if (b[c[b[i].a + 1][b[i].rv + 1].b].a == b[i].a + 1) {
            sum2 += a[b[c[b[i].a + 1][b[i].rv + 1].b].rc];
          } else {
            sum2 += a[b[c[b[i].a + 1][b[i].rv + 1].b].lc];
          }
        }
      }
      ans = min(ans, sum2);
    }
    cout << ans << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
ll a[1001];
struct B {
  ll h;      // high
  ll a;      // number(left)
  ll lb, rb; //?????£???????????????
  ll lc, rc; //?????£??????????????????
  ll lv, rv; //?????´???vector?????????????????´
};
B b[100001];
struct C {
  ll h; // high
  ll b; // number
};
vector<C> c[1001];
ll n, m, h, k;
struct U {
  ll ai, bi;
};
U u[100001];
bool operator<(U a, U b) { return a.bi < b.bi; }
int main() {
  while (cin >> n >> m >> h >> k && n + m + h + k > 0) {
    for (ll i = 1; i <= n; i++) {
      cin >> a[i];
      c[i].clear();
    }
    for (ll i = 1; i <= m; i++)
      cin >> u[i].ai >> u[i].bi;
    sort(u + 1, u + m + 1);
    for (ll i = 1; i <= m; i++) {
      ll ai = u[i].ai, bi = u[i].bi;
      ll io = (ll)c[ai].size(), oi = (ll)c[ai + 1].size();
      b[i] = B{bi, ai, -1, -1, -1, -1, io, oi};
      c[ai].push_back(C{bi, i});
      c[ai + 1].push_back(C{bi, i});
    }
    ll sum = 0;
    for (ll i = 1; i <= n; i++) {
      ll d = i;
      ll e = 0;
      while (e < c[d].size()) {
        if (b[c[d][e].b].a == d) {
          b[c[d][e].b].lb = i;
          e = b[c[d][e].b].rv;
          d++;
        } else {
          b[c[d][e].b].rb = i;
          e = b[c[d][e].b].lv;
          d--;
        }
        e++;
      }
      if (i <= k)
        sum += a[d];
    }
    for (ll i = 1; i <= m; i++) {
      ll d = i;
      ll e = c[d].size() - 1;
      while (e >= 0) {
        if (b[c[d][e].b].a == d) {
          b[c[d][e].b].lc = i;
          e = b[c[d][e].b].rv;
          d++;
        } else {
          b[c[d][e].b].rc = i;
          e = b[c[d][e].b].lv;
          d--;
        }
        e--;
      }
    }
    cout << 0 << endl;
    /*
    ll ans=sum;
    for(ll i=1;i<=m;i++){
            ll sum2=sum;
            if(b[i].lb<=k){
                    sum2-=a[b[i].rc];
                    if(b[i].lv==(ll)c[b[i].a].size()-1){
                            sum2+=a[b[i].a];
                    }
                    else{
                            if(b[c[b[i].a][b[i].lv+1].b].a==b[i].a){
                                    sum2+=a[b[c[b[i].a][b[i].lv+1].b].rc];
                            }
                            else{
                                    sum2+=a[b[c[b[i].a][b[i].lv+1].b].lc];
                            }
                    }
            }
            if(b[i].rb<=k){
                    sum2-=a[b[i].lc];
                    if(b[i].rv==(ll)c[b[i].a+1].size()-1){
                            sum2+=a[b[i].a+1];
                    }
                    else{
                            if(b[c[b[i].a+1][b[i].rv+1].b].a==b[i].a+1){
                                    sum2+=a[b[c[b[i].a+1][b[i].rv+1].b].rc];
                            }
                            else{
                                    sum2+=a[b[c[b[i].a+1][b[i].rv+1].b].lc];
                            }
                    }
            }
            ans=min(ans,sum2);
    }
    cout<<ans<<endl;*/
  }
}