Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define rep(N) for (int i = 0; i < N; ++i)
#define Rep(a, b) for (int i = a; i < b; ++i)
#define For(i, N) for (int i = 0; i < N; ++i)
#define all(v) v.begin(), v.end()
#define rev(v) v.rbegin(), v.rend()
#define makei(N)                                                               \
  int N;                                                                       \
  cin >> N;
#define makel(N)                                                               \
  ll N;                                                                        \
  cin >> N;
#define makes(s)                                                               \
  string s;                                                                    \
  cin >> s;
#define maked(d)                                                               \
  double d;                                                                    \
  cin >> d;
#define makev(v, N)                                                            \
  vi v(N);                                                                     \
  rep(N) cin >> v[i];
#define mod 1000000007
#define re return 0
#define distance(x1, y1, x2, y2)                                               \
  sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

using namespace std;
using ll = long long int;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vs = vector<string>;
using pii = pair<int, int>;
using pis = pair<int, string>;
using msi = map<string, int>;

template <typename T> void say(T s) { cout << s << "\n"; }

template <typename T> void say(vector<T> s) {
  auto itr = s.begin();
  cout << *(itr++);
  while (itr != s.end()) {
    cout << " " << *(itr++);
  }
  cout << "\n";
}

void yn(bool b) {
  if (b)
    say("Yes");
  else
    say("No");
}

template <typename T> void maxi(T &a, T b) { a = max(a, b); }

template <typename T> void mini(T &a, T b) { a = min(a, b); }

int main() {
  makei(N);
  makei(C);
  vvi d(C);
  vvi ans;
  vvi c(3);
  For(i, 3) For(j, C) ans[i].push_back(0);
  For(i, C) {
    For(j, C) {
      makei(D);
      d[i].push_back(D);
    }
  }
  For(i, N) {
    For(j, N) {
      makei(D);
      c[(i + j) % 3].push_back(D - 1);
    }
  }
  For(i, 3) {
    For(j, C) {
      for (int k : c[i]) {
        ans[i][j] += d[k][j];
      }
    }
  }
  int Ans = INT32_MAX;
  For(i, C) {
    For(j, C) {
      For(k, C) {
        if (i == j || j == k || k == i)
          continue;
        mini(Ans, ans[0][i] + ans[1][j] + ans[2][k]);
      }
    }
  }
  say(Ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define rep(N) for (int i = 0; i < N; ++i)
#define Rep(a, b) for (int i = a; i < b; ++i)
#define For(i, N) for (int i = 0; i < N; ++i)
#define all(v) v.begin(), v.end()
#define rev(v) v.rbegin(), v.rend()
#define makei(N)                                                               \
  int N;                                                                       \
  cin >> N;
#define makel(N)                                                               \
  ll N;                                                                        \
  cin >> N;
#define makes(s)                                                               \
  string s;                                                                    \
  cin >> s;
#define maked(d)                                                               \
  double d;                                                                    \
  cin >> d;
#define makev(v, N)                                                            \
  vi v(N);                                                                     \
  rep(N) cin >> v[i];
#define mod 1000000007
#define re return 0
#define distance(x1, y1, x2, y2)                                               \
  sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

using namespace std;
using ll = long long int;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vs = vector<string>;
using pii = pair<int, int>;
using pis = pair<int, string>;
using msi = map<string, int>;

template <typename T> void say(T s) { cout << s << "\n"; }

template <typename T> void say(vector<T> s) {
  auto itr = s.begin();
  cout << *(itr++);
  while (itr != s.end()) {
    cout << " " << *(itr++);
  }
  cout << "\n";
}

void yn(bool b) {
  if (b)
    say("Yes");
  else
    say("No");
}

template <typename T> void maxi(T &a, T b) { a = max(a, b); }

template <typename T> void mini(T &a, T b) { a = min(a, b); }

int main() {
  makei(N);
  makei(C);
  vvi d(C);
  vvi ans(3);
  vvi c(3);
  For(i, 3) For(j, C) ans[i].push_back(0);
  For(i, C) {
    For(j, C) {
      makei(D);
      d[i].push_back(D);
    }
  }
  For(i, N) {
    For(j, N) {
      makei(D);
      c[(i + j) % 3].push_back(D - 1);
    }
  }
  For(i, 3) {
    For(j, C) {
      for (int k : c[i]) {
        ans[i][j] += d[k][j];
      }
    }
  }
  int Ans = INT32_MAX;
  For(i, C) {
    For(j, C) {
      For(k, C) {
        if (i == j || j == k || k == i)
          continue;
        mini(Ans, ans[0][i] + ans[1][j] + ans[2][k]);
      }
    }
  }
  say(Ans);
}