Problem p03330 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, s, n) for (int i = s; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;

static const int INTINF = (2147483647);
static const ll LLINF = (9223372036854775807);
static const int MAX = 1e5 + 1;
static const ll MOD = 1e9 + 7;

namespace Printer {
template <class T> void print(T x) { cout << x << endl; }

template <class T> void print(vector<T> v) {
  rep(i, v.size()) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
}

template <class T> void print(vector<vector<T>> vv) {
  rep(i, vv.size()) {
    rep(j, vv[i].size()) {
      if (j)
        cout << " ";
      cout << vv[i][j];
    }
    cout << endl;
  }
}

void print(pair<int, int> p) {
  cout << "(" << p.first << ", " << p.second << ")" << endl;
}
}; // namespace Printer
using namespace Printer;

//--global--//

//----------//
ll calc(ll n, ll c, vector<vector<ll>> d, vector<vector<ll>> vv,
        vector<int> out) {
  ll res = 0;
  rep(i, n) {
    rep(j, n) {
      int tmp = i + j + 2;
      int change = out[tmp % 3];
      // vv[i][j] -> change
      res += d[vv[i][j] - 1][change];
    }
  }
  return res;
}

ll solve(ll n, ll c, vector<vector<ll>> d, vector<vector<ll>> vv,
         vector<int> out) {
  if (out.size() == 3) {
    return calc(n, c, d, vv, out);
  }
  ll res = LLINF;
  rep(i, c) {
    bool ok = true;
    rep(j, out.size()) {
      if (out[j] == i) {
        ok = false;
        break;
      }
    }
    if (!ok)
      continue;
    out.push_back(i);
    res = min(res, solve(n, c, d, vv, out));
    out.erase(out.end() - 1);
  }
  return res;
}

int main(int argc, const char *argv[]) {
  // 提出時、消す----//
  //--------------//

  ll n, c;
  cin >> n >> c;
  vector<vector<ll>> d(c, vector<ll>(c));
  vector<vector<ll>> vv(n, vector<ll>(n));
  rep(i, c) rep(j, c) cin >> d[i][j];
  rep(i, n) rep(j, n) cin >> vv[i][j];

  vector<int> tmp;
  cout << solve(n, c, d, vv, tmp) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, s, n) for (int i = s; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;

static const int INTINF = (2147483647);
static const ll LLINF = (9223372036854775807);
static const int MAX = 1e5 + 1;
static const ll MOD = 1e9 + 7;

namespace Printer {
template <class T> void print(T x) { cout << x << endl; }

template <class T> void print(vector<T> v) {
  rep(i, v.size()) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
}

template <class T> void print(vector<vector<T>> vv) {
  rep(i, vv.size()) {
    rep(j, vv[i].size()) {
      if (j)
        cout << " ";
      cout << vv[i][j];
    }
    cout << endl;
  }
}

void print(pair<int, int> p) {
  cout << "(" << p.first << ", " << p.second << ")" << endl;
}
}; // namespace Printer
using namespace Printer;

//--global--//

//----------//
ll calc(ll n, ll c, vector<vector<ll>> d, vector<vector<ll>> vv,
        vector<int> out) {
  ll res = 0;
  rep(i, n) {
    rep(j, n) {
      int tmp = i + j + 2;
      int change = out[tmp % 3];
      // vv[i][j] -> change
      res += d[vv[i][j] - 1][change];
    }
  }
  return res;
}

ll solve(ll n, ll c, vector<vector<ll>> d, vector<vector<ll>> vv,
         vector<int> out) {
  if (out.size() == 3) {
    return calc(n, c, d, vv, out);
  }
  ll res = LLINF;
  rep(i, c) {
    bool ok = true;
    rep(j, out.size()) {
      if (out[j] == i) {
        ok = false;
        break;
      }
    }
    if (!ok)
      continue;
    out.push_back(i);
    res = min(res, solve(n, c, d, vv, out));
    out.erase(out.end() - 1);
  }
  return res;
}

int main(int argc, const char *argv[]) {
  // 提出時、消す----//
  //--------------//

  ll n, c;
  cin >> n >> c;
  vector<vector<ll>> d(c, vector<ll>(c));
  vector<vector<ll>> vv(n, vector<ll>(n));
  rep(i, c) rep(j, c) cin >> d[i][j];
  rep(i, n) rep(j, n) cin >> vv[i][j];

  vector<vector<pair<ll, ll>>> foo(3);
  rep(i, c) {
    vector<vector<ll>> a(3);
    rep(p, n) {
      rep(q, n) {
        int tmp = p + q + 2;
        int val = d[vv[p][q] - 1][i];
        a[tmp % 3].push_back(val);
      }
    }
    rep(j, 3) {
      ll tmp = 0;
      rep(k, a[j].size()) { tmp += a[j][k]; }
      foo[j].push_back(make_pair(tmp, i));
    }
  }
  rep(i, 3) { sort(ALL(foo[i])); }
  // rep(i, foo.size()){
  //     rep(j, foo[i].size()){
  //         if(j) cout << " ";
  //         cout << "(" << foo[i][j].first << ", " << foo[i][j].second << ")";
  //     }
  //     cout << endl;
  // }
  ll res = LLINF;
  rep(i, 3) {
    rep(j, 3) {
      rep(k, 3) {
        if (foo[0][i].second == foo[1][j].second ||
            foo[1][j].second == foo[2][k].second ||
            foo[2][k].second == foo[0][i].second)
          continue;
        res = min(res, foo[0][i].first + foo[1][j].first + foo[2][k].first);
      }
    }
  }

  cout << res << endl;

  // vector<int> tmp;
  // cout << solve(n, c, d, vv, tmp) << endl;

  return 0;
}