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

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int k[1000][1000], x[1000][1000], n, cnt, maxn = -10000000;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &k[i][j]);
    }
  }
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    for (int j = 1; j <= n; j++) {
      cnt += k[i][j];
      x[i][j] = cnt + x[i - 1][j];
    }
  }
  if (n <= 200) {
    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++) {
            if (k - i >= 3 && l - j >= 3) {
              maxn = max(maxn, (x[i][j] + x[k][l] - x[i][l] - x[k][j]) -
                                   (x[i + 1][j + 1] + x[k - 1][l - 1] -
                                    x[i + 1][l - 1] - x[k - 1][j + 1]));
            } else {
              maxn = max(maxn, x[i][j] + x[k][l] - x[i][l] - x[k][j]);
            }
          }
        }
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = i + 1; k <= n; k++) {
          if (k - i >= 180) {
            for (int l = j + 1; l <= n; l++) {
              if (l - j >= 180) {
                maxn = max(maxn, (x[i][j] + x[k][l] - x[i][l] - x[k][j]) -
                                     (x[i + 1][j + 1] + x[k - 1][l - 1] -
                                      x[i + 1][l - 1] - x[k - 1][j + 1]));
              }
            }
          }
        }
      }
    }
  }
  cout << maxn << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int k[1000][1000], x[1000][1000], n, cnt, maxn = -10000000;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &k[i][j]);
    }
  }
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    for (int j = 1; j <= n; j++) {
      cnt += k[i][j];
      x[i][j] = cnt + x[i - 1][j];
    }
  }
  if (n <= 200) {
    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++) {
            if (k - i >= 3 && l - j >= 3) {
              maxn = max(maxn, (x[i][j] + x[k][l] - x[i][l] - x[k][j]) -
                                   (x[i + 1][j + 1] + x[k - 1][l - 1] -
                                    x[i + 1][l - 1] - x[k - 1][j + 1]));
            } else {
              maxn = max(maxn, x[i][j] + x[k][l] - x[i][l] - x[k][j]);
            }
          }
        }
      }
    }
  } else {
    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++) {
            if (k - i >= 220 && l - j >= 220) {
              maxn = max(maxn, (x[i][j] + x[k][l] - x[i][l] - x[k][j]) -
                                   (x[i + 1][j + 1] + x[k - 1][l - 1] -
                                    x[i + 1][l - 1] - x[k - 1][j + 1]));
            }
          }
        }
      }
    }
  }
  cout << maxn << endl;
}
Problem p00305 (C++) — buggy submission, verdict: Runtime Error

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

int p[333][333];
int yoko[333][333];
int tate[333][333];
int dp[301][301][301];

