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

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (int i = a; i < b; i++)

#define PB push_back

#define MOD 1000000007LL
typedef long long ll;

int n;
int a[321][321];
int sm[321][321];

int cnt(int x1, int x2, int y1, int y2) {
  return sm[x2][y2] - sm[x1][y2] - sm[x2][y1] + sm[x1][y1];
}

inline int gt(int i, int j, int k, int l) {
  int a = cnt(i, j, k, l), b = 0;
  if (j - i >= 3 && l - k >= 3) {
    b = cnt(i + 1, j - 1, k + 1, l - 1);
    // printf("*");
  }
  // printf("   gt(%d, %d, %d, %d)=%d-%d=%d\\n", i, j, k, l, a, b, a-b);
  return a - b;
}
int solve() {
  int ans = 0;
  REP(i, n + 1) {
    FOR(j, i + 1, n + 1) {
      REP(k, n + 1) {
        FOR(l, k + 1, n + 1) {
          int a = cnt(i, k, j, l), b = 0;
          if (j - i >= 3 && l - k >= 3) {
            b = cnt(i + 1, k + 1, j - 1, l - 1);
            // printf("*");
          }
          int x = a - b;
          // printf("(%d %d) (%d %d) : %d-%d=%d\\n", i, k, j, l, a, b, x);
          if (ans < x)
            ans = x;
        }
      }
    }
  }
  return ans;
}
#define LAR(a, b) a = max(a, (b))

int main() {
  scanf("%d", &n);
  REP(i, n) REP(j, n) scanf("%d", a[i] + j);
  REP(i, n) sm[i][0] = sm[0][i] = 0;
  REP(i, n) {
    REP(j, n) {
      sm[i + 1][j + 1] = sm[i][j + 1] + sm[i + 1][j] - sm[i][j] + a[i][j];
    }
  }
  // REP(i, n+1){
  // 	REP(j, n+1){
  // 		printf("%d ", sm[i][j]);
  // 	}
  // 	printf("\\n");
  // }

  int ans = INT_MIN;
  REP(i, n) {
    FOR(j, i + 1, n + 1) {
      int nwsm = 0, mxsm = 0, mxid = 0;
      int mxans = INT_MIN;
      FOR(l, 1, n + 1) {
        LAR(mxans, gt(i, j, l - 1, l));
        if (l >= 3) {
          nwsm -= cnt(i, j, l - 3, l - 2);
          // printf("  -(%d,%d,%d,%d)=%d\\n", i,j,l-3,l-2, cnt(i, j, l-3, l-2));
          if (j - i >= 3) {
            nwsm += cnt(i + 1, j - 1, l - 2, l - 1);
            // printf("  +(%d,%d,%d,%d)=%d\\n", i+1, j-1, l-2, l-1,
            // cnt(i+1, j-1, l-2, l-1));
          }
          if (mxsm < nwsm) {
            mxsm = nwsm;
            mxid = l - 2;
          }
        }
        LAR(mxans, gt(i, j, mxid, l));
        // printf(" %d-%d:%d %d\\n", mxid, l, mxans, nwsm);
      }
      LAR(ans, mxans);
      // printf("%d %d : %d\\n", i, j, mxans);
    }
  }
  printf("%d\\n", ans);
  // printf("%d\\n", solve());
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (int i = a; i < b; i++)

#define PB push_back

#define MOD 1000000007LL
typedef long long ll;

int a[321][321];
int sm[321][321];

int cnt(int a, int b, int c, int d) {
  return sm[c][d] - sm[a][d] - sm[c][b] + sm[a][b];
}

int main() {
  int n;
  scanf("%d", &n);
  REP(i, n) REP(j, n) scanf("%d", a[i] + j);
  REP(i, n) sm[i][0] = sm[0][i] = 0;
  REP(i, n) {
    REP(j, n) {
      sm[i + 1][j + 1] = sm[i][j + 1] + sm[i + 1][j] - sm[i][j] + a[i][j];
    }
  }
  // REP(i, n+1){
  // 	REP(j, n+1){
  // 		printf("%d ", sm[i][j]);
  // 	}
  // 	printf("\\n");
  // }
  int ans = 0;
  REP(i, n + 1) {
    FOR(j, i + 1, n + 1) {
      REP(k, n + 1) {
        FOR(l, k + 1, n + 1) {
          int a = cnt(i, k, j, l), b = 0;
          if (j - i >= 3 && l - k >= 3) {
            b = cnt(i + 1, k + 1, j - 1, l - 1);
            // printf("*");
          }
          int x = a - b;
          // printf("(%d %d) (%d %d) : %d-%d=%d\\n", i, k, j, l, a, b, x);
          if (ans < x)
            ans = x;
        }
      }
    }
  }
  printf("%d\\n", ans);
}