alexjercan/bugnet huggingface dataset:
Problem p02326 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
#define MAX_N 3000
int x[MAX_N][MAX_N];
int H, W;
int r(int X, int Y, int Z) {
  int B = x[X - 1][Y - 1] + x[X + Z - 1][Y + Z - 1] - x[X - 1][Y + Z - 1] -
          x[X + Z - 1][Y - 1];
  if (B == 0) {
    return true;
  }
  return false;
}
int main() {
  cin >> H >> W;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> x[i][j];
    }
  }
  for (int i = 1; i < MAX_N; i++) {
    for (int j = 1; j < MAX_N; j++) {
      if (i > H || j > W) {
        x[i][j] = 1;
      }
    }
  }
  for (int i = 1; i < MAX_N; i++) {
    for (int j = 1; j < MAX_N; j++) {
      x[i][j] += x[i][j - 1];
    }
  }
  for (int i = 1; i < MAX_N; i++) {
    for (int j = 1; j < MAX_N; j++) {
      x[j][i] += x[j - 1][i];
    }
  }
  int maxn = 0;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      int L = 0, R = 1500, M;
      for (int k = 0; k < 50; k++) {
        M = (L + R) / 2;
        bool a1 = r(i, j, M), a2 = r(i, j, M + 1);
        if (a1 == true && a2 == false) {
          maxn = max(maxn, M);
          break;
        }
        if (a1 == false) {
          R = M;
        }
        if (a2 == true) {
          L = M;
        }
      }
    }
  }
  cout << maxn * maxn << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define MAX_N 3000
int x[MAX_N][MAX_N];
int H, W;
int r(int X, int Y, int Z) {
  int B = x[X - 1][Y - 1] + x[X + Z - 1][Y + Z - 1] - x[X - 1][Y + Z - 1] -
          x[X + Z - 1][Y - 1];
  if (B == 0) {
    return true;
  }
  return false;
}
int main() {
  cin >> H >> W;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> x[i][j];
    }
  }
  for (int i = 1; i < MAX_N; i++) {
    for (int j = 1; j < MAX_N; j++) {
      if (i > H || j > W) {
        x[i][j] = 1;
      }
    }
  }
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      x[i][j] += x[i][j - 1];
    }
  }
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      x[i][j] += x[i - 1][j];
    }
  }
  int maxn = 0;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      int L = 0, R = 1500, M;
      for (int k = 0; k < 50; k++) {
        M = (L + R) / 2;
        bool a1 = r(i, j, M), a2 = r(i, j, M + 1);
        if (a1 == true && a2 == false) {
          maxn = max(maxn, M);
          break;
        }
        if (a1 == false) {
          M = R;
        }
        if (a2 == true) {
          M = L;
        }
      }
    }
  }
  cout << maxn << endl;
  return 0;
}
Problem p02326 (C++) — buggy submission, verdict: Runtime Error

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

static const int MAX_N = 1400;

int dp[MAX_N + 1][MAX_N + 1];

int main(void) {
  int H, W;
  cin >> H >> W;

  for (int i = 0; i < H; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i < W; i++) {
    dp[0][i] = 0;
  }
  int tmp;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> tmp;
      dp[i][j] = (tmp + 1) % 2;
    }
  }

  int maxWidth = 0;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (dp[i][j]) {
        dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
        maxWidth = max(maxWidth, dp[i][j]);
      }
    }
  }

  cout << maxWidth * maxWidth << endl;

  return 0;
}

Error: 0

Fixed submission:

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

static const int MAX_N = 1400;
int masu[MAX_N][MAX_N];
int dp[MAX_N][MAX_N];

