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

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

bool **logTable;

void func(int n, int m, int h, int k) {

  int work[n + 1], loc_value[n + 1], value[n + 1], sum = 0, left, height;

  for (int i = 1; i <= n; i++)
    work[i] = i;

  for (int i = 1; i <= n; i++)
    scanf("%d", &loc_value[i]);

  for (int i = 0; i < m; i++) {
    scanf("%d %d", &left, &height);
    logTable[height][left] = true;
  }

  for (int depth = 1; depth <= h; depth++) {
    for (int col = 1; col <= n; col++) {
      if (logTable[depth][col] == true) {
        swap(work[col], work[col + 1]);
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    value[work[i]] = loc_value[i];
    if (work[i] <= k) {
      sum += loc_value[i];
    }
  }

  int div_max = -1000000, tmp_div;

  for (int i = 1; i <= n; i++)
    work[i] = i;

  for (int depth = 1; depth <= h; depth++) {
    for (int col = 1; col <= n; col++) {
      if (logTable[depth][col] == true) {
        if (work[col] <= k && work[col + 1] > k) {
          tmp_div = value[work[col]] - value[work[col + 1]];
          if (tmp_div > div_max) {
            div_max = tmp_div;
          }
        } else if (work[col] > k && work[col + 1] <= k) {
          tmp_div = value[work[col + 1]] - value[work[col]];
          if (tmp_div > div_max) {
            div_max = tmp_div;
          }
        }
        swap(work[col], work[col + 1]);
      }
    }
  }

  if (div_max > 0) {
    sum -= div_max;
  }

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

int main() {

  int n, m, h, k;

  logTable = new bool *[1001];
  for (int i = 0; i < 1001; i++) {
    logTable[i] = new bool[1001];
  }

  while (true) {
    scanf("%d %d %d %d", &n, &m, &h, &k);
    if (n == 0 && m == 0 && h == 0 && k == 0)
      break;

    for (int i = 0; i < 1001; i++) {
      for (int k = 1; k <= 1000; k++)
        logTable[i][k] = false;
    }

    func(n, m, h, k);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

bool **logTable;

void func(int n, int m, int h, int k) {

  int work[n + 1], loc_value[n + 1], value[n + 1], sum = 0, left, height;

  for (int i = 1; i <= n; i++)
    work[i] = i;

  for (int i = 1; i <= n; i++)
    scanf("%d", &loc_value[i]);

  for (int i = 0; i < m; i++) {
    scanf("%d %d", &left, &height);
    logTable[height][left] = true;
    swap(work[left], work[left + 1]);
  }

  for (int i = 1; i <= n; i++) {
    value[work[i]] = loc_value[i];
    if (work[i] <= k) {
      sum += loc_value[i];
    }
  }

  int div_max = -1000000, tmp_div;

  for (int i = 1; i <= n; i++)
    work[i] = i;

  for (int depth = 1; depth <= h; depth++) {
    for (int col = 1; col <= n; col++) {
      if (logTable[depth][col] == true) {
        if (work[col] <= k && work[col + 1] > k) {
          tmp_div = value[work[col]] - value[work[col + 1]];
          if (tmp_div > div_max) {
            div_max = tmp_div;
          }
        } else if (work[col] > k && work[col + 1] <= k) {
          tmp_div = value[work[col + 1]] - value[work[col]];
          if (tmp_div > div_max) {
            div_max = tmp_div;
          }
        }
        swap(work[col], work[col + 1]);
      }
    }
  }

  if (div_max > 0) {
    sum -= div_max;
  }

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

int main() {

  int n, m, h, k;

  logTable = new bool *[1000];
  for (int i = 0; i < 1000; i++) {
    logTable[i] = new bool[1001];
  }

  while (true) {
    scanf("%d %d %d %d", &n, &m, &h, &k);
    if (n == 0 && m == 0 && h == 0 && k == 0)
      break;

    for (int i = 0; i < 1000; i++) {
      for (int k = 1; k <= 1000; k++)
        logTable[i][k] = false;
    }

    func(n, m, h, k);
  }
  return 0;
}