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

#include <bits/stdc++.h>
using namespace std;
// #define int long long
struct POINT {
  int a, b;
};

int id[1000][1000];
int sc[1000][1000];
int g[1000][1000];
int rev[1000][1000];
vector<POINT> bar;

signed main() {
  int n, m, h, k;
  while (scanf("%d %d %d %d", &n, &m, &h, &k), n) {
    memset(id, 0, sizeof(id));
    memset(sc, 0, sizeof(sc));
    memset(g, 0, sizeof(g));
    memset(rev, 0, sizeof(rev));
    bar.clear();

    h--;
    for (int i = 0; i < n; i++) {
      id[i][0] = i;
      scanf("%d", &sc[i][h]);
    }
    for (int i = 0; i < m; i++) {
      int a, b;
      scanf("%d %d", &a, &b);
      a--;
      b--;
      g[a][b] = 1;
      g[a + 1][b] = -1;
      rev[a][b + 1] = 1;
      rev[a + 1][b + 1] = -1;
      bar.push_back((POINT){a, b});
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < n; j++) {
        id[j + g[j][i]][i + 1] = id[j][i];
      }
    }
    for (int i = h; i > 0; i--) {
      for (int j = 0; j < n; j++) {
        sc[j + rev[j][i]][i - 1] = sc[j][i];
      }
    }

    int sum = 0;
    for (int i = 0; i < k; i++)
      sum += sc[i][0];
    int res = sum;
    for (int i = 0; i < bar.size(); i++) {
      int tmp = sum;
      // printf("%d %d\\n", id[bar[i].a][bar[i].b], id[bar[i].a + 1][bar[i].b]);
      if (id[bar[i].a][bar[i].b] < k && id[bar[i].a + 1][bar[i].b] >= k) {
        tmp -= sc[bar[i].a][bar[i].b];
        tmp += sc[bar[i].a + 1][bar[i].b];
        res = min(res, tmp);
      } else if (id[bar[i].a][bar[i].b] >= k &&
                 id[bar[i].a + 1][bar[i].b] < k) {
        tmp += sc[bar[i].a][bar[i].b];
        tmp -= sc[bar[i].a + 1][bar[i].b];
        res = min(res, tmp);
      }
    }
    printf("%d\\n", res);
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct POINT {
  int a, b;
};

int id[1000][1000];
int sc[1000][1000];
int g[1000][1000];
int rev[1000][1000];
vector<POINT> bar;

signed main() {
  int n, m, h, k;
  while (scanf("%d %d %d %d", &n, &m, &h, &k), n) {
    memset(id, 0, sizeof(0));
    memset(sc, 0, sizeof(0));
    memset(g, 0, sizeof(0));
    memset(rev, 0, sizeof(0));
    bar.clear();

    h--;
    for (int i = 0; i < n; i++) {
      id[i][0] = i;
      scanf("%d", &sc[i][h]);
    }
    for (int i = 0; i < m; i++) {
      int a, b;
      scanf("%d %d", &a, &b);
      a--;
      b--;
      g[a][b] = 1;
      g[a + 1][b] = -1;
      rev[a][b + 1] = 1;
      rev[a + 1][b + 1] = -1;
      bar.push_back((POINT){a, b});
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < n; j++) {
        id[j + g[j][i]][i + 1] = id[j][i];
      }
    }
    for (int i = h; i > 0; i--) {
      for (int j = 0; j < n; j++) {
        sc[j + rev[j][i]][i - 1] = sc[j][i];
      }
    }

    int sum = 0;
    for (int i = 0; i < k; i++)
      sum += sc[i][0];
    int res = sum;
    for (int i = 0; i < bar.size(); i++) {
      int tmp = sum;
      // printf("%d %d\\n", id[bar[i].a][bar[i].b], id[bar[i].a + 1][bar[i].b]);
      if (id[bar[i].a][bar[i].b] < k && id[bar[i].a + 1][bar[i].b] >= k) {
        tmp -= sc[bar[i].a][bar[i].b];
        tmp += sc[bar[i].a + 1][bar[i].b];
        res = min(res, tmp);
      } else if (id[bar[i].a][bar[i].b] >= k &&
                 id[bar[i].a + 1][bar[i].b] < k) {
        tmp += sc[bar[i].a][bar[i].b];
        tmp -= sc[bar[i].a + 1][bar[i].b];
        res = min(res, tmp);
      }
    }
    printf("%lld\\n", res);
  }

  return 0;
}