int main(void) {
  int H, W;
  int maxWidth = 0;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> masu[i][j];
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (masu[i][j]) {
        dp[i][j] = 0;
      } else {
        dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
        maxWidth = max(maxWidth, dp[i][j]);
      }
    }
  }

  printf("%d\\n", maxWidth * maxWidth);

  return 0;
}
Problem p02326 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;
int tate[1405][1405] = {};
int yoko[1405][1405] = {};
int dp[1405][1405] = {};
int c[1405][1405];
int main() {
  int h, w;
  scanf("%d %d", &h, &w);
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      scanf("%d", &c[i][j]);
  }
  int ans = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (c[i][j] == 0) {
        tate[i][j] = tate[i - 1][j] + 1;
        yoko[i][j] = yoko[i][j - 1] + 1;
        dp[i][j] = min(tate[i][j], yoko[i][j]);
        dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1);
      }
      ans = max(ans, dp[i][j] * dp[i][j]);
    }
  }
  printf("%d\\n", ans);
  return (0);
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
int tate[1005][1005] = {};
int yoko[1005][1005] = {};
int dp[1005][1005] = {};
int c[1005][1005];
int main() {
  int h, w;
  scanf("%d %d", &h, &w);
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      scanf("%d", &c[i][j]);
  }
  int ans = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (c[i][j] == 0) {
        tate[i][j] = tate[i - 1][j] + 1;
        yoko[i][j] = yoko[i][j - 1] + 1;
        dp[i][j] = min(tate[i][j], yoko[i][j]);
        dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1);
      }
      ans = max(ans, dp[i][j] * dp[i][j]);
    }
  }
  printf("%d\\n", ans);
  return (0);
}
Problem p02326 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define mo 1505
int a[mo][mo], dp[mo][mo];
int n, m;
int main() {
  while (cin >> n >> m) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
      }
    }
    int maxs = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i][j])
          dp[i][j] = 0;
        else
          dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i][j - 1])) + 1;
        maxs = max(maxs, dp[i][j]);
        // cout<<dp[i][j]<<' ';
      }
      // cout<<endl;
    }
    cout << maxs * maxs << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mo 1005
int a[mo][mo], dp[mo][mo];
int n, m;
int main() {
  while (cin >> n >> m) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
      }
    }
    int maxs = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i][j])
          dp[i][j] = 0;
        else
          dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i][j - 1])) + 1;
        maxs = max(maxs, dp[i][j]);
        // cout<<dp[i][j]<<' ';
      }
      // cout<<endl;
    }
    cout << maxs * maxs << endl;
  }
}
Problem p02326 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int h, w;
int maps[1401][1401];
int memo[1401][1401];
int ans = INT_MIN;

int dp(int y, int x) {
  if (y > h || y <= 0 || x > w || x <= 0)
    return memo[y][x] = 0;

  if (memo[y][x] != -1)
    return memo[y][x];

  int res = min(min(dp(y, x - 1), dp(y - 1, x)), dp(y - 1, x - 1)) + 1;

  // cout<<"("<<y<<","<<x<<")"<<res<<endl;

  if (maps[y][x] == 1)
    return memo[y][x] = 0;

  ans = max(ans, res);

  return memo[y][x] = res;
}

int main() {
  cin >> h >> w;
  memset(memo, -1, sizeof(memo));
  //  for(int i=0;i<=h;i++)maps[i][0]=1;
  //  for(int i=0;i<=w;i++)maps[0][i]=1;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> maps[i][j];
    }
  }
  dp(h, w);
  cout << ans * ans << endl;
}

Error: TLE

Fixed submission:

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

int h, w;
int maps[1401][1401];
int memo[1401][1401];
int ans = INT_MIN;

int dp(int y, int x) {
  if (y > h || y <= 0 || x > w || x <= 0)
    return 0;
  if (memo[y][x] != -1)
    return memo[y][x];
  int res = min(min(dp(y, x - 1), dp(y - 1, x)), dp(y - 1, x - 1)) + 1;
  // cout<<"("<<y<<","<<x<<")"<<res<<endl;
  if (maps[y][x] == 1)
    return 0;
  ans = max(ans, res);
  return memo[y][x] = res;
}
int main() {
  cin >> h >> w;
  memset(memo, -1, sizeof(memo));
  //  for(int i=0;i<=h;i++)maps[i][0]=1;
  //  for(int i=0;i<=w;i++)maps[0][i]=1;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> maps[i][j];
    }
  }
  dp(h, w);
  cout << ans * ans << endl;
}
Problem p02326 (C++) — buggy submission, verdict: Runtime Error

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

int dpTable[1400][1400];

int main() {
  int h, w;
  int t;
  cin >> h >> w;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> t;
      dpTable[i][j] = (t == 1 ? 0 : 1);
    }
  }
  int m = 0;
  if (h == 1) {
    for (int j = 0; j < w; ++j) {
      if (dpTable[0][j] != 0) {
        cout << 1 << endl;
        return 0;
      }
    }
    cout << 0 << endl;
    return 0;
  }
  if (w == 1) {
    for (int i = 0; i < h; ++i) {
      if (dpTable[i][0] != 0) {
        cout << 1 << endl;
        return 0;
      }
    }
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i < h; ++i) {
    for (int j = 1; j < w; ++j) {
      if (dpTable[i][j] != 0) {
        int s =
            min({dpTable[i - 1][j - 1], dpTable[i - 1][j], dpTable[i][j - 1]}) +
            1;
        m = max(m, s);
        dpTable[i][j] = s;
      }
    }
  }
  cout << m * m << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int dpTable[1400][1400];

