Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

using pll = pair<ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using gr = vector<vll>;
using wgr = vector<vpll>;

#define pb push_back
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define REP(i, n) FOR(i, 0, n)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
struct d_ {
  template <typename T> d_ &operator,(const T &x) {
    cerr << ' ' << x;
    return *this;
  }
  template <typename T> d_ &operator,(const vector<T> &x) {
    for (auto x : x)
      cerr << ' ' << x;
    return *this;
  }
} d_t;
#define dbg(args...)                                                           \
  { d_t, "|", __LINE__, "|", ":", args, "\n"; }
#define EPS (1e-10)
#define INF (1LL << 61)
#define CL(A, I) (memset(A, I, sizeof(A)))
#define all(x) begin(x), end(x)
#define IN(n)                                                                  \
  ll n;                                                                        \
  cin >> n;
#define x first
#define y second

#define MXC 33
#define MXN 33
ll D[MXC][MXC];
ll C[MXN][MXN];
ll n, c;
ll m2c[3][MXC];

ll solve(ll pos, ll msk) {
  if (pos == 3)
    return 0;
  ll ret = INF;
  F(c) if (((1ll << i) & (msk)) == 0) {
    ll price = 0;
    FF(c) price += m2c[pos][j] * D[j][i];
    ret = min(ret, solve(pos + 1, msk | (1ll << i)) + price);
  }
  return ret;
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin >> n >> c;
  F(c) FF(c) cin >> D[i][j];
  F(n) FF(n) {
    cin >> C[i][j];
    C[i][j]--;
  }
  F(n) FF(n) { m2c[(i + j + 2) % 3][C[i][j]]++; }
  cout << solve(0, 0) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

using pll = pair<ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using gr = vector<vll>;
using wgr = vector<vpll>;

#define pb push_back
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define REP(i, n) FOR(i, 0, n)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
struct d_ {
  template <typename T> d_ &operator,(const T &x) {
    cerr << ' ' << x;
    return *this;
  }
  template <typename T> d_ &operator,(const vector<T> &x) {
    for (auto x : x)
      cerr << ' ' << x;
    return *this;
  }
} d_t;
#define dbg(args...)                                                           \
  { d_t, "|", __LINE__, "|", ":", args, "\n"; }
#define EPS (1e-10)
#define INF (1LL << 61)
#define CL(A, I) (memset(A, I, sizeof(A)))
#define all(x) begin(x), end(x)
#define IN(n)                                                                  \
  ll n;                                                                        \
  cin >> n;
#define x first
#define y second

#define MXC 33
#define MXN 507
ll D[MXC][MXC];
ll C[MXN][MXN];
ll n, c;
ll m2c[3][MXC];

ll solve(ll pos, ll msk) {
  if (pos == 3)
    return 0;
  ll ret = INF;
  F(c) if (((1ll << i) & (msk)) == 0) {
    ll price = 0;
    FF(c) price += m2c[pos][j] * D[j][i];
    ret = min(ret, solve(pos + 1, msk | (1ll << i)) + price);
  }
  return ret;
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin >> n >> c;
  F(c) FF(c) cin >> D[i][j];
  F(n) FF(n) {
    cin >> C[i][j];
    C[i][j]--;
  }
  F(n) FF(n) { m2c[(i + j + 2) % 3][C[i][j]]++; }
  cout << solve(0, 0) << endl;
  return 0;
}