signed main() {
  int n;
  cin >> n;

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

  for (int i = 0; i < n; i++) {
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum += p[i][j];
      yoko[i][j] = sum;
    }
    sum = 0;
    for (int j = 0; j < n; j++) {
      sum += p[j][i];
      tate[j][i] = sum;
    }
  }
  int INF = 100000000;
  fill_n(**dp, 301 * 301 * 301, INF);
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      for (int k = n - 1; k >= 0; k--) {
        dp[i][j][k] =
            yoko[i][k] + yoko[j][k] + tate[j][k] - tate[i][k] - p[j][k];
        if (i == j) {
          dp[i][j][k] -= yoko[i][k];
          dp[i][j][k] += p[j][k];
        }
        if (k < n - 1)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j][k + 1]);
        //	cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
      }
    }
  }
  int ans = -INF;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      for (int k = 0; k < n; k++) {
        int sum = tate[j][k] - tate[i][k] + p[i][k];
        if (k < n - 1) {
          int tmp = sum;
          tmp += dp[i][j][k + 1];
          tmp -= (yoko[i][k] + yoko[j][k]);
          if (i == j)
            tmp += yoko[i][k];
          sum = max(sum, tmp);
        }
        ans = max(ans, sum);
        //	cout<<i<<" "<<j<<" "<<k<<" "<<ans<<endl;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

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

int p[333][333];
int yoko[333][333];
int tate[333][333];
int dp[301][301][301];

signed main() {
  int n;
  cin >> n;

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

  for (int i = 0; i < n; i++) {
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum += p[i][j];
      yoko[i][j] = sum;
    }
    sum = 0;
    for (int j = 0; j < n; j++) {
      sum += p[j][i];
      tate[j][i] = sum;
    }
  }
  int INF = 100000000;
  fill_n(**dp, 333 * 333 * 333, INF);
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      for (int k = n - 1; k >= 0; k--) {
        dp[i][j][k] =
            yoko[i][k] + yoko[j][k] + tate[j][k] - tate[i][k] - p[j][k];
        if (i == j) {
          dp[i][j][k] -= yoko[i][k];
          dp[i][j][k] += p[j][k];
        }
        if (k < n - 1)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j][k + 1]);
        //	cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
      }
    }
  }
  int ans = -INF;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      for (int k = 0; k < n; k++) {
        int sum = tate[j][k] - tate[i][k] + p[i][k];
        if (k < n - 1) {
          int tmp = sum;
          tmp += dp[i][j][k + 1];
          tmp -= (yoko[i][k] + yoko[j][k]);
          if (i == j)
            tmp += yoko[i][k];
          sum = max(sum, tmp);
        }
        ans = max(ans, sum);
        //	cout<<i<<" "<<j<<" "<<k<<" "<<ans<<endl;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00305 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n;
int tate[300][301]; //[x][y?´????]
int yoko[301][300]; //[x?´????][y]

int tateSum(int x, int y0, int y1) { return tate[x][y1 + 1] - tate[x][y0]; }
int yokoSum(int x0, int x1, int y) { return yoko[x1 + 1][y] - yoko[x0][y]; }

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    tate[i][0] = 0;
    yoko[0][i] = 0;
  }
  int tmp;
  for (int y = 0; y < n; y++) {
    for (int x = 0; x < n; x++) {
      cin >> tmp;
      tate[x][y + 1] = tate[x][y] + tmp;
      yoko[x + 1][y] = yoko[x][y] + tmp;
    }
  }

  int ans = -114514810;
  int koMax;
  int ko;

  int i, j, k;
  for (i = 0; i < n; i++) {
    for (j = i; j < n; j++) {
      koMax = -114514810;
      for (k = 0; k < n; k++) {
        if (i == j) { //?¨???????
          ko = yokoSum(k, k, i);
          koMax = max(koMax + yokoSum(k, k, i), ko);
          ans = max(ans, koMax);
        } else { //????????\\???
          ko = tateSum(k, i, j);
          ans = max(ans, max(koMax + ko, ko));
          koMax = max(koMax + yokoSum(k, k, i) + yokoSum(k, k, j), ko);
        }
      }
    }
  }

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

Error: TLE

Fixed submission:

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

int n;
int tate[300][301]; //[x][y?´????]
int yoko[301][300]; //[x?´????][y]