int main() {
  int h, w;
  int t;
  cin >> h >> w;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> t;
      dpTable[i][j] = (t == 1 ? 0 : 1);
    }
  }
  int m = 0;
  if (h == 1) {
    for (int j = 0; j < w; ++j) {
      if (dpTable[0][j] != 0) {
        return 1;
      }
    }
    return 0;
  }
  if (w == 1) {
    for (int i = 0; i < h; ++i) {
      if (dpTable[i][0] != 0) {
        return 1;
      }
    }
    return 0;
  }
  for (int i = 1; i < h; ++i) {
    for (int j = 1; j < w; ++j) {
      if (dpTable[i][j] != 0) {
        int s =
            min({dpTable[i - 1][j - 1], dpTable[i - 1][j], dpTable[i][j - 1]}) +
            1;
        m = max(m, s);
        dpTable[i][j] = s;
      }
    }
  }
  cout << m * m << endl;
}
Problem p02326 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i++)

using namespace std;

int main(void) {
  unsigned int count = 0;
  int h, w;
  cin >> h >> w;
  vector<vector<int>> indp(h);
  for (auto &x : indp)
    x.resize(w);

  int mv = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> indp[i][j];
      indp[i][j] = !indp[i][j];
      mv |= indp[i][j];
    }
  }

  for (int i = 1; i < h; i++) {
    for (int j = 1; j < w; j++) {
      indp[i][j] *=
          min({indp[i - 1][j - 1], indp[i - 1][j], indp[i][j - 1]}) + 1;
      mv = max(mv, indp[i][j]);
    }
  }
  cout << mv * mv << endl;

  return 0;
}

Error: 0
5 4
0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
2 4
3 0
3 1
3 2
3 3
3 4
hoge


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i++)

using namespace std;

int main(void) {
  unsigned int count = 0;
  int w, h;
  cin >> h >> w;
  vector<vector<int>> indp(h);
  for (auto &x : indp)
    x.resize(w);

  rep(i, h) { indp.resize(w); }
  cerr << w << ' ' << h << endl;

  rep(i, h) rep(j, w) {
    cerr << i << ' ' << j << endl;
    cin >> indp[i][j];
    indp[i][j] = !indp[i][j];
  }
  cerr << "hoge" << endl;

  int mv = 0;
  for (int t = 1; t < min(w, h); t++) {
    for (int i = t; i < w; i++) {
      indp[t][i] *=
          min({indp[t - 1][i - 1], indp[t - 1][i], indp[t][i - 1]}) + 1;
      mv = max(mv, indp[t][i]);
    }
    for (int i = t + 1; i < h; i++) {
      indp[i][t] *=
          min({indp[i - 1][t - 1], indp[i - 1][t], indp[i][t - 1]}) + 1;
      mv = max(mv, indp[i][t]);
    }
  }
  cout << mv * mv << endl;

  return 0;
}
Problem p02326 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
using namespace std;

const int MAX = 1400;

int main() {
  int H, W;
  int tile[MAX][MAX];
  int ans = 0;

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

  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (tile[y][x] == 1)
        tile[y][x] = 0;
      else if (x < 1 || y < 1)
        tile[y][x] = 1;
      else
        tile[y][x] =
            min(tile[y - 1][x - 1], min(tile[y - 1][x], tile[y][x - 1])) + 1;
      ans = max(tile[y][x], ans);
    }
  }

  printf("%d\\n", ans * ans);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;

const int MAX = 140;

int main() {
  int H, W;
  int tile[MAX][MAX];
  int ans = 0;

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

  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (tile[y][x] == 1)
        tile[y][x] = 0;
      else if (x < 1 || y < 1)
        tile[y][x] = 1;
      else
        tile[y][x] =
            min(tile[y - 1][x - 1], min(tile[y - 1][x], tile[y][x - 1])) + 1;
      ans = max(tile[y][x], ans);
    }
  }

  printf("%d\\n", ans * ans);
  return 0;
}
Problem p02327 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

int H, W;
int C[1410][1410];
int S[1410][1410];

deque<pair<int, int>> Q;
int main() {
  iostream::sync_with_stdio(false);
  cin >> H >> W;
  for (int y = 0; y < H; y++)
    for (int x = 0; x < W; x++)
      cin >> C[y][x];
  for (int x = 0; x <= W; x++)
    S[0][x] = 0;
  // for(int y=0;y<=H;y++)S[y][0]=0;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++)
      S[y + 1][x] = C[y][x] == 0 ? S[y][x] + 1 : 0;
    // cerr<<endl;
  }
  int m = 0;

  for (int y = 0; y <= H; y++) {
    Q.emplace_front(-1, 0);
    for (int x = 0; x <= W; x++) {
      int last_pop = x;
      while (Q.front().second > S[y][x]) {
        m = max(m, (x - Q.front().first) * Q.front().second);
        last_pop = Q.front().first;
        Q.pop_front();
      }
      if (S[y][x] > Q.front().second) {
        Q.emplace_front(last_pop, S[y][x]);
        // cerr<<x<<" "<<S[y][x]<<endl;
      }
    }
  }

  cout << m << endl;
  return 0;
}

