Problem p03330 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define start_routine                                                          \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  int begtime = clock();
#define end_routine                                                            \
  int endtime = clock();                                                       \
  cerr << endl                                                                 \
       << "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC      \
       << " ms";                                                               \
  return 0
#define contests_de_diya_kar 0
#define PB push_back
#define MP make_pair
#define int long long
#define MOD ((long long)(1000 * 1000 * 1000 + 7))
#define inf (int)2e15
#define exit(a, b) return cout << a, b;
#define eps (double)1e-8
#define forn(a, b, i) for (int i = a; i < b; i += 1)
#define vi vector<int>
#define vb vector<bool>
#define all(v) v.begin(), v.end()
#define ll long long
#define pii pair<int, int>
#define mii map<int, int>
#define print(stuff) cout << stuff << endl
#define len(stuff) stuff.size()

const int MODULO = MOD;
int n;
bool vis[3][1001][4];
int dp[3][1001][4];
bool blocked[3][1001];

int rec(int row, int col, int type) {
  if (vis[row][col][type])
    return dp[row][col][type];
  vis[row][col][type] = true;
  if (blocked[row][col]) {
    dp[row][col][type] = 0;
    return 0;
  }
  if (type == 0) {
    if (row >= 1) {
      if (col >= n - 1 && row != 2) {
        dp[row][col][type] = 0;
        return 0;
      }
      dp[row][col][type] += rec(row - 1, col, 2);
      dp[row][col][type] %= MODULO;
    }
  }
  if (type == 1) {
    if (row <= 1) {
      if (col >= n - 1) {
        dp[row][col][type] = 0;
        return 0;
      }
      dp[row][col][type] += rec(row + 1, col, 3);
      dp[row][col][type] %= MODULO;
    }
  }
  if (type == 2) {
    if (col == 0 && row == 0) {
      dp[row][col][type] = 1;
      return dp[row][col][type];
    }
    if (col >= 1 && row < 2) {
      dp[row][col][type] += rec(row, col - 1, 0);
      dp[row][col][type] %= MODULO;
      dp[row][col][type] += rec(row, col - 1, 1);
      dp[row][col][type] %= MODULO;
    }
  }
  if (type == 3) {
    if (col >= 1 && row >= 1) {
      dp[row][col][type] += rec(row, col - 1, 0);
      dp[row][col][type] %= MODULO;
      dp[row][col][type] += rec(row, col - 1, 1);
      dp[row][col][type] %= MODULO;
    } else {
      dp[row][col][type] = 0;
      return dp[row][col][type];
    }
  }
}
signed main() {
  start_routine;

#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif

  int n, C;
  cin >> n >> C;

  int D[C + 1][C + 1];
  for (int i = 1; i <= C; i += 1) {
    for (int j = 1; j <= C; j += 1)
      cin >> D[i][j];
  }
  int c[n + 1][n + 1];
  for (int i = 1; i <= n; i += 1) {
    for (int j = 1; j <= n; j += 1) {
      cin >> c[i][j];
    }
  }

  int count[C + 1][3];
  for (int i = 1; i <= C; i += 1) {
    for (int j = 0; j < 3; j += 1) {
      count[i][j] = 0;
    }
  }

  for (int i = 1; i <= n; i += 1)
    for (int j = 1; j <= n; j += 1)
      count[c[i][j]][(i + j) % 3] += 1;

  int mn = inf;
  for (int i = 1; i <= C; i += 1) {
    for (int j = 1; j <= C; j += 1) {
      for (int k = 1; k <= C; k += 1) {
        int cost = 0;
        if (i == j || j == k || i == k)
          continue;
        for (int a = 1; a <= n; a += 1) {
          for (int b = 1; b <= n; b += 1) {
            if ((a + b) % 3 == 0) {
              if (c[a][b] != i)
                cost += D[c[a][b]][i];
            }
            if ((a + b) % 3 == 1) {
              if (c[a][b] != j)
                cost += D[c[a][b]][j];
            }
            if ((a + b) % 3 == 2) {
              if (c[a][b] != k)
                cost += D[c[a][b]][k];
            }
          }
        }
        mn = min(mn, cost);
      }
    }
  }

  cout << mn;
  end_routine;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define start_routine                                                          \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  int begtime = clock();
#define end_routine                                                            \
  int endtime = clock();                                                       \
  cerr << endl                                                                 \
       << "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC      \
       << " ms";                                                               \
  return 0
#define contests_de_diya_kar 0
#define PB push_back
#define MP make_pair
#define int long long
#define MOD ((long long)(1000 * 1000 * 1000 + 7))
#define inf (int)2e15
#define exit(a, b) return cout << a, b;
#define eps (double)1e-8
#define forn(a, b, i) for (int i = a; i < b; i += 1)
#define vi vector<int>
#define vb vector<bool>
#define all(v) v.begin(), v.end()
#define ll long long
#define pii pair<int, int>
#define mii map<int, int>
#define print(stuff) cout << stuff << endl
#define len(stuff) stuff.size()

const int MODULO = MOD;
int n;
bool vis[3][1001][4];
int dp[3][1001][4];
bool blocked[3][1001];

int rec(int row, int col, int type) {
  if (vis[row][col][type])
    return dp[row][col][type];
  vis[row][col][type] = true;
  if (blocked[row][col]) {
    dp[row][col][type] = 0;
    return 0;
  }
  if (type == 0) {
    if (row >= 1) {
      if (col >= n - 1 && row != 2) {
        dp[row][col][type] = 0;
        return 0;
      }
      dp[row][col][type] += rec(row - 1, col, 2);
      dp[row][col][type] %= MODULO;
    }
  }
  if (type == 1) {
    if (row <= 1) {
      if (col >= n - 1) {
        dp[row][col][type] = 0;
        return 0;
      }
      dp[row][col][type] += rec(row + 1, col, 3);
      dp[row][col][type] %= MODULO;
    }
  }
  if (type == 2) {
    if (col == 0 && row == 0) {
      dp[row][col][type] = 1;
      return dp[row][col][type];
    }
    if (col >= 1 && row < 2) {
      dp[row][col][type] += rec(row, col - 1, 0);
      dp[row][col][type] %= MODULO;
      dp[row][col][type] += rec(row, col - 1, 1);
      dp[row][col][type] %= MODULO;
    }
  }
  if (type == 3) {
    if (col >= 1 && row >= 1) {
      dp[row][col][type] += rec(row, col - 1, 0);
      dp[row][col][type] %= MODULO;
      dp[row][col][type] += rec(row, col - 1, 1);
      dp[row][col][type] %= MODULO;
    } else {
      dp[row][col][type] = 0;
      return dp[row][col][type];
    }
  }
}
signed main() {
  start_routine;

#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif

  int n, C;
  cin >> n >> C;

  int D[C + 1][C + 1];
  for (int i = 1; i <= C; i += 1) {
    for (int j = 1; j <= C; j += 1)
      cin >> D[i][j];
  }
  int c[n + 1][n + 1];
  for (int i = 1; i <= n; i += 1) {
    for (int j = 1; j <= n; j += 1) {
      cin >> c[i][j];
    }
  }

  int count[C + 1][3];
  for (int i = 1; i <= C; i += 1) {
    for (int j = 0; j < 3; j += 1) {
      count[i][j] = 0;
    }
  }

  for (int i = 1; i <= n; i += 1)
    for (int j = 1; j <= n; j += 1)
      count[c[i][j]][(i + j) % 3] += 1;

  int mn = inf;
  for (int i = 1; i <= C; i += 1) {
    for (int j = 1; j <= C; j += 1) {
      for (int k = 1; k <= C; k += 1) {
        int cost = 0;
        if (i == j || j == k || i == k)
          continue;
        for (int col = 1; col <= C; col += 1) {
          for (int mod = 0; mod < 3; mod += 1) {
            if (mod == 0) {
              if (col != i)
                cost += count[col][mod] * D[col][i];
            } else if (mod == 1) {
              if (col != j)
                cost += count[col][mod] * D[col][j];
            } else {
              if (col != k)
                cost += count[col][mod] * D[col][k];
            }
          }
        }
        mn = min(mn, cost);
      }
    }
  }

  cout << mn;
  end_routine;
}