alexjercan/bugnet huggingface dataset:
Problem p00097 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int dp[11][1001] = {};
  dp[0][0] = 1;
  for (int i = 0; i <= 100; i++) {
    for (int j = 8; j >= 0; j--) {
      for (int k = 0; k < 1000; k++) {
        if (k + i > 1000)
          continue;
        dp[j + 1][k + i] += dp[j][k];
      }
    }
  }
  int n, s;
  while (1) {
    cin >> n >> s;
    if (!n && !s)
      break;
    cout << dp[n][s] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int dp[11][1001];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i <= 100; i++) {
    for (int j = 8; j >= 0; j--) {
      for (int k = 0; k < 1000; k++) {
        if (k + i > 1000)
          continue;
        dp[j + 1][k + i] += dp[j][k];
      }
    }
  }
  int n, s;
  while (1) {
    cin >> n >> s;
    cout << dp[n][s] << endl;
  }
}
Problem p00097 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;
int n, s, memo[9][102][1001];

int rec(int c = 0, int pre = -1, int sum = 0) {
  if (c == n) {
    return sum == s;
  }
  if (sum > s) {
    return 0;
  }
  if (memo[c][pre + 1][sum] != -1) {
    return memo[c][pre + 1][sum];
  }
  int res = 0;
  range(i, pre + 1, 101) { res += rec(c + 1, i, sum + i); }
  return memo[c][pre + 1][sum] = res;
}

signed main() {
  while (cin >> n >> s, n) {
    memset(memo, -1, sizeof(memo));
    cout << rec() << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;
int n, s, memo[9][1001];

int rec(int c = 0, int pre = -1, int sum = 0) {
  if (c == n) {
    return sum == s;
  }
  if (sum > s) {
    return 0;
  }
  int res = 0;
  range(i, pre + 1, 101) { res += rec(c + 1, i, sum + i); }
  return memo[c][sum] = res;
}

int main() {
  while (cin >> n >> s, n) {
    memset(memo, -1, sizeof(memo));
    cout << rec() << endl;
  }
  return 0;
}
Problem p00098 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {

  int n;
  int a[101][101] = {0};
  int sum[101][101] = {0};
  int cal, ans;

  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      sum[i][j] = a[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
    }
  }

  ans = sum[1][1];

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = i; k <= n; k++) {
        for (int l = j; l <= n; l++) {
          cal = sum[k][l] - sum[i - 1][l] - sum[k][j - 1] + sum[i - 1][j - 1];
          if (cal > ans)
            ans = cal;
        }
      }
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

#define MAX 100

int main(void) {

  int n;
  int sq[MAX][MAX];
  int sumsq[MAX][MAX];
  int sum;
  int max = -10000 * 100 - 10000;

  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> sq[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    sumsq[i][0] = sq[i][0];
    for (int j = 1; j < n; j++) {
      sumsq[i][j] = sumsq[i][j - 1] + sq[i][j];
    }
  }

  for (int i = 0; i < n; i++) {        /*x座標*/
    for (int j = 0; j < n; j++) {      /*y座標*/
      for (int k = i; k < n; k++) {    /*dy*/
        for (int l = 0; l <= j; l++) { /*-dx*/
          sum = 0;
          for (int m = i; m <= k; m++) { /*回数*/
            if (l == j)
              sum += sumsq[m][j];
            else
              sum += sumsq[m][j] - sumsq[m][l];
          }
          /*cout<<sumsq[i][j]<<","<<i<<","<<j<<","<<k<<","<<l<<","<<sum<<","<<max<<endl;*/
          if (max < sum)
            max = sum;
        }
      }
    }
  }

  cout << max << endl;

  return 0;
}
Problem p00098 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>
#include <vector>

using namespace std;

int sumMat(vector<vector<int>> &table, int x, int y, int w, int h) {
  int lt = x == 0 || y == 0 ? 0 : table[x - 1][y - 1];
  int lb = x == 0 ? 0 : table[x - 1][y - 1 + h];
  int rt = y == 0 ? 0 : table[x - 1 + w][y - 1];
  int rb = table[x - 1 + w][y - 1 + h];
  return rb - rt - lb + lt;
}

int main(void) {
  int n;
  vector<vector<int>> mat;
  vector<vector<int>> table;

  cin >> n;
  for (int i = 0; i < n; i++) {
    vector<int> row;
    for (int j = 0; j < n; j++) {
      int inp;
      cin >> inp;
      row.push_back(inp);
    }
    mat.push_back(row);
  }

  table = mat;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      table[i][j + 1] += table[i][j];
    }
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < n; j++) {
      table[i + 1][j] += table[i][j];
    }
  }

  int maxSum = INT_MIN;
  for (int x = 0; x < n; x++) {
    for (int y = 0; y < n; y++) {
      for (int w = 1; w + x <= n; w++) {
        for (int h = 1; h + y <= n; h++) {
          maxSum = max(maxSum, sumMat(table, x, y, w, h));
        }
      }
    }
  }

  cout << maxSum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