Error: 0
1 1 0 1 1 
0 2 1 2 2 
1 3 2 0 3 
2 4 3 0 4 
0 1
3 1
1 2
2 1
3 2
0 1
1 3
2 2
4 3
0 2
1 4
2 3
4 4


Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

int H, W;
int C[1410][1410];
int S[1410][1410];

deque<pair<int, int>> Q;
template <typename T> T debug(const T &x) {
  cerr << x << " ";
  return x;
}

template <typename T> T debugln(const T &x) {
  cerr << x << endl;
  return x;
}
int main() {
  iostream::sync_with_stdio(false);
  cin >> H >> W;
  for (int y = 0; y < H; y++)
    for (int x = 0; x < W; x++)
      cin >> C[y][x];
  for (int x = 0; x <= W; x++)
    S[0][x] = 0;
  // for(int y=0;y<=H;y++)S[y][0]=0;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++)
      S[y + 1][x] = debug(C[y][x] == 0 ? S[y][x] + 1 : 0);
    cerr << endl;
  }
  int m = 0;

  for (int y = 0; y <= H; y++) {
    Q.emplace_front(-1, 0);
    for (int x = 0; x <= W; x++) {
      int last_pop = x;
      while (Q.front().second > S[y][x]) {
        m = max(m, (x - Q.front().first) * Q.front().second);
        last_pop = Q.front().first;
        Q.pop_front();
      }
      if (S[y][x] > Q.front().second) {
        Q.emplace_front(last_pop, S[y][x]);
        cerr << x << " " << S[y][x] << endl;
      }
    }
  }

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

#include <stack>
#include <stdio.h>
#include <utility>
using namespace std;
typedef pair<int, int> pi;
#define F first
#define S second
#define MP make_pair
const int N = 15e2 + 10;
int main() {
  int n, m, a[N][N], now, ans, nx;
  pi temp;
  stack<pi> s;
  scanf("%d%d", &n, &m);
  ans = 0;
  for (int i = 0; i < n; i++) {
    now = 0;
    for (int j = 0; j < m; j++) {
      scanf("%d", &nx);
      if (nx == 0)
        now++;
      else
        now = 0;
      a[i][j] = now;
    }
  }
  for (int j = 0; j < m; j++) {
    while (!s.empty())
      s.pop();
    for (int i = 0; i < n; i++) {
      while (!s.empty()) {
        if (s.top().F > a[i][j]) {
          temp = s.top();
          s.pop();
          if (s.empty())
            ans = max(ans, temp.F * i);
          else
            ans = max(ans, temp.F * (i - 1 - s.top().S));
        } else
          break;
      }
      s.push(MP(a[i][j], i));
    }
    while (!s.empty()) {
      temp = s.top();
      s.pop();
      if (s.empty())
        ans = max(ans, temp.F * n);
      else
        ans = max(ans, temp.F * (n - s.top().S - 1));
    }
  }
  printf("%d\\n", ans);
}


Error: TLE

Fixed submission:

