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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;
const static int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const static int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};

int main() {
  int H, W;
  char S[1000][1000];

  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  queue<Pii> Que;
  int ret = 0;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '.')
        Que.push(Pii(0, Pi(j, i)));
      else
        S[i][j] -= '0';
    }
  }
  while (!Que.empty()) {
    Pii &p = Que.front();
    ret = max(ret, p.first);
    for (int i = 0; i < 8; i++) {
      int ny = p.second.second + dy[i];
      int nx = p.second.first + dx[i];
      if (ny >= 0 && ny < H && nx >= 0 && nx < W && S[ny][nx] != '.') {
        --S[ny][nx];
        if (S[ny][nx] == 0) {
          Que.push(Pii(p.first + 1, Pi(nx, ny)));
        }
      }
    }
    Que.pop();
  }
  cout << ret << endl;
  return (0);
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;
const static int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const static int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};

int main() {
  int H, W;
  char S[1000][1001];

  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  queue<Pii> Que;
  int ret = 0;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '.')
        Que.push(Pii(0, Pi(j, i)));
      else
        S[i][j] -= '0';
    }
  }
  while (!Que.empty()) {
    Pii &p = Que.front();
    ret = max(ret, p.first);
    for (int i = 0; i < 8; i++) {
      int ny = p.second.second + dy[i];
      int nx = p.second.first + dx[i];
      if (ny >= 0 && ny < H && nx >= 0 && nx < W && S[nx][ny] != '.') {
        --S[ny][nx];
        if (S[ny][nx] == 0) {
          Que.push(Pii(p.first + 1, Pi(nx, ny)));
        }
      }
      Que.pop();
    }
  }
  cout << ret << endl;
  return (0);
}
Problem p00535 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int W, H, m[1000][1000];
int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1}, dy[] = {1, 1, 1, 0, -1, -1, -1, 0};

int main() {
  cin >> H >> W;
  queue<int> up1, up2, bks;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      char c;
      cin >> c;
      if (c == '.') {
        m[y][x] = -1;
      } else {
        m[y][x] = c - '0';
        up1.push(x);
        up1.push(y);
      }
    }
  }
  int ans = 0;
  while (!(up1.empty() && up2.empty())) {
    queue<int> *upp1, *upp2;
    if (up1.empty()) {
      upp2 = &up1;
      upp1 = &up2;
    } else {
      upp1 = &up1;
      upp2 = &up2;
    }
    while (!upp1->empty()) {
      int x = upp1->front();
      upp1->pop();
      int y = upp1->front();
      upp1->pop();
      if (m[y][x] == -1)
        continue;
      int c = 0;
      for (int i = 0; i < 8; i++) {
        int tx = x + dx[i], ty = y + dy[i];
        if (m[ty][tx] == -1)
          c++;
      }
      if (c < m[y][x])
        continue;
      bks.push(x);
      bks.push(y);
    }
    if (bks.empty())
      break;
    while (!bks.empty()) {
      int x = bks.front();
      bks.pop();
      int y = bks.front();
      bks.pop();
      if (m[y][x] == -1)
        continue;
      m[y][x] = -1;
      for (int i = 0; i < 8; i++) {
        int tx = x + dx[i], ty = y + dy[i];
        if (m[ty][tx] != -1) {
          upp2->push(tx);
          upp2->push(ty);
        }
      }
    }

    ans += 1;
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

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

int W, H, m[1000][1000];
int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1}, dy[] = {1, 1, 1, 0, -1, -1, -1, 0};

int main() {
  cin >> H >> W;
  queue<int> up1, up2, bks;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      char c;
      cin >> c;
      if (c == '.') {
        m[y][x] = -1;
      } else {
        m[y][x] = c - '0';
        up1.push(x);
        up1.push(y);
      }
    }
  }
  int ans = 0;
  while (!(up1.empty() && up2.empty())) {
    ans += 1;
    queue<int> *upp1, *upp2;
    if (up1.empty()) {
      upp2 = &up1;
      upp1 = &up2;
    } else {
      upp1 = &up1;
      upp2 = &up2;
    }
    while (!upp1->empty()) {
      int x = upp1->front();
      upp1->pop();
      int y = upp1->front();
      upp1->pop();
      if (m[y][x] == -1)
        continue;
      int c = 0;
      for (int i = 0; i < 8; i++) {
        int tx = x + dx[i], ty = y + dy[i];
        if (m[ty][tx] == -1)
          c++;
      }
      if (c < m[y][x])
        continue;
      bks.push(x);
      bks.push(y);
      for (int i = 0; i < 8; i++) {
        int tx = x + dx[i], ty = y + dy[i];
        if (m[ty][tx] != -1) {
          upp2->push(tx);
          upp2->push(ty);
        }
      }
    }
    while (!bks.empty()) {
      int x = bks.front();
      bks.pop();
      int y = bks.front();
      bks.pop();
      m[y][x] = -1;
    }
  }
  cout << ans - 1 << endl;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <set>