int tateSum(int x, int y0, int y1) { return tate[x][y1 + 1] - tate[x][y0]; }
int yokoSum(int x0, int x1, int y) { return yoko[x1 + 1][y] - yoko[x0][y]; }

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    tate[i][0] = 0;
    yoko[0][i] = 0;
  }
  int tmp;
  for (int y = 0; y < n; y++) {
    for (int x = 0; x < n; x++) {
      cin >> tmp;
      tate[x][y + 1] = tate[x][y] + tmp;
      yoko[x + 1][y] = yoko[x][y] + tmp;
    }
  }

  int i, j, k, l; //(i,j)-(k,l)
  int ans = -114514810;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      for (k = i; k < n; k++) {
        for (l = j; l < n; l++) {
          if (i == k && j == l) { //???
            ans = max(ans, tateSum(i, j, j));
          } else if (i == k) { //??????
            ans = max(ans, tateSum(i, j, l));
          } else if (j == l) { //?¨????
            ans = max(ans, yokoSum(i, k, j));
          } else { //????§?
            ans = max(ans, tateSum(i, j, l - 1) + yokoSum(i, k - 1, l) +
                               tateSum(k, j + 1, l) + yokoSum(i + 1, k, j));
          }
        }
      }
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p00305 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int n, mp[302][302], x[302][302] = {}, y[302][302] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> y[i][j], mp[i][j] = x[i][j] = y[i][j];

  int ans = max(y[0][n - 1], y[n - 1][0]);
  for (int i = 0; i < n; i++)
    for (int j = n - 2; j >= 0; j--) {
      x[i][j] += x[i][j + 1];
      y[j][i] += y[j + 1][i];
      if (ans < x[i][j])
        ans = x[i][j];
      if (ans < y[j][i])
        ans = y[j][i];
    }

  int sum[302][302], K[2][302][302] = {};

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      sum[i][j] = -100000000;

  int t = 0;
  while (1) {
    t++;
    for (int i = 0; i < n; i++)         // yoko
      for (int j = i + 1; j < n; j++) { // yoko
        int a = K[t % 2][i][j];
        K[(t + 1) % 2][i][j] = a;
        sum[i][j] = -100000000;
        for (int k = a + 1; k < n; k++) { // tate
          int X = (x[a][i] - x[a][j + 1]) + (x[k][i] - x[k][j + 1]);
          int Y = (y[a + 1][i] - y[k][i]) + (y[a + 1][j] - y[k][j]);
          if (sum[i][j] <= X + Y)
            sum[i][j] = X + Y, K[(t + 1) % 2][i][j] = k;
          ans = max(ans, sum[i][j]);
        }
      }

    int f = 1;
    for (int i = 0; i < n; i++)       // yoko
      for (int j = i + 1; j < n; j++) // yoko
        for (int k = K[t % 2][i][j] + 1; k < K[(t + 1) % 2][i][j]; k++) {
          f = 0;
          int X = (x[k][i + 1] - x[k][j]) - (x[k - 1][i] - x[k - 1][j + 1]);
          sum[i][j] += X;
          ans = max(ans, sum[i][j]);
        }
    if (f)
      break;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
int mem[2][302][302];
int main() {
  int n, x[302][302] = {}, y[302][302] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> y[i][j], x[i][j] = y[i][j];

  int ans = max(y[0][n - 1], y[n - 1][0]);
  for (int i = 0; i < n; i++)
    for (int j = n - 2; j >= 0; j--) {
      x[i][j] += x[i][j + 1];
      y[j][i] += y[j + 1][i];
      if (ans < x[i][j])
        ans = x[i][j];
      if (ans < y[j][i])
        ans = y[j][i];
    }
  // n=300;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      mem[0][i][j] = mem[1][i][j] = -1000000000;

  for (int i = 0; i < n; i++) {           // tate
    for (int j = 0; j < n; j++) {         // yoko
      for (int k = i + 1; k < n; k++) {   // tate
        for (int l = j + 1; l < n; l++) { // yoko
          int X = (x[i][j] - x[i][l + 1]);
          // if(X<=mem[0][j][l+1])continue;
          mem[0][j][l + 1] = X;
          int Y = (y[i + 1][j] - y[k][j]) + (y[i + 1][l] - y[k][l]);
          X += (x[k][j] - x[k][l + 1]);
          if (ans < X + Y)
            ans = X + Y;
        }
      }
    }
  }
  cout << ans << endl;

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

#include <iostream>
#define INF 10000000
using namespace std;

int main() {
  int n, x[302][302] = {}, y[302][302] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> y[i][j], x[i][j] = y[i][j];

  for (int i = 0; i < n; i++)
    for (int j = n - 2; j >= 0; j--) {
      x[i][j] += x[i][j + 1];
      y[j][i] += y[j + 1][i];
    }

  int ans = y[0][n - 1], sum, K[302][302] = {}, t = 0, f = 1;
  while (f) {
    t++, f = 0;
    for (int i = 0; i < n; i++)         // yoko
      for (int j = i + 1; j < n; j++) { // yoko
        int a = K[i][j], nk = a;
        sum = -INF;
        for (int k = a + 1; k < n; k++) { // tate
          int X = (x[a][i] - x[a][j + 1]) + (x[k][i] - x[k][j + 1]);
          int Y = (y[a + 1][i] - y[k][i]) + (y[a + 1][j] - y[k][j]);
          if (sum <= X + Y)
            sum = X + Y, nk = k;
        }
        ans = max(ans, sum);

        if (nk != n - 1)
          f = 1;
        for (int k = a + 1; k < nk; k++) { // tate
          sum += (x[k][i + 1] - x[k][j]) - (x[k - 1][i] - x[k - 1][j + 1]);
          ans = max(ans, sum);
        }
        K[i][j] = nk;
      }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define INF 10000000
using namespace std;

int main() {
  int n, x[302][302] = {}, y[302][302] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> y[i][j], x[i][j] = y[i][j];

  for (int i = 0; i < 300; i++)
    for (int j = 0; j < 300; j++)
      y[i][j] = x[i][j] = -1;
  n = 300;

  for (int i = 0; i < n; i++)
    for (int j = n - 2; j >= 0; j--) {
      x[i][j] += x[i][j + 1];
      y[j][i] += y[j + 1][i];
    }

  int ans = max(y[0][n - 1], y[n - 1][0]), sum, K[302][302] = {}, t = 0, f = 1;
  while (f) {
    t++, f = 0;
    for (int i = 0; i < n; i++)         // yoko
      for (int j = i + 1; j < n; j++) { // yoko
        int a = K[i][j], nk = a;
        sum = -INF;
        for (int k = a + 1; k < n; k++) { // tate
          int X = (x[a][i] - x[a][j + 1]) + (x[k][i] - x[k][j + 1]);
          int Y = (y[a + 1][i] - y[k][i]) + (y[a + 1][j] - y[k][j]);
          if (sum <= X + Y)
            sum = X + Y, nk = k;
        }
        ans = max(ans, sum);

        if (nk != n - 1)
          f = 1;
        for (int k = a + 1; k < nk; k++) { // tate
          sum += (x[k][i + 1] - x[k][j]) - (x[k - 1][i] - x[k - 1][j + 1]);
          ans = max(ans, sum);
        }
        K[i][j] = nk;
      }
  }
  cout << ans << endl;

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

#include <iostream>
#include <time.h>
#define INF 10000000
using namespace std;

int main() {
  clock_t start = clock();
  int n, x[302][302] = {}, y[302][302] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> y[i][j], x[i][j] = y[i][j];
  /*
  n=300;
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++) x[i][j]=y[i][j]=-1;
  */

  for (int i = 0; i < n; i++)
    for (int j = n - 2; j >= 0; j--) {
      x[i][j] += x[i][j + 1];
      y[j][i] += y[j + 1][i];
    }

  int ans = y[0][n - 1], sum, K[302][302] = {}, t = 0, f = 1;
  while (f) {
    t++, f = 0;
    for (int i = 0; i < n; i++)         // yoko
      for (int j = i + 1; j < n; j++) { // yoko
        int a = K[i][j], nk = a;
        sum = -INF;
        for (int k = a + 1; k < n; k++) { // tate
          int X = (x[a][i] - x[a][j + 1]) + (x[k][i] - x[k][j + 1]);
          int Y = (y[a + 1][i] - y[k][i]) + (y[a + 1][j] - y[k][j]);
          if (sum <= X + Y)
            sum = X + Y, nk = k;
        }
        ans = max(ans, sum);

        if (nk < n - 1)
          f = 1;
        for (int k = a + 1; k < nk; k++) { // tate
          sum += (x[k][i + 1] - x[k][j]) - (x[k - 1][i] - x[k - 1][j + 1]);
          ans = max(ans, sum);
        }
        if (a + 1 == nk && j - i != 1)
          nk = n - 1;
        K[i][j] = nk;
      }
  }
  clock_t end = clock();
  //  cout << (double)(end-start)/CLOCKS_PER_SEC <<endl;
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <time.h>
#define INF 10000000
using namespace std;

int main() {
  clock_t start = clock();
  int n, x[302][302] = {}, y[302][302] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> y[i][j], x[i][j] = y[i][j];

  n = 300;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      x[i][j] = y[i][j] = -1;

  for (int i = 0; i < n; i++)
    for (int j = n - 2; j >= 0; j--) {
      x[i][j] += x[i][j + 1];
      y[j][i] += y[j + 1][i];
    }

  int ans = y[0][n - 1], sum, K[302][302] = {}, t = 0, f = 1;
  while (f) {
    t++, f = 0;
    for (int i = 0; i < n; i++)         // yoko
      for (int j = i + 1; j < n; j++) { // yoko
        int a = K[i][j], nk = a;
        sum = -INF;
        for (int k = a + 1; k < n; k++) { // tate
          int X = (x[a][i] - x[a][j + 1]) + (x[k][i] - x[k][j + 1]);
          int Y = (y[a + 1][i] - y[k][i]) + (y[a + 1][j] - y[k][j]);
          if (sum <= X + Y)
            sum = X + Y, nk = k;
        }
        ans = max(ans, sum);

        if (nk < n - 1)
          f = 1;
        for (int k = a + 1; k < nk; k++) { // tate
          sum += (x[k][i + 1] - x[k][j]) - (x[k - 1][i] - x[k - 1][j + 1]);
          ans = max(ans, sum);
        }
        K[i][j] = nk + (nk == a + 1);
      }
  }
  clock_t end = clock();
  //  cout << (double)(end-start)/CLOCKS_PER_SEC <<endl;
  cout << ans << endl;

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

// 0310
#include <bits/stdc++.h>

using namespace std;

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

const int MAX_N = 300;

const int INF = 1e8;

int N;
int p[MAX_N + 1][MAX_N + 1];

int dp[MAX_N + 2][MAX_N + 2], dp2[MAX_N + 2][MAX_N + 2][2];

int main() {
  scanf("%d", &N);
  FOR(i, 1, N + 1) {
    FOR(j, 1, N + 1) { scanf("%d", &p[i][j]); }
  }
  int res = -INF;
  FOR(i, 1, N + 1) {
    for (int j = i; j >= 1; j--) {
      dp2[j][i][1] = dp2[j + 1][i][1] + p[j][1];
      res = max(res, dp2[j][i][1]);
    }
  }
  FOR(j, 2, N + 1) {
    FOR(i, 1, N + 1) {
      for (int k = i; k >= 1; k--) {
        dp2[k][i][j & 1] = dp2[k + 1][i][j & 1] + p[k][j];
        res = max(res, dp2[k][i][j & 1]);
      }
      FOR(k, 1, i + 1) {
        if (j == 2) {
          dp[k][i] = dp2[k][i][0] + dp2[k][i][1];
        } else {
          dp[k][i] =
              dp2[k][i][j & 1] + max(dp2[k][i][(j - 1) & 1],
                                     dp[k][i] - dp2[k + 1][i - 1][(j - 1) & 1]);
        }
        res = max(res, dp[k][i]);
      }
    }
  }
  printf("%d\\n", res);
  return 0;
}

Error: MLE

Fixed submission:

// 0310
#include <bits/stdc++.h>

using namespace std;

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

const int MAX_N = 300;

const int INF = 1e8;

int N;
int p[MAX_N + 1][MAX_N + 1];

int dp[MAX_N + 1][MAX_N + 1][MAX_N + 1], dp2[MAX_N + 1][MAX_N + 1][MAX_N + 2];

int main() {
  scanf("%d", &N);
  int i, j, k;
  FOR(i, 1, N + 1) {
    FOR(j, 1, N + 1) { scanf("%d", &p[i][j]); }
  }
  int res = -INF;
  FOR(i, 1, N + 1) {
    FOR(j, 1, N + 1) {
      for (int k = i; k >= 1; k--) {
        if (i == k) {
          dp2[i][j][k] = p[i][j];
        } else {
          dp2[i][j][k] = dp2[i][j][k + 1] + p[k][j];
        }
        res = max(res, dp2[i][j][k]);
      }
    }
  }
  FOR(i, 1, N + 1) {
    FOR(j, 2, N + 1) {
      FOR(k, 1, i + 1) {
        if (j == 2) {
          dp[i][j][k] = dp2[i][j - 1][k] + dp2[i][j][k];
        } else {
          dp[i][j][k] =
              dp2[i][j][k] +
              max(dp2[i][j - 1][k], dp[i][j - 1][k] - dp2[i - 1][j - 1][k + 1]);
        }
        res = max(res, dp[i][j][k]);
      }
    }
  }
  printf("%d\\n", res);
  return 0;
}
Problem p00305 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define INF 10000000
using namespace std;

int main() {
  int n, x[302][302] = {}, y[302][302] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> y[i][j], x[i][j] = y[i][j];

  /*  for(int i=0;i<300;i++)
    for(int j=0;j<300;j++) y[i][j]=x[i][j]=-1;
    n=300;*/

  for (int i = 0; i < n; i++)
    for (int j = n - 2; j >= 0; j--) {
      x[i][j] += x[i][j + 1];
      y[j][i] += y[j + 1][i];
    }

  int ans = max(y[0][n - 1], y[n - 1][0]), sum, K[302][302] = {}, t = 0, f = 1;
  while (f) {
    t++, f = 0;
    for (int i = 0; i < n; i++)         // yoko
      for (int j = i + 1; j < n; j++) { // yoko
        int a = K[i][j], nk = a;
        sum = -INF;
        for (int k = a + 1; k < n; k++) { // tate
          int X = (x[a][i] - x[a][j + 1]) + (x[k][i] - x[k][j + 1]);
          int Y = (y[a + 1][i] - y[k][i]) + (y[a + 1][j] - y[k][j]);
          if (sum <= X + Y)
            sum = X + Y, nk = k;
        }
        ans = max(ans, sum);

        if (nk != n - 1)
          f = 1;
        for (int k = a + 1; k < nk; k++) { // tate
          sum += (x[k][i + 1] - x[k][j]) - (x[k - 1][i] - x[k - 1][j + 1]);
          ans = max(ans, sum);
        }
        K[i][j] = nk;
      }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define INF 10000000
using namespace std;

int main() {
  int n, x[302][302] = {}, y[302][302] = {};
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> y[i][j], x[i][j] = y[i][j];

  for (int i = 0; i < 300; i++)
    for (int j = 0; j < 300; j++)
      y[i][j] = x[i][j] = -1;
  n = 300;

  for (int i = 0; i < n; i++)
    for (int j = n - 2; j >= 0; j--) {
      x[i][j] += x[i][j + 1];
      y[j][i] += y[j + 1][i];
    }

  int ans = max(y[0][n - 1], y[n - 1][0]), sum[302][302], K[2][302][302] = {},
      t = 0, f = 1;
  while (f) {
    t++, f = 0;
    for (int i = 0; i < n; i++)         // yoko
      for (int j = i + 1; j < n; j++) { // yoko
        int a = K[t % 2][i][j], &b = K[(t + 1) % 2][i][j];
        b = a;
        sum[i][j] = -INF;

        for (int k = a + 1; k < n; k++) { // tate
          int X = (x[a][i] - x[a][j + 1]) + (x[k][i] - x[k][j + 1]);
          int Y = (y[a + 1][i] - y[k][i]) + (y[a + 1][j] - y[k][j]);
          if (sum[i][j] <= X + Y)
            sum[i][j] = X + Y, b = k;
        }
        ans = max(ans, sum[i][j]);

        if (b != n - 1)
          f = 1;
        for (int k = a + 1; k < b; k++) { // tate
          sum[i][j] +=
              (x[k][i + 1] - x[k][j]) - (x[k - 1][i] - x[k - 1][j + 1]);
          ans = max(ans, sum[i][j]);
        }
      }
  }
  cout << ans << endl;

  return 0;
}