#include <stack>
#include <stdio.h>
#include <utility>
using namespace std;
typedef pair<int, int> pi;
#define F first
#define S second
#define MP make_pair
const int N = 5e2 + 10;
int main() {
  int n, m, a[N][N], now, ans, nx;
  pi temp;
  stack<pi> s;
  while (true) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    ans = 0;
    for (int i = 0; i < n; i++) {
      now = 0;
      for (int j = 0; j < m; j++) {
        scanf("%d", &nx);
        if (nx == 1)
          now++;
        else
          now = 0;
        a[i][j] = now;
      }
    }
    for (int j = 0; j < m; j++) {
      while (!s.empty())
        s.pop();
      for (int i = 0; i < n; i++) {
        while (!s.empty()) {
          if (s.top().F > a[i][j]) {
            temp = s.top();
            s.pop();
            if (s.empty())
              ans = max(ans, temp.F * i);
            else
              ans = max(ans, temp.F * (i - 1 - s.top().S));
          } else
            break;
        }
        s.push(MP(a[i][j], i));
      }
      while (!s.empty()) {
        temp = s.top();
        s.pop();
        if (s.empty())
          ans = max(ans, temp.F * n);
        else
          ans = max(ans, temp.F * (n - s.top().S - 1));
      }
    }
    printf("%d\\n", ans);
  }
}
Problem p02327 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define MAX_N 2000
int x[MAX_N][MAX_N];
int H, W;
int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> x[i][j];
    }
    if (x[i][0] == 1) {
      x[i][0] = 0;
    } else {
      x[i][0] = 1;
    }
    for (int j = 1; j < W; j++) {
      if (x[i][j] == 1) {
        x[i][j] = 0;
      } else {
        x[i][j] = x[i][j - 1] + 1;
      }
    }
  }
  int maxn = 0;
  for (int i = 0; i < W; i++) {
    for (int j = 0; j < H; j++) {
      int X = x[j][i], cnt = 0;
      if (x[j][i] == 0)
        continue;
      for (int k = j; k >= 0; k--) {
        cnt++;
        X = min(X, x[k][i]);
        if (X * (j + 1) <= maxn) {
          break;
        }
        maxn = max(maxn, X * cnt);
      }
    }
  }
  cout << maxn << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define MAX_N 2000
int x[MAX_N][MAX_N];
int H, W;
int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> x[i][j];
    }
    if (x[i][0] == 1) {
      x[i][0] = 0;
    } else {
      x[i][0] = 1;
    }
    for (int j = 1; j < W; j++) {
      if (x[i][j] == 1) {
        x[i][j] = 0;
      } else {
        x[i][j] = x[i][j - 1] + 1;
      }
    }
  }
  int maxn = 0;
  for (int i = 0; i < W; i++) {
    for (int j = 0; j < H; j++) {
      int X = x[j][i], cnt = 0;
      if (x[j][i] == 0)
        continue;
      for (int k = j; k >= 0; k--) {
        cnt++;
        X = min(X, x[k][i]);
        if (X == 0) {
          break;
        }
        maxn = max(maxn, X * cnt);
      }
    }
  }
  cout << maxn << endl;
  return 0;
}
Problem p02327 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int m[1400][1400];
int dp[1400][1400];

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

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

  for (int i = 0; i < b; ++i) {
    dp[0][i] = (m[0][i] == 0) ? 1 : 0;
  }

  for (int i = 1; i < a; ++i) {
    for (int j = 0; j < b; ++j) {
      dp[i][j] = (m[i][j] == 0) ? 1 + dp[i - 1][j] : 0;
    }
  }

  int resp = 0;

  for (int i = 0; i < a; ++i) {

    vector<int> izq(b, 0);
    izq[0] = -1;

    stack<pair<int, int>> el;
    el.push(make_pair(dp[i][0], 0));

    for (int j = 1; j < b; ++j) {
      while (el.top().first >= dp[i][j]) {
        el.pop();
        if (el.empty())
          break;
      }
      izq[j] = (el.empty()) ? -1 : el.top().second;
      el.push(make_pair(dp[i][j], j));
    }

    stack<pair<int, int>> el2;
    vector<int> der(b, 0);
    der[b - 1] = b;
    el2.push(make_pair(dp[i][b - 1], b - 1));

    for (int j = b - 2; j >= 0; --j) {
      while (el2.top().first >= dp[i][j]) {
        el2.pop();
        if (el2.empty())
          break;
      }
      der[j] = (el2.empty()) ? b : el2.top().second;
      el2.push(make_pair(dp[i][j], j));
    }

    for (int j = 0; j < b; ++j) {
      resp = max(resp, dp[i][j] * (der[j] - izq[j] - 1));
    }
  }

  printf("%d\\n", resp);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define N 1405
int H, W;
int R[N][N];

int rec(int f1, int c1, int f2, int c2) {
  if (f1 < 0 || f1 > H - 1 || f2 < 0 || f2 > H - 1 || c1 < 0 || c1 > W - 1 ||
      c2 < 0 || c2 > W - 1 || f1 > f2 || c1 > c2)
    return -1;
  if (f1 == f2 && c1 == c2)
    return 1 - R[f1][c1];
  for (int i = f1; i <= f2; i++)
    for (int j = c1; j <= c2; j++)
      if (R[i][j]) {
        return max(rec(f1, c1, i - 1, c2),
                   max(rec(i + 1, c1, f2, c2),
                       max(rec(f1, c1, f2, j - 1), rec(f1, j + 1, f2, c2))));
      }
  return (f2 - f1 + 1) * (c2 - c1 + 1);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> R[i][j];
    }
  }
  int a = rec(0, 0, H - 1, W - 1);
  cout << a << "\\n";

  return 0;
}