using namespace std;
typedef pair<int, int> pa;
typedef pair<int, pa> pp;

int ary[1010][1010];
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}, dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int H, W;

int main() {
  cin >> H >> W;
  char ch;
  queue<pa> quf;
  set<pa> se[2];
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> ch;
      if (ch == '.')
        ary[i][j] = 0;
      else
        ary[i][j] = ch - '0';
      se[1].insert(pa(i, j));
    }
  }

  int tmx, tmy, now = 0, cnt = 0, res = 0;
  quf.push(pa(0, 0));
  for (int i = 1;; i++) {
    if (quf.empty()) {
      res = i - 2;
      break;
    }
    while (!quf.empty()) {
      ary[quf.front().first][quf.front().second] = 0;
      quf.pop();
    }
    se[1 - (i & 1)].clear();
    for (auto it = se[i & 1].begin(); it != se[i & 1].end(); it++) {
      if (ary[it->first][it->second] == 0)
        continue;
      cnt = 0;
      for (int j = 0; j < 8; j++) {
        if (ary[it->first + dx[j]][it->second + dy[j]] == 0) {
          cnt++;
        }
      }
      if (ary[it->first][it->second] <= cnt) {
        quf.push(pa(it->first, it->second));
        for (int j = 0; j < 8; j++) {
          if ((1 <= it->first + dx[j]) && (it->first + dx[j] <= H) &&
              (1 <= it->second + dy[j]) && (it->second + dy[j] <= W))
            se[1 - (i & 1)].insert(pa(it->first + dx[j], it->second + dy[j]));
        }
      }
    }
    cout << "";
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> pa;
typedef pair<int, pa> pp;

int ary[1010][1010];
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}, dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int H, W;

int main() {
  cin >> H >> W;
  char ch;
  int in = 0, ou = 1;
  queue<pp> que;
  queue<pa> quf;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> ch;
      if (ch == '.')
        ary[i][j] = 0;
      else
        ary[i][j] = ch - '0';
      que.push(pp(0, pa(i, j)));
    }
  }

  int tmx, tmy, now = 0, cnt = 0, res = 0;
  while (!que.empty()) {
    tmx = que.front().second.first;
    tmy = que.front().second.second;
    if (now != que.front().first) {
      now = que.front().first;
      while (!quf.empty()) {
        ary[quf.front().first][quf.front().second] = 0;
        quf.pop();
      }
      res++;
    }
    que.pop();
    if (ary[tmx][tmy] == 0)
      continue;
    cnt = 0;
    for (int i = 0; i < 8; i++) {
      if (ary[tmx + dx[i]][tmy + dy[i]] == 0) {
        cnt++;
      }
    }
    if (ary[tmx][tmy] <= cnt) {
      quf.push(pa(tmx, tmy));
      for (int i = 0; i < 8; i++) {
        que.push(pp(now + 1, pa(tmx + dx[i], tmy + dy[i])));
      }
    }
  }
  cout << res << endl;
  return 0;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for (int i = 0; i < n; i++)