#include <vector>

using namespace std;

int sumMat(vector<vector<int>> mat, int x, int y, int w, int h) {
  int sum = 0;
  for (int i = x; i < x + w; i++) {
    for (int j = y; j < y + h; j++) {
      sum += mat[i][j];
    }
  }
  return sum;
}

int main(void) {
  int n;
  vector<vector<int>> mat;

  cin >> n;
  for (int i = 0; i < n; i++) {
    vector<int> row;
    for (int j = 0; j < n; j++) {
      int inp;
      cin >> inp;
      row.push_back(inp);
    }
    mat.push_back(row);
  }

  int maxSum = INT_MIN;
  for (int x = 0; x < n; x++) {
    for (int y = 0; y < n; y++) {
      for (int w = 1; w + x <= n; w++) {
        for (int h = 1; h + y <= n; h++) {
          maxSum = max(maxSum, sumMat(mat, x, y, w, h));
        }
      }
    }
  }

  cout << maxSum << endl;

  return 0;
}
Problem p00098 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <cstdio>
using namespace std;

int main() {
  int array[200][200];
  int n;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &array[i][j]);

      if (i > 0)
        array[i][j] += array[i - 1][j];
      if (j > 0)
        array[i][j] += array[i][j - 1];
      if (i > 0 && j > 0)
        array[i][j] -= array[i - 1][j - 1];
    }
  }

  long Max = LONG_MIN;
  for (int si = 0; si < n; si++) {
    for (int sj = 0; sj < n; sj++) {
      for (int ei = si; ei < n; ei++) {
        for (int ej = sj; ej < n; ej++) {
          int sum = array[ei][ej];
          //					sum = array[ei][ej] -= array[si -
          //1][ej]
          //					-= array[ei][sj - 1] += array[si - 1][sj -
          //1];

          if (si > 0)
            sum -= array[si - 1][ej];
          if (sj > 0)
            sum -= array[ei][sj - 1];
          if (si > 0 && sj > 0)
            sum += array[si - 1][sj - 1];

          if (Max < sum) {
            Max = sum;
          }
        }
      }
    }
  }

  printf("%d\\n", Max);

  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <cstdio>
using namespace std;

int main() {
  int array[200][200];
  int n;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &array[i][j]);
    }
  }

  long Max = LONG_MIN;
  for (int si = 0; si < n; si++) {
    for (int sj = 0; sj < n; sj++) {
      for (int ei = si; ei < n; ei++) {
        for (int ej = sj; ej < n; ej++) {
          int sum = 0;
          for (int i = si; i <= ei; i++) {
            for (int j = sj; j <= ej; j++) {
              sum += array[i][j];
            }
          }

          if (Max < sum) {
            Max = sum;
          }
        }
      }
    }
  }

  printf("%d\\n", Max);

  return 0;
}
Problem p00098 (C++) — buggy submission, verdict: Time Limit Exceeded

