Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int INF = 1001001001;

int main() {
  int N, C;
  cin >> N >> C;
  vector<vector<int>> D(C, vector<int>(C));
  for (int i = 0; i < C; ++i) {
    for (int j = 0; j < C; ++j) {
      cin >> D[i][j];
    }
  }
  // 色を0...C-1にしておく
  vector<vector<int>> c(N, vector<int>(C));
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> c[i][j];
      c[i][j]--;
    }
  }

  // 0, 1, 2(mod3)に塗られている色の個数を調べておく
  vector<int> zero(30, 0), one(30, 0), two(30, 0);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      int temp = ((i + 1) + (j + 1)) % 3;
      switch (temp) {
      case 0:
        zero[c[i][j]]++;
        break;

      case 1:
        one[c[i][j]]++;
        break;

      default:
        two[c[i][j]]++;
        break;
      }
    }
  }

  int ans = INF;
  // 色を決める
  for (int a = 0; a < C; ++a) {   // 0(mod3)
    for (int b = 0; b < C; ++b) { // 1(mod3)
      if (b == a)
        continue;
      for (int c = 0; c < C; ++c) { // 2(mod3)
        if (c == a || c == b)
          continue;

        int temp = 0;

        // 0(mod3)
        for (int i = 0; i < C; ++i) {
          if (i == a)
            continue;

          // 色iを色aに変えるコスト
          temp += D[i][a] * zero[i];
        }

        // 1(mod3)
        for (int i = 0; i < C; ++i) {
          if (i == b)
            continue;

          temp += D[i][b] * one[i];
        }

        // 2(mod3)
        for (int i = 0; i < C; ++i) {
          if (i == c)
            continue;

          temp += D[i][c] * two[i];
        }

        ans = min(ans, temp);
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int INF = 1001001001;

int main() {
  int N, C;
  cin >> N >> C;
  vector<vector<int>> D(C, vector<int>(C));
  for (int i = 0; i < C; ++i) {
    for (int j = 0; j < C; ++j) {
      cin >> D[i][j];
    }
  }
  // 色を0...C-1にしておく
  vector<vector<int>> c(N, vector<int>(N));
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> c[i][j];
      c[i][j]--;
    }
  }

  // 0, 1, 2(mod3)に塗られている色の個数を調べておく
  vector<int> zero(30, 0), one(30, 0), two(30, 0);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      int temp = ((i + 1) + (j + 1)) % 3;
      switch (temp) {
      case 0:
        zero[c[i][j]]++;
        break;

      case 1:
        one[c[i][j]]++;
        break;

      default:
        two[c[i][j]]++;
        break;
      }
    }
  }

  int ans = INF;
  // 色を決める
  for (int a = 0; a < C; ++a) {   // 0(mod3)
    for (int b = 0; b < C; ++b) { // 1(mod3)
      if (b == a)
        continue;
      for (int c = 0; c < C; ++c) { // 2(mod3)
        if (c == a || c == b)
          continue;

        int temp = 0;

        // 0(mod3)
        for (int i = 0; i < C; ++i) {
          if (i == a)
            continue;

          // 色iを色aに変えるコスト
          temp += D[i][a] * zero[i];
        }

        // 1(mod3)
        for (int i = 0; i < C; ++i) {
          if (i == b)
            continue;

          temp += D[i][b] * one[i];
        }

        // 2(mod3)
        for (int i = 0; i < C; ++i) {
          if (i == c)
            continue;

          temp += D[i][c] * two[i];
        }

        ans = min(ans, temp);
      }
    }
  }

  cout << ans << endl;
}