struct int2 {
  int x;
  int y;
};
int h, w;
int a[] = {1, 1, 1, 0, 0, -1, -1, -1};
int b[] = {-1, 0, 1, -1, 1, 1, 0, -1};
queue<int2> q1;
queue<int2> q2;
string beach1;
int beach[1000][1000];
int sarachi[1000][1000];
int main() {
  cin >> h >> w;
  int count = 0;
  for (int i = 0; i < h; i++) {
    cin >> beach1;
    for (int j = 0; j < w; j++) {
      sarachi[i][j] = 0;
      if (beach1[j] == '.')
        beach[i][j] = 10;
      else
        beach[i][j] = beach1[j] - '0';
    }
  }
  for (int i = 1; i < h - 1; i++) {
    for (int j = 1; j < w - 1; j++) {
      FOR(k, 8) {
        if (beach[i + a[k]][j + b[k]] == 10)
          sarachi[i][j]++;
      }
      if (sarachi[i][j] >= beach[i][j]) {
        int2 num;
        num.x = i;
        num.y = j;
        q1.push(num);
      }
    }
  }
  while (!q1.empty()) {
    int2 num = q1.front();
    q1.pop();
    beach[num.x][num.y] = 10;
    q2.push(num);
  }
  count = 0;
  while (!q2.empty()) {
    while (!q2.empty()) {
      int2 num = q2.front();
      q2.pop();
      FOR(i, 8) {
        sarachi[num.x + a[i]][num.y + b[i]]++;
        if (sarachi[num.x + a[i]][num.y + b[i]] >=
            beach[num.x + a[i]][num.y + b[i]]) {
          int2 num2;
          num2.x = num.x + a[i];
          num2.y = num.y + b[i];
          beach[num.x + a[i]][num.y + b[i]] = 10;
          q1.push(num2);
        }
      }
    }
    while (!q1.empty()) {
      int2 num = q1.front();
      q1.pop();
      q2.push(num);
    }
    count++;
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int h, w;
char beach[1000][1000];
char beachcopy[1000][1000];
int saraticount(int a, int b) {
  int ret = 0;
  if (beach[a][b + 1] == '.')
    ret++;
  if (beach[a][b - 1] == '.')
    ret++;
  if (beach[a - 1][b - 1] == '.')
    ret++;
  if (beach[a - 1][b] == '.')
    ret++;
  if (beach[a - 1][b + 1] == '.')
    ret++;
  if (beach[a + 1][b - 1] == '.')
    ret++;
  if (beach[a + 1][b] == '.')
    ret++;
  if (beach[a + 1][b + 1] == '.')
    ret++;
  return ret;
}
int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> beach[i][j];
      beachcopy[i][j] = beach[i][j];
    }
  }
  int count = 0;
  while (true) {
    bool change = false;
    for (int i = 1; i < h - 1; i++) {
      for (int j = 1; j < w - 1; j++) {
        int num;
        if (beach[i][j] != '.') {
          stringstream ss;
          ss << beach[i][j];
          ss >> num;
          if (num <= saraticount(i, j)) {
            beachcopy[i][j] = '.';
            change = true;
          }
        }
      }
    }
    if (change)
      count++;
    else
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        beach[i][j] = beachcopy[i][j];
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p00535 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int H, W;
int cas[1000][1000];
vector<string> vs;
queue<PP> que; // PP(P(y, x),???i??????)?????¢?????\\???
int vy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int vx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> H >> W;
  FOR(i, 0, H) {
    string s;
    cin >> s;
    vs.push_back(s);
  }
  FOR(i, 0, H) {
    FOR(j, 0, W) {
      if (isdigit(vs[i][j])) {
        cas[i][j] = 1e9;
        que.push(PP(P(i, j), 1));
      }
    }
  }
  int ans = 0;
  while (!que.empty()) {
    PP p = que.front();
    que.pop();
    int y = p.first.first;
    int x = p.first.second;
    int t = p.second;
    if (cas[y][x] <= t)
      continue; // ?????§????´?????????????
    int cnt = 0;
    FOR(i, 0, 8) if (cas[y + vy[i]][x + vx[i]] < t) cnt++;
    // ?´???????
    if (cnt >= vs[y][x] - '0') {
      cas[y][x] = t;
      ans = max(ans, t);
      FOR(i, 0, 8) {
        if (cas[y + vy[i]][x + vx[i]] > t) {
          que.push(PP(P(y + vy[i], x + vx[i]), t + 1));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int H, W;
int cas[1000][1000];
vector<string> vs;
queue<PP> que; // PP(P(y, x),???i??????)?????¢?????\\???
int vy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int vx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> H >> W;
  FOR(i, 0, H) {
    string s;
    cin >> s;
    vs.push_back(s);
  }
  FOR(i, 0, H) {
    FOR(j, 0, W) {
      if (isdigit(vs[i][j])) {
        cas[i][j] = 1e9;
        que.push(PP(P(i, j), 1));
      }
    }
  }
  int ans = 0;
  while (!que.empty()) {
    PP p = que.front();
    que.pop();
    int y = p.first.first;
    int x = p.first.second;
    int t = p.second;
    if (cas[y][x] < t)
      continue; // ?????§????´?????????????
    int cnt = 0;
    FOR(i, 0, 8) if (cas[y + vy[i]][x + vx[i]] < t) cnt++;
    // ?´???????
    if (cnt >= vs[y][x] - '0') {
      cas[y][x] = t;
      ans = max(ans, t);
      FOR(i, 0, 8) {
        if (cas[y + vy[i]][x + vx[i]] > t) {
          que.push(PP(P(y + vy[i], x + vx[i]), t + 1));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

typedef pair<int, int> pint;
queue<pint> nowque, nexque;
int h, w;
int castle[1002][1002] = {};
bool inq[1002][1002] = {};
char str[1001];

const int dir[9] = {-1, -1, 0, 1, 1, 0, -1, 1, -1};
inline bool check(int i, int j) {
  if (castle[i][j] == 0)
    return false;
  int c = 0;
  for (int k = 0; k < 8; ++k) {
    if (castle[i + dir[k]][j + dir[k + 1]] == 0)
      ++c;
  }
  if (c >= castle[i][j]) {
    inq[i][j] = true;
    return true;
  }
  return false;
}

int main() {
  scanf("%d %d", &h, &w);
  for (int i = 1; i <= h; ++i) {
    scanf("%s", str);
    for (int j = 1; j <= w; ++j) {
      castle[i][j] = ((str[j - 1] == '.') ? 0 : (str[j - 1] - '0'));
      if (castle[i][j] == 0) {
        nowque.emplace(i, j);
      }
    }
  }
  int time = 0;
  for (; true; ++time) {
    while (!nowque.empty()) {
      pint x = nowque.front();
      nowque.pop();
      for (int k = 0; k < 8; ++k) {
        if (!inq[x.first + dir[k]][x.second + dir[k + 1]]) {
          if (check(x.first + dir[k], x.second + dir[k + 1])) {
            nexque.emplace(x.first + dir[k], x.second + dir[k + 1]);
          }
        }
      }
    }
    nowque = nexque;
    while (!nexque.empty()) {
      pint y = nexque.front();
      nexque.pop();
      castle[y.first][y.second] = 0;
    }
    if (nowque.empty())
      break;
  }
  printf("%d\\n", time);
  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

typedef pair<int, int> pint;
queue<pint> nowque, nexque;
int h, w;
int castle[1002][1002] = {};
char str[1001];

const int dir[9] = {-1, -1, 0, 1, 1, 0, -1, 1, -1};
bool check(int i, int j) {
  if (castle[i][j] == 0)
    return false;
  int c = 0;
  for (int k = 0; k < 8; ++k) {
    if (castle[i + dir[k]][j + dir[k + 1]] == 0)
      ++c;
  }
  if (c >= castle[i][j]) {
    return true;
  }
  return false;
}

int main() {
  scanf("%d %d", &h, &w);
  for (int i = 1; i <= h; ++i) {
    scanf("%s", str);
    for (int j = 1; j <= w; ++j) {
      castle[i][j] = ((str[j - 1] == '.') ? 0 : (str[j - 1] - '0'));
      if (castle[i][j] == 0) {
        nowque.emplace(i, j);
      }
    }
  }
  int time = 0;
  for (; true; ++time) {
    while (!nowque.empty()) {
      pint x = nowque.front();
      nowque.pop();
      for (int k = 0; k < 8; ++k) {
        if (check(x.first + dir[k], x.second + dir[k + 1])) {
          nexque.emplace(x.first + dir[k], x.second + dir[k + 1]);
        }
      }
    }
    nowque = nexque;
    while (!nexque.empty()) {
      pint y = nexque.front();
      nexque.pop();
      castle[y.first][y.second] = 0;
    }
    if (nowque.empty())
      break;
  }
  printf("%d\\n", time);
  return 0;
}
Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int h, w;
int a[1010][1010];
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

queue<pair<int, int>> q;
vector<pair<int, int>> n;

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      if (c == '.')
        c = '0' - 1;
      a[i][j] = c - '0';
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] != -1)
        continue;
      for (int k = 0; k < 8; k++) {
        int x = j + dx[k];
        int y = i + dy[k];
        if (x < 0 || w <= x || y < 0 || h <= y)
          continue;
        if (a[y][x] <= 0)
          continue;
        a[y][x]--;
        if (a[y][x] == 0)
          n.push_back(make_pair(y, x));
      }
    }
  }

  int cnt = 0;
  while (n.size()) {
    cnt++;
    for (auto v : n) {
      q.push(v);
    }
    n.clear();
    while (!q.empty()) {
      auto v = q.front();
      q.pop();
      int i = v.first, j = v.second;
      a[i][j]--;
      for (int k = 0; k < 8; k++) {
        int x = j + dx[k];
        int y = i + dy[k];
        if (x < 0 || w <= x || y < 0 || h <= y)
          continue;
        if (a[y][x] <= 0)
          continue;
        a[y][x]--;
        if (a[y][x] == 0)
          n.push_back(make_pair(y, x));
      }
    }
  }

  cout << cnt << endl;
}

Error: TLE

Fixed submission:

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

int h, w;
int a[1010][1010];

int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      if (c == '.')
        a[i][j] = 0;
      else
        a[i][j] = c - '0';
    }
  }

  int cnt = 0;
  bool f = true;

  while (f) {
    /*    cout << cnt << endl;
        for (int i = 0; i < h; i++){
          for (int j = 0; j < w; j++){
            if (a[i][j] != -1) cout << " ";
            cout << a[i][j] << " ";
          }
          cout << endl;
        }
        cout << endl;
    */

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] == 0)
          a[i][j] = -1;
      }
    }

    f = false;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] != -1)
          continue;
        a[i][j]--;

        for (int k = 0; k < 8; k++) {
          int y = i + dy[k];
          int x = j + dx[k];
          if (y < 0 || h <= y || x < 0 || w <= x)
            continue;
          if (a[y][x] <= 0)
            continue;
          a[y][x]--;
        }
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] == 0)
          f = true;
      }
    }

    cnt++;
  }

  cout << cnt - 1 << endl;
}
Problem p00536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, d;
ll x[30], y[30];
vector<pair<ll, ll>> s[2];
void dfs(int id, int now, int end, ll a, ll b) { // 半分全列挙
  if (now == end) {
    s[id].push_back(make_pair(a, b));
    return;
  }
  dfs(id, now + 1, end, a + x[now], b + y[now]);
  dfs(id, now + 1, end, a - x[now], b - y[now]);
  dfs(id, now + 1, end, a, b);
}

