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

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

vector<int> e[100], v;
int n, s[100][100], res;
void F() {
  int i, j, t, c;
  if (v.size() > 1) {
    for (c = i = 0; i < v.size(); ++i) {
      t = 2e9;
      for (j = 0; j < v.size(); ++j) {
        if (i == j)
          continue;
        t = min(t, s[v[i]][v[j]]);
      }
      c += t;
    }
    res = max(res, c);
  }
  if (v.size()) {
    for (i = v[v.size() - 1] + 1; i < n; ++i) {
      for (j = 0; j < v.size(); ++j)
        if (!std::binary_search(e[i].begin(), e[i].end(), v[j]))
          goto N;
      v.push_back(i);
      F();
      v.pop_back();
    N:;
    }
  } else {
    for (i = 0; i < n - 1; ++i) {
      v.push_back(i);
      F();
      v.pop_back();
    }
  }
}
int main() {
  int m, a, b, f;
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d%d", &a, &b, &f);
    --a, --b;
    e[a].push_back(b);
    e[b].push_back(a);
    s[a][b] = s[b][a] = f;
  }
  for (m = 0; m < n; ++m)
    sort(e[m].begin(), e[m].end());
  F();
  printf("%d\\n", res);
  return 0;
}

Error: TLE

Fixed submission:

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

vector<int> e[100], v;
int n, s[100][100], res;
void F() {
  int i, j, t, c;
  if (v.size() > 1) {
    for (c = i = 0; i < v.size(); ++i) {
      t = INT_MAX;
      for (j = 0; j < v.size(); ++j) {
        if (i == j)
          continue;
        t = min(t, s[v[i]][v[j]]);
      }
      c += t;
    }
    res = max(res, c);
  }
  if (v.size()) {
    for (i = v[0] + 1; i < n; ++i) {
      for (j = 1; j < v.size(); ++j)
        if (find(e[v[j]].begin(), e[v[j]].end(), i) == e[v[j]].end())
          goto N;
      v.push_back(i);
      F();
      v.pop_back();
    N:;
    }
  } else {
    for (i = 0; i < n; ++i) {
      v.push_back(i);
      F();
      v.pop_back();
    }
  }
}
int main() {
  int m, a, b, f;
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d%d", &a, &b, &f);
    --a, --b;
    e[a].push_back(b);
    e[b].push_back(a);
    s[a][b] = s[b][a] = f;
  }
  F();
  printf("%d\\n", res);
  return 0;
}
Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define INF (1 << 30)
const int N = 105;
int n, m;
int adj[N][N], set[N], res, sum;
bool judge(int ind, int cnt) {
  for (int i = 0; i < cnt; i++) {
    if (adj[ind][set[i]] == 0)
      return false;
  }
  return true;
}
int calu(int cnt) {
  if (cnt <= 1)
    return 0;
  int sum = 0;
  for (int i = 0; i < cnt; i++) {
    int imin = INF;
    for (int j = 0; j < cnt; j++) {
      if (i == j)
        continue;
      imin = min(imin, adj[set[i]][set[j]]);
    }
    sum += imin;
  }
  return sum;
}
void dfs(int ind, int cnt) {
  // cout<<ind<<" "<<cnt<<endl;
  if (ind == n + 1) {
    res = max(res, calu(cnt));
    return;
  }
  if (judge(ind, cnt)) {
    set[cnt] = ind;
    dfs(ind + 1, cnt + 1);
  }
  dfs(ind + 1, cnt);
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    res = sum = 0;
    memset(adj, 0, sizeof(adj));
    memset(set, 0, sizeof(set));

    for (int i = 0; i < m; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      adj[a][b] = adj[b][a] = c;
    }
    dfs(1, 0);
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string.h>
#define clearAll(a) memset(a, 0, sizeof(a))
#define oo 1000000000
#define maxn 105
using namespace std;

struct edge {
  int x, y, d;
};

int n, m;
int h[maxn];
int g[maxn][maxn];
int set[maxn];
edge a[maxn * maxn];

bool cmp(edge a, edge b) { a.d > b.d; }

int find(int x) { return set[x] == x ? x : set[x] = find(set[x]); }

int main() {
  scanf("%d %d", &n, &m);
  clearAll(g);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].d);
    g[a[i].x][a[i].y] = g[a[i].y][a[i].x] = a[i].d;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++)
      if (g[i][j] == 0) {
        a[++m].x = i;
        a[m].y = j;
        a[m].d = 0;
      }

  sort(a + 1, a + m + 1, cmp);
  bool isIn[maxn];
  clearAll(isIn);
  int ans = 0;
  int rest = n;
  int tot;
  while (true) {
    int cnt = 0;
    if (rest == 0)
      break;
    tot = 0;
    clearAll(h);
    for (int i = 1; i <= n; i++)
      set[i] = i;
    for (int i = 1; i <= m; i++) {
      if (cnt == rest - 1)
        break;
      int x = a[i].x, y = a[i].y;
      if (isIn[x] || isIn[y])
        continue;
      int xx = find(x), yy = find(y);
      if (xx == yy)
        continue;
      set[xx] = yy;
      if (h[xx] == 0) {
        h[xx] = a[i].d;
        tot += h[xx];
      }
      if (h[yy] == 0) {
        h[yy] = a[i].d;
        tot += h[yy];
      }
      cnt++;
    }

    if (tot > ans)
      ans = tot;
    isIn[a[1].x] = isIn[a[1].y] = true;
    rest -= 2;
  }

  printf("%d\\n", tot);
  return 0;
}
Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define debug(x) cerr << #x << ": " << x << endl

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct Edge {
  int from, to;
  ll cost;
};

