Problem p03833 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
ll N, M;
ll A[5005];
ll B[5005][300];
ll Point[5005];
ll field[5005][5005];
int main() {
  // cout.precision(10);
  cin >> N >> M;
  for (int i = 2; i <= N; i++) {
    cin >> A[i];
    Point[i] = Point[i - 1] + A[i];
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      cin >> B[i][j];
      B[0][j] = INF;
      B[N + 1][j] = INF;
    }
  }
  for (int ticket = 1; ticket <= M; ticket++) {
    for (int shop = 1; shop <= N; shop++) {
      int before = shop - 1;
      int after = shop + 1;
      while (B[before][ticket] <= B[shop][ticket])
        before--;
      before++;
      while (B[after][ticket] < B[shop][ticket])
        after++;
      // cout << ticket << " " << shop << " " << before << " " << after << endl;
      ll val = B[shop][ticket];
      field[before][shop] += val;
      field[before][after] -= val;
      field[shop + 1][shop] -= val;
      field[shop + 1][after] += val;
    }
  }
  /*
  cout << endl;
  for(int i = 1; i <= N; i++) {
      for(int j = 1; j <= N; j++) {
          cout << field[i][j] << " ";
      }
      cout << endl;
  }
  cout << endl;
  */
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      field[i][j] += field[i][j - 1];
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      field[i][j] += field[i - 1][j];
    }
  }
  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = i; j <= N; j++) {
      ll now = field[i][j] - (Point[j] - Point[i]);
      ans = max(ans, now);
    }
  }
  cout << ans << endl;
  /*
  for(int i = 1; i <= N; i++) {
      for(int j = 1; j <= N; j++) {
          cout << field[i][j] << " ";
      }
      cout << endl;
  }
  */
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define EPS (1e-7)
#define INF (1e9 + 5)
#define PI (acos(-1))
// const ll mod = 1000000007;
ll N, M;
ll A[5005];
ll B[5005][300];
ll Point[5005];
ll field[5005][5005];
int main() {
  // cout.precision(10);
  cin >> N >> M;
  for (int i = 2; i <= N; i++) {
    cin >> A[i];
    Point[i] = Point[i - 1] + A[i];
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      cin >> B[i][j];
      B[0][j] = INF;
      B[N + 1][j] = INF;
    }
  }
  for (int ticket = 1; ticket <= M; ticket++) {
    for (int shop = 1; shop <= N; shop++) {
      int before = shop - 1;
      int after = shop + 1;
      while (B[before][ticket] <= B[shop][ticket])
        before--;
      before++;
      while (B[after][ticket] < B[shop][ticket])
        after++;
      // cout << ticket << " " << shop << " " << before << " " << after << endl;
      ll val = B[shop][ticket];
      field[before][shop] += val;
      field[before][after] -= val;
      field[shop + 1][shop] -= val;
      field[shop + 1][after] += val;
    }
  }
  /*
  cout << endl;
  for(int i = 1; i <= N; i++) {
      for(int j = 1; j <= N; j++) {
          cout << field[i][j] << " ";
      }
      cout << endl;
  }
  cout << endl;
  */
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      field[i][j] += field[i][j - 1];
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      field[i][j] += field[i - 1][j];
    }
  }
  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = i; j <= N; j++) {
      ll now = field[i][j] - (Point[j] - Point[i]);
      ans = max(ans, now);
    }
  }
  cout << ans << endl;
  /*
  for(int i = 1; i <= N; i++) {
      for(int j = 1; j <= N; j++) {
          cout << field[i][j] << " ";
      }
      cout << endl;
  }
  */
  return 0;
}