// 0098.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define roop(a, t) for (long long a = 0; a < t; a++)
// #define startfor(a,b,t) for(long long a=b;a<t;a++)
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  ll sum[105][105];
  ll ans = -9223372036854775;
  cin >> n;
  roop(i, n) {
    roop(j, n) {
      cin >> sum[i][j];
      if (j > 0) {
        sum[i][j] += sum[i][j - 1];
      }
      ans = max(ans, sum[i][j]);
    }
  }
  for (ll j = 0; j < n; j++) {
    for (ll i = 0; i < n; i++) {
      if (i > 0) {
        sum[i][j] += sum[i - 1][j];
      }
      ans = max(ans, sum[i][j]);
    }
  }

  for (ll ix = -1; ix < n - 1; ix++) {
    for (ll jx = ix + 1; jx < n; jx++) {
      for (ll iy = -1; iy < n - 1; iy++) {
        if (ix == iy && ix == -1) {
          continue;
        }
        for (ll jy = iy + 1; jy < n; jy++) {
          ll jxjy = sum[jx][jy];
          ll jxiy = sum[jx][iy];
          ll ixjy = sum[ix][jy];
          ll ixiy = sum[ix][iy];
          if (ix == -1) {
            ixiy = 0;
            ixjy = 0;
          }
          if (iy == -1) {
            jxiy = 0;
            ixiy = 0;
          }

          ll s = jxjy - ixjy - jxiy + ixiy;
          ans = max(ans, s);
        }
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

// 0098.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  ll a[105][105];
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }
  ll ans = -9223372036854775;
  for (int ix = 0; ix < n; ix++) {
    for (int jx = ix; jx < n; jx++) {
      for (int iy = 0; iy < n; iy++) {
        for (int jy = iy; jy < n; jy++) {
          ll mem = 0;
          for (int i = ix; i <= jx; i++) {
            for (int j = iy; j <= jy; j++) {
              mem += a[i][j];
            }
          }
          ans = max(mem, ans);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00098 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n, a[110][110], rs[110][110], sum[110][110];
  cin >> n;
  rep(i, n) rep(j, n) cin >> a[i][j];

  rep(i, n) rs[0][i] = 0;
  rep(i, n) {
    rs[i + 1][0] = 0;
    rep(j, n) { rs[i + 1][j + 1] = rs[i + 1][j] + a[i][j]; }
  }

  rep(i, n + 1) {
    sum[0][i] = 0;
    rep(j, n) { sum[j + 1][i] = sum[j][i] + rs[j + 1][i]; }
  }

  int ans = -1000000000;
  rep(i, n) rep(j, n) rep(k, n + 1) rep(l, n + 1) {
    if (i < k && j < l) {
      ans = std::max(ans, sum[k][l] - sum[k][j] - sum[i][l] + sum[i][j]);
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n, a[100][100], rs[100][100], sum[100][100];
  cin >> n;
  rep(i, n) rep(j, n) cin >> a[i][j];

  rep(i, n) rs[0][i] = 0;
  rep(i, n) {
    rs[i + 1][0] = 0;
    rep(j, n) { rs[i + 1][j + 1] = rs[i + 1][j] + a[i][j]; }
  }

  rep(i, n + 1) {
    sum[0][i] = 0;
    rep(j, n) { sum[j + 1][i] = sum[j][i] + rs[j + 1][i]; }
  }

  int ans = -1000000000;
  rep(i, n) rep(j, n) rep(k, n + 1) rep(l, n + 1) {
    if (i < k && j < l) {
      ans = std::max(ans, sum[k][l] - sum[k][j] - sum[i][l] + sum[i][j]);
    }
  }
  cout << ans << endl;
}
Problem p00098 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define rep(i, N) for (i = 1; i <= N; i++)
using namespace std;

int n;
int data[102][102];
int rui[102][102]; // rui[i][j] = (1,1)を左上とするi*j行列の要素和

int main() {
  int i, j, k, l;
  cin >> n;
  rep(i, n) {
    rep(j, n) { cin >> data[i][j]; }
  }

  // 左→右
  rep(i, n) {
    rep(j, n) { rui[i][j] = rui[i][j - 1] + data[i][j]; }
  }
  // 上→下
  rep(j, n) {
    rep(i, n) { rui[i][j] = rui[i - 1][j] + rui[i][j]; }
  }

  int ans = -1145141919;
  rep(i, n) {
    rep(j, n) {
      for (k = i; k <= n; k++) {
        for (l = j; l <= n; l++) {
          // 左上(i,j), 右下(k,l)とする行列の要素和を求める
          int mats =
              rui[k][l] - rui[k][j - 1] - rui[i - 1][l] + rui[i - 1][j - 1];
          ans = max(ans, mats);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

/*Σ(k=a, k=b)A[k]を知りたい！
愚直に計算O(b-a)

先に
Σ(k=1, k=n)A[k] = rui[n]としておく.
すると、
Σ(k=a, k=b)A[k] = rui[b] - rui[a];
で計算できる。
O(1)
*/

Error: TLE

Fixed submission:

#include <iostream>
#define rep(i, N) for (i = 1; i <= N; i++)
using namespace std;

int n;
int data[102][102];
int rui[102][102]; // rui[i][j] = (1,1)を左上とするi*j行列の要素和

int main() {
  int i, j, k, l;
  cin >> n;
  rep(i, n) {
    rep(j, n) { cin >> data[i][j]; }
  }

  // 左→右
  rep(i, n) {
    rep(j, n) { rui[i][j] = rui[i][j - 1] + data[i][j]; }
  }
  // 上→下
  rep(j, n) {
    rep(i, n) { rui[i][j] = rui[i - 1][j] + rui[i][j]; }
  }

  int ans = -1145141919;
  rep(i, n) {
    rep(j, n) {
      for (k = i; k < n; k++) {
        for (l = j; k < n; l++) {
          // 左上(i,j), 右下(k,l)とする行列の要素和を求める
          int mats =
              rui[k][l] - rui[k][j - 1] - rui[i - 1][l] + rui[i - 1][j - 1];
          ans = max(ans, mats);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

/*Σ(k=a, k=b)A[k]を知りたい！
愚直に計算O(b-a)

先に
Σ(k=1, k=n)A[k] = rui[n]としておく.
すると、
Σ(k=a, k=b)A[k] = rui[b] - rui[a];
で計算できる。
O(1)
*/
Problem p00098 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

int n, a[100][100], sum[101][101];

int main() {
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &a[i][j]);
    }
  }

  sum[1][1] = a[0][0];

  for (int i = 2; i <= n; i++) {
    sum[1][i] = sum[1][i - 1] + a[0][i - 1];
    sum[i][1] = sum[i - 1][1] + a[i - 1][0];
  }

  for (int i = 2; i <= n; i++) {
    for (int j = 2; j <= n; j++) {
      sum[i][j] =
          sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i - 1][j - 1];
    }
  }

  int ret = -1000000001;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = i; k <= n; k++) {
        for (int l = j; l <= n; l++) {
          ret = max(ret, sum[k][l] + sum[i - 1][j - 1] - sum[i - 1][l] -
                             sum[k][j - 1]);
        }
      }
    }
  }

  printf("%d\\n", ret);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

int n, a[100][100], sum[100][100];

int main() {
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; i++) {
      scanf("%d", &a[i][j]);
    }
  }

  sum[0][0] = a[0][0];

  for (int i = 1; i < n; i++) {
    sum[0][i] = sum[0][i - 1] + a[0][i];
    sum[i][0] = sum[i - 1][0] + a[i][0];
  }

  for (int i = 1; i < n; i++) {
    for (int j = 1; j < n; j++) {
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
    }
  }

  int ret = -100000001;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = i + 1; k <= n; k++) {
        for (int l = j + 1; l <= n; l++) {
          ret = max(ret, sum[k - 1][l - 1] + sum[i][j] - sum[i][l - 1] -
                             sum[k - 1][j]);
        }
      }
    }
  }

  printf("%d\\n", ret);

  return 0;
}
Problem p00098 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

#define reep(i, n, m) for (int i = (n); i < (m); i++)
#define rep(i, n) reep(i, 0, n)

typedef long long int ll;

int main() {
  int n;
  cin >> n;
  int a[100][100] = {0};
  rep(i, n) {
    rep(k, n) { scanf("%d", &a[k][i]); }
  }

  ll sum[100][100] = {0};
  rep(i, n) {
    sum[0][i] = a[0][i];
    reep(k, 1, n) { sum[k][i] += sum[k - 1][i] + a[k][i]; }
  }

  rep(i, n) {
    reep(k, 1, n) { sum[i][k] += sum[i][k - 1]; }
  }

  /*rep(i,n){
      rep(k,n){
          printf("%3d",sum[k][i]);
      }
      puts("");
  }*/

  ll ans = -10000000000000;
  rep(lx, n) {
    rep(ty, n) {
      reep(rx, lx, n) {
        reep(uy, ty, n) {
          ll cou;
          if (rx == lx) {
            if (ty == uy) {
              cou = sum[rx][uy];
            } else {
              cou = sum[rx][uy] - sum[rx][ty];
            }
          } else {
            if (ty == uy) {
              cou = sum[rx][uy] - sum[lx][uy];
            } else {
              cou = (sum[rx][uy] - sum[rx][ty]) - (sum[lx][uy] - sum[lx][ty]);
            }
          }
          ans = max(cou, ans);
        }
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

#define reep(i, n, m) for (int i = (n); i < (m); i++)
#define rep(i, n) reep(i, 0, n)

typedef long long int ll;

int main() {
  int n;
  cin >> n;
  int a[100][100] = {0};
  rep(i, n) {
    rep(k, n) { scanf("%d", &a[k][i]); }
  }

  ll width[100][100] = {0};
  rep(i, n) {
    width[0][i] = a[0][i];
    reep(k, 1, n) { width[k][i] += width[k - 1][i] + a[k][i]; }
  }

  ll ans = -1 << 18;
  rep(lx, n) {
    rep(ty, n) {
      reep(rx, lx, n) {
        reep(uy, ty, n) {
          ll cou = 0;
          reep(i, ty, uy + 1) {
            if (lx == 0) {
              cou += width[rx][i];
            } else {
              cou += width[rx][i] - width[lx - 1][i];
            }
          }
          // if(ans<cou)
          //     printf("%d->%d %d->%d\\n",lx,rx, ty,uy);
          ans = max(cou, ans);
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p00098 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int ans = -1000000000;
    int memo[101][101] = {0};
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        cin >> memo[i][j];
      }
    }
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        memo[i][j] += memo[i][j - 1];
      }
    }
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        memo[i][j] += memo[i - 1][j];
      }
    }
    for (int by = 1; by <= n; ++by) {
      for (int ey = by; ey <= n; ++ey) {
        for (int bx = 1; bx <= n; ++bx) {
          for (int ex = bx; ex <= n; ++ex) {
            int a = memo[ey][ex];
            int b = memo[by - 1][ex];
            int c = memo[ey][bx - 1];
            int d = memo[by - 1][bx - 1];
            ans = max(ans, a - b - c + d);
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int ans = -1000000000;
    vector<vector<vector<vector<int>>>> memo(
        n + 1, vector<vector<vector<int>>>(
                   n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, 0))));
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        cin >> memo[i][j][i][j];
      }
    }
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        memo[i][1][i][j] = memo[i][1][i][j - 1] + memo[i][j][i][j];
        memo[1][1][i][j] = memo[1][1][i - 1][j] + memo[i][1][i][j];
      }
    }
    for (int by = 1; by <= n; ++by) {
      for (int ey = by; ey <= n; ++ey) {
        for (int bx = 1; bx <= n; ++bx) {
          for (int ex = bx; ex <= n; ++ex) {
            int a = memo[1][1][ey][ex];
            int b = memo[1][1][by - 1][ex];
            int c = memo[1][1][ey][bx - 1];
            int d = memo[1][1][by - 1][bx - 1];
            memo[by][bx][ey][ex] = a - b - c + d;
            ans = max(ans, memo[by][bx][ey][ex]);
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00098 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

typedef long long ll;

const int MAX_N = 102;
ll a[MAX_N][MAX_N];
ll sum[MAX_N][MAX_N];
int n;

void solve() {
  sum[0][0] = a[0][0];
  for (int i = 1; i < n; ++i) {
    sum[0][i] = sum[0][i - 1] + a[0][i];
    sum[i][0] = sum[i - 1][0] + a[i][0];
  }
  for (int i = 1; i < n; ++i)
    for (int j = 1; j < n; ++j)
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];

  // (i, j)-(k, m)
  ll ans = sum[0][0];
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      for (int k = i; k < n; ++k)
        for (int m = j; m < n; ++m) {
          ll area = sum[k][m];

          if (i != 0)
            area -= sum[i - 1][m];
          if (j != 0)
            area -= sum[k][j - 1];
          if (i != 0 && j != 0)
            area += sum[i - 1][j - 1];
          ans = max(ans, area);
        }
  cout << ans << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> a[i][j];

  if (n == 1)
    cout << a[0][0] << endl;
  else
    solve();

  return 0;
}

Error: 0

Fixed submission:

#include <climits>
#include <iostream>

using namespace std;

typedef long long ll;

const int MAX_N = 200;
ll a[MAX_N][MAX_N];
ll sum[MAX_N][MAX_N];
int n;

void solve() {
  sum[0][0] = a[0][0];
  for (int i = 1; i < n; ++i) {
    sum[0][i] = sum[0][i - 1] + a[0][i];
    sum[i][0] = sum[i - 1][0] + a[i][0];
  }
  for (int i = 1; i < n; ++i)
    for (int j = 1; j < n; ++j)
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];

  // (i, j)-(k, m)
  ll ans = -LONG_MAX;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      for (int k = i; k < n; ++k)
        for (int m = j; m < n; ++m)
          ans = max(ans, sum[k][m] - sum[i - 1][m] - sum[k][j - 1] +
                             sum[i - 1][j - 1]);
  cout << ans << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> a[i][j];

  if (n == 1)
    cout << a[0][0] << endl;
  else
    solve();

  return 0;
}