const ll MOD = 1000000007;
const long double EPS = 10e-10;

int n, m;
ll ans = 0LL;
ll G[100][100];

void dfs(int i, int cnt, int max_cnt, vector<bool> v) {
  if (cnt == max_cnt) {
    ll tmp_ans = 0LL;
    repp(x, n) {
      if (!v[x])
        continue;
      ll tmp = LLONG_MAX;
      repp(y, n) {
        if (!v[y] || x == y)
          continue;
        tmp = min(tmp, G[x][y]);
      }
      tmp_ans += tmp;
    }
    ans = max(ans, tmp_ans);
    return;
  }
  if (i == n)
    return;

  for (int j = i; j < n; j++) {
    bool flag = true;
    repp(x, n) {
      if (v[x] && G[j][x] == 0LL) {
        flag = false;
        break;
      }
    }
    if (flag) {
      v[j] = true;
      dfs(j + 1, cnt + 1, max_cnt, v);
      v[j] = false;
    }
  }
  return;
}

int main() {
  int u, v;
  ll f;
  cin >> n >> m;
  rep(m) {
    cin >> u >> v >> f;
    u--;
    v--;
    G[u][v] = G[v][u] = f;
  }

  reppp(i, 2, 15) {
    if (2 * m / (i - 1) < i)
      break;
    dfs(0, 0, i, vector<bool>(n, false));
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define debug(x) cerr << #x << ": " << x << endl

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct Edge {
  int from, to;
  ll cost;
};

const ll MOD = 1000000007;
const long double EPS = 10e-10;

int n, m;
ll ans = 0LL;
ll G[100][100];

void dfs(set<int> st, vector<bool> used, size_t max_size) {
  if (st.size() <= max_size) {
    ll new_score = 0LL;
    if (st.size() > 1)
      for (int x : st) {
        ll tmp = LLONG_MAX;
        for (int y : st) {
          if (x == y)
            continue;
          tmp = min(tmp, G[x][y]);
        }
        new_score += tmp;
      }
    ans = max(ans, new_score);
    if (st.size() == max_size)
      return;
  }

  rep(n) {
    if (used[i])
      continue;
    bool can_add = true;
    for (int x : st) {
      if (!G[i][x]) {
        can_add = false;
        break;
      }
    }
    if (can_add) {
      st.insert(i);
      used[i] = true;
      dfs(st, used, max_size);
      used[i] = false;
      st.erase(i);
    }
  }
}

int main() {
  int u, v;
  ll f;
  cin >> n >> m;
  rep(m) {
    cin >> u >> v >> f;
    u--;
    v--;
    G[u][v] = G[v][u] = f;
  }

  ll i = 1LL;
  while (i == 1 || 2 * m / (i - 1) >= i)
    i++;
  dfs(set<int>(), vector<bool>(n, false), i - 1);
  cout << ans << endl;
}
Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m;
int v[100][100];

set<set<int>> mem;

int dfs(set<int> used) {
  int cost = 0;
  for (auto i : used) {
    int min_cost = 1 << 28;
    for (auto j : used) {
      if (i == j)
        continue;
      min_cost = min(min_cost, v[i][j]);
    }
    if (min_cost == 1 << 28)
      min_cost = 0;
    cost += min_cost;
  }

  mem.insert(used);

  for (int i = 0; i < n; i++) {
    if (used.find(i) != used.end())
      continue;

    bool flag = true;
    for (auto j : used) {
      if (v[i][j] == 0)
        flag = false;
    }

    if (flag) {
      auto tmp = used;
      tmp.insert(i);
      if (mem.find(tmp) == mem.end())
        cost = max(cost, dfs(tmp));
    }
  }

  return cost;
}

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

  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    v[x - 1][y - 1] = v[y - 1][x - 1] = z;
  }

  cout << dfs(set<int>()) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int n, m;
int v[100][100];

int dfs(set<int> used) {
  int cost = 0;
  for (auto i : used) {
    int min_cost = 1 << 28;
    for (auto j : used) {
      if (i == j)
        continue;
      min_cost = min(min_cost, v[i][j]);
    }
    if (min_cost == 1 << 28)
      min_cost = 0;
    cost += min_cost;
  }

  for (int i = 0; i < n; i++) {
    if (used.find(i) != used.end())
      continue;

    bool flag = true;
    for (auto j : used) {
      if (v[i][j] == 0)
        flag = false;
    }

    if (flag) {
      auto tmp = used;
      tmp.insert(i);
      cost = max(cost, dfs(tmp));
    }
  }

  return cost;
}

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

  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    v[x - 1][y - 1] = v[y - 1][x - 1] = z;
  }

  cout << dfs(set<int>()) << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define INF 0X7FFFFFFF