int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];
  dfs(0, 0, n / 2, 0, 0);
  dfs(1, n / 2, n, 0, 0);
  sort(s[0].begin(), s[0].end());
  sort(s[1].begin(), s[1].end());
  //+はA君が盗った。-はB君が盗った
  ll ans = 0;
  int cnt = 0;
  deque<pair<ll, ll>> dq;
  for (int i = 0; i < (int)s[0].size(); i++) {
    while (cnt < (int)s[1].size() && s[1][cnt].first <= s[0][i].first + d) {
      while (!dq.empty() && dq.back().second > s[1][cnt].second)
        dq.pop_back();
      dq.push_back(s[1][cnt]);
      cnt++;
    }
    while (!dq.empty() && dq.front().first < s[0][i].first - d)
      dq.pop_front();
    if (!dq.empty())
      ans = max(ans, s[0][i].second - dq.front().second);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n, d;
ll x[30], y[30];
vector<pair<ll, ll>> s[2];
void dfs(int id, int now, int end, ll a, ll b) { // 半分全列挙
  if (now == end) {
    s[id].push_back(make_pair(a, b));
    return;
  }
  dfs(id, now + 1, end, a + x[now], b + y[now]);
  dfs(id, now + 1, end, a - x[now], b - y[now]);
  dfs(id, now + 1, end, a, b);
}

int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];
  dfs(0, 0, n / 2, 0, 0);
  dfs(1, n / 2, n, 0, 0);
  sort(s[0].begin(), s[0].end());
  sort(s[1].begin(), s[1].end());
  //+はA君が盗った。-はB君が盗った
  ll ans = 0;
  int acnt = 0, bcnt = 0;
  multiset<ll> ms;
  for (int i = 0; i < (int)s[0].size(); i++) {
    while (acnt < (int)s[1].size() && s[1][acnt].first <= s[0][i].first + d) {
      ms.insert(s[1][acnt].second);
      acnt++;
    }
    while (bcnt < (int)s[1].size() && s[1][bcnt].first < s[0][i].first - d) {
      ms.erase(ms.find(s[1][bcnt].second));
      bcnt++;
    }
    if (!ms.empty())
      ans = max(ans, s[0][i].second - *ms.begin());
  }
  cout << ans << endl;
}