Problem p00098 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;

using namespace std;

int main() {

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

  int table[n][n];

  int ***memo = new int **[n];
  for (int i = 0; i < n; i++) {
    memo[i] = new int *[n];
    for (int k = 0; k < n; k++) {
      memo[i][k] = new int[n + 1];
    }
  }

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

  int maximum = -2000000000, tmp, pre, pre_row;

  for (int H = 1; H <= n; H++) {   //??????????????????
    for (int W = 1; W <= n; W++) { //???????????????
      for (int row = 0; row <= n - H; row++) {
        for (int col = 0; col <= n - W; col++) {
          if (W == 1) {     // W == 1??????????????¢???
            if (col == 0) { //?????????
              if (row == 0) {
                tmp = 0;
                for (int i = 0; i < H; i++) {
                  for (int k = 0; k < W; k++)
                    tmp += table[i][k];
                }
                memo[row][col][1] =
                    tmp; //????§????[row][col],??????H,???W?????????????¨???????
                maximum = max(maximum, tmp);
                pre = tmp;
                pre_row = tmp;
              } else {
                for (int i = 0; i < W; i++)
                  pre_row -= table[row - 1][i]; //?????????1????????????
                for (int i = 0; i < W; i++)
                  pre_row += table[row + H - 1][i]; //????????????????????????
                memo[row][col][1] =
                    pre_row; //????§????[row][col],??????H,???W?????????????¨???????
                maximum = max(maximum, pre_row);
                pre = pre_row;
              }
            } else {
              for (int i = 0; i < H; i++)
                pre -= table[row + i][col - 1]; //?????????1????????????
              for (int i = 0; i < H; i++)
                pre += table[row + i][col + W - 1]; //???????????????
              memo[row][col][1] =
                  pre; //????§????[row][col],??????H,???W?????????????¨???????
              maximum = max(maximum, pre);
            }

          } else {          // W > 1??????????????¢????´???¨??????
            if (col == 0) { //?????????
              if (row == 0) {
                tmp = memo[row][col][W - 1];
                for (int i = 0; i < H; i++) {
                  tmp += table[i][W - 1];
                }
                memo[row][col][W] =
                    tmp; //????§????[row][col],??????H,???W?????????????¨???????
                maximum = max(maximum, tmp);
              } else {
                tmp = memo[row][col][W - 1];
                for (int i = 0; i < H; i++) {
                  tmp += table[row + i][W - 1];
                }
                memo[row][col][W] = tmp;
                maximum = max(maximum, tmp);
              }
            } else {
              tmp = memo[row][col][W - 1];
              for (int i = 0; i < H; i++) {
                tmp += table[row + i][col + W - 1];
              }
              memo[row][col][W] = tmp;
              maximum = max(maximum, tmp);
            }
          }
        }
      }
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

int main() {

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

  int table[n][n];

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

  int maximum = -2000000000, tmp, pre, pre_row;

  for (int H = 1; H <= n; H++) {
    for (int W = 1; W <= n; W++) {
      for (int row = 0; row <= n - H; row++) {
        for (int col = 0; col <= n - W; col++) {
          if (col == 0) {
            if (row == 0) {
              tmp = 0;
              for (int i = 0; i < H; i++) {
                for (int k = 0; k < W; k++)
                  tmp += table[i][k];
              }
              maximum = max(maximum, tmp);
              pre = tmp;
              pre_row = tmp;
            } else {
              for (int i = 0; i < W; i++)
                pre_row -= table[row - 1][i];
              for (int i = 0; i < W; i++)
                pre_row += table[row + H - 1][i];
              maximum = max(maximum, pre_row);
              pre = pre_row;
            }
          } else {
            for (int i = 0; i < H; i++)
              pre -= table[row + i][col - 1];
            for (int i = 0; i < H; i++)
              pre += table[row + i][col + W - 1];
            maximum = max(maximum, pre);
          }
        }
      }
    }
  }

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

  return 0;
}