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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
int N;
int f[305][305], imos[305][305];
int l[305], r[305];
int sum(int i, int j, int k) {
  return imos[j][k] - imos[j][k - 1] - imos[i - 1][k] + imos[i - 1][k - 1];
}
int add(int i, int j, int k) {
  if (i == j)
    return f[i][k];
  return f[i][k] + f[j][k];
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      scanf("%d", &f[i][j]);
      imos[i][j] = f[i][j];
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      imos[i][j + 1] += imos[i][j];
    }
  }
  for (int j = 1; j <= N; j++) {
    for (int i = 1; i <= N; i++) {
      imos[i + 1][j] += imos[i][j];
    }
  }
  int ans = -INF;
  for (int i = 1; i <= N; i++) {
    for (int j = i; j <= N; j++) {
      l[1] = sum(i, j, 1);
      for (int k = 1; k <= N; k++)
        ans = max(ans, sum(i, j, k));
      for (int k = 2; k <= N; k++)
        l[k] = max(l[k - 1] + add(i, j, k), sum(i, j, k));
      r[N] = sum(i, j, N);
      for (int k = N - 1; k >= 1; k--)
        r[k] = max(r[k + 1] + add(i, j, k), sum(i, j, k));
      for (int k = 1; k < N; k++)
        ans = max(ans, l[k] + r[k + 1]);
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
int N;
int f[305][305], imos[305][305];
int l[305], r[305];
int sum(int i, int j, int k) {
  return imos[j][k] - imos[j][k - 1] - imos[i - 1][k] + imos[i - 1][k - 1];
}
int add(int i, int j, int k) {
  if (i == j)
    return f[i][k];
  return f[i][k] + f[j][k];
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      scanf("%d", &f[i][j]);
      imos[i][j] = f[i][j];
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      imos[i][j + 1] += imos[i][j];
    }
  }
  for (int j = 1; j <= N; j++) {
    for (int i = 1; i <= N; i++) {
      imos[i + 1][j] += imos[i][j];
    }
  }
  while (1) {
    int x, y, z;
    cin >> x >> y >> z;
    cout << sum(x, y, z) << ' ' << add(x, y, z) << endl;
  }
  int ans = -INF;
  for (int i = 1; i <= N; i++) {
    for (int j = i; j <= N; j++) {
      l[1] = sum(i, j, 1);
      for (int k = 1; k <= N; k++)
        ans = max(ans, sum(i, j, k));
      for (int k = 2; k <= N; k++)
        l[k] = max(l[k - 1] + add(i, j, k), sum(i, j, k));
      r[N] = sum(i, j, N);
      for (int k = N - 1; k >= 1; k--)
        r[k] = max(r[k + 1] + add(i, j, k), sum(i, j, k));
      for (int k = 1; k < N; k++)
        ans = max(ans, l[k] + r[k + 1]);
    }
  }
  printf("%d\\n", ans);
  return 0;
}