#define LL long long
using namespace std;
int N, M;
LL ans = 0;
int map[105][105];
int F[105];
int Node[105];
bool flag[105];
inline void dfs(int cnt) {
  LL temp = 0;
  for (int i = 1; i <= cnt - 1; i++)
    Node[F[i]] = INF;
  for (int i = 1; i <= cnt - 1; i++)
    for (int j = 1; j <= cnt - 1; j++) {
      if (i == j)
        continue;
      Node[F[i]] = min(Node[F[i]], map[F[i]][F[j]]);
    }
  for (int i = 1; i <= cnt - 1; i++) {
    if (Node[F[i]] == INF)
      Node[F[i]] = 0;
    temp += Node[F[i]];
  }
  if (temp > ans)
    ans = temp;
  if (cnt > 14)
    return;
  for (int i = F[cnt - 1]; i <= N; i++) {
    if (flag[i])
      continue;
    bool tag = 0;
    for (int j = 1; j < cnt; j++) {
      if (i == F[j])
        continue;
      if (!map[i][F[j]]) {
        tag = 1;
        break;
      }
    }
    if (tag == 1)
      continue;
    F[cnt] = i;
    flag[i] = 1;
    dfs(cnt + 1);
    flag[i] = 0;
  }
}
int main() {
  int i, a, b, c;
  memset(map, 0, sizeof(map));
  scanf("%d%d", &N, &M);
  for (i = 1; i <= M; i++) {
    scanf("%d%d%d", &a, &b, &c);
    map[a][b] = c;
    map[b][a] = c;
  }
  dfs(1);
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define INF 0X7FFFFFFF
#define LL long long
using namespace std;
int N, M;
LL ans = 0;
int map[105][105];
int F[105];
int Node[105];
bool flag[105];
inline void dfs(int cnt) {
  LL temp = 0;
  for (int i = 1; i <= cnt - 1; i++)
    Node[F[i]] = INF;
  for (int i = 1; i <= cnt - 1; i++)
    for (int j = 1; j <= cnt - 1; j++) {
      if (i == j)
        continue;
      Node[F[i]] = min(Node[F[i]], map[F[i]][F[j]]);
    }
  for (int i = 1; i <= cnt - 1; i++) {
    if (Node[F[i]] == INF)
      Node[F[i]] = 0;
    temp += Node[F[i]];
  }
  if (temp > ans)
    ans = temp;
  if (cnt > N)
    return;
  for (int i = 1; i <= N; i++) {
    if (flag[i])
      continue;
    bool tag = 0;
    for (int j = 1; j < cnt; j++) {
      if (i == F[j])
        continue;
      if (!map[i][F[j]]) {
        tag = 1;
        break;
      }
    }
    if (tag == 1)
      continue;
    F[cnt] = i;
    flag[i] = 1;
    dfs(cnt + 1);
    flag[i] = 0;
  }
}
int main() {
  int i, a, b, c;
  memset(map, 0, sizeof(map));
  scanf("%d%d", &N, &M);
  for (i = 1; i <= M; i++) {
    scanf("%d%d%d", &a, &b, &c);
    map[a][b] = c;
    map[b][a] = c;
  }
  dfs(1);
  printf("%lld\\n", ans);
  return 0;
}
Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
const int INF = 1e8;
using namespace std;

#define int long long

int n, m;
int g[105][105];
int ans;
set<vector<bool>> memo;

void dfs(vector<bool> used) {
  if (not(memo.emplace(used)).second)
    return;
  // for(auto i : used){ cout << i << ' ' ; } cout << endl;

  vector<int> to;
  rep(i, n) {
    if (used[i])
      continue;
    bool f = true;
    rep(j, n) {
      if (not used[j])
        continue;
      if (i == j || g[j][i] == 0) {
        f = false;
        break;
      }
    }
    if (f)
      to.emplace_back(i);
  }

  for (auto i : to) {
    used[i] = true;
    dfs(used);
    used[i] = false;
  }

  int sum = 0;
  rep(i, n) {
    if (not used[i])
      continue;
    int mini = INF;
    rep(j, n) {
      if (not used[j])
        continue;
      if (g[i][j] == 0)
        continue;
      mini = min(mini, g[i][j]);
    }
    if (mini == INF)
      continue;
    sum += mini;
  }
  ans = max(ans, sum);
}

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

  vector<int> v;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[a][b] = g[b][a] = c;
  }

  rep(i, n) {
    vector<bool> tmp(n, 0);
    tmp[i] = true;
    dfs(tmp);
  }
  cout << ans << endl;

  // for(auto i : memo){ for(auto j : i){ cout << j << ' '; } cout << endl; }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
