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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef pair<int,int> P;
// typedef pair<ll, ll> P;
// typedef pair<int, pair<int, int>> P;
// typedef pair<ll, pair<int, int>> P;
typedef pair<ll, pair<ll, ll>> T;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll INFL = 1000000000000000010; // 10^18 = 2^60
int INF = 2147483600;          // 10^9
int INFM = 100000000;
ll MOD = 1000000007;
int cost[3][35];
int main() {
  int N, C;
  cin >> N >> C;
  int D[N][N];
  for (int i = 0; i < C; i++) {
    for (int j = 0; j < C; j++) {
      cin >> D[i][j];
    }
  }
  int c[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> c[i][j];
      c[i][j]--;
    }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < C; j++) {
      for (int h = 0; h < N; h++) {
        for (int w = 0; w < N; w++) {
          if ((h + w) % 3 != i)
            continue;
          cost[i][j] += D[c[h][w]][j];
        }
      }
    }
  }
  int ans = INF;
  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;
        ans = min(ans, cost[0][i] + cost[1][j] + cost[2][k]);
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef pair<int,int> P;
// typedef pair<ll, ll> P;
// typedef pair<int, pair<int, int>> P;
// typedef pair<ll, pair<int, int>> P;
typedef pair<ll, pair<ll, ll>> T;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll INFL = 1000000000000000010; // 10^18 = 2^60
int INF = 2147483600;          // 10^9
int INFM = 100000000;
ll MOD = 1000000007;
int cost[3][35];
int main() {
  int N, C;
  cin >> N >> C;
  int D[C][C];
  for (int i = 0; i < C; i++) {
    for (int j = 0; j < C; j++) {
      cin >> D[i][j];
    }
  }
  int c[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> c[i][j];
      c[i][j]--;
    }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < C; j++) {
      for (int h = 0; h < N; h++) {
        for (int w = 0; w < N; w++) {
          if ((h + w) % 3 != i)
            continue;
          cost[i][j] += D[c[h][w]][j];
        }
      }
    }
  }
  int ans = INF;
  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;
        ans = min(ans, cost[0][i] + cost[1][j] + cost[2][k]);
      }
    }
  }
  cout << ans << endl;
}
Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
using Pll = pair<ll, ll>;

