Problem p03833 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#pragma GCC target("avx")
// #include<bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

// 解説ガン見

const int cm = 1 << 17;
char cn[cm], *ci = cn + cm, ct;
inline char getcha() {
  if (ci - cn == cm) {
    fread_unlocked(cn, 1, cm, stdin);
    ci = cn;
  }
  return *ci++;
}
inline int getint() {
  int A = 0;
  if (ci - cn + 16 > cm)
    while ((ct = getcha()) >= '0')
      A = A * 10 + ct - '0';
  else
    while ((ct = *ci++) >= '0')
      A = A * 10 + ct - '0';
  return A;
}

ll A[5001], B[200][5001], C[5001][5001];
int daishou[5002];
int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  int N = getint(), M = getint();
  rep1(i, N - 1) A[i + 1] = getint() + A[i];
  rep1(i, N) rep(j, M) B[j][i] = ((ll)getint() << 20) + i;

  const int m = (1 << 20) - 1;
  rep(i, M) {
    memset(daishou, 0, sizeof(daishou));
    sort(B[i] + 1, B[i] + N + 1);
    rep1(j, N) {
      int x = B[i][j] & m;
      int b = B[i][j] >> 20;

      daishou[x] = x;
      int shou = daishou[x - 1];
      if (!shou)
        shou = x;
      int dai = daishou[x + 1];
      if (!dai)
        dai = x;

      C[shou][dai] += b;
      C[x + 1][dai] -= b;
      C[shou][x - 1] -= b;
      daishou[dai] = shou;
      daishou[shou] = dai;
    }
  }

  rep1(i, N) for (int j = N; j > i; j--) { C[i][j - 1] += C[i][j]; }
  for (int j = N; j > 0; j--)
    rep1(i, j - 1) { C[i + 1][j] += C[i][j]; }

  ll kotae = 0;
  rep1(i, N) for (int j = N; j >= i; j--) {
    ll kari = A[i] - A[j] + C[i][j];
    if (kotae < kari)
      kotae = kari;
  }
  printf("%lld", kotae);

  Would you please return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC target("avx")
// #include<bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

// 解説ガン見

const int cm = 1 << 17;
char cn[cm], *ci = cn + cm, ct;
inline char getcha() {
  if (ci - cn == cm) {
    fread_unlocked(cn, 1, cm, stdin);
    ci = cn;
  }
  return *ci++;
}
inline int getint() {
  int A = 0;
  if (ci - cn + 16 > cm)
    while ((ct = getcha()) >= '0')
      A = A * 10 + ct - '0';
  else
    while ((ct = *ci++) >= '0')
      A = A * 10 + ct - '0';
  return A;
}

ll A[5001], B[200][5001], C[5002][5002];
int daishou[5002];
int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  int N = getint(), M = getint();
  rep1(i, N - 1) A[i + 1] = getint() + A[i];
  rep1(i, N) rep(j, M) B[j][i] = ((ll)getint() << 20) + i;

  const int m = (1 << 20) - 1;
  rep(i, M) {
    memset(daishou, 0, sizeof(daishou));
    sort(B[i] + 1, B[i] + N + 1);
    rep1(j, N) {
      int x = B[i][j] & m;
      int b = B[i][j] >> 20;

      daishou[x] = x;
      int shou = daishou[x - 1];
      if (!shou)
        shou = x;
      int dai = daishou[x + 1];
      if (!dai)
        dai = x;

      C[shou][dai] += b;
      C[x + 1][dai] -= b;
      C[shou][x - 1] -= b;
      daishou[dai] = shou;
      daishou[shou] = dai;
    }
  }

  rep1(i, N) for (int j = N; j > i; j--) { C[i][j - 1] += C[i][j]; }
  for (int j = N; j > 0; j--)
    rep1(i, j - 1) { C[i + 1][j] += C[i][j]; }

  ll kotae = 0;
  rep1(i, N) for (int j = N; j >= i; j--) {
    ll kari = A[i] - A[j] + C[i][j];
    if (kotae < kari)
      kotae = kari;
  }
  printf("%lld", kotae);

  Would you please return 0;
}