const int INF = 1e8;
using namespace std;

#define int long long

int n, m;
int g[105][105];
int ans;
set<vector<bool>> memo;

void dfs(vector<bool> used) {
  if (not(memo.emplace(used)).second)
    return;
  // for(auto i : used){ cout << i << ' ' ; } cout << endl;

  vector<int> to;
  rep(i, n) {
    if (used[i])
      continue;
    bool f = true;
    rep(j, n) {
      if (not used[i])
        continue;
      if (i == j || g[j][i] == 0) {
        f = false;
        break;
      }
    }
    if (f)
      to.emplace_back(i);
  }

  for (auto i : to) {
    used[i] = true;
    dfs(used);
    used[i] = false;
  }

  int sum = 0;
  rep(i, n) {
    if (not used[i])
      continue;
    int mini = INF;
    rep(j, n) {
      if (not used[j])
        continue;
      if (g[i][j] == 0)
        continue;
      mini = min(mini, g[i][j]);
    }
    if (mini == INF)
      continue;
    sum += mini;
  }
  ans = max(ans, sum);
}

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

  vector<int> v;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[a][b] = g[b][a] = c;
  }

  rep(i, n) {
    vector<bool> tmp(n, 0);
    tmp[i] = true;
    dfs(tmp);
  }
  cout << ans << endl;

  // for(auto i : memo){ for(auto j : i){ cout << j << ' '; } cout << endl; }
}
Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m;
int ans;
vector<int> neighbors[100];
set<set<int>> used;
int weight[100][100];

