prithivMLmods/Coder-Stat huggingface dataset:
Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 100010;

int memo[MAX_N];
int rec(int n) {
  if (n == 0)
    return 0;
  if (memo[n] != -1)
    return memo[n];

  int res = n;

  for (int i = 1; i <= n; i *= 6)
    res = min(res, rec(n - i) + 1);
  for (int i = 1; i <= n; i * -9)
    res = min(res, rec(n - i) + 1);
  return memo[n] = res;
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < MAX_N; i++)
    memo[i] = -1;
  cout << rec(N) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 100010;

int memo[MAX_N];
int rec(int n) {
  if (n == 0)
    return 0;
  if (memo[n] != -1)
    return memo[n];

  int res = n;

  for (int i = 1; i <= n; i *= 6)
    res = min(res, rec(n - i) + 1);
  for (int i = 1; i <= n; i *= 9)
    res = min(res, rec(n - i) + 1);
  return memo[n] = res;
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < MAX_N; i++)
    memo[i] = -1;
  cout << rec(N) << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> dp(10001, 1e9);
  vector<int> d;
  d.push_back(1);
  for (int i = 6; i <= n; i *= 6)
    d.push_back(i);
  for (int i = 9; i <= n; i *= 9)
    d.push_back(i);
  dp[0] = 0;
  for (int i = 0; i < d.size(); i++) {
    for (int j = 0; j <= n; j++) {
      if (j + d[i] > n)
        break;
      dp[j + d[i]] = min(dp[j + d[i]], dp[j] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> dp(100010, 1e9);
  vector<int> d;
  d.push_back(1);
  for (int i = 6; i <= n; i *= 6)
    d.push_back(i);
  for (int i = 9; i <= n; i *= 9)
    d.push_back(i);
  dp[0] = 0;
  for (int i = 0; i < d.size(); i++) {
    for (int j = 0; j <= n; j++) {
      if (j + d[i] > n)
        break;
      dp[j + d[i]] = min(dp[j + d[i]], dp[j] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

typedef long long ll;
using namespace std;

int main() {
  int n = 0;
  cin >> n;
  vector<ll> cost;
  cost.push_back(1);
  ll buf = 9;
  while (buf <= 100000) {
    cost.push_back(buf);
    buf *= 9;
  }
  buf = 6;
  while (buf <= 100000) {
    cost.push_back(buf);
    buf *= 6;
  }
  sort(cost.begin(), cost.end());
  vector<int> dp(n + 1);
  for (int i = 1; i < n + 1; i++) {
    int buf = 10000001;
    for (int j = 0;; j++) {
      if (i >= cost.at(j)) {
        buf = min(buf, dp.at(i - cost.at(j)) + 1);
      } else
        break;
    }
    dp.at(i) = buf;
  }
  cout << dp.at(n) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

typedef long long ll;
using namespace std;

int main() {
  int n = 0;
  cin >> n;
  vector<ll> cost;
  cost.push_back(1);
  ll buf = 9;
  while (buf <= 100000) {
    cost.push_back(buf);
    buf *= 9;
  }
  buf = 6;
  while (buf <= 100000) {
    cost.push_back(buf);
    buf *= 6;
  }
  sort(cost.begin(), cost.end());
  vector<int> dp(n + 1);
  for (int i = 1; i < n + 1; i++) {
    int buf = 10000001;
    for (int j = 0; j < cost.size(); j++) {
      if (i >= cost.at(j)) {
        buf = min(buf, dp.at(i - cost.at(j)) + 1);
      } else
        break;
    }
    dp.at(i) = buf;
  }
  cout << dp.at(n) << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> dp(2 * N, 1 << 30);
  for (int i = 0; i <= N; i++)
    dp[i] = i;

  vector<int> v;

  int a = 6;
  for (int i = 0; i < 100; i++) {
    if (a > N)
      break;
    v.push_back(a);
    a *= 6;
  }
  a = 9;
  for (int i = 0; i < 100; i++) {
    if (a > N)
      break;
    v.push_back(a);
    a *= 9;
  }
  for (int x : v) {
    for (int i = 0; i < N; i++) {
      if (dp[i] == 1 << 30)
        continue;
      dp[i + x] = min(dp[i + x], dp[i] + 1);
    }
  }

  cout << dp[N] << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> dp(2 * N, 1 << 30);
  for (int i = 0; i <= N; i++)
    dp[i] = i;

  vector<int> v;

  int a = 6;
  for (int i = 0; i < 100; i++) {
    if (a > N)
      break;
    v.push_back(a);
    a *= 6;
  }
  a = 9;
  for (int i = 0; i < 100; i++) {
    if (a > N)
      break;
    v.push_back(a);
    a *= 9;
  }
  for (int x : v) {
    for (int i = 0; i < N; i++) {
      if (dp[i] == 1 << 30)
        continue;
      dp[i + x] = min(dp[i + x], dp[i] + 1);
    }
  }

  cout << dp[N] << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 100010001;
int main() {
  int n;
  cin >> n;
  vector<int> dp(n);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = 1 + dp[i - 1];

    for (int p = 6; p <= i; p *= 6) {
      dp[i] = min(dp[i], dp[i - p] + 1);
    }
    for (int p = 9; p <= i; p *= 9) {
      dp[i] = min(dp[i], dp[i - p] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 100010001;
int main() {
  int n;
  cin >> n;
  vector<int> dp(n + 10, INF);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = 1 + dp[i - 1];

    for (int p = 6; p <= i; p *= 6) {
      dp[i] = min(dp[i], dp[i - p] + 1);
    }
    for (int p = 9; p <= i; p *= 9) {
      dp[i] = min(dp[i], dp[i - p] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll MOD = 1000000007;
int dp[100000 + 1000];
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
long long longpow(long long n, long long a, long long mod) {
  if (a == 0)
    return 1;
  if (a == 1)
    return n % mod;
  if (a % 2 == 1)
    return (n * longpow(n, a - 1, mod)) % mod;
  ll t = longpow(n, a / 2, mod) % mod;
  return (t * t) % mod;
}
int main() {
  int N;
  cin >> N;
  REP(i, N + 10) dp[i] = i;
  REP(i, N + 10) {
    for (int j = 1; longpow(6, j, MOD) <= N; j++) {
      chmin(dp[i + longpow(6, j, MOD)], dp[i] + 1);
    }
  }
  REP(i, N + 10) {
    for (int j = 1; longpow(9, j, MOD) <= N; j++) {
      chmin(dp[i + longpow(9, j, MOD)], dp[i] + 1);
    }
  }
  cout << dp[N] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll MOD = 1000000007;
int dp[200000];
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
long long longpow(long long n, long long a, long long mod) {
  if (a == 0)
    return 1;
  if (a == 1)
    return n % mod;
  if (a % 2 == 1)
    return (n * longpow(n, a - 1, mod)) % mod;
  ll t = longpow(n, a / 2, mod) % mod;
  return (t * t) % mod;
}
int main() {
  int N;
  cin >> N;
  REP(i, N + 10) dp[i] = i;
  REP(i, N + 10) {
    for (int j = 1; longpow(6, j, MOD) <= N; j++) {
      chmin(dp[i + longpow(6, j, MOD)], dp[i] + 1);
    }
  }
  REP(i, N + 10) {
    for (int j = 1; longpow(9, j, MOD) <= N; j++) {
      chmin(dp[i + longpow(9, j, MOD)], dp[i] + 1);
    }
  }
  cout << dp[N] << endl;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SIZE 200005
#define INF 1000000005LL

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int n;
ll K;

int C[25];
int T[50005];

void dps() {
  C[0] = 1;
  C[1] = 6;
  C[2] = 9;
  for (int i = 1; i <= 7; i++) {
    int j = i * 2;
    C[j + 1] = C[j - 1] * 6;
    C[j + 2] = C[j] * 9;
  }
  sort(C, C + 17);
  reverse(C, C + 17);
}

int main() {
  int m = 17;
  scanf("%d", &n);
  dps();

  for (int i = 0; i <= n; i++)
    T[i] = 99999999;
  T[0] = 0;

  for (int i = 1; i <= m; i++) {
    for (int j = C[i]; j <= n; j++) {
      T[j] = min(T[j], T[j - C[i]] + 1);
    }
  }

  printf("%d\n", T[n]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SIZE 200005
#define INF 1000000005LL

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int n;
ll K;

int C[25];
int T[100005];

void dps() {
  C[0] = 1;
  C[1] = 6;
  C[2] = 9;
  for (int i = 1; i <= 7; i++) {
    int j = i * 2;
    C[j + 1] = C[j - 1] * 6;
    C[j + 2] = C[j] * 9;
  }
  sort(C, C + 17);
  reverse(C, C + 17);
}

int main() {
  int m = 17;
  scanf("%d", &n);
  dps();

  for (int i = 0; i <= n; i++)
    T[i] = 99999999;
  T[0] = 0;

  for (int i = 1; i <= m; i++) {
    for (int j = C[i]; j <= n; j++) {
      T[j] = min(T[j], T[j - C[i]] + 1);
    }
  }

  printf("%d\n", T[n]);
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

long long a[12] = {59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6, 1};

long long counter(long long N, int index, long long count) {
  if (index == 11) {
    return N;
  }
  long long maxindex = N / a[index];
  long long minreturn = 99999999999;
  for (long long i = 0; i <= maxindex; i++) {
    if (i > count)
      break;
    minreturn = min(minreturn, i + counter(N - a[index] * i, index + 1, count));
  }
  return minreturn;
}

int main() {
  long long N;
  long long shou, count = 0;
  cin >> N;
  long long tempN = N;

  for (int i = 0; i < 12; i++) {
    shou = N / a[i];
    count += shou;
    N -= shou * a[i];
  }

  cout << counter(tempN, 0, count) << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

long long a[12] = {59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6, 1};

long long counter(long long N, int index, long long count) {
  if (index == 11) {
    return N;
  }
  long long maxindex = N / a[index];
  long long minreturn = 99999999999;
  for (long long i = 0; i <= maxindex; i++) {
    if (i > count)
      break;
    if (maxindex > count)
      break;
    minreturn = min(minreturn, i + counter(N - a[index] * i, index + 1, count));
  }
  return minreturn;
}

int main() {
  long long N;
  long long shou, count = 0;
  cin >> N;
  long long tempN = N;

  for (int i = 0; i < 12; i++) {
    shou = N / a[i];
    count += shou;
    N -= shou * a[i];
  }

  cout << counter(tempN, 0, count) << endl;
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define INF 2000000000
#define MOD 1000000007
#define ll long long
#define vi vector<int>
#define pb push_back
#define ii pair<int, int>
#define vii vector<ii>
#define si set<int>
#define msi map<string, int>
#define REP(i, a, b) for (int i = int(a); i <= int(b); i++)
#define TRvi(c, it) for (vi::iterator it = (c).begin(); it != (c).end(); it++)
#define TRvii(c, it) for (vii::iterator it = (c).begin(); it != (c).end(); it++)
#define TRmsi(c, it) for (msi::iterator it = (c).begin(); it != (c).end(); it++)

using namespace std;

int main() {
  IOS;
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  int N;
  cin >> N;
  int dp[N + 1];
  dp[0] = 0;
  REP(i, 1, N)
  dp[i] = 1000000;
  REP(i, 0, N - 1) {
    dp[i + 1] = min(dp[i] + 1, dp[i + 1]);
    for (int j = 6; i + j <= N; j *= 6)
      dp[i + j] = min(dp[i + j], dp[i] + 1);
    for (int j = 9; i + j <= N; j *= 9)
      dp[i + j] = min(dp[i + j], dp[i] + 1);
  }
  cout << dp[N];
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define INF 2000000000
#define MOD 1000000007
#define ll long long
#define vi vector<int>
#define pb push_back
#define ii pair<int, int>
#define vii vector<ii>
#define si set<int>
#define msi map<string, int>
#define REP(i, a, b) for (int i = int(a); i <= int(b); i++)
#define TRvi(c, it) for (vi::iterator it = (c).begin(); it != (c).end(); it++)
#define TRvii(c, it) for (vii::iterator it = (c).begin(); it != (c).end(); it++)
#define TRmsi(c, it) for (msi::iterator it = (c).begin(); it != (c).end(); it++)

using namespace std;

int main() {
  IOS;
  /*#ifndef ONLINE_JUDGE
      // for getting input from input.txt
      freopen("input.txt", "r", stdin);
      // for writing output to output.txt
      freopen("output.txt", "w", stdout);
  #endif*/
  int N;
  cin >> N;
  int dp[N + 1];
  dp[0] = 0;
  REP(i, 1, N)
  dp[i] = 1000000;
  REP(i, 0, N - 1) {
    dp[i + 1] = min(dp[i] + 1, dp[i + 1]);
    for (int j = 6; i + j <= N; j *= 6)
      dp[i + j] = min(dp[i + j], dp[i] + 1);
    for (int j = 9; i + j <= N; j *= 9)
      dp[i + j] = min(dp[i + j], dp[i] + 1);
  }
  cout << dp[N];
}
Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, dp[100010];
  cin >> n;
  dp[0] = 0;
  for (int i = 1; i <= 100000; i++) {
    dp[i] = 1e9;
    int power = 1;
    while (power <= i) {
      dp[i] = min(dp[i], dp[i - power] + 1);
      power *= 6;
    }
    int power2 = 1;
    while (power2 <= i) {
      dp[i] = min(dp[i], dp[i - power2] + 1);
      power *= 9;
    }
  }
  cout << dp[n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, dp[100010];
  cin >> n;
  dp[0] = 0;
  for (int i = 1; i <= 100000; i++) {
    dp[i] = 1e9;
    int power = 1;
    while (power <= i) {
      dp[i] = min(dp[i], dp[i - power] + 1);
      power *= 6;
    }
    int power2 = 1;
    while (power2 <= i) {
      dp[i] = min(dp[i], dp[i - power2] + 1);
      power2 *= 9;
    }
  }
  cout << dp[n] << endl;
}
Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *      ABC 99 C
 *      author  : kyomukyomupurin
 *      created : 2018-10-05 14:55:45
 **/

#include <bits/stdc++.h>
#define int32 int_fast32_t
#define int64 int_fast64_t
using namespace std;

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

  int32 N;
  cin >> N;

  int32 ans = 1e5 + 1;

  for (auto i = 0; i <= N; ++i) {
    int32 cnt = 0;
    int32 t = i;
    int32 tt = N - i;
    while (t > 0) {
      cnt += (t % 6);
      t /= 6;
    }
    while (tt > 0) {
      cnt += (tt % 6);
      t /= 9;
    }
    if (cnt < ans)
      ans = cnt;
  }
  cout << ans << '\n';

  return 0;
}

Fixed submission:
/**
 *      ABC 99 C
 *      author  : kyomukyomupurin
 *      created : 2018-10-05 14:55:45
 **/

#include <bits/stdc++.h>
#define int32 int_fast32_t
#define int64 int_fast64_t
using namespace std;

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

  int32 N;
  cin >> N;

  int32 ans = 1e5 + 1;

  for (auto i = 0; i <= N; ++i) {
    int32 cnt = 0;
    int32 t = i;
    int32 tt = N - i;
    while (t > 0) {
      cnt += (t % 6);
      t /= 6;
    }
    while (tt > 0) {
      cnt += (tt % 9);
      tt /= 9;
    }
    if (cnt < ans)
      ans = cnt;
  }
  cout << ans << '\n';

  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
#define INF 1000000007
using namespace std;

int main(void) {
  int n;
  cin >> n;
  vector<int> dp(n + 1, 10000000);
  dp[0] = 0;
  for (int i = 0; i <= n; i++) {
    dp[i + 1] = min(dp[i + 1], dp[i] + 1);
    for (int j = 6; i + j <= n; j *= 6)
      dp[i + j] = min(dp[i + j], dp[i] + 1);
    for (int j = 9; i + j <= n; j *= 9)
      dp[i + j] = min(dp[i + j], dp[i] + 1);
  }
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
#define INF 1000000007
using namespace std;

int main(void) {
  int n;
  cin >> n;
  vector<int> dp(n + 10, 10000000);
  dp[0] = 0;
  for (int i = 0; i <= n; i++) {
    dp[i + 1] = min(dp[i + 1], dp[i] + 1);
    for (int j = 6; i + j <= n; j *= 6)
      dp[i + j] = min(dp[i + j], dp[i] + 1);
    for (int j = 9; i + j <= n; j *= 9)
      dp[i + j] = min(dp[i + j], dp[i] + 1);
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03329 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
num = [1]

x = 6
while x < 100000:
    num.append(x)
    x *= 6

x = 9
while x < 100000:
    num.append(x)
    x *= 9

dp = [n] * 110000
dp[0] = 0

for i in range(n):
    for j in num:
        dp[i + j] = min(dp[i + j], dp[i] + 1)

print(dp[n])


Fixed submission:
n = int(input())
num = [1]

x = 6
while x < 100000:
    num.append(x)
    x *= 6

x = 9
while x < 100000:
    num.append(x)
    x *= 9

dp = [n] * 1100000
dp[0] = 0

for i in range(n):
    for j in num:
        dp[i + j] = min(dp[i + j], dp[i] + 1)

print(dp[n])
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); (i)++)
#define all(x) x.begin(), x.end()

int N;
int main() {
  cin >> N;
  vector<i64> ans(N, 1e9);
  ans[0] = 0;
  for (int i = 6; i <= N; i *= 6) {
    rep(j, i, N) { ans[j] = min(ans[j], ans[j - i] + 1); }
  }
  for (int i = 9; i <= N; i *= 9) {
    rep(j, i, N) { ans[j] = min(ans[j], ans[j - i] + 1); }
  }

  i64 res = N;
  rep(i, 0, N) { res = min(res, i + ans[N - i]); }
  cout << res << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); (i)++)
#define all(x) x.begin(), x.end()

int N;
int main() {
  cin >> N;
  vector<i64> ans(N + 1, 1e9);
  ans[0] = 0;
  for (int i = 6; i <= N; i *= 6) {
    rep(j, i, N) { ans[j] = min(ans[j], ans[j - i] + 1); }
  }
  for (int i = 9; i <= N; i *= 9) {
    rep(j, i, N) { ans[j] = min(ans[j], ans[j - i] + 1); }
  }

  i64 res = N;
  rep(i, 0, N) { res = min(res, i + ans[N - i]); }
  cout << res << endl;
}
Problem p03329 (Python)

Buggy submission (status: Runtime Error):
from collections import deque


max_n = 11000
dp = [-1] * max_n  # -1を未探索とする
dp[0] = 0  # 初期条件の設定
n = int(input())
que = deque([])
que.append(dp[0])
while que:
    v = que.popleft()
    pow = 1
    while v + pow <= n:
        if dp[v + pow] == -1:
            dp[v + pow] = dp[v] + 1
            que.append(v + pow)
        pow *= 6
    pow = 1
    while v + pow <= n:
        if dp[v + pow] == -1:
            dp[v + pow] = dp[v] + 1
            que.append(v + pow)
        pow *= 9
# print(dp)
print(dp[n])


Fixed submission:
from collections import deque


max_n = 110000
dp = [-1] * max_n  # -1を未探索とする
dp[0] = 0  # 初期条件の設定
n = int(input())
que = deque([])
que.append(dp[0])
while que:
    v = que.popleft()
    pow = 1
    while v + pow <= n:
        if dp[v + pow] == -1:
            dp[v + pow] = dp[v] + 1
            que.append(v + pow)
        pow *= 6
    pow = 1
    while v + pow <= n:
        if dp[v + pow] == -1:
            dp[v + pow] = dp[v] + 1
            que.append(v + pow)
        pow *= 9
# print(dp)
print(dp[n])
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
/*DPを用いて実装してみる、解説は結構難しい問題なので気にしない方が良いかもです。
DPを思いついたけど、使わなかった理由の一つは、
DPを使うときに、1000000ぐらいの個数の最小値を出さないといけないと思ったから。
この辺りがしっかり実装できるのかを確認しよう
*/

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1009991
vector<ll> memo;
ll dp(int n) {
  if (n < 0) {
    return INF;
  }
  if (n == 0) {
    return 0;
  }
  if (memo[n] != -1) {
    return memo[n];
  }
  ll res = INF;
  ll k = 1;
  while (k <= n) {
    res = min(res, dp(n - k) + 1); // 最も小さいもの、
    k *= 6;
  }
  ll j = 1;
  while (j <= n) {
    res = min(res, dp(n - j) + 1); // 最も小さいもの、
    j *= 9;
  }
  memo[n] = res;
  return res;
}

int main() {

  ll N;
  cin >> N;
  memo.resize(N);
  rep(i, N + 10) { memo[i] = -1; }
  cout << dp(N);
  // cout << memo[1];
}

Fixed submission:
/*DPを用いて実装してみる、解説は結構難しい問題なので気にしない方が良いかもです。
DPを思いついたけど、使わなかった理由の一つは、
DPを使うときに、1000000ぐらいの個数の最小値を出さないといけないと思ったから。
この辺りがしっかり実装できるのかを確認しよう
*/

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1009991
vector<ll> memo;
ll dp(int n) {
  if (n < 0) {
    return INF;
  }
  if (n == 0) {
    return 0;
  }
  if (memo[n] != -1) {
    return memo[n];
  }
  ll res = INF;
  ll k = 1;
  while (k <= n) {
    res = min(res, dp(n - k) + 1); // 最も小さいもの、
    k *= 6;
  }
  ll j = 1;
  while (j <= n) {
    res = min(res, dp(n - j) + 1); // 最も小さいもの、
    j *= 9;
  }
  memo[n] = res;
  return res;
}

int main() {

  ll N;
  cin >> N;
  memo.resize(N + 10);
  rep(i, N + 10) { memo[i] = -1; }
  cout << dp(N);
  // cout << memo[1];
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

static const int INF = 1000000007;
using ll = long long;
using u32 = unsigned;
using namespace std;

int main() {
  vector<int> v = {1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049};
  u32 n;
  cin >> n;
  vector<int> dp(n + 1, INF);
  dp[0] = 0;
  for (int i = 1; i <= n + 1; ++i) {
    for (auto &&x : v) {
      if (i - x >= 0) {
        dp[i] = min(dp[i - x] + 1, dp[i]);
      } else
        break;
    }
  }
  cout << dp[n] << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

static const int INF = 1000000007;
using ll = long long;
using u32 = unsigned;
using namespace std;

int main() {
  vector<int> v = {1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049};
  u32 n;
  cin >> n;
  vector<int> dp(n + 1, INF);
  dp[0] = 0;
  for (int i = 1; i <= n; ++i) {
    for (auto &&x : v) {
      if (i - x >= 0) {
        dp[i] = min(dp[i - x] + 1, dp[i]);
      } else
        break;
    }
  }
  cout << dp[n] << "\n";
  return 0;
}
Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll dp[15][100010];
int main() {
  int w;
  scanf("%d", &w);

  vector<int> weight;
  for (int i = 0; i <= w; i *= 6)
    weight.push_back(i);
  for (int i = 9; i <= w; i *= 9)
    weight.push_back(i);
  int n = (int)weight.size();

  for (int i = 0; i < 15; i++) {
    for (int j = 0; j < 100010; j++) {
      dp[i][j] = 1145141919810;
    }
  }
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= w; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j >= weight[i])
        dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][j - weight[i]] + 1);
    }
  }
  printf("%lld\n", dp[n][w]);
}

Fixed submission:
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll dp[15][100010];
int main() {
  int w;
  scanf("%d", &w);

  vector<int> weight;
  for (int i = 1; i <= w; i *= 6)
    weight.push_back(i);
  for (int i = 9; i <= w; i *= 9)
    weight.push_back(i);
  int n = (int)weight.size();

  for (int i = 0; i < 15; i++) {
    for (int j = 0; j < 100010; j++) {
      dp[i][j] = 1145141919810;
    }
  }
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= w; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j >= weight[i])
        dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][j - weight[i]] + 1);
    }
  }
  printf("%lld\n", dp[n][w]);
}
Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
int dfs(int N) {
  if (N <= 5)
    return mp[N] = N;
  else {
    int tmp = 1e9;
    for (int i = 1; i <= N; i *= 6)
      tmp = min(tmp, dfs(N - i) + 1);
    for (int i = 1; i <= N; i *= 9)
      tmp = min(tmp, dfs(N - i) + 1);
    return mp[N] = tmp;
  }
}
int main() {
  int n;
  cin >> n;
  cout << dfs(n) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
int dfs(int N) {
  if (N <= 5)
    return mp[N] = N;
  else {
    if (mp[N] > 0)
      return mp[N];
    else {
      int tmp = 1e9;
      for (int i = 1; i <= N; i *= 6)
        tmp = min(tmp, dfs(N - i) + 1);
      for (int i = 1; i <= N; i *= 9)
        tmp = min(tmp, dfs(N - i) + 1);
      return mp[N] = tmp;
    }
  }
}
int main() {
  int n;
  cin >> n;
  cout << dfs(n) << endl;
}
Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)

typedef long long ll;
using namespace std;
int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  // dp[i]に遷移するのはdp[i-1], dp[i-2],dp[i-3],dp[i-4],dp[i-5],
  // dp[i-6],dp[i-36],..., dp[i-9],dp[i-81],...
  vector<ll> dp(N + 1, mod);
  vector<ll> fac6, fac9;
  fac6.push_back(1);
  fac9.push_back(1);
  rep(i, 10) {
    fac6.push_back(fac6.back() * 6);
    fac9.push_back(fac9.back() * 9);
  }
  rep(i, 6) dp[i] = i;

  for (int i = 6; i <= N; i++) {
    for (int j = 1; j <= 5; j++)
      dp[i] = min(dp[i], dp[i - j] + j);
    for (int j = 1; i - fac6[j] >= 0; j++)
      dp[i] = min(dp[i], dp[i - fac6[j]] + 1);
    for (int j = 1; i - fac9[j] >= 0; j++)
      dp[i] = min(dp[i], dp[i - fac9[j]] + 1);
  }
  cout << dp[N] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)

typedef long long ll;
using namespace std;
int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  // dp[i]に遷移するのはdp[i-1], dp[i-2],dp[i-3],dp[i-4],dp[i-5],
  // dp[i-6],dp[i-36],..., dp[i-9],dp[i-81],...
  vector<ll> dp(100001, mod);
  vector<ll> fac6, fac9;
  fac6.push_back(1);
  fac9.push_back(1);
  rep(i, 10) {
    fac6.push_back(fac6.back() * 6);
    fac9.push_back(fac9.back() * 9);
  }
  rep(i, 6) dp[i] = i;

  for (int i = 6; i <= N; i++) {
    for (int j = 1; j <= 5; j++)
      dp[i] = min(dp[i], dp[i - j] + j);
    for (int j = 1; i - fac6[j] >= 0; j++)
      dp[i] = min(dp[i], dp[i - fac6[j]] + 1);
    for (int j = 1; i - fac9[j] >= 0; j++)
      dp[i] = min(dp[i], dp[i - fac9[j]] + 1);
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main() {
  int n, c;
  cin >> n >> c;
  vector<vector<int>> d(c, vector<int>(c));
  rep(i, c) {
    rep(j, c) { cin >> d.at(i).at(j); }
  }
  vector<vector<int>> vc(n, vector<int>(n));
  rep(i, n) rep(j, n) {
    cin >> vc.at(i).at(j);
    vc.at(i).at(j)--;
  }

  vector<map<int, int>> mpv(3);
  rep(i, n) {
    rep(j, n) { mpv.at((i + j) % c)[vc.at(i).at(j)]++; }
  }
  ll ans = 1e18;
  rep(i, c) rep(j, c) rep(k, c) {
    if (i == j || j == k || i == k)
      continue;
    ll add = 0;
    for (auto p : mpv.at(0)) {
      add += (ll)p.second * d.at(p.first).at(i);
    }
    for (auto p : mpv.at(1)) {
      add += (ll)p.second * d.at(p.first).at(j);
    }
    for (auto p : mpv.at(2)) {
      add += (ll)p.second * d.at(p.first).at(k);
    }
    // cerr << add << endl;
    ans = min(ans, add);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main() {
  int n, c;
  cin >> n >> c;
  vector<vector<int>> d(c, vector<int>(c));
  rep(i, c) {
    rep(j, c) { cin >> d.at(i).at(j); }
  }
  vector<vector<int>> vc(n, vector<int>(n));
  rep(i, n) rep(j, n) {
    cin >> vc.at(i).at(j);
    vc.at(i).at(j)--;
  }

  vector<map<int, int>> mpv(3);
  rep(i, n) {
    rep(j, n) { mpv.at((i + j) % 3)[vc.at(i).at(j)]++; }
  }
  ll ans = 1e18;
  rep(i, c) rep(j, c) rep(k, c) {
    if (i == j || j == k || i == k)
      continue;
    ll add = 0;
    for (auto p : mpv.at(0)) {
      add += (ll)p.second * d.at(p.first).at(i);
    }
    for (auto p : mpv.at(1)) {
      add += (ll)p.second * d.at(p.first).at(j);
    }
    for (auto p : mpv.at(2)) {
      add += (ll)p.second * d.at(p.first).at(k);
    }
    // cerr << add << endl;
    ans = min(ans, add);
  }
  cout << ans << endl;
  return 0;
}
Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long color[3][31];
  long long D[30][30];

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 31; j++) {
      color[i][j] = 0;
    }
  }

  int N, C;
  cin >> N >> C;
  for (int i = 0; i < C; i++) {
    for (int j = 0; j < C; j++) {
      cin >> D[i + 1][j + 1];
    }
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int c;
      cin >> c;
      color[(i + j) % 3][c]++;
    }
  }
  long long ans = pow(10, 12);

  // 0がi,1がj,2がk
  for (int i = 1; i <= C; i++) {
    for (int j = 1; j <= C; j++) {
      if (j == i)
        continue;
      for (int k = 1; k <= C; k++) {
        if (k == i || k == j)
          continue;
        long long dif = 0;
        for (int l = 1; l <= C; l++) {
          dif += D[l][i] * color[0][l];
          dif += D[l][j] * color[1][l];
          dif += D[l][k] * color[2][l];
        }
        ans = min(ans, dif);
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long color[3][31];
  long long D[31][31];

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 31; j++) {
      color[i][j] = 0;
    }
  }

  int N, C;
  cin >> N >> C;
  for (int i = 0; i < C; i++) {
    for (int j = 0; j < C; j++) {
      cin >> D[i + 1][j + 1];
    }
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int c;
      cin >> c;
      color[(i + j) % 3][c]++;
    }
  }
  long long ans = pow(10, 12);

  // 0がi,1がj,2がk
  for (int i = 1; i <= C; i++) {
    for (int j = 1; j <= C; j++) {
      if (j == i)
        continue;
      for (int k = 1; k <= C; k++) {
        if (k == i || k == j)
          continue;
        long long dif = 0;
        for (int l = 1; l <= C; l++) {
          dif += D[l][i] * color[0][l];
          dif += D[l][j] * color[1][l];
          dif += D[l][k] * color[2][l];
        }
        ans = min(ans, dif);
      }
    }
  }

  cout << ans << endl;
}
Problem p03330 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() { cout << endl; }
template <typename First, typename... Rest>
void print(const First &first, const Rest &...rest) {
  cout << first << ' ';
  print(rest...);
}

template <typename T> void print(const vector<T> &v) {
  for (auto e : v)
    cout << e << ' ';
  cout << endl;
}

//------------------------- Libraries -------------------------//

//--------------------------- Solve ---------------------------//

void solve() {
  int N, C;
  cin >> N >> C;
  vector<vector<int>> D(C + 1, vector<int>(C + 1));
  for (int i = 1; i <= C; i++) {
    for (int j = 1; j <= C; j++) {
      cin >> D[i][j];
    }
  }

  vector<vector<int>> costs(3, vector<int>(C));
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      int c;
      cin >> c;
      for (int k = 1; k <= C; k++) {
        costs[(i + j) % 3][k] += D[c][k];
      }
    }
  }

  int ans = 1000000000;
  for (int i = 1; i <= C; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 1; k <= C; k++) {
        if (i == j || j == k || k == i)
          continue;
        int tmp = costs[0][i] + costs[1][j] + costs[2][k];
        ans = min(ans, tmp);
      }
    }
  }

  cout << ans << '\n';
}

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

  solve();

  return 0;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() { cout << endl; }
template <typename First, typename... Rest>
void print(const First &first, const Rest &...rest) {
  cout << first << ' ';
  print(rest...);
}

template <typename T> void print(const vector<T> &v) {
  for (auto e : v)
    cout << e << ' ';
  cout << endl;
}

//------------------------- Libraries -------------------------//

//--------------------------- Solve ---------------------------//

void solve() {
  int N, C;
  cin >> N >> C;
  vector<vector<int>> D(C + 1, vector<int>(C + 1));
  for (int i = 1; i <= C; i++) {
    for (int j = 1; j <= C; j++) {
      cin >> D[i][j];
    }
  }

  vector<vector<int>> costs(3, vector<int>(C + 1));
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      int c;
      cin >> c;
      for (int k = 1; k <= C; k++) {
        costs[(i + j) % 3][k] += D[c][k];
      }
    }
  }

  int ans = 1000000000;
  for (int i = 1; i <= C; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 1; k <= C; k++) {
        if (i == j || j == k || k == i)
          continue;
        int tmp = costs[0][i] + costs[1][j] + costs[2][k];
        ans = min(ans, tmp);
      }
    }
  }

  cout << ans << '\n';
}

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

  solve();

  return 0;
}
Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF 1 << 30
#define LINF (ll)1 << 62
#define MAX 510000
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

int main() {
  ll n, c;
  cin >> n >> c;
  vvl r(3, vl(c, 0));
  ll tab[n][n];
  rep(i, c) rep(j, c) cin >> tab[i][j];
  rep(i, n) {
    rep(j, n) {
      ll d;
      cin >> d;
      d--;
      r[(i + j + 2) % 3][d]++;
    }
  }
  ll ans = INF;
  rep(i, c) {
    rep(j, c) {
      rep(k, c) {
        if (i == j || j == k || k == i)
          continue;
        ll sum = 0;
        rep(l, c) {
          sum += r[0][l] * tab[l][i];
          sum += r[1][l] * tab[l][j];
          sum += r[2][l] * tab[l][k];
        }
        ans = min(ans, sum);
        // if(ans == sum) cout << i << j << k << endl;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF 1 << 30
#define LINF (ll)1 << 62
#define MAX 510000
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

int main() {
  ll n, c;
  cin >> n >> c;
  vvl r(3, vl(c, 0));
  ll tab[c][c];
  rep(i, c) rep(j, c) cin >> tab[i][j];
  rep(i, n) {
    rep(j, n) {
      ll d;
      cin >> d;
      d--;
      r[(i + j + 2) % 3][d]++;
    }
  }
  ll ans = INF;
  rep(i, c) {
    rep(j, c) {
      rep(k, c) {
        if (i == j || j == k || k == i)
          continue;
        ll sum = 0;
        rep(l, c) {
          sum += r[0][l] * tab[l][i];
          sum += r[1][l] * tab[l][j];
          sum += r[2][l] * tab[l][k];
        }
        ans = min(ans, sum);
        // if(ans == sum) cout << i << j << k << endl;
      }
    }
  }
  cout << ans << endl;
}
Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = 1 << 30;
const int mod = 1e9 + 7;
const int MOD = 998244353;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, c;
  cin >> n >> c;
  int a[c][c];
  int b[n][n];
  int ans = inf;
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < c; j++) {
      cin >> a[i][j];
    }
  }
  vector<vector<int>> t(c, vector<int>(3));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> b[i][j];
      b[i][j]--;
      t[(i + j) % 3][b[i][j]]++;
    }
  }
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k < c; k++) {
        if (i == j || j == k || k == i) {
          continue;
        }
        int res = 0;
        int p[3] = {i, j, k};
        for (int l = 0; l < 3; l++) {
          for (int m = 0; m < c; m++) {
            res += a[m][p[l]] * t[l][m];
          }
        }
        chmin(ans, res);
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = 1 << 30;
const int mod = 1e9 + 7;
const int MOD = 998244353;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, c;
  cin >> n >> c;
  int a[c][c];
  int b[n][n];
  int ans = inf;
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < c; j++) {
      cin >> a[i][j];
    }
  }
  vector<vector<int>> t(3, vector<int>(c));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> b[i][j];
      b[i][j]--;
      t[(i + j) % 3][b[i][j]]++;
    }
  }
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k < c; k++) {
        if (i == j || j == k || k == i) {
          continue;
        }
        int res = 0;
        int p[3] = {i, j, k};
        for (int l = 0; l < 3; l++) {
          for (int m = 0; m < c; m++) {
            res += a[m][p[l]] * t[l][m];
          }
        }
        chmin(ans, res);
      }
    }
  }
  cout << ans << endl;
}
Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <vector>

