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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); ++(i))

int n;
vector<i64> a;
int zero;
int one;

int main() {
  cin >> n;
  rep(i, 0, n - 1) {
    i64 t;
    cin >> t;
    if (t == 0)
      zero++;
    else if (t == 1)
      one++;
    else
      a.push_back(t);
  }
  sort(a.begin(), a.end());

  vector<i64> r;

  if (zero % 2 == 1 && one == 0) {
    for (int i = 0; i < zero - 1; i++) {
      r.push_back(0);
    }
    if (a.size() > 0) {
      r.push_back(a.front());
      a.erase(a.begin());
    }
    r.push_back(0);
  } else {
    for (int i = 0; i < zero - (zero % 2); i++) {
      r.push_back(0);
    }
    if (zero % 2 == 1) {
      r.push_back(1);
      one--;
      r.push_back(0);
      zero--;
    }
    for (int i = 0; i < one; i++) {
      r.push_back(1);
    }
  }

  if (a.size() >= 2 && a[a.size() - 2] == 2 && a[a.size() - 1] == 3) {
    swap(a[a.size() - 2], a[a.size() - 1]);
  }

  for (i64 an : a) {
    cout << an << endl;
  }
  for (i64 an : r) {
    cout << an << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); ++(i))

int n;
vector<i64> a;
int zero;
int one;

int main() {
  cin >> n;
  rep(i, 0, n - 1) {
    i64 t;
    cin >> t;
    if (t == 0)
      zero++;
    else if (t == 1)
      one++;
    else
      a.push_back(t);
  }
  sort(a.begin(), a.end());

  vector<i64> r;

  if (zero % 2 == 1 && one == 0) {
    r.push_back(a.front());
    if (a.size() > 0)
      a.erase(a.begin());
    for (int i = 0; i < zero; i++) {
      r.push_back(0);
    }
  } else {
    for (int i = 0; i < zero - (zero % 2); i++) {
      r.push_back(0);
    }
    if (zero % 2 == 1) {
      r.push_back(1);
      one--;
      r.push_back(0);
      zero--;
    }
    for (int i = 0; i < one; i++) {
      r.push_back(1);
    }
  }

  if (a.size() >= 2 && a[a.size() - 2] == 2 && a[a.size() - 1] == 3) {
    swap(a[a.size() - 2], a[a.size() - 1]);
  }

  for (i64 an : a) {
    cout << an << endl;
  }
  for (i64 an : r) {
    cout << an << endl;
  }
}
Problem p01412 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;

int n;
int a[101];
int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  reverse(a, a + n);
  int all = n;
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] == 0)
      all--;
    else
      break;
  }
  int zero = all;
  for (int i = all - 1; i >= 0; i--) {
    if (a[i] == 1)
      all--;
    else
      break;
  }
  int sw = (n - all) - (zero - all);
  sort(a + all, a + n);
  if ((n - zero) % 2 == 1) {
    if (zero - all >= 1)
      swap(a[all + sw - 1], a[all + sw]);
    else {
      for (int i = all - 1; i + 2 < n; i += 2) {
        swap(a[i], a[i + 2]);
      }
      all--;
    }
  }
  if (all >= 0)
    sort(a, a + all);
  if (all >= 2 && a[all - 2] == 2 && a[all - 1] == 3)
    swap(a[all - 2], a[all - 1]);
  for (int i = 0; i < n; i++) {
    printf("%d\\n", a[i]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;

int n;
int a[101];
int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  reverse(a, a + n);
  int all = n;
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] == 0)
      all--;
    else
      break;
  }
  int zero = all;
  for (int i = all - 1; i >= 0; i--) {
    if (a[i] == 1)
      all--;
    else
      break;
  }
  int sw = (n - all) - (zero - all);
  sort(a + all, a + n);
  if ((n - zero) % 2 == 1) {
    if (zero - all >= 1)
      swap(a[all + sw - 1], a[all + sw]);
    else {
      for (int i = all - 1; i + 2 < n; i += 2) {
        swap(a[i], a[i + 2]);
      }
      all--;
    }
  }
  sort(a, a + all);
  if (all >= 2 && a[all - 2] == 2 && a[all - 1] == 3)
    swap(a[all - 2], a[all - 1]);
  for (int i = 0; i < n; i++) {
    printf("%d\\n", a[i]);
  }
  return 0;
}
Problem p01412 (C++) — buggy submission, verdict: Runtime Error

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