void dfs(set<int> vs) {
  if (used.count(vs))
    return;
  used.insert(vs);

  int score = 0;
  if (vs.size() > 1) {
    for (auto &&i : vs) {
      int c = 1e9;
      for (auto &&j : vs) {
        if (i != j) {
          c = min(weight[i][j], c);
        }
      }
      score += c;
    }
  }
  ans = max(ans, score);

  const int representative = *vs.begin();

  int cost[100];
  for (auto &&i : neighbors[representative])
    cost[i] = 1e9;

  for (auto &&i : vs) {
    for (auto &&j : neighbors[representative]) {
      cost[j] = min(weight[i][j], cost[j]);
    }
  }
  for (auto &&i : neighbors[representative])
    if (!vs.count(i) and cost[i] > 0) {
      set<int> nvs = vs;
      nvs.insert(i);
      dfs(nvs);
    }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    weight[a][b] = c;
    weight[b][a] = c;
    neighbors[a].push_back(b);
    neighbors[b].push_back(a);
  }
  ans = 0;
  for (int i = 0; i < n; i++) {
    dfs({i});
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int n, m;
int ans;
vector<int> neighbors[100];
set<set<int>> used;
int weight[100][100];

void dfs(set<int> vs) {
  if (used.count(vs))
    return;

  int score = 0;
  if (vs.size() > 1) {
    for (auto &&i : vs) {
      int c = 1e9;
      for (auto &&j : vs) {
        if (i != j) {
          c = min(weight[i][j], c);
        }
      }
      score += c;
    }
  }
  ans = max(ans, score);

  const int representative = *vs.begin();

  int cost[100];
  for (auto &&i : neighbors[representative])
    cost[i] = 1e9;

  for (auto &&i : vs) {
    for (auto &&j : neighbors[representative]) {
      cost[j] = min(weight[i][j], cost[j]);
    }
  }
  for (auto &&i : neighbors[representative])
    if (!vs.count(i) and cost[i] > 0) {
      set<int> nvs = vs;
      nvs.insert(i);
      dfs(nvs);
    }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    weight[a][b] = c;
    weight[b][a] = c;
    neighbors[a].push_back(b);
    neighbors[b].push_back(a);
  }
  ans = 0;
  for (int i = 0; i < n; i++) {
    dfs({i});
  }
  cout << ans << endl;
}
Problem p01427 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>

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

using namespace std;

struct angle {
  int x, y, delta;
  bool operator<(const angle &a) const {
    return y * a.x == x * a.y ? delta < a.delta : y * a.x < x * a.y;
  }
};

#define O(i, j) (0 <= i && i < h && 0 <= j && j < w && B[i][j] == '#')
#define X(i, j) (!O(i, j))

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  char B[600][601];
  rep(i, h) scanf("%s", B[i]);

  vector<angle> A;
  rep(i, h + 1) rep(j, w + 1) {
    if (O(i - 1, j - 1) && X(i - 1, j) && X(i, j - 1) && X(i, j))
      A.push_back((angle){j, h - i, 1});
    if (X(i - 1, j - 1) && O(i - 1, j) && O(i, j - 1) && O(i, j))
      A.push_back((angle){j, h - i, 1});
    if (O(i - 1, j - 1) && O(i - 1, j) && O(i, j - 1) && X(i, j))
      A.push_back((angle){j, h - i, -1});
    if (X(i - 1, j - 1) && X(i - 1, j) && X(i, j - 1) && O(i, j))
      A.push_back((angle){j, h - i, -1});
  }
  sort(A.begin(), A.end());

  int ans = 1, num = 1;
  rep(i, A.size()) num += A[i].delta, ans = max(ans, num);
  printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

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

struct Arg {
  int x, y, t;
  bool operator<(const Arg &a) const {
    if (y * a.x == a.y * x)
      return t < a.t;
    else
      y *a.x < a.y *x;
  }
};

