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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second

ll n;

vector<ll> t[1100];

bool search(ll now, ll num, bool used[]) {
  // cout << now << " " << num << endl;
  bool res = false;
  REP(i, t[now].size()) {
    if (used[t[now][i]])
      continue;
    if (t[now][i] == num)
      res = true;
    else {
      used[t[now][i]] = true;
      res |= search(t[now][i], num, used);
    }
  }
  return res;
}

int main() {
  bool ans = false;
  cin >> n;
  REP(i, n) {
    ll a, b;
    string s;
    cin >> a >> s >> b;
    b += 100;
    if (s == "lock") {
      t[a].push_back(b);
    } else {
      t[b].push_back(a);
    }
  }
  REP(i, 100) {
    bool used[1100] = {};
    ans |= search(i, i, used);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e15 + 9
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n;
int u, d;
string rel;
int data[110], user[110];
bool ans = 0;

void check(bool mode, int num, int search) {
  // cout << num << endl;
  if (mode == 0) {
    if (user[num] == 0)
      return;
    check(1, user[num], search);
  } else {
    if (data[num] == search) {
      ans = 1;
      return;
    }
    if (data[num] == 0)
      return;
    check(0, data[num], search);
  }
}

int main() {
  cin >> n;
  REP(i, n) {
    cin >> u >> rel >> d;
    if (rel == "lock") {
      data[d] = u;
    } else {
      user[u] = d;
    }
  }

  FOR(i, 1, 101) {
    if (user[i])
      check(1, user[i], i);
  }

  if (ans)
    cout << 1 << endl;
  else
    cout << 0 << endl;
}
Problem p00303 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

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

vector<int> g[200];

int main() {
  int n;
  cin >> n;
  REP(i, n) {
    int a, b;
    string str;
    cin >> a >> str >> b;
    a--;
    b--;
    b += 100;
    if (str == "lock")
      g[b].push_back(a);
    else
      g[a].push_back(b);
  }

  bool done[200];
  bool flag = false;
  REP(i, 100) {
    fill(done, done + 200, false);
    int st = i + 100;

    queue<int> que;
    REP(i, g[st].size()) que.push(g[st][i]);

    while (!que.empty()) {
      int now = que.front();
      que.pop();
      if (done[now])
        continue;
      done[now] = true;

      if (now == st) {
        flag = true;
        break;
      }

      REP(i, g[now].size()) que.push(g[now][i]);
    }
    if (flag)
      break;
  }

  puts(flag ? "1" : "0");

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

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

vector<int> g[200];

int main() {
  int n;
  cin >> n;
  REP(i, n) {
    int a, b;
    string str;
    cin >> a >> str >> b;
    a--;
    b--;
    b += 100;
    if (str == "lock")
      g[b].push_back(a);
    else
      g[a].push_back(b);
  }

  bool done[200];
  bool flag = false;
  REP(i, n) {
    fill(done, done + 200, false);
    int st = i + 100;

    queue<int> que;
    REP(i, g[st].size()) que.push(g[st][i]);

    while (!que.empty()) {
      int now = que.front();
      que.pop();
      if (done[now])
        continue;
      done[now] = true;

      if (now == st) {
        flag = true;
        break;
      }

      REP(i, g[now].size()) que.push(g[now][i]);
    }
    if (flag)
      break;
  }

  puts(flag ? "1" : "0");

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

#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 MP make_pair
typedef pair<int, int> pii;

int n;
vector<int> rods[200];
bool visited[200];
int nw;

bool chk(int pl) {
  visited[pl] = true;
  REP(i, rods[pl].size()) {
    int k = rods[pl][i];
    if (k == nw)
      return true;
    if (visited[k])
      continue;
    if (chk(k))
      return true;
  }
  return false;
}

int main() {
  scanf("%d", &n);
  REP(i, n) {
    int u, d;
    char s[5];
    scanf("%d%s%d", &u, s, &d);
    u--;
    d--;
    if (s[0] == 'l')
      rods[d + 100].PB(u);
    else
      rods[u].PB(d + 100);
  }
  REP(i, 100) {
    nw = i;
    fill(visited, visited + 200, false);
    if (chk(nw)) {
      printf("1\\n");
      return 0;
    }
  }
  printf("0\\n");
}

Error: 0

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 MP make_pair
typedef pair<int, int> pii;

int n;
vector<int> rods[200];
bool visited[200];
int nw;

bool chk(int pl) {
  visited[pl] = true;
  REP(i, rods[pl].size()) {
    int k = rods[pl][i];
    if (k == nw)
      return true;
    if (visited[k])
      continue;
    if (chk(k))
      return true;
  }
  return false;
}

int main() {
  scanf("%d", &n);
  REP(i, n) {
    int u, d;
    char s[5];
    scanf("%d%s%d", &u, s, &d);
    u--;
    d--;
    if (s[0] == 'l')
      rods[d + 100].PB(u);
    else
      rods[u].PB(d + 100);
  }
  REP(i, n) {
    nw = i;
    fill(visited, visited + 200, false);
    if (chk(nw)) {
      printf("1\\n");
      return 0;
    }
  }
  printf("0\\n");
}
Problem p00303 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

struct Person {
  Person() {
    lock_data = wait_data = -1;
    check = false;
  }
  int lock_data, wait_data;
  bool check;
};

struct Data {
  Data() { lock_person = -1; }
  int lock_person;
};

int main() {
  int n;

  Person person[101];
  Data data[101];

  char info[5];
  int left, right;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d %s %d", &left, info, &right);

    if (info[0] == 'w') {
      person[left].wait_data = right;
    } else {
      person[left].lock_data = right;
      data[right].lock_person = left;
    }
  }

  bool FLG = false;
  int tmp;
  for (int i = 1; i <= 100; i++) {
    if (person[i].wait_data != -1) {
      person[i].check = true;
      tmp = data[person[i].wait_data].lock_person;

      while (person[tmp].check == false && person[tmp].wait_data != -1) {
        person[tmp].check = true;
        tmp = data[person[tmp].wait_data].lock_person;
      }

      if (person[tmp].check == true) {
        FLG = true;
        break;
      } else {
        // tmp????§£?????????
        person[i].check = false;
        tmp = data[person[i].wait_data].lock_person;

        while (person[tmp].wait_data != -1) {
          person[tmp].check = false;
          tmp = data[person[tmp].wait_data].lock_person;
        }
      }
    }
  }

  if (!FLG) {
    printf("0\\n");
  } else {
    printf("1\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

struct Person {
  Person() { lock_data = wait_data = -1; }
  int lock_data, wait_data;
};

struct Data {
  Data() { lock_person = -1; }
  int lock_person;
};

int main() {
  int n;

  Person person[101];
  Data data[101];

  char info[5];
  int left, right;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d %s %d", &left, info, &right);

    if (info[0] == 'w') {
      person[left].wait_data = right;
    } else {
      person[left].lock_data = right;
      data[right].lock_person = left;
    }
  }

  bool FLG = false;
  int tmp;
  for (int i = 1; i <= 100; i++) {
    if (person[i].wait_data != -1) {
      tmp = data[person[i].wait_data].lock_person;

      while (tmp != i && person[tmp].wait_data != -1) {
        tmp = data[person[tmp].wait_data].lock_person;
      }

      if (tmp == i) {
        FLG = true;
        break;
      }
    }
  }

  if (!FLG) {
    printf("0\\n");
  } else {
    printf("1\\n");
  }
  return 0;
}
Problem p00303 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
vector<int> G[240];
int dist[240];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    string S;
    cin >> a >> S >> b;
    a--;
    b--;
    if (S == "wait") {
      G[a].push_back(b + 120);
    }
    if (S == "lock") {
      G[b + 120].push_back(a);
    }
  }
  int C = 0;
  for (int i = 0; i < 240; i++) {
    queue<int> Q;
    for (int j = 0; j < 240; j++)
      dist[j] = 0;
    Q.push(i);
    int cnt = 0;
    while (!Q.empty()) {
      cnt++;
      int a1 = Q.front();
      Q.pop();
      if (cnt >= 100000) {
        C = 1;
        break;
      }
      for (int j = 0; j < G[a1].size(); j++) {
        if (dist[G[a1][j]] > dist[a1] - 1) {
          dist[G[a1][j]] = dist[a1] - 1;
          Q.push(G[a1][j]);
        }
      }
    }
  }
  cout << C << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
vector<int> G[240];
int dist[240];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    string S;
    cin >> a >> S >> b;
    a--;
    b--;
    if (S == "wait") {
      G[a].push_back(b + 120);
    }
    if (S == "lock") {
      G[b + 120].push_back(a);
    }
  }
  int C = 0;
  for (int i = 0; i < 2 * n; i++) {
    queue<int> Q;
    for (int j = 0; j < 240; j++)
      dist[j] = 0;
    Q.push(i);
    int cnt = 0;
    while (!Q.empty()) {
      cnt++;
      int a1 = Q.front();
      Q.pop();
      if (cnt >= 100000) {
        C = 1;
        break;
      }
      for (int j = 0; j < G[a1].size(); j++) {
        if (dist[G[a1][j]] > dist[a1] - 1) {
          dist[G[a1][j]] = dist[a1] - 1;
          Q.push(G[a1][j]);
        }
      }
    }
  }
  cout << C << endl;
  return 0;
}
Problem p00303 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

bool visited[200];
vector<int> path[200];

bool solve(int num, int s) {

  bool ret = false;

  if (num == s && visited[num]) {
    return true;
  }

  for (int i = 0; i < path[num].size(); ++i) {
    if (!visited[path[num][i]]) {
      visited[path[num][i]] = true;
      ret |= solve(path[num][i], s);
    } else {
      // return true;
    }
  }

  return ret;
}

int main() {

  int n;
  bool dead_locked = false;

  cin >> n;

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

    int u, d;
    string command;

    cin >> u >> command >> d;

    --u;
    --d;

    if (command == "lock") {
      path[100 + d].push_back(u);
    } else if (command == "wait") {
      path[u].push_back(100 + d);
    }
  }

  for (int i = 0; i < 200; ++i) {
    memset(visited, false, sizeof(visited));
    // visited[i] = true;
    dead_locked |= solve(i, i);
    /*
    if(dead_locked) {
        cout << i << endl;
    }
    */
  }

  cout << dead_locked << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

bool visited[200];
vector<int> path[200];

bool solve(int num) {

  bool ret = false;

  for (int i = 0; i < path[num].size(); ++i) {
    if (!visited[path[num][i]]) {
      visited[path[num][i]] = true;
      ret |= solve(path[num][i]);
      visited[path[num][i]] = false;
    } else {
      return true;
    }
  }

  return ret;
}

int main() {

  int n;
  bool dead_locked = false;

  cin >> n;

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

    int u, d;
    string command;

    cin >> u >> command >> d;

    --u;
    --d;

    if (command == "lock") {
      path[100 + d].push_back(u);
    } else if (command == "wait") {
      path[u].push_back(100 + d);
    }
  }

  for (int i = 0; i < 200; ++i) {
    memset(visited, false, sizeof(visited));
    visited[i] = true;
    dead_locked |= solve(i);
    /*
    if(dead_locked) {
        cout << i << endl;
    }
    */
  }

  cout << dead_locked << endl;
}
Problem p00303 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod 1e9 + 7

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int n;
vector<int> u[101];
int d[101];
int f = 0;

void solve(int i, int m, map<int, int> loopU, map<int, int> loopD) {
  if (f)
    return;
  if (m == 0) {
    if (loopD[i]) {
      f = 1;
      return;
    }
    loopD[i] = 1;
    if (d[i])
      solve(d[i], !m, loopU, loopD);
  } else {
    if (loopU[i]) {
      f = 1;
      return;
    }
    loopU[i] = 1;
    rep(j, 0, u[i].size()) { solve(u[i][j], !m, loopU, loopD); }
  }
}

int main() {
  cin >> n;
  rep(i, 0, n) {
    int a, b;
    string s;
    cin >> a >> s >> b;
    if (s[0] == 'l') {
      d[b] = a;
    } else {
      u[a].pb(b);
    }
  }
  rep(i, 1, 101) {
    if (d[i]) {
      map<int, int> a, b;
      solve(i, 0, a, b);
    }
  }
  rep(i, 1, 101) {
    if (u[i].size() > 0) {
      map<int, int> a, b;
      solve(i, 1, a, b);
    }
  }
  cout << f << endl;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod 1e9 + 7

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int n;
vector<int> u[101];
int d[101] = {};
int f = 0;

void solve(int i, int m, map<int, int> loopU, map<int, int> loopD) {
  if (m == 0) {
    if (loopD[i]) {
      f = 1;
      return;
    }
    loopD[i] = 1;
    if (d[i])
      solve(d[i], !m, loopU, loopD);
  } else {
    if (loopU[i]) {
      f = 1;
      return;
    }
    loopU[i] = 1;
    rep(j, 0, u[i].size()) { solve(u[i][j], !m, loopU, loopD); }
  }
}

int main() {
  cin >> n;
  rep(i, 0, n) {
    int a, b;
    string s;
    cin >> a >> s >> b;
    if (s[0] == 'l') {
      d[b] = a;
    } else {
      u[a].pb(b);
    }
  }
  rep(i, 1, 101) {
    if (d[i]) {
      map<int, int> a, b;
      solve(i, 0, a, b);
    }
  }
  rep(i, 1, 101) {
    if (u[i].size() > 0) {
      map<int, int> a, b;
      solve(i, 1, a, b);
    }
  }
  cout << f << endl;
}
Problem p00305 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

const int IINF = INT_MAX;
const int MAX = 310;
int n, p[MAX][MAX], sum[2][MAX][MAX]; // 0 : h, 1 : w

void compute() {
  rep(y, n) {
    sum[0][y][0] = p[y][0];
    REP(x, 1, n) { sum[0][y][x] = sum[0][y][x - 1] + p[y][x]; }
  }
  rep(x, n) {
    sum[1][0][x] = p[0][x];
    REP(y, 1, n) { sum[1][y][x] = sum[1][y - 1][x] + p[y][x]; }
  }
  int maxi = -IINF;
  rep(y1, n) REP(y2, y1 + 1, n) {
    int maxi2 = 0;
    int v = sum[0][y1][0] + sum[0][y2][0];
    v += sum[1][y2][0];
    if (y1 - 1 >= 0)
      v -= sum[1][y1 - 1][0];
    v -= p[y1][0];
    v -= p[y2][0];
    maxi = max(maxi, v);
    REP(x, 1, n) {
      int t = sum[1][y2][x - 1];
      if (y1 - 1 >= 0)
        t -= sum[1][y1 - 1][x - 1];
      t -= p[y1][x - 1];
      t -= p[y2][x - 1];
      if (x - 2 >= 0)
        t -= sum[0][y1][x - 2];
      if (x - 2 >= 0)
        t -= sum[0][y2][x - 2];
      maxi2 = max(maxi2, t);

      int v = sum[0][y1][x] + sum[0][y2][x];
      v += sum[1][y2][x];
      if (y1 - 1 >= 0)
        v -= sum[1][y1 - 1][x];
      v -= p[y1][x];
      v -= p[y2][x];
      maxi = max(maxi, v + maxi2);
    }
  }

  rep(y1, n) {
    int mini = 0;
    rep(x, n) {
      maxi = max(maxi, sum[0][y1][x] - mini);
      mini = min(mini, sum[0][y1][x]);
    }
  }

  cout << maxi << endl;
}

int main() {
  scanf("%d", &n);
  rep(i, n) rep(j, n) scanf(" %d", &p[i][j]);
  compute();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

const int MAX = 310;
int n, p[MAX][MAX], sum[MAX][MAX];

//??????(x1,y1), ??????(x2,y2)
int getRangeSum(int x1, int y1, int x2, int y2) {
  int ret = sum[y2][x2];
  if (x1 - 1 >= 0 && y1 - 1 >= 0)
    ret += sum[y1 - 1][x1 - 1];
  if (x1 - 1 >= 0)
    ret -= sum[y2][x1 - 1];
  if (y1 - 1 >= 0)
    ret -= sum[y1 - 1][x2];
  return ret;
}

void compute() {
  rep(i, n) rep(j, n) {
    sum[i][j] = p[i][j];
    if (i - 1 >= 0 && j - 1 >= 0)
      sum[i][j] -= sum[i - 1][j - 1];
    if (i - 1 >= 0)
      sum[i][j] += sum[i - 1][j];
    if (j - 1 >= 0)
      sum[i][j] += sum[i][j - 1];
  }

  int maxi = -INT_MAX;
  rep(i, n * n) REP(j, i, n * n) {
    int x1 = i % n, y1 = i / n;
    int x2 = j % n, y2 = j / n;
    if (x2 < x1 || y2 < y1)
      continue;
    if (abs(x1 - x2) <= 1 || abs(y1 - y2) <= 1)
      maxi = max(maxi, getRangeSum(x1, y1, x2, y2));
    else {
      int inner_x1 = x1 + 1, inner_y1 = y1 + 1;
      int inner_x2 = x2 - 1, inner_y2 = y2 - 1;
      maxi = max(maxi, getRangeSum(x1, y1, x2, y2) -
                           getRangeSum(inner_x1, inner_y1, inner_x2, inner_y2));
    }
  }
  cout << maxi << endl;
}

int main() {
  scanf("%d", &n);
  assert(1 <= n && n <= 300);
  rep(i, n) rep(j, n) scanf(" %d", &p[i][j]);
  compute();
  return 0;
}
Problem p00305 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int sum[310][310];
int N;

int get(int x1, int y1, int x2, int y2) {
  return sum[y2 + 1][x2 + 1] - sum[y2 + 1][x1] - sum[y1][x2 + 1] + sum[y1][x1];
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      scanf("%d", &sum[i + 1][j + 1]);
      sum[i + 1][j + 1] += sum[i][j + 1] + sum[i + 1][j] - sum[i][j];
    }
  }

  int ma = INT_MIN;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      int prev = get(0, i, 0, j);
      ma = max(ma, prev);
      for (int k = 1; k < N; k++) {
        ma = max(ma, max(0, prev) + get(k, i, k, j));
        prev = max(prev + get(k, i, k, i) + get(k, j, k, j), get(k, i, k, j));
      }
    }
  }

  for (int i = 0; i < N; i++) {
    int prev = get(0, i, 0, i);
    ma = max(prev, ma);
    for (int j = 1; j < N; j++) {
      prev = max(get(j, i, j, i), prev + get(j, i, j, i));
      ma = max(ma, prev);
    }
  }

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