int N;
int idx;
int zero;
int one;
int as[114514];

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int a;
    scanf("%d", &a);
    if (a == 0)
      zero++;
    else if (a == 1)
      one++;
    else
      as[idx++] = a;
  }

  sort(as, as + idx);
  if (zero % 2 == 1 && one == 0 && idx) {
    swap(as[0], as[idx - 1]);
    idx--;
    sort(as, as + idx);
  }

  if (idx >= 2 && as[idx - 2] == 2 && as[idx - 1] == 3) {
    swap(as[idx - 1], as[idx - 2]);
  }

  for (int i = 0; i < idx; i++)
    printf("%d\\n", as[i]);
  if (zero % 2 == 1 && one == 0) {
    for (int i = 0; i < zero - 1; i++)
      puts("0");
    if (idx >= 0 && zero < N)
      printf("%d\\n", as[idx]);
    puts("0");
  } else {
    if (zero % 2 == 1) {
      zero--;
      one--;
      while (zero--)
        puts("0");
      puts("1");
      puts("0");
      while (one--)
        puts("1");
    } else {
      while (zero--)
        puts("0");
      while (one--)
        puts("1");
    }
  }
}

Error: 0

Fixed submission:

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

int N;
int idx;
int zero;
int one;
int as[114514];

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    int a;
    scanf("%d", &a);
    if (a == 0)
      zero++;
    else if (a == 1)
      one++;
    else
      as[idx++] = a;
  }

  sort(as, as + idx);
  if (zero % 2 == 1 && one == 0) {
    swap(as[0], as[idx - 1]);
    idx--;
    sort(as, as + idx);
  }

  if (idx >= 2 && as[idx - 2] == 2 && as[idx - 1] == 3) {
    swap(as[idx - 1], as[idx - 2]);
  }

  for (int i = 0; i < idx; i++)
    printf("%d\\n", as[i]);
  if (zero % 2 == 1 && one == 0) {
    printf("%d\\n", as[idx]);
    while (zero--)
      puts("0");
  } else {
    if (zero % 2 == 1) {
      zero--;
      one--;
      while (zero--)
        puts("0");
      puts("1");
      puts("0");
      while (one--)
        puts("1");
    } else {
      while (zero--)
        puts("0");
      while (one--)
        puts("1");
    }
  }
}
Problem p01414 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int main() {
  int n;
  cin >> n;
  int h[n], w[n];
  for (int i = 0; i < n; i++)
    cin >> h[i] >> w[i];
  string s[4];
  for (int i = 0; i < 4; i++)
    cin >> s[i];
  char c[4] = "RGB";
  int dp[1 << 16];
  memset(dp, -1, sizeof(dp));
  bool used[1 << 16];
  memset(used, 0, sizeof(used));
  queue<P> q;
  q.push(P(0, 0));
  while (!q.empty()) {
    int bit = q.front().first, d = q.front().second;
    q.pop();
    if (~dp[bit])
      continue;
    dp[bit] = d;
    for (int i = 0; i < n; i++) {
      for (int j = -h[i] + 1; j < 4; j++) {
        for (int k = -w[i] + 1; k < 4; k++) {
          for (int l = 0; l < 3; l++) {
            int z = bit;
            for (int a = max(0, -j); a < min(h[i], 4 - j); a++) {
              for (int b = max(0, -k); b < min(w[i], 4 - k); b++) {
                int y = j + a, x = k + b;
                if (s[y][x] == c[l])
                  z |= 1 << (y * 4 + x);
                else
                  z &= (1 << 16) - (1 << (y * 4 + x)) - 1;
              }
            }
            if (dp[z] < 0 && !used[z])
              q.push(P(z, d + 1)), used[z] = 1;
          }
        }
      }
    }
  }
  cout << dp[(1 << 16) - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int main() {
  int n;
  cin >> n;
  int h[n], w[n];
  for (int i = 0; i < n; i++)
    cin >> h[i] >> w[i];
  string s[4];
  for (int i = 0; i < 4; i++)
    cin >> s[i];
  char c[4] = "RGB";
  int dp[1 << 16];
  memset(dp, -1, sizeof(dp));
  bool used[1 << 16];
  memset(used, 0, sizeof(used));
  queue<P> q;
  q.push(P(0, 0));
  while (!q.empty()) {
    int bit = q.front().first, d = q.front().second;
    q.pop();
    if (~dp[bit])
      continue;
    dp[bit] = d;
    for (int i = 0; i < n; i++) {
      for (int j = -h[i] + 1; j < 4; j++) {
        for (int k = -w[i] + 1; k < 4; k++) {
          for (int l = 0; l < 3; l++) {
            int z = bit;
            for (int a = 0; a < h[i]; a++) {
              for (int b = 0; b < w[i]; b++) {
                int y = j + a, x = k + b;
                if (y < 0 || y >= 4 || x < 0 || x >= 4)
                  continue;
                if (s[y][x] == c[l])
                  z |= 1 << (y * 4 + x);
                else
                  z &= (1 << 16) - (1 << (y * 4 + x)) - 1;
              }
            }
            if (dp[z] < 0 && !used[z])
              q.push(P(z, d + 1)), used[z] = 1;
          }
        }
      }
    }
  }
  cout << dp[(1 << 16) - 1] << endl;
  return 0;
}
Problem p01414 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef int ll;

ll N, H[16], W[16];
char S[4][4];
const char Z[] = "RGB";
ll dp[1 << 16];

int main(void) {
  scanf("%d", &N);
  REP(i, 0, N) scanf("%d %d", H + i, W + i);
  REP(i, 0, 4) scanf("%s", S[i]);

  REP(i, 0, 1 << 16) dp[i] = -1;
  dp[0] = 0;
  queue<ll> q;
  q.push(0);
  while (q.size()) {
    if (dp[(1 << 16) - 1] != -1)
      break;
    ll s = q.front();
    q.pop();
    REP(i, 0, N) {
      REP(py, -H[i] + 1, 4) REP(px, -W[i] + 1, 4) REP(c, 0, 3) {
        ll n = s;
        REP(y, max(py, 0), min(py + H[i], 4))
        REP(x, max(px, 0), min(px + W[i], 4)) {
          if (S[y][x] == Z[c])
            n = n | (1 << (y * 4 + x));
          else
            n = n & (~(1 << (y * 4 + x)));
        }
        if (dp[n] == -1) {
          dp[n] = dp[s] + 1;
          q.push(n);
        }
      }
    }
  }
  printf("%d\\n", dp[(1 << 16) - 1]);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;

int main(void) {
  ll N;
  cin >> N;
  vector<ll> H(N), W(N);
  REP(i, 0, N) cin >> H[i] >> W[i];
  char S[4][4];
  REP(i, 0, 4) REP(j, 0, 4) cin >> S[i][j];

  vector<ll> dp(1LL << 16, -1);
  queue<ll> q;
  q.push(0);
  dp[0] = 0;
  while (q.size()) {
    ll s = q.front();
    q.pop();
    REP(i, 0, N) {
      REP(py, -H[i] + 1, 4) REP(px, -W[i] + 1, 4) REP(c, 0, 3) {
        ll n = s;
        REP(y, max(py, 0LL), min(py + H[i], 4LL))
        REP(x, max(px, 0LL), min(px + W[i], 4LL)) {
          const string Z = "RGB";
          if (S[y][x] == Z[c])
            n = n | (1LL << (y * 4 + x));
          else
            n = ~(~n | (1LL << (y * 4 + x)));
        }
        if (dp[n] == -1) {
          dp[n] = dp[s] + 1;
          q.push(n);
          if (n == (1LL << 16) - 1) {
            cout << dp[(1LL << 16) - 1] << endl;
            return 0;
          }
        }
      }
    }
  }
}
Problem p01414 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;
vector<int> h, w;

vector<vector<int>> fie(4, vector<int>(4));

vector<int> dist(1 << 16, -1);

int main() {
  cin >> n;
  h.resize(n);
  w.resize(n);
  rep(i, 0, n - 1) cin >> h[i] >> w[i];
  rep(i, 0, 3) rep(j, 0, 3) {
    char c;
    cin >> c;
    if (c == 'R')
      fie[i][j] = 0;
    if (c == 'G')
      fie[i][j] = 1;
    if (c == 'B')
      fie[i][j] = 2;
  }
  queue<int> que;
  que.push(0);
  dist[0] = 0;

  while (!que.empty()) {
    int v = que.front();
    que.pop();
    if (v == (1 << 16) - 1) {
      cout << dist[v] << endl;
      return 0;
    }
    rep(c, 0, 2) rep(s, 0, n - 1) {
      rep(i, -h[s] + 1, 3) rep(j, -w[s] + 1, 3) {
        int u = v;
        rep(x, max(i, 0), min(i + h[s] - 1, 3))
            rep(y, max(j, 0), min(j + w[s] - 1, 3)) {
          int index = (x * 4 + y);
          if (u & (1 << index)) {
            if (fie[x][y] != c)
              u ^= (1 << index);
          } else {
            if (fie[x][y] == c)
              u ^= (1 << index);
          }
        }
        if (dist[u] == -1) {
          dist[u] = dist[v] + 1;
          que.push(u);
        }
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;
vector<int> h, w;

vector<vector<int>> fie(4, vector<int>(4));

vector<int> dist(1 << 16, -1);

int main() {
  cin >> n;
  h.resize(n);
  w.resize(n);
  rep(i, 0, n - 1) cin >> h[i] >> w[i];
  rep(i, 0, 3) rep(j, 0, 3) {
    char c;
    cin >> c;
    if (c == 'R')
      fie[i][j] = 0;
    if (c == 'G')
      fie[i][j] = 1;
    if (c == 'B')
      fie[i][j] = 2;
  }
  queue<int> que;
  que.push(0);
  dist[0] = 0;

  while (!que.empty()) {
    int v = que.front();
    que.pop();
    if (v == (1 << 16) - 1) {
      cout << dist[v] << endl;
      return 0;
    }
    rep(c, 0, 2) rep(s, 0, n - 1) {
      rep(i, -3, 3) rep(j, -3, 3) {
        int u = v;
        rep(x, max(i, 0), min(i + h[s] - 1, 3))
            rep(y, max(j, 0), min(j + w[s] - 1, 3)) {
          int index = (x * 4 + y);
          if (u & (1 << index)) {
            if (fie[x][y] != c)
              u ^= (1 << index);
          } else {
            if (fie[x][y] == c)
              u ^= (1 << index);
          }
        }
        if (dist[u] == -1) {
          dist[u] = dist[v] + 1;
          que.push(u);
        }
      }
    }
  }
}
Problem p01414 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool dp[17][1 << 16];
int main() {
  int n, h[16], w[16];
  char r[3] = {'R', 'G', 'B'};
  string c[4];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i] >> w[i];
  for (int i = 0; i < 4; i++)
    cin >> c[i];

  int a1[16][7][7], o1[16][3][7][7] = {};
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 7; j++)
      for (int k = 0; k < 7; k++)
        a1[i][j][k] = (1 << 16) - 1;

  for (int k = 0; k < n; k++)
    for (int cl = 0; cl < 3; cl++)
      for (int y = -h[k] + 1; y < 4; y++)
        for (int x = -w[k] + 1; x < 4; x++) {
          for (int l = max(0, y); l < min(4, y + h[k]); l++)
            for (int m = max(0, x); m < min(4, x + w[k]); m++) {
              int xx = l * 4 + m;
              if (c[l][m] == r[cl])
                o1[k][cl][y + 3][x + 3] |= 1 << xx;
              if (!cl)
                a1[k][y + 3][x + 3] -= 1 << xx;
            }
        }

  dp[0][0] = 1;
  for (int j = 0; j < 16; j++)
    for (int i = 0; i < 1 << 16; i++)
      if (dp[j][i])
        for (int k = 0; k < n; k++)
          for (int cl = 0; cl < 3; cl++)
            for (int y = -h[k] + 1; y < 4; y++)
              for (int x = -w[k] + 1; x < 4; x++) {
                int ni = (i & a1[k][y + 3][x + 3]) | o1[k][cl][y + 3][x + 3];
                dp[j + 1][ni] = 1;
                if (ni == (1 << 16) - 1) {
                  cout << j + 1 << endl;
                  return 0;
                }
              }
}

Error: TLE

Fixed submission:

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

bool dp[17][1 << 16];
int main() {
  int n, h[16], w[16];
  char r[3] = {'R', 'G', 'B'};
  string c[4];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i] >> w[i];
  for (int i = 0; i < 4; i++)
    cin >> c[i];
  dp[0][0] = 1;
  for (int j = 0; j < 16; j++)
    for (int i = 0; i < 1 << 16; i++)
      if (dp[j][i])
        for (int k = 0; k < n; k++)
          for (int cl = 0; cl < 3; cl++)
            for (int y = -h[k] + 1; y < 4; y++)
              for (int x = -w[k] + 1; x < 4; x++) {
                int ni = i;
                for (int l = max(0, y); l < min(4, y + h[k]); l++)
                  for (int m = max(0, x); m < min(4, x + w[k]); m++) {
                    int xx = l * 4 + m;
                    if (c[l][m] == r[cl])
                      ni |= (1 << xx);
                    else if (ni & (1 << xx))
                      ni -= (1 << xx);
                  }
                dp[j + 1][ni] = 1;
                if (ni == (1 << 16) - 1) {
                  cout << j + 1 << endl;
                  return 0;
                }
              }
}
Problem p01414 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
int p[20];
int q[20];
char str[5][5];
int bfs[1 << 16];
char col[5] = "RGB";
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    scanf("%d%d", p + i, q + i);
  }
  for (int i = 0; i < 4; i++)
    scanf("%s", str[i]);
  for (int i = 0; i < (1 << 16); i++)
    bfs[i] = -1;
  bfs[0] = 0;
  queue<int> Q;
  Q.push(0);
  while (Q.size()) {
    if (~bfs[(1 << 16) - 1])
      break;
    int at = Q.front();
    Q.pop();
    for (int i = 0; i < a; i++) {
      for (int j = -p[i] + 1; j < 4; j++) {
        for (int k = -q[i] + 1; k < 4; k++) {
          for (int c = 0; c < 3; c++) {
            int to = at;
            for (int l = max(0, -j); l < min(p[i], 4 - j); l++)
              for (int m = max(0, -k); m < min(q[i], 4 - k); m++) {
                // if(j+l<0||k+m<0||j+l>3||k+m>3)continue;
                if (str[j + l][k + m] == col[c])
                  to |= (1 << ((j + l) * 4 + k + m));
                else
                  to &= (~(1 << ((j + l) * 4 + k + m)));
              }
            if (!~bfs[to]) {
              bfs[to] = bfs[at] + 1;
              Q.push(to);
            }
          }
        }
      }
    }
  }
  printf("%d\\n", bfs[(1 << 16) - 1]);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
int p[20];
int q[20];
char str[5][5];
int bfs[1 << 16];
char col[5] = "RGB";
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    scanf("%d%d", p + i, q + i);
  }
  for (int i = 0; i < 4; i++)
    scanf("%s", str[i]);
  for (int i = 0; i < (1 << 16); i++)
    bfs[i] = -1;
  bfs[0] = 0;
  queue<int> Q;
  Q.push(0);
  while (Q.size()) {
    if (~bfs[(1 << 16) - 1])
      break;
    int at = Q.front();
    Q.pop();
    for (int i = 0; i < a; i++) {
      for (int j = -p[i] + 1; j < 4; j++) {
        for (int k = -q[i] + 1; k < 4; k++) {
          for (int c = 0; c < 3; c++) {
            int to = at;
            for (int l = 0; l < p[i]; l++)
              for (int m = 0; m < q[i]; m++) {
                if (j + l < 0 || k + m < 0 || j + l > 3 || k + m > 3)
                  continue;
                if (str[j + l][k + m] == col[c])
                  to |= (1 << ((j + l) * 4 + k + m));
                else
                  to &= (~(1 << ((j + l) * 4 + k + m)));
              }
            if (!~bfs[to]) {
              bfs[to] = bfs[at] + 1;
              Q.push(to);
            }
          }
        }
      }
    }
  }
  printf("%d\\n", bfs[(1 << 16) - 1]);
}
Problem p01414 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

int main() {

  const string cols = "RGB";

  int N;
  cin >> N;
  vector<pair<int, int>> vs(N);
  rep(i, N) { cin >> vs[i].first >> vs[i].second; }

  vector<string> G(4);
  rep(i, 4) cin >> G[i];

  static int dist[1 << 16];
  minus(dist);
  dist[0] = 0;

  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    if (~dist[(1 << 16) - 1])
      break;

    int curr = q.front();
    q.pop();

    for (auto hw : vs) {
      int h = hw.first, w = hw.second;

      for (auto col : cols)
        REP(i, -h + 1, 4) REP(j, -w + 1, 4) {
          int next = curr;
          REP(y, max(0, i), min(i + h, 4)) REP(x, max(0, j), min(j + w, 4)) {
            if (G[y][x] == col)
              next |= 1 << (y * 4 + x);
            else
              next &= ~(1 << (y * 4 + x));
          }
          if (dist[next] < 0) {
            dist[next] = dist[curr] + 1;
            q.push(next);
          }
        }
    }
  }

  cout << dist[(1 << 16) - 1] << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

int main() {

  const string cols = "RGB";

  int N;
  cin >> N;
  vector<pair<int, int>> vs(N);
  rep(i, N) { cin >> vs[i].first >> vs[i].second; }

  vector<string> G(4);
  rep(i, 4) cin >> G[i];

  static int dist[1 << 16];
  minus(dist);
  dist[0] = 0;

  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    if (~dist[(1 << 16) - 1])
      break;

    int curr = q.front();
    q.pop();

    for (auto hw : vs) {
      int h = hw.first, w = hw.second;

      for (auto col : cols)
        REP(i, -h + 1, 4) REP(j, -w + 1, 4) {
          int next = curr;
          REP(y, i, min(i + h, 4)) REP(x, j, min(j + w, 4)) {
            if (G[y][x] == col)
              next |= 1 << (y * 4 + x);
            else
              next &= ~(1 << (y * 4 + x));
          }
          if (dist[next] < 0) {
            dist[next] = dist[curr] + 1;
            q.push(next);
          }
        }
    }
  }

  cout << dist[(1 << 16) - 1] << endl;

  return 0;
}