using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;

int main(void) {
  int n, c;

  while (scanf("%d%d", &n, &c) == 2) {
    vvi d(c);
    vvi a(n);
    vvll s(c);
    for (auto &md : d)
      md.resize(c);
    for (auto &ma : a)
      ma.resize(n);
    for (auto &ms : s)
      ms.resize(3);

    for (int i = 0; i < c; i++) {
      for (int j = 0; j < c; j++)
        scanf("%d", &d[i][j]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        scanf("%d", &a[i][j]);
        for (int k = 0; k < c; k++) {
          s[k][(i + j) % 3] += d[a[i][j] - 1][k];
        }
      }
    }

    if (0) {
      for (auto &ms : s) {
        printf("%d %d %d\n", ms[0], ms[1], ms[2]);
      }
    }

    ll ans = s[0][0] + s[1][1] + s[2][2];
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < c; j++) {
        if (i == j)
          continue;
        int k = 3 - (i + j);
        ll x = s[i][0] + s[j][1] + s[k][2];
        if (ans > x)
          ans = x;
      }
    }
    printf("%lld\n", ans);
  }

  return 0;
}


Fixed submission:
#include <cstdio>
#include <vector>

using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;

int main(void) {
  int n, c;

  while (scanf("%d%d", &n, &c) == 2) {
    vvi d(c);
    vvi a(n);
    vvll s(c);
    for (auto &md : d)
      md.resize(c);
    for (auto &ma : a)
      ma.resize(n);
    for (auto &ms : s)
      ms.resize(3);

    for (int i = 0; i < c; i++) {
      for (int j = 0; j < c; j++)
        scanf("%d", &d[i][j]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        scanf("%d", &a[i][j]);
        for (int k = 0; k < c; k++) {
          s[k][(i + j) % 3] += d[a[i][j] - 1][k];
        }
      }
    }

    if (0) {
      for (auto &ms : s) {
        printf("%d %d %d\n", ms[0], ms[1], ms[2]);
      }
    }

    ll ans = s[0][0] + s[1][1] + s[2][2];
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < c; j++) {
        if (i == j)
          continue;
        for (int k = 0; k < c; k++) {
          if (k == i || k == j)
            continue;
          ll x = s[i][0] + s[j][1] + s[k][2];
          if (ans > x)
            ans = x;
        }
      }
    }
    printf("%lld\n", ans);
  }

  return 0;
}
Problem p03330 (C++)