int H, W;
map<int, map<int, char>> G;

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> G[i][j];
    }
  }
  vector<Arg> v;
  for (int i = 0; i <= H; ++i) {
    for (int j = 0; j <= W; ++j) {
      bool a = G[i - 1][j - 1] == '#';
      bool b = G[i - 1][j] == '#';
      bool c = G[i][j - 1] == '#';
      bool d = G[i][j] == '#';
      if (a && !b && !c && !d)
        v.push_back((Arg){j, H - i, 1});
      if (!a && b && c && d)
        v.push_back((Arg){j, H - i, 1});
      if (!a && !b && !c && d)
        v.push_back((Arg){j, H - i, -1});
      if (a && b && c && !d)
        v.push_back((Arg){j, H - i, -1});
    }
  }
  sort(v.begin(), v.end());
  int now = 1, maxi = now;
  for (int i = 0; i < v.size(); ++i) {
    now += v[i].t;
    maxi = max(maxi, now);
  }
  cout << maxi << endl;
  return 0;
}
Problem p01427 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> pii;

int main() {
  int H, W;
  cin >> H >> W;
  string F[H];
  for (int i = 0; i < H; ++i) {
    cin >> F[i];
  }
  int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  pair<pii, pii> e[4];
  e[0] = make_pair(pii(1, 0), pii(1, 1));
  e[1] = make_pair(pii(0, 1), pii(1, 1));
  e[2] = make_pair(pii(0, 0), pii(0, 1));
  e[3] = make_pair(pii(0, 0), pii(1, 0));
  vector<pair<double, int>>
      A; //"#"?????°???N??¨????????¨2*N+2????????????????´???°
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (F[i][j] == '#') {
        for (int k = 0; k < 4; ++k) {
          if (i + dx[k] < 0 || j + dy[k] < 0 || i + dx[k] > H - 1 ||
              j + dy[k] > W - 1 || F[i + dx[k]][j + dy[k]] != '#') {
            pii s = pii(H - i - e[k].first.first, j + e[k].first.second);
            pii t = pii(H - i - e[k].second.first, j + e[k].second.second);
            // printf("(%d, %d), (%d, %d)\\n",s.first,s.second,t.first,t.second);
            if (s.second && s.first)
              A.push_back(make_pair((double)s.first / s.second, -1));
            if (t.second && s.first)
              A.push_back(make_pair((double)t.first / t.second, +1));
          }
        }
      }
    }
  }
  sort(A.begin(), A.end());
  int ans = 0, t = 0;

  for (int i = 0; i < (int)A.size(); ++i) {

    t += A[i].second;
    t = max(t, 0);
    ans = max(t, ans);
  }

  cout << (ans + 1) / 2 + 1 << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> pii;

int main() {
  int H, W;
  cin >> H >> W;
  string F[H];
  for (int i = 0; i < H; ++i) {
    cin >> F[i];
  }
  map<pair<pii, pii>, int> E;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (F[i][j] == '#') {
        ++E[make_pair(pii(H - i, j), pii(H - i - 1, j))];
        ++E[make_pair(pii(H - i, j), pii(H - i, j + 1))];
        ++E[make_pair(pii(H - i - 1, j), pii(H - i - 1, j + 1))];
        ++E[make_pair(pii(H - i, j + 1), pii(H - i - 1, j + 1))];
      }
    }
  }
  vector<pair<double, int>> A;
  for (map<pair<pii, pii>, int>::iterator itr = E.begin(); itr != E.end();
       ++itr) {
    if (itr->second == 1) {
      pii s = itr->first.first, t = itr->first.second;
      // cout << "(" << s.first << ", " << s.second << "), (" << t.first << ", "
      // << t.second << ")" << endl;
      if (s.second && s.first)
        A.push_back(make_pair((double)s.first / s.second, -1));
      if (t.second && s.first)
        A.push_back(make_pair((double)t.first / t.second, +1));
    }
  }
  sort(A.begin(), A.end());
  int ans = 0, t = 0;

  for (int i = 0; i < (int)A.size(); ++i) {

    t += A[i].second;
    t = max(t, 0);
    ans = max(t, ans);
  }
  // cout << ans << endl;
  cout << (ans + 1) / 2 + 1 << endl;
  return 0;
}