Problem p03330 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
typedef pair<int, int> P;
typedef pair<Int, Int> Pl;
const int mod = 1e9 + 7;
static constexpr Int Inf = numeric_limits<Int>::max();
#define END                                                                    \
  {                                                                            \
    cout << ans << '\n';                                                       \
    return 0;                                                                  \
  }
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
#define mp make_pair
#define Pr(t) priority_queue<t>
#define gPr(t) priority_queue<t, vector<t>, greater<t>>
#define V(t) vector<t>
#define rep(i, n) for (int(i) = 0; i < n; i++)
#define rer(i, l, r) for (int i = l; i <= r; i++)
#define gnr(i, l, r) for (int i = r; i >= l; i--)
#define eb emplace_back
#define pri1(a) cout << (a) << '\n'
#define pri2(a, n)                                                             \
  {                                                                            \
    rep(pri2i, n - 1) cout << a[pri2i] << ' ';                                 \
    cout << a[n - 1] << '\n';                                                  \
  }
#define prip(p) cout << p.fi << ' ' << p.se << '\n'
template <class T> inline bool cmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool cmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n, m, _, x, y, q;
string s, sb;
bool ok;
char ch;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  cin >> n >> m;
  int c[m][m];
  rep(i, m) rep(j, m) cin >> c[i][j];
  V(int) grp[3];
  rep(i, n) rep(j, n) {
    cin >> x;
    grp[(i + j) % 3].eb(x - 1);
  }
  Int ans = 1e18, cost;
  rep(i, m) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < m; k++) {
        if (i == j || j == k || i == k)
          continue;
        cost = 0;
        for (int e : grp[0])
          cost += c[e][i];
        for (int e : grp[1])
          cost += c[e][j];
        for (int e : grp[2])
          cost += c[e][k];
        cmin(ans, cost);
      }
    }
  }
  END
}

Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
typedef pair<int, int> P;
typedef pair<Int, Int> Pl;
const int mod = 1e9 + 7;
static constexpr Int Inf = numeric_limits<Int>::max();
#define END                                                                    \
  {                                                                            \
    cout << ans << '\n';                                                       \
    return 0;                                                                  \
  }
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
#define mp make_pair
#define Pr(t) priority_queue<t>
#define gPr(t) priority_queue<t, vector<t>, greater<t>>
#define V(t) vector<t>
#define rep(i, n) for (int(i) = 0; i < n; i++)
#define rer(i, l, r) for (int i = l; i <= r; i++)
#define gnr(i, l, r) for (int i = r; i >= l; i--)
#define eb emplace_back
#define pri1(a) cout << (a) << '\n'
#define pri2(a, n)                                                             \
  {                                                                            \
    rep(pri2i, n - 1) cout << a[pri2i] << ' ';                                 \
    cout << a[n - 1] << '\n';                                                  \
  }
#define prip(p) cout << p.fi << ' ' << p.se << '\n'
template <class T> inline bool cmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool cmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n, m, _, x, y, q;
string s, sb;
bool ok;
char ch;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  cin >> n >> m;
  int c[m][m];
  rep(i, m) rep(j, m) cin >> c[i][j];
  V(int) grp[3];
  rep(i, n) rep(j, n) {
    cin >> x;
    grp[(i + j) % 3].eb(x - 1);
  }
  Int cl[3][m];
  fill(cl[0], cl[3], 0);
  rep(i, 3) rep(j, m) for (int e : grp[i]) cl[i][j] += c[e][j];
  Int ans = 1e18;
  rep(i, m) for (int j = 0; j < m; j++) for (int k = 0; k < m; k++) {
    if (i == j || j == k || i == k)
      continue;
    cmin(ans, cl[0][i] + cl[1][j] + cl[2][k]);
  }
  END
}