Buggy submission (status: Runtime Error):
/*
g++ -std=c++11 -o2 -Wall ABC099D.cpp -o ./out/ABC099D.out
./out/ABC099D.out
*/

#include <bits/stdc++.h>
using namespace std;

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define tr(c, i) for (iter(c) != (c).begin(); i != (c).end(); ++i)
#define pb push_back
#define mp make_pair

typedef long long ll;

int n, C;
int d[30][30];
int c[500][500];
long a[3][30];

int main() {
  freopen("./input/input.txt", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> C;
  for (int i = 0; i < C; i++)
    for (int j = 0; j < C; j++)
      cin >> d[i][j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> c[i][j];
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < n; k++)
        for (int l = 0; l < n; l++)
          if ((k + l) % 3 == i)
            a[i][j] += d[c[k][l] - 1][j];
    }
  }
  long ans = 1e15;
  for (int i = 0; i < C; i++)
    for (int j = 0; j < C; j++)
      for (int k = 0; k < C; k++) {
        if (i == j || i == k || j == k)
          continue;
        ans = min(ans, a[0][i] + a[1][j] + a[2][k]);
      }
  cout << ans << endl;
}


Fixed submission:
/*
g++ -std=c++11 -o2 -Wall ABC099D.cpp -o ./out/ABC099D.out
./out/ABC099D.out
*/

#include <bits/stdc++.h>
using namespace std;

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define tr(c, i) for (iter(c) != (c).begin(); i != (c).end(); ++i)
#define pb push_back
#define mp make_pair

typedef long long ll;

int n, C;
int d[30][30];
int c[500][500];
long a[3][30];

int main() {
  //    freopen("./input/input.txt", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> C;
  for (int i = 0; i < C; i++)
    for (int j = 0; j < C; j++)
      cin >> d[i][j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> c[i][j];
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < n; k++)
        for (int l = 0; l < n; l++)
          if ((k + l) % 3 == i)
            a[i][j] += d[c[k][l] - 1][j];
    }
  }
  long ans = 1e15;
  for (int i = 0; i < C; i++)
    for (int j = 0; j < C; j++)
      for (int k = 0; k < C; k++) {
        if (i == j || i == k || j == k)
          continue;
        ans = min(ans, a[0][i] + a[1][j] + a[2][k]);
      }
  cout << ans << endl;
}