Error: TLE

Fixed submission:

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

int a[400][400];
int N;
int main() {
  cin >> N;
  int ma = INT_MIN;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i + 1][j + 1];
      ma = max(ma, a[i + 1][j + 1]);
      a[i + 1][j + 1] += a[i][j + 1] + a[i + 1][j] - a[i][j];
    }
  }

  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++) {
          int x = a[k + 1][l + 1] - a[k + 1][j] - a[i][l + 1] + a[i][j];
          int y = a[k][l] - a[k][j + 1] - a[i + 1][l] + a[i + 1][j + 1];
          ma = max(ma, x - y);
        }
      }
    }
  }
  cout << ma << endl;
  return 0;
}
Problem p00305 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define N 310
#define INF (1e9)
using namespace std;
int n, p[N][N], ans, c, b, ma, d[N][N];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      cin >> p[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      d[i][j] = p[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 2; j <= n; j++)
      d[i][j] = d[i][j - 1] + p[i][j];
  for (int i = 2; i <= n; i++)
    for (int j = 1; j <= n; j++)
      d[i][j] = d[i - 1][j] + d[i][j];
  ans = p[1][1];
  for (int k = 1; k <= n; k++) {
    for (int l = k; l <= n; l++) {
      b = c = 1;
      while (c != n) {
        ma = -INF;
        for (int i = c + 1; i <= n; i++) {
          int sum = d[i][l] - d[b - 1][l] - d[i][k - 1] + d[b - 1][k - 1],
              sum2 = 0;
          if (i - b >= 2 && l - k >= 2)
            sum2 = d[i - 1][l - 1] - d[b][l - 1] - d[i - 1][k] + d[b][k];
          if (ma < sum - sum2)
            ma = sum - sum2, c = i;
        }
        ans = max(ans, ma);
        for (int i = b; i <= c; i++) {
          int sum = d[c][l] - d[i - 1][l] - d[c][k - 1] + d[i - 1][k - 1],
              sum2 = 0;
          if (c - i >= 2 && l - k >= 2)
            sum2 = d[c - 1][l - 1] - d[i][l - 1] - d[c - 1][k] + d[i][k];
          ans = max(ans, sum - sum2);
        }
        b = c;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define N 310
#define INF (1e9)
using namespace std;
int n, p[N][N], ans, c, b, ma, d[N][N];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      cin >> p[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      d[i][j] = p[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 2; j <= n; j++)
      d[i][j] = d[i][j - 1] + p[i][j];
  for (int i = 2; i <= n; i++)
    for (int j = 1; j <= n; j++)
      d[i][j] = d[i - 1][j] + d[i][j];
  ans = p[0][0];
  for (int k = 1; k <= n; k++) {
    for (int l = k; l <= n; l++) {
      b = c = 1;
      while (b != n) {
        ma = -INF;
        for (int i = c; i <= n; i++) {
          int sum = d[i][l] - d[b - 1][l] - d[i][k - 1] + d[b - 1][k - 1],
              sum2 = 0;
          if (i - b >= 2 && l - k >= 2)
            sum2 = d[i - 1][l - 1] - d[b][l - 1] - d[i - 1][k] + d[b][k];
          if (ma < sum - sum2)
            ma = sum - sum2, c = i;
        }
        ans = max(ans, ma);
        for (int i = b + 1; i < c; i++) {
          int sum = d[i][l] - d[b - 1][l] - d[i][k - 1] + d[b - 1][k - 1],
              sum2 = 0;
          if (i - b >= 2 && l - k >= 2)
            sum2 = d[i - 1][l - 1] - d[b][l - 1] - d[i - 1][k] + d[b][k];
          ans = max(ans, sum - sum2);
        }
        b = c;
      }
    }
  }
  cout << ans << endl;
  return 0;
}