Problem p03833 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sqr(x) ((x) * (x))
#define mp make_pair
#define uint unsigned
#define PI pair<int, int>
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline int read() {
  int x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  write(a);
  puts("");
}
inline int rnd(int x) { return rand() % x; }
inline ull rnd() { return ((ull)rand() << 30 ^ rand()) << 4 | rand() % 4; }
const int M = 205, N = 5005;
int n, m, g[M][N], a[M][N];
ll de[N], sum[N], ans;
inline int ggg(int id, int pos, int x) {
  if (a[id][pos] > x)
    return pos;
  return ggg(id, g[id][pos], x);
}
int main() {
  n = read();
  m = read();
  for (int i = 2; i <= n; i++)
    sum[i] = sum[i - 1] + read();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      a[j][i] = read();
  }
  for (int i = 1; i <= m; i++) {
    a[i][0] = a[i][n + 1] = 1e9;
    for (int j = n; j; j--)
      g[i][j] = ggg(i, j + 1, a[i][j]);
  }
  for (int i = n; i; i--) {
    for (int j = 1; j <= m; j++) {
      de[i + 1] -= a[j][i + 1];
      for (int k = i + 1; a[j][k] <= a[j][i]; k = g[j][k]) {
        de[g[j][k]] -= a[j][g[j][k]] - a[j][k];
      }
      de[i] += a[j][i];
      de[g[j][i]] += a[j][g[j][i]] - a[j][i];
    }
    ll dq = 0;
    for (int j = i; j <= n; j++) {
      dq += de[j];
      ans = max(ans, dq - (sum[j] - sum[i]));
      // cout<<i<<" "<<j<<" "<<dq<<endl;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sqr(x) ((x) * (x))
#define mp make_pair
#define uint unsigned
#define PI pair<int, int>
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline int read() {
  int x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  write(a);
  puts("");
}
inline int rnd(int x) { return rand() % x; }
inline ull rnd() { return ((ull)rand() << 30 ^ rand()) << 4 | rand() % 4; }
const int M = 205, N = 5005;
int n, m, g[M][N], a[M][N];
ll de[N], sum[N], ans;
inline int ggg(int id, int pos, int x) {
  if (a[id][pos] > x)
    return pos;
  return ggg(id, g[id][pos], x);
}
int main() {
  n = read();
  m = read();
  for (int i = 2; i <= n; i++)
    sum[i] = sum[i - 1] + read();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      a[j][i] = read();
  }
  for (int i = 1; i <= m; i++) {
    a[i][0] = a[i][n + 1] = 2e9;
    for (int j = n; j; j--)
      g[i][j] = ggg(i, j + 1, a[i][j]);
  }
  for (int i = n; i; i--) {
    for (int j = 1; j <= m; j++) {
      de[i + 1] -= a[j][i + 1];
      for (int k = i + 1; a[j][k] <= a[j][i]; k = g[j][k]) {
        de[g[j][k]] -= a[j][g[j][k]] - a[j][k];
      }
      de[i] += a[j][i];
      de[g[j][i]] += a[j][g[j][i]] - a[j][i];
    }
    ll dq = 0;
    for (int j = i; j <= n; j++) {
      dq += de[j];
      ans = max(ans, dq - (sum[j] - sum[i]));
      // cout<<i<<" "<<j<<" "<<dq<<endl;
    }
  }
  cout << ans << endl;
}