int main() {
  ll N, C;
  cin >> N >> C;
  vector<vector<ll>> D(C, vector<ll>(C));
  vector<vector<ll>> c(N, vector<ll>(N));
  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];
    }
  }
  ll ans = 1LL << 60;
  vector<vector<ll>> num(3, vector<ll>(C));
  for (int i = 0; i < C; i++) {
    ll tmp = 0;
    for (int s = 0; s < N; s++) {
      for (int t = 0; t < N; t++) {
        if ((s + t) % 3 == 0)
          num[i][0] += D[c[s][t] - 1][i];
        else if ((s + t) % 3 == 1)
          num[i][1] += D[c[s][t] - 1][i];
        else if ((s + t) % 3 == 2)
          num[i][2] += D[c[s][t] - 1][i];
      }
    }
  }
  for (int i = 0; i < C; i++) {
    for (int j = 0; j < C; j++) {
      for (int l = 0; l < C; l++) {
        if (i == j || j == l || l == i)
          continue;
        ans = min(ans, num[i][0] + num[j][1] + num[l][2]);
        // cout << num[i][0]+num[j][1]+num[l][2] << endl;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
using Pll = pair<ll, ll>;

int main() {
  ll N, C;
  cin >> N >> C;
  vector<vector<ll>> D(C, vector<ll>(C));
  vector<vector<ll>> c(N, vector<ll>(N));
  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];
    }
  }
  ll ans = 1LL << 60;
  vector<vector<ll>> num(C, vector<ll>(3));
  for (int i = 0; i < C; i++) {
    ll tmp = 0;
    for (int s = 0; s < N; s++) {
      for (int t = 0; t < N; t++) {
        if ((s + t) % 3 == 0)
          num[i][0] += D[c[s][t] - 1][i];
        else if ((s + t) % 3 == 1)
          num[i][1] += D[c[s][t] - 1][i];
        else if ((s + t) % 3 == 2)
          num[i][2] += D[c[s][t] - 1][i];
      }
    }
  }
  for (int i = 0; i < C; i++) {
    for (int j = 0; j < C; j++) {
      for (int l = 0; l < C; l++) {
        if (i == j || j == l || l == i)
          continue;
        ans = min(ans, num[i][0] + num[j][1] + num[l][2]);
        // cout << num[i][0]+num[j][1]+num[l][2] << endl;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03330 (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++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()    // 昇順ソート
#define rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define FastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
typedef long long ll;
using P = pair<int, int>;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VP = vector<P>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int D[32][32];
int C[32][32];
int RC[3][32];

const ll INF = 1LL << 60;

ll f(int c, int r, int y) {
  ll res = 0;
  rep(x, c) { res += D[x][y] * RC[r][x]; }
  return res;
}

int main() {
  int n, c;
  cin >> n >> c;
  rep(i, c) rep(j, c) { cin >> D[i][j]; }
  rep(i, n) rep(j, n) {
    cin >> C[i][j];
    C[i][j]--;
  }

  rep(i, n) rep(j, n) {
    int r = (i + j + 2) % 3;
    int col = C[i][j];
    RC[r][col]++;
  }

  ll ans = INF;
  rep(y0, c) {
    rep(y1, c) {
      if (y0 == y1)
        continue;
      rep(y2, c) {
        if (y0 == y2 || y1 == y2)
          continue;
        ll sum = 0;
        sum += f(c, 0, y0);
        sum += f(c, 1, y1);
        sum += f(c, 2, y2);
        ans = min(ans, sum);
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()    // 昇順ソート
#define rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define FastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
typedef long long ll;
using P = pair<int, int>;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VP = vector<P>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int D[32][32];
int C[502][502];
int RC[3][32];

const ll INF = 1LL << 60;

ll f(int c, int r, int y) {
  ll res = 0;
  rep(x, c) { res += D[x][y] * RC[r][x]; }
  return res;
}

int main() {
  int n, c;
  cin >> n >> c;
  rep(i, c) rep(j, c) { cin >> D[i][j]; }
  rep(i, n) rep(j, n) {
    cin >> C[i][j];
    C[i][j]--;
  }

  rep(i, n) rep(j, n) {
    int r = (i + j + 2) % 3;
    int col = C[i][j];
    RC[r][col]++;
  }

  ll ans = INF;
  rep(y0, c) {
    rep(y1, c) {
      if (y0 == y1)
        continue;
      rep(y2, c) {
        if (y0 == y2 || y1 == y2)
          continue;
        ll sum = 0;
        sum += f(c, 0, y0);
        sum += f(c, 1, y1);
        sum += f(c, 2, y2);
        ans = min(ans, sum);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03330 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
using VI = vector<int>;
using VII = vector<VI>;

int main() {
  int N, C;
  cin >> N >> C;
  VII Dif(C, VI(C));
  VII Grid(N, VI(N));
  REP(i, C) REP(j, C) cin >> Dif.at(i).at(j);
  REP(i, N) REP(j, N) cin >> Grid.at(i).at(j);

  VII SumDiff(C, VI(3));
  VI sum(3);
  REP(c, C) {
    sum.at(0) = 0;
    sum.at(1) = 0;
    sum.at(2) = 0;
    REP(i, N) {
      REP(j, N) { sum.at((i + j) % 3) += Dif.at(Grid.at(i).at(j) - 1).at(c); }
    }
    SumDiff.at(c) = sum;
  }

  int min = SumDiff.at(0).at(0) + SumDiff.at(1).at(1) + SumDiff.at(2).at(2);
  REP(i, C) {
    REP(j, C) {
      if (j == i)
        continue;
      REP(k, C) {
        if (k == i || k == j)
          continue;
        int total =
            SumDiff.at(0).at(i) + SumDiff.at(1).at(j) + SumDiff.at(2).at(k);
        if (min > total)
          min = total;
      }
    }
  }
  cout << min << endl;
}

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
using VI = vector<int>;
using VII = vector<VI>;

int main() {
  int N, C;
  cin >> N >> C;
  VII Dif(C, VI(C));
  VII Grid(N, VI(N));
  REP(i, C) REP(j, C) cin >> Dif.at(i).at(j);
  REP(i, N) REP(j, N) cin >> Grid.at(i).at(j);

  VII SumDiff(C, VI(3));
  VI sum(3);
  REP(c, C) {
    sum.at(0) = 0;
    sum.at(1) = 0;
    sum.at(2) = 0;
    REP(i, N) {
      REP(j, N) { sum.at((i + j) % 3) += Dif.at(Grid.at(i).at(j) - 1).at(c); }
    }
    SumDiff.at(c) = sum;
  }

  int min = SumDiff.at(0).at(0) + SumDiff.at(1).at(1) + SumDiff.at(2).at(2);
  REP(i, C) {
    REP(j, C) {
      if (j == i)
        continue;
      REP(k, C) {
        if (k == i || k == j)
          continue;
        int total =
            SumDiff.at(i).at(0) + SumDiff.at(j).at(1) + SumDiff.at(k).at(2);
        if (min > total)
          min = total;
      }
    }
  }
  cout << min << endl;
}
Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
const int MOD = 1e9 + 7;

int main() {
  int n, c;
  cin >> n >> c;
  vector<vector<int>> t(3, vector<int>(c)), d(3, vector<int>(c));
  rep(i, c) { rep(j, c) cin >> d[i][j]; }
  int tin;
  rep(i, n) {
    rep(j, n) {
      cin >> tin;
      ++t[(i + j) % 3][tin - 1];
    }
  }
  int ans = 1000000000;
  rep(i, c) {
    rep(j, c) {
      rep(k, c) {
        if (i == j || j == k || k == i)
          continue;
        int tans = 0;
        rep(l, c) {
          tans += t[0][l] * (d[l][i]);
          tans += t[1][l] * (d[l][j]);
          tans += t[2][l] * (d[l][k]);
        }
        ans = min(ans, tans);
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
const int MOD = 1e9 + 7;

int main() {
  int n, c;
  cin >> n >> c;
  vector<vector<int>> d(c, vector<int>(c)), t(3, vector<int>(c));
  rep(i, c) { rep(j, c) cin >> d[i][j]; }
  int tin;
  rep(i, n) {
    rep(j, n) {
      cin >> tin;
      ++t[(i + j) % 3][tin - 1];
    }
  }
  int ans = 1000000000;
  rep(i, c) {
    rep(j, c) {
      rep(k, c) {
        if (i == j || j == k || k == i)
          continue;
        int tans = 0;
        rep(l, c) {
          tans += t[0][l] * (d[l][i]);
          tans += t[1][l] * (d[l][j]);
          tans += t[2][l] * (d[l][k]);
        }
        ans = min(ans, tans);
      }
    }
  }
  cout << ans << endl;
}
Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define range(a) (a).begin(), (a).end()
#define print(A, x, n)                                                         \
  rep(i, 0, n) { cout << (i ? " " : "") << A[i] x; }                           \
  cout << endl;
#define pprint(A, y, m, n)                                                     \
  rep(j, 0, m) { print(A[j], y, n); }
const long mod = 1e9 + 7;
const int size = 1e6;
const int inf = 1e9;
int main() {
  int N, C;
  cin >> N >> C;
  int D[N][N];
  rep(i, 0, C) rep(j, 0, C) { cin >> D[i][j]; }
  int c;
  int cnt[3][C];
  rep(i, 0, 3) rep(j, 0, C) cnt[i][j] = 0;
  rep(i, 0, N) rep(j, 0, N) {
    cin >> c;
    c--;
    cnt[(i + j) % 3][c]++;
  }
  int ans = inf;
  rep(i, 0, C) rep(j, 0, C) {
    if (j == i)
      continue;
    rep(k, 0, C) {
      if (k == i || k == j)
        continue;
      int sum = 0;
      rep(l, 0, C) {
        sum += cnt[0][l] * D[l][i];
        sum += cnt[1][l] * D[l][j];
        sum += cnt[2][l] * D[l][k];
      }
      ans = min(ans, sum);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define range(a) (a).begin(), (a).end()
#define print(A, x, n)                                                         \
  rep(i, 0, n) { cout << (i ? " " : "") << A[i] x; }                           \
  cout << endl;
#define pprint(A, y, m, n)                                                     \
  rep(j, 0, m) { print(A[j], y, n); }
const long mod = 1e9 + 7;
const int size = 1e6;
const int inf = 1e9;
int main() {
  int N, C;
  cin >> N >> C;
  int D[C][C];
  rep(i, 0, C) rep(j, 0, C) { cin >> D[i][j]; }
  int c;
  int cnt[3][C];
  rep(i, 0, 3) rep(j, 0, C) cnt[i][j] = 0;
  rep(i, 0, N) rep(j, 0, N) {
    cin >> c;
    c--;
    cnt[(i + j) % 3][c]++;
  }
  int ans = inf;
  rep(i, 0, C) rep(j, 0, C) {
    if (j == i)
      continue;
    rep(k, 0, C) {
      if (k == i || k == j)
        continue;
      int sum = 0;
      rep(l, 0, C) {
        sum += cnt[0][l] * D[l][i];
        sum += cnt[1][l] * D[l][j];
        sum += cnt[2][l] * D[l][k];
      }
      ans = min(ans, sum);
    }
  }
  cout << ans << endl;
}
Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
void makeCombiUtil(vector<vector<int>> &ans, vector<int> &tmp, int n, int left,
                   int k) {
  // Pushing this vector to a vector of vector
  if (k == 0) {
    ans.push_back(tmp);
    return;
  }

  // i iterates from left to n. First time
  // left will be 1
  for (int i = left; i <= n; ++i) {
    tmp.push_back(i);
    makeCombiUtil(ans, tmp, n, i + 1, k - 1);

    // Popping out last inserted element
    // from the vector
    tmp.pop_back();
  }
}

vector<vector<int>> makeCombi(int n, int k) {
  vector<vector<int>> ans;
  vector<int> tmp;
  makeCombiUtil(ans, tmp, n, 1, k);
  return ans;
}
int main() {
  int c, N, C, answer = INT_MAX;
  cin >> N >> C;
  int D[N + 1][N + 1];
  for (int i = 1; i <= C; i++) {
    for (int j = 1; j <= C; j++) {
      cin >> D[i][j];
    }
  }
  vector<vector<int>> d(3, vector<int>(C + 1));
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; d[(i + j) % 3][c]++, j++) {
      cin >> c;
    }
  }
  for (auto p : makeCombi(C, 3)) {
    do {
      int wrongness = 0;
      for (int c = 1; c <= C; c++) {
        wrongness += d[0][c] * D[c][p[0]];
        wrongness += d[1][c] * D[c][p[1]];
        wrongness += d[2][c] * D[c][p[2]];
      }
      answer = min(answer, wrongness);
    } while (next_permutation(p.begin(), p.end()));
  }
  cout << answer;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
void makeCombiUtil(vector<vector<int>> &ans, vector<int> &tmp, int n, int left,
                   int k) {
  // Pushing this vector to a vector of vector
  if (k == 0) {
    ans.push_back(tmp);
    return;
  }

  // i iterates from left to n. First time
  // left will be 1
  for (int i = left; i <= n; ++i) {
    tmp.push_back(i);
    makeCombiUtil(ans, tmp, n, i + 1, k - 1);

    // Popping out last inserted element
    // from the vector
    tmp.pop_back();
  }
}

vector<vector<int>> makeCombi(int n, int k) {
  vector<vector<int>> ans;
  vector<int> tmp;
  makeCombiUtil(ans, tmp, n, 1, k);
  return ans;
}
int main() {
  int c, N, C, answer = INT_MAX;
  cin >> N >> C;
  int D[C + 1][C + 1];
  for (int i = 1; i <= C; i++) {
    for (int j = 1; j <= C; j++) {
      cin >> D[i][j];
    }
  }
  vector<vector<int>> d(3, vector<int>(C + 1));
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; d[(i + j) % 3][c]++, j++) {
      cin >> c;
    }
  }
  for (auto p : makeCombi(C, 3)) {
    do {
      int wrongness = 0;
      for (int c = 1; c <= C; c++) {
        wrongness += d[0][c] * D[c][p[0]];
        wrongness += d[1][c] * D[c][p[1]];
        wrongness += d[2][c] * D[c][p[2]];
      }
      answer = min(answer, wrongness);
    } while (next_permutation(p.begin(), p.end()));
  }
  cout << answer;
}
Problem p03330 (Python)

Buggy submission (status: Runtime Error):
from itertools import permutations

n, c = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(c)]
cmat = [list(map(int, input().split())) for _ in range(n)]

dcnt = [[0] * c for _ in range(3)]
for i in range(n):
    for j in range(n):
        mod = (i + j) % 3
        dcnt[mod][c[i][j] - 1] += 1

ans = 10**9
for color in permutations(range(c), 3):
    tmp = 0
    for tgt, cnt in zip(color, dcnt):
        tmp += sum(d[i][tgt] * cnt[i] for i in range(c))
    ans = min(ans, tmp)
print(ans)


Fixed submission:
from itertools import permutations

n, c = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(c)]
cmat = [list(map(int, input().split())) for _ in range(n)]

dcnt = [[0] * c for _ in range(3)]
for i in range(n):
    for j in range(n):
        mod = (i + j) % 3
        dcnt[mod][cmat[i][j] - 1] += 1

ans = 10**9
for color in permutations(range(c), 3):
    tmp = 0
    for tgt, cnt in zip(color, dcnt):
        tmp += sum(d[i][tgt] * cnt[i] for i in range(c))
    ans = min(ans, tmp)
print(ans)
Problem p03330 (Python)

Buggy submission (status: Time Limit Exceeded):
from itertools import permutations


N, C = map(int, input().split())
D = [list(map(int, input().split())) for _ in range(C)]
c = [list(map(int, input().split())) for _ in range(N)]
cost = [[0] * 3 for _ in range(C)]

for i in range(N):
    for j in range(N):
        for k in range(C):
            cost[k][(i + j) % 3] += D[c[i][j] - 1][k]


ans = min(
    sum(cost[c][i] for i, c in enumerate(colors))
    for colors in permutations(range(C), 3)
)
print(ans)


Fixed submission:
from itertools import permutations


N, C = map(int, input().split())
D = [list(map(int, input().split())) for _ in range(C)]
c = [list(map(int, input().split())) for _ in range(N)]
cost = [[0] * 3 for _ in range(C)]

for i in range(N):
    for j in range(N):
        for k in range(C):
            cost[k][(i + j) % 3] += D[c[i][j] - 1][k]

ans = min(
    sum(cost[c][i] for i, c in enumerate(colors))
    for colors in permutations(range(C), 3)
)
print(ans)
Problem p03330 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
const int mod = 1000000007;
const int INF = 1001001001;

vector<vector<int>> d, c;
vector<vector<int>> conv;

int n, C;
ll calc(void) {
  ll res = INF;
  rep(i, C) rep(j, C) if (i != j) rep(k, C) if (i != k && j != k) {
    ll discomfort = 0;
    rep(l, C) discomfort += d[l][i] * conv[0][l];
    rep(l, C) discomfort += d[l][j] * conv[1][l];
    rep(l, C) discomfort += d[l][k] * conv[2][l];
    res = min(res, discomfort);
  }
  return res;
}

int main() {

  cin >> n >> C;
  d.resize(C, vector<int>(C));
  c.resize(n, vector<int>(C));
  conv.resize(3, vector<int>(C));
  rep(i, C) rep(j, C) {
    int a;
    cin >> a;
    d[i][j] = a;
  }
  rep(i, n) rep(j, n) {
    int a;
    cin >> a;
    --a;
    c[i][j] = a;
  }
  rep(i, n) {
    rep(j, n) {
      int remainder = (i + j + 2) % 3;
      conv[remainder][c[i][j]]++;
    }
  }
  ll ans = 0;
  ans += calc();
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
const int mod = 1000000007;
const int INF = 1001001001;

vector<vector<int>> d, c;
vector<vector<int>> conv;

int n, C;
ll calc(void) {
  ll res = INF;
  rep(i, C) rep(j, C) if (i != j) rep(k, C) if (i != k && j != k) {
    ll discomfort = 0;
    rep(l, C) discomfort += d[l][i] * conv[0][l];
    rep(l, C) discomfort += d[l][j] * conv[1][l];
    rep(l, C) discomfort += d[l][k] * conv[2][l];
    res = min(res, discomfort);
  }
  return res;
}

int main() {

  cin >> n >> C;
  d.resize(C, vector<int>(C));
  c.resize(n, vector<int>(n));
  conv.resize(3, vector<int>(C));
  rep(i, C) rep(j, C) {
    int a;
    cin >> a;
    d[i][j] = a;
  }
  rep(i, n) rep(j, n) {
    int a;
    cin >> a;
    --a;
    c[i][j] = a;
  }
  rep(i, n) {
    rep(j, n) {
      int remainder = (i + j + 2) % 3;
      conv[remainder][c[i][j]]++;
    }
  }
  ll ans = 0;
  ans += calc();
  cout << ans << endl;
}
Problem p03331 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define int long long
#define vec(a, n) vector<int>(a)((n))
#define Vec(a, n) vector<int>(a)((n))
using namespace std;
signed main() {
  int n, ans = 0;
  cin >> n;
  while (n != 0) {
    int a = n % 10;
    ans += a;
    n / 10;
  }
  if (ans == 1) {
    ans = 10;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define int long long
#define vec(a, n) vector<int>(a)((n))
#define Vec(a, n) vector<int>(a)((n))
using namespace std;
signed main() {
  int n, ans = 0;
  cin >> n;
  while (n != 0) {
    int a = n % 10;
    ans += a;
    n /= 10;
  }
  if (ans == 1) {
    ans = 10;
  }
  cout << ans << endl;

  return 0;
}
Problem p03331 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//  Digits_Sum
//
//  Created by Uttam Datta on 03/06/19.
//  Copyright © 2019 Uttam Datta. All rights reserved.
//

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int N;

int sum(pair<int, int> k) {
  int res = 0;
  while (k.first > 0) {
    res += k.first % 10;
    k.first /= 10;
  }
  while (k.second > 0) {
    res += k.second % 10;
    k.second /= 10;
  }
  return res;
}

int ans() {
  int i = 1, j = N - 1;
  vector<pair<int, int>> A;
  while (j - i != 1 && j != i) {
    A.push_back(make_pair(i, j));
    i++;
    j--;
  }
  vector<int> ans;
  for (int k = 0; k < A.size(); k++) {
    ans.push_back(sum(A[k]));
  }
  sort(ans.begin(), ans.end());
  return ans[0];
}

int main() {
  cin >> N;
  cout << ans() << endl;
}


Fixed submission:
//
//  main.cpp
//  Digits_Sum
//
//  Created by Uttam Datta on 03/06/19.
//  Copyright © 2019 Uttam Datta. All rights reserved.
//

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int N;

int sum(pair<int, int> k) {
  int res = 0;
  while (k.first > 0) {
    res += k.first % 10;
    k.first /= 10;
  }
  while (k.second > 0) {
    res += k.second % 10;
    k.second /= 10;
  }
  return res;
}

int ans() {
  int i = 1, j = N - 1;
  if (N / 10 == 0) {
    return N;
  }
  vector<pair<int, int>> A;
  while (j - i != 1 && j != i) {
    A.push_back(make_pair(i, j));
    i++;
    j--;
  }
  vector<int> ans;
  for (int k = 0; k < A.size(); k++) {
    ans.push_back(sum(A[k]));
  }
  sort(ans.begin(), ans.end());
  return ans[0];
}

int main() {
  cin >> N;
  cout << ans() << endl;
}
Problem p03331 (C++)

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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define fr(x, v) for (auto &x : v)
#define pr(a, b) cout << a << " " << b << endl
#define pr1(a, b, c) cout << a << " " << b << " " << c << endl

typedef long long int ll;
typedef vector<ll> vi;

struct input {
  ll a;
};

const ll M = 1e9 + 7;
const ll N = 1e5;
template <class T> T gcd(const T a, const T b) {
  return (b ? gcd<T>(b, a % b) : a);
}

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("in.txt", "r", stdin);
  // for writing output to output.txt
  freopen("out.txt", "w", stdout);
#endif
  fastio;

  ll n, m, i, j, k, temp, x, y, z, t, p;
  cin >> n;
  ll a, b;
  ll s1 = 0, s2 = 0, mn = 1e16;
  for (i = 1; i <= n - 1; i++) {
    a = i, b = (n - i);
    s1 = 0, s2 = 0;
    while (a > 0) {
      s1 += a % 10;
      a /= 10;
    }
    while (b > 0) {
      s1 += b % 10;
      b /= 10;
    }
    if (s1 + s2 < mn)
      mn = s1 + s2;
  }
  cout << mn << endl;
  return 0;
}

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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define fr(x, v) for (auto &x : v)
#define pr(a, b) cout << a << " " << b << endl
#define pr1(a, b, c) cout << a << " " << b << " " << c << endl

typedef long long int ll;
typedef vector<ll> vi;

struct input {
  ll a;
};

const ll M = 1e9 + 7;
const ll N = 1e5;
template <class T> T gcd(const T a, const T b) {
  return (b ? gcd<T>(b, a % b) : a);
}

int main() {

  fastio;

  ll n, m, i, j, k, temp, x, y, z, t, p;
  cin >> n;
  ll a, b;
  ll s1 = 0, s2 = 0, mn = 1e16;
  for (i = 1; i <= n - 1; i++) {
    a = i, b = (n - i);
    s1 = 0, s2 = 0;
    while (a > 0) {
      s1 += a % 10;
      a /= 10;
    }
    while (b > 0) {
      s1 += b % 10;
      b /= 10;
    }
    if (s1 + s2 < mn)
      mn = s1 + s2;
  }
  cout << mn << endl;
  return 0;
}
Problem p03331 (C++)

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

int sumOf(int n) {

  int sum = 0;
  while (n) {
    sum += n % 10;
    n = n / 10;
  }
  return sum;
}

int main() {

  int N;
  cin >> N;

  vector<int> num;

  for (int i = 1; i < N; i++) {
    for (int j = 1; j < N; j++) {
      if (i + j == N) {
        num.push_back(sumOf(i) + sumOf(j));
      }
    }
  }

  int min = num[0];
  for (unsigned int i = 0; i < num.size(); i++) {
    if (min > num[i]) {
      min = num[i];
    }
  }

  cout << min << endl;

  return 0;
}

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

int sumOf(int n) {

  int sum = 0;
  while (n) {
    sum += n % 10;
    n = n / 10;
  }
  return sum;
}

int main() {

  int N;
  cin >> N;

  vector<int> num;

  for (int i = 1; i < N; i++) {
    num.push_back(sumOf(i) + sumOf(N - i));
  }

  int min = num[0];
  for (unsigned int i = 0; i < num.size(); i++) {
    if (min > num[i]) {
      min = num[i];
    }
  }

  cout << min << endl;

  return 0;
}
Problem p03331 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
print(min([sum(map(int, str(i) + str(n - i))) for i in range(1, n // 2)]))


Fixed submission:
n = int(input())
print(min([sum(map(int, str(i) + str(n - i))) for i in range(1, n // 2 + 1)]))
Problem p03331 (Python)

Buggy submission (status: Runtime Error):
N = input()

ans = 0
for n in N:
    ans += n

print(ans)


Fixed submission:
N = input()

ans = 0
for n in N:
    ans += int(n)

if ans == 1:
    ans = 10

print(ans)
Problem p03331 (C++)

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

int f(int x) {
  int res = 0;
  while (x > 0) {
    res = res + x % 10;
    x /= 10;
  }
  return res;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;

  int ans = INT_MAX;
  for (int i = 1; i < n; i++) {
    ans = min(ans, f(i) + f(n - i));
  }
  cout << ans << endl;
}

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

int f(int x) {
  int res = 0;
  while (x > 0) {
    res = res + x % 10;
    x /= 10;
  }
  return res;
}

int main() {
  int n;
  cin >> n;

  int ans = INT_MAX;
  for (int i = 1; i < n; i++) {
    ans = min(ans, f(i) + f(n - i));
  }
  cout << ans << endl;
}
Problem p03331 (C++)

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

int main() {
  int n;
  cin >> n;
  int mn = 10000;
  for (int i = 1; i <= n / 2; i++) {
    int a = i, b = n - i;
    int c = 0, d = 0;
    while (a > 0) {
      c += a % 10;
      a /= 10;
    }
    while (b > 0) {
      d += b % 10;
      d /= 10;
    }
    mn = min(mn, c + d);
  }
  cout << mn << endl;
  return 0;
}


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

int main() {
  int n;
  cin >> n;
  int mn = 10000;
  for (int i = 1; i <= n / 2; i++) {
    int a = i, b = n - i;
    int c = 0, d = 0;
    while (a > 0) {
      c += a % 10;
      a /= 10;
    }
    while (b > 0) {
      d += b % 10;
      b /= 10;
    }
    mn = min(mn, c + d);
  }
  cout << mn << endl;
  return 0;
}
Problem p03331 (C++)

Buggy submission (status: Time Limit Exceeded):
#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 sum(int a) {
  int ret = 0;
  while (a > 0) {
    ret += a % 10;
    ret /= 10;
  }
  return ret;
}

int main() {
  int n = 0;
  int mini = 1000000;
  cin >> n;
  int a = 0, b = 0;
  for (int i = 1; i < n; i++) {
    a = i;
    b = n - i;
    int buf = sum(a) + sum(b);
    if (buf < mini)
      mini = buf;
  }
  cout << mini << 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 sum(int a) {
  int ret = 0;
  while (a > 0) {
    ret += a % 10;
    a /= 10;
  }
  return ret;
}

int main() {
  int n = 0;
  int mini = 1000000;
  cin >> n;
  int a = 0, b = 0;
  for (int i = 1; i < n; i++) {
    a = i;
    b = n - i;
    int buf = sum(a) + sum(b);
    if (buf < mini)
      mini = buf;
  }
  cout << mini << endl;
  return 0;
}
Problem p03331 (C++)

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

#define ld long double
#define int long long
#define pii pair<int, int>
#define X first
#define Y second
#define mp make_pair
#define MOD 1000000007ll
#define inf 1000000000000000ll

using namespace std;

inline int binexp(int a, int b, int m) {
  int res = 1;
  a %= m;
  while (b) {
    if (b & 1) {
      res = (res * 1ll * a) % m;
    }
    a = (a * 1ll * a) % m;
    b >>= 1;
  }
  return res;
}

inline int digsum(int n) {
  int ans = 0;
  while (n) {
    ans += n % 10;
    n /= 10;
  }
  return ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt.txt", "r", stdin);
  freopen("output.txt.txt", "w", stdout);
#endif
  int n, mn = inf;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a = i, b = n - i;
    mn = min(mn, digsum(a) + digsum(b));
  }
  cout << mn;
  return 0;
}


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

#define ld long double
#define int long long
#define pii pair<int, int>
#define X first
#define Y second
#define mp make_pair
#define MOD 1000000007ll
#define inf 1000000000000000ll

using namespace std;

inline int binexp(int a, int b, int m) {
  int res = 1;
  a %= m;
  while (b) {
    if (b & 1) {
      res = (res * 1ll * a) % m;
    }
    a = (a * 1ll * a) % m;
    b >>= 1;
  }
  return res;
}

inline int digsum(int n) {
  int ans = 0;
  while (n) {
    ans += n % 10;
    n /= 10;
  }
  return ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, mn = inf;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a = i, b = n - i;
    mn = min(mn, digsum(a) + digsum(b));
  }
  cout << mn;
  return 0;
}
Problem p03331 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1000000010
#define EPS 1e-9
#define fst first
#define scd second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\n"

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int ans = 0;
  while (n % 10 == 0)
    n = 10;
  if (n == 1)
    cout << 10 << endl;
  else {
    while (n) {
      ans += n % 10;
      n /= 10;
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1000000010
#define EPS 1e-9
#define fst first
#define scd second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\n"

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int ans = 0;
  while (n % 10 == 0)
    n /= 10;
  if (n == 1)
    cout << 10 << endl;
  else {
    while (n) {
      ans += n % 10;
      n /= 10;
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p03331 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int f(int a, int b) {
  int i = 0, j = 0;
  while (a) {
    i += a % 10;
    a /= 10;
  }
  while (b) {
    j += b % 10;
    b /= 10;
  }
  return i + j;
}

int main() {
  int n;
  cin >> n;
  int ans = n;
  rep(b, n) rep(a, b) {
    if (a + b == n)
      ans = min(ans, f(a, b));
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int f(int a, int b) {
  int i = 0, j = 0;
  while (a) {
    i += a % 10;
    a /= 10;
  }
  while (b) {
    j += b % 10;
    b /= 10;
  }
  return i + j;
}

int main() {
  int n;
  cin >> n;
  int ans = n;
  rep(a, n) {
    int b = n - a;
    if (a >= b)
      ans = min(ans, f(a, b));
  }
  cout << ans;
  return 0;
}
Problem p03331 (C++)

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

int main() {
  int N;
  cin >> N;

  int sum_min = 100;
  int sum_A = 0;
  int sum_B = 0;
  int A = 0, B = 0;
  for (int x = 1; x < N; x++) {
    for (int y = 1; y < N; y++) {
      if (x + y == N) {
        A = x;
        B = y;
        sum_A = 0;
        sum_B = 0;
        // cout<<A<<" "<<B<<endl;
        for (int i = 1; A + B > 0; i++) {
          sum_A += A % 10;
          A /= 10;
          sum_B += B % 10;
          B /= 10;
          // cout<<A<<" "<<B<<endl;
          // cout<<sum_A<<" "<<sum_B<<endl;
        }
        if (sum_A + sum_B < sum_min) {
          sum_min = sum_A + sum_B;
          // cout<<sum_min<<endl;
        }
      }
    }
  }

  cout << sum_min << endl;
  return 0;
}

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

int main() {
  int N;
  cin >> N;

  int sum_min = 100;
  int sum_A = 0;
  int sum_B = 0;
  int A = 0, B = 0;
  for (int x = 1; x < N; x++) {
    A = x;
    B = N - x;
    sum_A = 0;
    sum_B = 0;
    // cout<<A<<" "<<B<<endl;
    for (int i = 1; A + B > 0; i++) {
      sum_A += A % 10;
      A /= 10;
      sum_B += B % 10;
      B /= 10;
      // cout<<A<<" "<<B<<endl;
      // cout<<sum_A<<" "<<sum_B<<endl;
    }
    if (sum_A + sum_B < sum_min) {
      sum_min = sum_A + sum_B;
      // cout<<sum_min<<endl;
    }
  }

  cout << sum_min << endl;
  return 0;
}
Problem p03331 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int sum = 0;
  while (N > 0) {
    sum += N % 10;
    sum /= 10;
  }
  cout << sum;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int sum = 0;
  if (N == 10 || N == 100 || N == 1000 || N == 10000 || N == 100000)
    sum = 10;
  else {
    while (N > 0) {
      sum += N % 10;
      N /= 10;
    }
  }
  cout << sum;
}
Problem p03331 (Python)

Buggy submission (status: Runtime Error):
n = input().split()
if n[0] == 1 and sum(n):
    print(10)
else:
    print(sum(n))


Fixed submission:
n = list(map(int, list(input())))

if n[0] == 1 and sum(n) == 1:
    print(10)
else:
    print(sum(n))
Problem p03331 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main() {
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  int ans = 100000;
  for (int i = 0; i <= N; i++) {
    int j = N - i;
    int sum = 0;
    while (i > 0) {
      sum += i % 10;
      i /= 10;
    }
    while (j > 0) {
      sum += j % 10;
      j /= 10;
    }
    ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main() {
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  if (N == 10 || N == 100 || N == 1000 || N == 10000 || N == 100000) {
    cout << 10 << endl;
    return 0;
  }
  int ans = 0;
  while (N > 0) {
    ans += N % 10;
    N /= 10;
  }
  cout << ans << endl;
  return 0;
}
Problem p03331 (C++)

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

int wa(int x) {
  int wa_x = 0;
  while (x % 10 >= 0) {
    wa_x = wa_x + x % 10;
    x = x / 10;
  }
  wa_x = wa_x + x;
  return wa_x;
}

int main() {
  int N;
  cin >> N;
  int min = 100;
  for (int i = 1; i < N; i = i + 1) {
    int A = i;
    int a = A;
    int wa_A = wa(a);

    int B = N - i;
    int b = B;
    int wa_B = wa(b);

    if (min > wa_A + wa_B)
      min = wa_A + wa_B;
  }
  cout << min << endl;
}


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

int wa(int x) {
  int wa_x = 0;
  while (x % 10 == 0) {
    x = x / 10;
  }
  while (x % 10 > 0) {
    wa_x = wa_x + x % 10;
    x = x / 10;
  }
  wa_x = wa_x + x;
  return wa_x;
}

int main() {
  int N;
  cin >> N;
  int min = 100;
  for (int i = 1; i < N; i = i + 1) {
    int A = i;
    int a = A;
    int wa_A = wa(a);

    int B = N - i;
    int b = B;
    int wa_B = wa(b);

    if (min > wa_A + wa_B)
      min = wa_A + wa_B;
  }
  cout << min << endl;
}
Problem p03331 (C++)

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

int getSum(int n) {
  int sum = 0;
  while (n > 0) {
    sum += n % 10;
    n /= 10;
  }
  return sum;
}

int main() {
  int N;
  cin >> N;

  int min = getSum(1) + getSum(N - 1);

  int sum;
  for (int i = 1; i < N; i++) {
    for (int j = 1; j < N; j++) {
      if (N == i + j) {
        sum = getSum(i) + getSum(j);
        if (min > sum)
          min = sum;
      }
    }
  }
  cout << min << endl;
  return 0;
}


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

int getSum(int n) {
  int sum = 0;
  while (n > 0) {
    sum += n % 10;
    n /= 10;
  }
  return sum;
}

int main() {
  int N;
  cin >> N;

  int min = getSum(1) + getSum(N - 1);

  int sum;
  for (int i = 1; i < N; i++) {
    int j = N - i;
    sum = getSum(i) + getSum(j);
    if (min > sum)
      min = sum;
  }
  cout << min << endl;
  return 0;
}
Problem p03331 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const long long mod = pow(10, 9) + 7;
const long long mod2 = 998244353;
const long long INF = 1LL << 60;
const int inf = pow(10, 9) + 7;
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int divCeil(int A, int B) { return (A + (B - 1)) / B; }
long long divCeil(long long A, int B) { return (A + (B - 1)) / B; }
long long divCeil(int A, long long B) { return (A + (B - 1)) / B; }
long long divCeil(long long A, long long B) { return (A + (B - 1)) / B; }

int digitSum(int N) {
  int ret = 0;
  while (N > 0) {
    ret += N % 10;
    ret /= 10;
  }
  return ret;
}

int main() {
  int N;
  cin >> N;

  int ans = inf;
  for (int i = 1; i < N; i++) {
    int A = i;
    int B = N - A;
    chmin(ans, digitSum(A) + digitSum(B));
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long mod = pow(10, 9) + 7;
const long long mod2 = 998244353;
const long long INF = 1LL << 60;
const int inf = pow(10, 9) + 7;
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int divCeil(int A, int B) { return (A + (B - 1)) / B; }
long long divCeil(long long A, int B) { return (A + (B - 1)) / B; }
long long divCeil(int A, long long B) { return (A + (B - 1)) / B; }
long long divCeil(long long A, long long B) { return (A + (B - 1)) / B; }

int digitSum(int N) {
  int ret = 0;
  while (N > 0) {
    ret += N % 10;
    N /= 10;
  }
  return ret;
}

int main() {
  int N;
  cin >> N;

  int ans = inf;
  for (int i = 1; i < N; i++) {
    int A = i;
    int B = N - A;
    chmin(ans, digitSum(A) + digitSum(B));
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int sum, n, minn = 5000, a, b;
int main() {
  cin >> n;
  for (int i = 1; i < n; i++)
    for (int j = 1; j < n; j++) {
      if (i + j == n) {
        a = i / 100000 + i % 100000 / 10000 + i % 10000 / 1000 +
            i % 1000 / 100 + i % 100 / 10 + i % 10;
        b = j / 100000 + j % 100000 / 10000 + j % 10000 / 1000 +
            j % 1000 / 100 + j % 100 / 10 + j % 10;
        sum = a + b;
        if (minn >= sum)
          minn = sum;
      }
    }
  cout << minn;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int sum, n, minn = 5000, a, b;
int main() {
  cin >> n;
  for (int i = 1; i < n; i++)

  {
    int j = n - i;

    a = i / 100000 + i % 100000 / 10000 + i % 10000 / 1000 + i % 1000 / 100 +
        i % 100 / 10 + i % 10;
    b = j / 100000 + j % 100000 / 10000 + j % 10000 / 1000 + j % 1000 / 100 +
        j % 100 / 10 + j % 10;
    sum = a + b;
    if (minn >= sum)
      minn = sum;
  }
  cout << minn;
  return 0;
}
Problem p03331 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a;
  cin >> n;
  while (n) {
    a += n % 10;
    n / 10;
  }
  if (a == 1)
    cout << 10 << endl;
  else
    cout << a << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a;
  cin >> n;
  a = n % 10 + (n / 10) % 10 + (n / 100) % 10 + (n / 1000) % 10 + n / 10000;
  if (a == 1)
    cout << 10 << endl;
  else
    cout << a << endl;
}
Problem p03331 (C++)

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

#define INF INT_MAX / 2
#define MOD 1000000007

using namespace std;

using ll = long long;
using ull = unsigned long long;

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

  int N;
  cin >> N;
  int Min = INF;
  vector<int> sum(N, 0);
  for (int i = 0; i <= 100000; i++) {
    int n = i;
    for (int j = 0; j < 6; j++) {
      sum[i] += n % 10;
      n /= 10;
    }
  }
  for (int i = 1; i < N; i++) {
    int a = i;
    int b = N - i;
    Min = min(sum[a] + sum[b], Min);
  }
  cout << Min << endl;
  return 0;
}


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

#define INF INT_MAX / 2
#define MOD 1000000007

using namespace std;

using ll = long long;
using ull = unsigned long long;

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

  int N;
  cin >> N;
  int Min = INF;
  vector<int> sum(100001, 0);
  for (int i = 0; i <= 100000; i++) {
    int n = i;
    for (int j = 0; j < 6; j++) {
      sum[i] += n % 10;
      n /= 10;
    }
  }
  for (int i = 1; i < N; i++) {
    int a = i;
    int b = N - i;
    Min = min(sum[a] + sum[b], Min);
  }
  cout << Min << endl;
  return 0;
}
Problem p03331 (C++)

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

using namespace std;

int main() {
  int n, ans;
  scanf("%d", &n);
  if (n % 10 == 0)
    return puts("10");
  for (ans = 0; n; n /= 10)
    ans += n % 10;
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int main() {
  int n, ans;
  scanf("%d", &n);
  if (n % 10 == 0)
    return puts("10"), 0;
  for (ans = 0; n; n /= 10)
    ans += n % 10;
  printf("%d\n", ans);
  return 0;
}
Problem p03331 (C++)

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

int Digits_sum(int a, int b) {
  int sum = 0;
  while (a > 0) {
    sum += a % 10;
    a /= 10;
  }
  while (b > 0) {
    sum += b % 10;
    b /= 10;
  }
  return sum;
}

int main() {
  int i, j;
  int min = 100001;
  int digits_sum;
  int n;
  cin >> n;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (i + j == n) {
        digits_sum = Digits_sum(i, j);
        if (digits_sum < min)
          min = digits_sum;
      }
    }
  }
  cout << min << endl;
  return 0;
}

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

int Digits_sum(int a, int b) {
  int sum = 0;
  while (a > 0) {
    sum += a % 10;
    a /= 10;
  }
  while (b > 0) {
    sum += b % 10;
    b /= 10;
  }
  return sum;
}

int main() {
  int i, j;
  int min = 100001;
  int digits_sum;
  int n;
  cin >> n;
  for (i = 1; i <= n; i++) {
    j = n - i;
    if (j > 0) {
      digits_sum = Digits_sum(i, j);
      if (digits_sum < min)
        min = digits_sum;
    }
  }
  cout << min << endl;
  return 0;
}