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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

int main() {
  int N, M;
  cin >> N >> M;
  ll X;
  cin >> X;
  vector<vector<ll>> a(N, vector<ll>());
  vector<ll> c;
  rep(i, N) {
    ll C;
    cin >> C;
    c.push_back(C);
    rep(j, M) {
      ll A;
      cin >> A;
      a[i].push_back(A);
    }
  }

  ll ans = INF;
  rep(i, pow(2, N)) {
    ll total = 0;
    vector<ll> tmp(N, 0);
    rep(j, N) {
      int bit = (i >> j);
      if ((bit & 1) == 1) {
        total += c[j];
        rep(k, M) { tmp[k] += a[j][k]; }
      }
    }
    bool isOK = true;
    rep(j, M) {
      if (tmp[j] < X)
        isOK = false;
    }
    if (isOK) {
      ans = min(ans, total);
    }
  }

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

int main() {
  int N, M;
  cin >> N >> M;
  ll X;
  cin >> X;
  vector<vector<ll>> a(N, vector<ll>());
  vector<ll> c;
  rep(i, N) {
    ll C;
    cin >> C;
    c.push_back(C);
    rep(j, M) {
      ll A;
      cin >> A;
      a[i].push_back(A);
    }
  }

  ll ans = INF;
  rep(i, pow(2, N)) {
    ll total = 0;
    vector<ll> tmp(M, 0);
    rep(j, N) {
      int bit = (i >> j);
      if ((bit & 1) == 1) {
        total += c[j];
        rep(k, M) { tmp[k] += a[j][k]; }
      }
    }
    bool isOK = true;
    rep(j, M) {
      if (tmp[j] < X)
        isOK = false;
    }
    if (isOK) {
      ans = min(ans, total);
    }
  }

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
Problem p02683 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define Sort(a) sort(a.begin(), a.end())
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 1 << 30;

int n, m, x;
int min_ = INF;
int c[15];
int a[15][15];

void dfs(vector<int> &A) {
  int len = A.size();
  if (len == n) {
    ll sum = 0;
    vi f(n, 0);
    bool flag = true;
    rep(i, len) {
      if (A.at(i) == 1) {
        sum += c[i];
        rep(j, m) f[j] += a[i][j];
      }
    }
    rep(j, m) {
      if (f[j] < x)
        flag = false;
    }
    if (sum <= min_ && flag)
      min_ = sum;
    return;
  }
  for (int v = 0; v < 2; ++v) {
    A.push_back(v);
    dfs(A);
    A.pop_back();
  }
}

int main() {
  cin >> n >> m >> x;
  vi f(m, 0);
  rep(i, n) {
    cin >> c[i];
    rep(j, m) {
      cin >> a[i][j];
      f[j] += a[i][j];
    }
  }
  rep(i, m) {
    if (f[i] < x) {
      cout << -1 << endl;
      return 0;
    }
  }
  vector<int> A;
  dfs(A);
  cout << min_ << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define Sort(a) sort(a.begin(), a.end())
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 1 << 30;

int n, m, x;
int min_ = INF;
int c[15];
int a[15][15];

void dfs(vector<int> &A) {
  int len = A.size();
  if (len == n) {
    ll sum = 0;
    vi f(m, 0);
    bool flag = true;
    rep(i, len) {
      if (A.at(i) == 1) {
        sum += c[i];
        rep(j, m) f[j] += a[i][j];
      }
    }
    rep(j, m) {
      if (f[j] < x)
        flag = false;
    }
    if (sum <= min_ && flag)
      min_ = sum;
    return;
  }
  for (int v = 0; v < 2; ++v) {
    A.push_back(v);
    dfs(A);
    A.pop_back();
  }
}

int main() {
  cin >> n >> m >> x;
  vi f(m, 0);
  rep(i, n) {
    cin >> c[i];
    rep(j, m) {
      cin >> a[i][j];
      f[j] += a[i][j];
    }
  }
  rep(i, m) {
    if (f[i] < x) {
      cout << -1 << endl;
      return 0;
    }
  }
  vector<int> A;
  dfs(A);
  cout << min_ << endl;
  return 0;
}
Problem p02683 (C++)

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

bool provera(vector<long long> &niz, int &m, long long &x) {
  for (int i = 0; i < m; i++)
    if (niz[i] < x)
      return false;
  return true;
}

long long f(vector<vector<long long>> &v, vector<long long> &cene, int &n,
            int &m, long long &x, vector<long long> niz, int index,
            long long cena) {
  if (index == n) {
    if (provera(niz, m, x))
      return cena;
    else
      return LLONG_MAX;
  }
  long long m1, m2;
  m1 = f(v, cene, n, m, x, niz, index + 1, cena);
  for (int i = 0; i < m; i++)
    niz[i] += v[index][i];
  cena += cene[index];
  m2 = f(v, cene, n, m, x, niz, index + 1, cena);
  return min(m1, m2);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  long long x, resenje;
  cin >> n >> m >> x;
  vector<long long> cena(n);
  vector<vector<long long>> v(n, vector<long long>(m));
  for (int i = 0; i < n; i++) {
    cin >> cena[i];
    for (int j = 0; j < m; j++)
      cin >> v[i][j];
  }
  vector<long long> niz(n, 0);
  resenje = f(v, cena, n, m, x, niz, 0, 0);
  if (resenje == LLONG_MAX)
    cout << -1;
  else
    cout << resenje;
}


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

bool provera(vector<long long> &niz, int &m, long long &x) {
  for (int i = 0; i < m; i++)
    if (niz[i] < x)
      return false;
  return true;
}

long long f(vector<vector<long long>> &v, vector<long long> &cene, int &n,
            int &m, long long &x, vector<long long> niz, int index,
            long long cena) {
  if (index == n) {
    if (provera(niz, m, x))
      return cena;
    else
      return LLONG_MAX;
  }
  long long m1, m2;
  m1 = f(v, cene, n, m, x, niz, index + 1, cena);
  for (int i = 0; i < m; i++)
    niz[i] += v[index][i];
  cena += cene[index];
  m2 = f(v, cene, n, m, x, niz, index + 1, cena);
  return min(m1, m2);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  long long x, resenje;
  cin >> n >> m >> x;
  vector<long long> cena(n);
  vector<vector<long long>> v(n, vector<long long>(m));
  for (int i = 0; i < n; i++) {
    cin >> cena[i];
    for (int j = 0; j < m; j++)
      cin >> v[i][j];
  }
  vector<long long> niz(m, 0);
  resenje = f(v, cena, n, m, x, niz, 0, 0);
  if (resenje == LLONG_MAX)
    cout << -1;
  else
    cout << resenje;
}
Problem p02683 (C++)

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

int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  int r = x % y;
  if (r == 0) {
    return y;
  }
  return gcd(y, r);
}

int main() {
  int N, M, X;
  cin >> N >> M >> X;
  vector<int> C(N);
  vector<vector<int>> A(N, vector<int>(M));
  for (int i = 0; i < N; i++) {
    cin >> C[i];
    for (int j = 0; j < M; j++) {
      cin >> A[i][j];
    }
  }
  ll minSumC = 1e9;
  bool bigFlag = false;
  for (int tmp = 0; tmp < (1 << N); tmp++) {
    bitset<12> book(tmp);
    vector<ll> know(N);
    ll sumC = 0;
    bool flag = true;
    for (int i = 0; i < N; i++) {
      if (book.test(i)) {
        sumC += C[i];
        for (int j = 0; j < M; j++) {
          know[j] += A[i][j];
        }
      }
    }
    for (int j = 0; j < M; j++) {
      if (know[j] < X) {
        flag = false;
        break;
      }
    }
    if (flag) {
      minSumC = min(minSumC, sumC);
      bigFlag = true;
    }
  }
  if (bigFlag) {
    cout << minSumC << endl;
  } else {
    cout << -1 << endl;
  }
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  int r = x % y;
  if (r == 0) {
    return y;
  }
  return gcd(y, r);
}

int main() {
  int N, M, X;
  cin >> N >> M >> X;
  vector<int> C(N);
  vector<vector<int>> A(N, vector<int>(M));
  for (int i = 0; i < N; i++) {
    cin >> C[i];
    for (int j = 0; j < M; j++) {
      cin >> A[i][j];
    }
  }
  ll minSumC = 1e9;
  bool bigFlag = false;
  for (int tmp = 0; tmp < (1 << N); tmp++) {
    bitset<12> book(tmp);
    vector<ll> know(M);
    ll sumC = 0;
    bool flag = true;
    for (int i = 0; i < N; i++) {
      if (book.test(i)) {
        sumC += C[i];
        for (int j = 0; j < M; j++) {
          know[j] += A[i][j];
        }
      }
    }
    for (int j = 0; j < M; j++) {
      if (know[j] < X) {
        flag = false;
        break;
      }
    }
    if (flag) {
      minSumC = min(minSumC, sumC);
      bigFlag = true;
    }
  }
  if (bigFlag) {
    cout << minSumC << endl;
  } else {
    cout << -1 << endl;
  }
}
Problem p02683 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}

template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int main() {
  int n, m, x;
  cin >> n >> m >> x;

  vector<int> c(n);
  vector<vector<int>> a(n, vector<int>(m));
  rep(i, n) {
    cin >> c[i];
    rep(j, m) cin >> a[i][j];
  }

  int ans = numeric_limits<int>::max();
  rep(b, (1 << n)) {
    int t = 0;
    vector<int> now(m, 0);
    rep(i, n) {
      if (b & (1 << i)) {
        t += c[i];
        rep(j, n) { now[j] += a[i][j]; }
      }
    }
    bool f = all_of(now.begin(), now.end(), [&](int v) { return v >= x; });

    if (f)
      ans = min(ans, t);
  }

  if (ans == numeric_limits<int>::max())
    ans = -1;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}

template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int main() {
  int n, m, x;
  cin >> n >> m >> x;

  vector<int> c(n);
  vector<vector<int>> a(n, vector<int>(m));
  rep(i, n) {
    cin >> c[i];
    rep(j, m) cin >> a[i][j];
  }

  int ans = numeric_limits<int>::max();
  rep(b, (1 << n)) {
    int t = 0;
    vector<int> now(m, 0);
    rep(i, n) {
      if (b & (1 << i)) {
        t += c[i];
        rep(j, m) { now[j] += a[i][j]; }
      }
    }
    bool f = all_of(now.begin(), now.end(), [&](int v) { return v >= x; });

    if (f)
      ans = min(ans, t);
  }

  if (ans == numeric_limits<int>::max())
    ans = -1;
  cout << ans << endl;

  return 0;
}
Problem p02683 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define two(a) (1 << (a))
#define LINF (1ll << 61)
#define EPS (1e-14)
#define Lshift(a, b) ((a) << (b))
#define Rshift(a, b) ((a) >> (b))
#define rep(a, b) for (a = 0; a < b; a++)
#define xrep(a, b, c) for (a = b; a < c; a++)
#define INF (1 << 29)
#define sz(a) int(a.size())
#define mset(x) memset(x, 0, sizeof(x))
#define swap(a, b) ((a ^= b), (b ^= a), (a ^= b))
#define GET(x) (mark[x >> 5] >> (x & 31) & 1)
#define SET(x) (mark[x >> 5] |= 1 << (x & 31))
#define maxL (10000000 >> 5) + 1
#define mod 1000000007
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define si set<int>
#define pb push_back
typedef long long ll;
using namespace std;

int c[13];
int in[13][13];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m, x;
  cin >> n >> m >> x;
  int i, j;
  rep(i, n) {
    cin >> c[i];
    rep(j, m) { cin >> in[i][j]; }
  }
  ll s[n], k;
  ll ans(LINF);
  rep(i, two(n)) {
    ll t(0);
    memset(s, 0, sizeof(s));
    rep(j, n) {
      int tj = two(j);
      if (tj & i) {
        t += c[j];
        rep(k, m) s[k] += in[j][k];
      }
    }
    rep(j, m) {
      if (s[j] < x)
        break;
    }
    if (j == m) {
      ans = min(ans, t);
    }
  }
  if (ans == LINF)
    ans = -1;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define two(a) (1 << (a))
#define LINF (1ll << 61)
#define EPS (1e-14)
#define Lshift(a, b) ((a) << (b))
#define Rshift(a, b) ((a) >> (b))
#define rep(a, b) for (a = 0; a < b; a++)
#define xrep(a, b, c) for (a = b; a < c; a++)
#define INF (1 << 29)
#define sz(a) int(a.size())
#define mset(x) memset(x, 0, sizeof(x))
#define swap(a, b) ((a ^= b), (b ^= a), (a ^= b))
#define GET(x) (mark[x >> 5] >> (x & 31) & 1)
#define SET(x) (mark[x >> 5] |= 1 << (x & 31))
#define maxL (10000000 >> 5) + 1
#define mod 1000000007
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define si set<int>
#define pb push_back
typedef long long ll;
using namespace std;

int c[13];
int in[13][13];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m, x;
  cin >> n >> m >> x;
  int i, j;
  rep(i, n) {
    cin >> c[i];
    rep(j, m) { cin >> in[i][j]; }
  }
  ll s[m], k;
  ll ans(LINF);
  rep(i, two(n)) {
    ll t(0);
    memset(s, 0, sizeof(s));
    rep(j, n) {
      int tj = two(j);
      if (tj & i) {
        t += c[j];
        rep(k, m) s[k] += in[j][k];
      }
    }
    rep(j, m) {
      if (s[j] < x)
        break;
    }
    if (j == m) {
      ans = min(ans, t);
    }
  }
  if (ans == LINF)
    ans = -1;
  cout << ans << endl;
}
Problem p02683 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.1415926535897932384626433832795028
#define INF (1 << 29)
#define LINF (1LL << 60)
using namespace std;
using INT = long long;
using P = pair<int, int>;

int main() {
  int n, m, x;
  cin >> n >> m >> x;
  vector<vector<int>> c;
  c.resize(n);
  vector<int> ar(m, 0);
  vector<int> cost(n);
  rep(i, n) {
    cin >> cost[i];
    rep(j, m) {
      int a;
      cin >> a;
      c[i].emplace_back(a);
    }
  }
  int mini = INF;
  rep(i, 1 << n) {
    int cos = 0;
    rep(j, n) {
      if (i >> j & 1) {
        rep(k, n) { ar[k] += c[j][k]; }
        cos += cost[j];
      }
    }
    rep(j, m) {
      if (ar[j] < x) {
        cos += INF;
        break;
      }
    }
    mini = min(mini, cos);
    ar = vector<int>(m);
  }
  if (mini == INF)
    cout << -1 << endl;
  else
    cout << mini << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.1415926535897932384626433832795028
#define INF (1 << 29)
#define LINF (1LL << 60)
using namespace std;
using INT = long long;
using P = pair<int, int>;

int main() {
  int n, m, x;
  cin >> n >> m >> x;
  vector<vector<int>> c;
  c.resize(n);
  vector<int> ar(m, 0);
  vector<int> cost(n);
  rep(i, n) {
    cin >> cost[i];
    rep(j, m) {
      int a;
      cin >> a;
      c[i].emplace_back(a);
    }
  }
  int mini = INF;
  rep(i, 1 << n) {
    int cos = 0;
    rep(j, n) {
      if (i >> j & 1) {
        rep(k, m) { ar[k] += c[j][k]; }
        cos += cost[j];
      }
    }
    rep(j, m) {
      if (ar[j] < x) {
        cos += INF;
        break;
      }
    }
    mini = min(mini, cos);
    ar = vector<int>(m);
  }
  if (mini == INF)
    cout << -1 << endl;
  else
    cout << mini << endl;
  return 0;
}
Problem p02683 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, x;
  cin >> n >> m >> x;
  int c[50], a[50][50], sum[50], ans[50] = {0}, cnt = 0;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  }
  for (int i = 0; i < (1 << n); i++) {
    memset(sum, 0, sizeof(sum));
    int cost = 0;
    for (int j = 0; j < n; j++)
      if (i & (1 << j)) {
        cost += c[j + 1];
        for (int k = 1; k <= m; k++)
          sum[k] += a[j + 1][k];
      }
    bool bad = 0;
    for (int j = 1; j <= m; j++)
      if (sum[j] < x) {
        bad = 1;
        break;
      }
    if (!bad)
      ans[++cnt] = cost;
  }
  sort(ans + 1, ans + cnt + 1);
  if (ans[1] == 0)
    cout << -1;
  else
    cout << ans[1];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, x;
  cin >> n >> m >> x;
  int c[50], a[50][50], sum[50], ans[4097], cnt = 0;
  memset(ans, 0, sizeof(ans));
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  }
  for (int i = 0; i < (1 << n); i++) {
    memset(sum, 0, sizeof(sum));
    int cost = 0;
    for (int j = 0; j < n; j++)
      if (i & (1 << j)) {
        cost += c[j + 1];
        for (int k = 1; k <= m; k++)
          sum[k] += a[j + 1][k];
      }
    bool bad = 0;
    for (int j = 1; j <= m; j++)
      if (sum[j] < x) {
        bad = 1;
        break;
      }
    if (!bad)
      ans[++cnt] = cost;
  }
  sort(ans + 1, ans + cnt + 1);
  if (ans[1] == 0)
    cout << -1;
  else
    cout << ans[1];
  return 0;
}
Problem p02683 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

int a[15][15];
int price[15];

int main() {
  int n, m, x;
  cin >> n >> m >> x;
  rep(i, n) {
    int c;
    cin >> c;
    price[i] = c;
    rep(j, m) { cin >> a[i][j]; }
  }
  int ans = INF;
  for (int bit = 0; bit < (1 << n); bit++) {
    int understanding[n];
    int sum_price = 0;
    rep(i, m) understanding[i] = 0;
    rep(i, n) {
      if (bit & (1 << i)) {
        sum_price += price[i];
        rep(j, m) { understanding[j] += a[i][j]; }
      }
    }
    bool ok = true;
    rep(i, m) {
      if (understanding[i] < x) {
        ok = false;
        break;
      }
    }
    if (ok)
      ans = min(ans, sum_price);
  }
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

int a[15][15];
int price[15];

int main() {
  int n, m, x;
  cin >> n >> m >> x;
  rep(i, n) {
    int c;
    cin >> c;
    price[i] = c;
    rep(j, m) { cin >> a[i][j]; }
  }
  int ans = INF;
  for (int bit = 0; bit < (1 << n); bit++) {
    int understanding[m];
    int sum_price = 0;
    rep(i, m) understanding[i] = 0;
    rep(i, n) {
      if (bit & (1 << i)) {
        sum_price += price[i];
        rep(j, m) { understanding[j] += a[i][j]; }
      }
    }
    bool ok = true;
    rep(i, m) {
      if (understanding[i] < x) {
        ok = false;
        break;
      }
    }
    if (ok)
      ans = min(ans, sum_price);
  }
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}
Problem p02683 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MIN_VAL = 1001001001;
int main(void) {
  int N, M, X;
  cin >> N >> M >> X;

  vector<int> C(N);
  int A[N][N];

  for (int i = 0; i < N; i++) {
    cin >> C[i];
    for (int j = 0; j < M; j++) {
      cin >> A[i][j];
    }
  }

  long long int ans = MIN_VAL;

  for (int b = 0; b < (1 << N); b++) {
    long long int C_cktmp = 0;
    vector<int> X_cktmp(M);
    for (int i = 0; i < N; i++) {
      if (b >> i & 1) {
        C_cktmp += C[i];
        for (int j = 0; j < M; j++) {
          X_cktmp[j] += A[i][j];
        }
      }
    }
    bool flag = true;
    for (int j = 0; j < M; j++) {
      if (X_cktmp[j] < X)
        flag = false;
    }
    if (flag)
      ans = min(ans, C_cktmp);
  }

  if (ans == MIN_VAL)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MIN_VAL = 1001001001;
int main(void) {
  int N, M, X;
  cin >> N >> M >> X;

  vector<int> C(N);
  int A[12][12];

  for (int i = 0; i < N; i++) {
    cin >> C[i];
    for (int j = 0; j < M; j++) {
      cin >> A[i][j];
    }
  }

  long long int ans = MIN_VAL;

  for (int b = 0; b < (1 << N); b++) {
    long long int C_cktmp = 0;
    vector<int> X_cktmp(M);
    for (int i = 0; i < N; i++) {
      if (b >> i & 1) {
        C_cktmp += C[i];
        for (int j = 0; j < M; j++) {
          X_cktmp[j] += A[i][j];
        }
      }
    }
    bool flag = true;
    for (int j = 0; j < M; j++) {
      if (X_cktmp[j] < X)
        flag = false;
    }
    if (flag)
      ans = min(ans, C_cktmp);
  }

  if (ans == MIN_VAL)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}
Problem p02683 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<ll>>;
int main() {
  ll N, K, b;
  cin >> N >> K >> b;
  vector<vector<ll>> vec(N, vector<ll>(K + 1));
  ll d = 0;
  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < K + 1; j++) {
      cin >> vec.at(i).at(j);
    }
    d += vec.at(i).at(0);
  }
  d = d + 1;
  ll re = d;
  const ll n = N;
  for (int tmp = 0; tmp < (1 << N); tmp++) {
    bitset<8> s(tmp);
    vector<ll> veco(K + 1, 0);
    // ビット列を出力
    for (ll i = 0; i < N; i++) {
      for (ll k = 0; k < K + 1; k++) {
        if (s.test(i)) {
          veco.at(k) += vec.at(i).at(k);
        }
      }
    }
    for (ll i = 1; i < K + 1; i++) {
      if (veco.at(i) < b)
        break;
      if (i == K) {
        re = min(re, veco.at(0));
      }
    }
  }
  if (re == d) {
    re = -1;
  }
  cout << re << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<ll>>;
int main() {
  ll N, K, b;
  cin >> N >> K >> b;
  vector<vector<ll>> vec(N, vector<ll>(K + 1));
  ll d = 0;
  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < K + 1; j++) {
      cin >> vec.at(i).at(j);
    }
    d += vec.at(i).at(0);
  }
  d = d + 1;
  ll re = d;
  const ll n = N;
  for (int tmp = 0; tmp < (1 << N); tmp++) {
    bitset<12> s(tmp);
    vector<ll> veco(K + 1, 0);
    // ビット列を出力
    for (ll i = 0; i < N; i++) {
      for (ll k = 0; k < K + 1; k++) {
        if (s.test(i)) {
          veco.at(k) += vec.at(i).at(k);
        }
      }
    }
    for (ll i = 1; i < K + 1; i++) {
      if (veco.at(i) < b)
        break;
      if (i == K) {
        re = min(re, veco.at(0));
      }
    }
  }
  if (re == d) {
    re = -1;
  }
  cout << re << endl;
  return 0;
}
Problem p02684 (C++)

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

typedef long long ll;
typedef pair<int, int> pii;

long long MOD = 1000000000 + 7;

int main() {
  int N;
  ll K;
  cin >> N >> K;
  int log_K = floor(log2(K));
  cerr << N << " " << log_K << endl;
  vector<vector<int>> next(log_K + 1, vector<int>(N));

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    cerr << i << " : " << tmp << endl;
    next.at(0).at(i) = tmp - 1;
  }

  for (int k = 0; k < log_K; k++) {
    for (int i = 0; i < N; i++) {
      if (next[k][i] == -1) {
        next[k + 1][i] = -1;
      } else {
        next[k + 1][i] = next[k][next[k][i]];
        cerr << k + 1 << " " << i << " " << next[k + 1][i] << endl;
      }
    }
  }

  int ans = 0;
  for (ll k = log_K; k >= 0; k--) {
    if ((K >> k) & 1) {
      ans = next[k][ans];
    }
  }
  cout << ans + 1 << endl;
}

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

typedef long long ll;
typedef pair<int, int> pii;

long long MOD = 1000000000 + 7;

int main() {
  int N;
  ll K;
  cin >> N >> K;
  int log_K = floor(log2(K));
  cerr << N << " " << log_K << endl;
  vector<vector<int>> next(log_K + 1, vector<int>(N));

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    cerr << i << " : " << tmp << endl;
    next.at(0).at(i) = tmp - 1;
  }

  for (int k = 0; k < log_K; k++) {
    for (int i = 0; i < N; i++) {
      if (next[k][i] == -1) {
        next[k + 1][i] = -1;
      } else {
        next[k + 1][i] = next[k][next[k][i]];
        // cerr << k+1 << " " << i << " " << next[k+1][i] << endl;
      }
    }
  }

  int ans = 0;
  for (ll k = log_K; k >= 0; k--) {
    if ((K >> k) & 1) {
      ans = next[k][ans];
    }
  }
  cout << ans + 1 << endl;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
// 17571@nith.ac.in @Ankit Verma
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define MP make_pair
#define int long long
#define ll long long
#define inf 0x3f3f3f3f
#define MOD 1000000007
#define ld long double
#define eb emplace_back
#define pii pair<int, int>
#define ull unsigned long long
#define all(v) v.begin(), v.end()
#define f(i, x, n) for (int i = x; i < n; i++)
#define fr(i, x, n) for (int i = x; i >= n; i--)
#define fastio ios::sync_with_stdio(false)

void IO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int32_t main() {
  // IO();
  fastio;
  cin.tie(nullptr);
  cout.tie(nullptr);
  ll n, k;
  cin >> n >> k;
  vector<int> ar(n);
  vector<int> C;
  vector<bool> vis(200002, 0);
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  int pos = 1;
  int stp = 0;
  while (k && stp <= 100000000) {
    pos = ar[pos];
    ++stp;
    --k;
  }
  int st = pos;
  while (1) {
    vis[st] = true;
    C.pb(st);
    st = ar[st];
    if (vis[st])
      break;
  }
  cout << C[k % (C.size())] << endl;

  return 0;
}

Fixed submission:
// 17571@nith.ac.in @Ankit Verma
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define MP make_pair
#define int long long
#define ll long long
#define inf 0x3f3f3f3f
#define MOD 1000000007
#define ld long double
#define eb emplace_back
#define pii pair<int, int>
#define ull unsigned long long
#define all(v) v.begin(), v.end()
#define f(i, x, n) for (int i = x; i < n; i++)
#define fr(i, x, n) for (int i = x; i >= n; i--)
#define fastio ios::sync_with_stdio(false)

void IO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int32_t main() {
  // IO();
  fastio;
  cin.tie(nullptr);
  cout.tie(nullptr);
  ll n, k;
  cin >> n >> k;
  vector<int> ar(200002);
  vector<int> C;
  vector<bool> vis(200002, 0);
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  int pos = 1;
  int stp = 0;
  while (k && stp <= 100000000) {
    pos = ar[pos];
    ++stp;
    --k;
  }
  int st = pos;
  while (1) {
    vis[st] = true;
    C.pb(st);
    st = ar[st];
    if (vis[st])
      break;
  }
  cout << C[k % (C.size())] << endl;

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define loop(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n), s, done(n + 1, -1);
  int c = 1, l = 0;

  loop(i, n) {
    cin >> a[i];
    done[i] = false;
  }

  int v = 1;
  while (done[v] == -1) {
    done[v] = s.size();
    s.emplace_back(v);
    v = a[v - 1];
  }
  c = s.size() - done[v];
  l = done[v];

  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << s[l + k] << endl;
  }
  return 0;
}

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

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n), s, done(n + 1, -1);
  int c = 1, l = 0;

  loop(i, n) { cin >> a[i]; }

  int v = 1;
  while (done[v] == -1) {
    done[v] = s.size();
    s.emplace_back(v);
    v = a[v - 1];
  }
  c = s.size() - done[v];
  l = done[v];

  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << s[l + k] << endl;
  }
  return 0;
}
Problem p02684 (C++)

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

signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  vector<int> G(0);
  vector<int> kai(N, 0);
  int now = 0, fin = 0, saki = 0, sta = 0;
  G.push_back(0);
  while (G.size() < N) {
    G.push_back(A[now]);
    if (kai[A[now]] == 0) {
      kai[A[now]]++;
      now = A[now];
      continue;
    } else {
      fin = G.size();
      saki = A[now];
      G.erase(G.end() - 1);
      for (int i = 0; i < fin - 1; i++) {
        if (G[i] == saki) {
          sta = i;
          break;
        }
      }
      fin -= 1;
      break;
    }
  }
  int ans;
  if (K < fin)
    ans = G[K];
  else {
    int mod = fin - sta;
    ans = G[sta + (K - sta) % mod];
  }
  cout << ans + 1 << endl;
}

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

signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  vector<int> G(0);
  vector<int> kai(N, 0);
  int now = 0, fin = 0, saki = 0, sta = 0;
  G.push_back(0);
  while (G.size() <= N) {
    G.push_back(A[now]);
    if (kai[A[now]] == 0) {
      kai[A[now]]++;
      now = A[now];
      continue;
    } else {
      fin = G.size();
      saki = A[now];
      G.erase(G.end() - 1);
      for (int i = 0; i < fin - 1; i++) {
        if (G[i] == saki) {
          sta = i;
          break;
        }
      }
      fin -= 1;
      break;
    }
  }
  int ans;
  if (K < fin)
    ans = G[K];
  else {
    int mod = fin - sta;
    ans = G[sta + (K - sta) % mod];
  }
  cout << ans + 1 << endl;
}
Problem p02684 (C++)

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

/* 大文字を小文字に変換 */
char tolower(char c) { return (c + 0x20); }
/* 小文字を大文字に変換 */
char toupr(char c) { return (c - 0x20); }

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_N = 100100;

int P[65][MAX_N]; // 10**18 == 2*60だから

int main() {
  ll N, K;
  cin >> N >> K;
  int A[N];
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  rep(i, 65) {
    if (i == 0) {
      rep(j, N) P[i][j] = A[j];
    } else {
      rep(j, N) P[i][j] = P[i - 1][P[i - 1][j]];
    }
  }
  bitset<65> Kbit(K);
  int cur = 0;
  for (int i = 64; i >= 0; i--) {
    if (Kbit.test(i)) {
      cur = P[i][cur];
    }
  }
  cur++;
  cout << cur << endl;
}

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

/* 大文字を小文字に変換 */
char tolower(char c) { return (c + 0x20); }
/* 小文字を大文字に変換 */
char toupr(char c) { return (c - 0x20); }

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_N = 200100;

int P[65][MAX_N]; // 10**18 == 2*60だから

int main() {
  ll N, K;
  cin >> N >> K;
  int A[N];
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  rep(i, 65) {
    if (i == 0) {
      rep(j, N) P[i][j] = A[j];
    } else {
      rep(j, N) P[i][j] = P[i - 1][P[i - 1][j]];
    }
  }
  bitset<65> Kbit(K);
  int cur = 0;
  for (int i = 64; i >= 0; i--) {
    if (Kbit.test(i)) {
      cur = P[i][cur];
    }
  }
  cur++;
  cout << cur << endl;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lli = long long;
using pii = pair<int, int>;

#define rep(i, k, n) for (int i = (k); i != int(n); ++i)
#define sz(x) x.size()
#define all(x) x.begin(), x.end()

#define dbg(x) cout << #x << '=' << x << '\n'
#define dbg2(x, y) cout << #x << '=' << x << ' ' << #y << '=' << y << '\n'

#define input() (*istream_iterator<lli>(cin))
#define strin() (*istream_iterator<string>(cin))
#define output(x) cout << x << '\n'

const int mxn = 2e5 + 10;
lli arr[mxn];
bool is_visit[mxn];
int find_loop(int cur = 1) {
  is_visit[cur] = true;
  int nx = arr[cur];
  if (is_visit[nx])
    return nx;
  find_loop(nx);
}
int main() {
  ios::sync_with_stdio(false);
  lli n = input(), k = input();
  vector<int> cycle;
  int cur = 1, dist = 0;
  for (int i = 1; i <= n; ++i)
    arr[i] = input();
  int loop_town = find_loop();
  // dbg(loop_town);
  while (cur != loop_town) {
    cur = arr[cur];
    dist++;
    if (dist == k) {
      cout << cur;
      return 0;
    }
  }
  // dbg(dist);
  k -= dist;
  cur = arr[loop_town];
  cycle.push_back(loop_town);
  while (cur != loop_town) {
    cycle.push_back(cur);
    cur = arr[cur];
  }
  // for(int cur: cycle) cout << cur;
  k %= cycle.size();
  cout << cycle[k];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lli = long long;
using pii = pair<int, int>;

#define rep(i, k, n) for (int i = (k); i != int(n); ++i)
#define sz(x) x.size()
#define all(x) x.begin(), x.end()

#define dbg(x) cout << #x << '=' << x << '\n'
#define dbg2(x, y) cout << #x << '=' << x << ' ' << #y << '=' << y << '\n'

#define input() (*istream_iterator<lli>(cin))
#define strin() (*istream_iterator<string>(cin))
#define output(x) cout << x << '\n'

const int mxn = 2e5 + 10;
lli arr[mxn];
bool is_visit[mxn];
int find_loop(int cur = 1) {
  is_visit[cur] = true;
  int nx = arr[cur];
  if (is_visit[nx])
    return nx;
  return find_loop(nx);
}
int main() {
  ios::sync_with_stdio(false);
  lli n = input(), k = input();
  vector<int> cycle;
  int cur = 1, dist = 0;
  for (int i = 1; i <= n; ++i)
    arr[i] = input();
  int loop_town = find_loop();
  // dbg(loop_town);
  while (cur != loop_town) {
    cur = arr[cur];
    dist++;
    if (dist == k) {
      cout << cur;
      return 0;
    }
  }
  // dbg(dist);
  k -= dist;
  cur = arr[loop_town];
  cycle.push_back(loop_town);
  while (cur != loop_town) {
    cycle.push_back(cur);
    cur = arr[cur];
  }
  // for(int cur: cycle) cout << cur;
  k %= cycle.size();
  cout << cycle[k];
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  if (k <= 200000) {
    int q = 0;
    while (k) {
      q = a[q];
      k--;
    }
    cout << q + 1 << endl;
    return 0;
  }
  vector<int> visited(n, -1);
  vector<int> last(n, -1);
  int p = 0, c = 0;
  for (int i = 0; i < last.size(); i++) {
    visited[p]++;
    if (visited[p] == 2) {
      c = i - last[p];
      break;
    }
    last[p] = i;
    p = a[p];
  }
  // cout << c << endl;
  k -= last[p];
  k %= c;
  while (k) {
    p = a[p];
    k--;
  }
  cout << p + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  if (k <= 200000) {
    int q = 0;
    while (k) {
      q = a[q];
      k--;
    }
    cout << q + 1 << endl;
    return 0;
  }
  vector<int> visited(n, 0);
  vector<int> last(n, 0);
  int p = 0, c = 0;
  for (int i = 0; i < last.size(); i++) {
    visited[p]++;
    if (visited[p] == 2) {
      c = i - last[p];
      break;
    }
    last[p] = i;
    p = a[p];
  }
  // cout << c << endl;
  k -= last[p];
  k %= c;
  while (k) {
    p = a[p];
    k--;
  }
  cout << p + 1 << endl;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  map<ll, ll> mp;

  ll point = 1;
  ll loop, start;
  vector<ll> vec;
  for (int i = 0; i <= n + 3; i++) {
    if (mp.count(point)) {
      start = mp[point];
      loop = i - start;
      break;
    }
    // cout << point << endl;
    vec.push_back(point);
    mp[point] = i;
    point = a[point];
  }
  // cout << a[6] << endl;

  // for (ll x : vec) cout << x << endl;
  if (k < start) {
    cout << vec[k] << endl;
    return 0;
  }
  // cout << loop << endl;
  // cout << start << endl;
  k -= start;
  ll p = k % loop;
  ll ans = vec[start + p];
  cout << ans << endl;

  return 0;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  map<ll, ll> mp;

  ll point = 1;
  ll loop, start;
  vector<ll> vec;
  for (int i = 0; i <= n + 3; i++) {
    if (mp.count(point)) {
      start = mp[point];
      loop = i - start;
      break;
    }
    // cout << point << endl;
    vec.push_back(point);
    mp[point] = i;
    point = a[point];
  }
  // cout << a[6] << endl;

  // for (ll x : vec) cout << x << endl;
  if (k < start) {
    cout << vec[k] << endl;
    return 0;
  }
  // cout << loop << endl;
  // cout << start << endl;
  k -= start;
  ll p = k % loop;
  ll ans = vec[start + p];
  cout << ans << endl;

  return 0;
}
Problem p02684 (C++)

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

int main() {
  ll n, k;
  vector<ll> v(10000), c(10000);
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
    v[i]--;
  }
  ll now = 0;
  bool can = true;
  for (ll i = 1; i <= k; i++) {
    if (c[now] && can) {
      can = false;
      ll tmp = abs(c[now] - i);
      i += tmp * ((k - i) / tmp);
    }
    c[now] = i;
    now = v[now];
  }
  cout << now + 1 << endl;
  return 0;
}


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

int main() {
  ll n, k;
  vector<ll> v(1000000), c(1000000);
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
    v[i]--;
  }
  ll now = 0;
  bool can = true;
  for (ll i = 1; i <= k; i++) {
    if (c[now] && can) {
      can = false;
      ll tmp = abs(c[now] - i);
      i += tmp * ((k - i) / tmp);
    }
    c[now] = i;
    now = v[now];
  }
  cout << now + 1 << endl;
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

const int D = 60; // 2の60乗まででダブリングの表が足りるので60．
const int MAX_N = 200010;
int to[D][MAX_N];

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

  int n;
  long long k;
  cin >> n >> k;

  REP(i, n) {
    cin >> to[0][i];
    to[0][i]--;
  }

  // ダブリングの表をセットする
  REP(i, D - 1) REP(j, n) { to[i + 1][j] = to[i][to[j][i]]; }

  // k を2の指数表記で分解して，
  // 指数の大きい方から辿っていく．
  int v = 0;
  for (int i = D - 1; i >= 0; i--) {
    long long l = 1ll << i;
    if (l <= k) {
      v = to[i][v];
      k -= l;
    }
  }

  cout << v + 1 << '\n';
  return 0;
}

Fixed submission:
#include <iostream>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

const int D = 60; // 2の60乗まででダブリングの表が足りるので60．
const int MAX_N = 200010;
int to[D][MAX_N];

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

  int n;
  long long k;
  cin >> n >> k;

  REP(i, n) {
    cin >> to[0][i];
    to[0][i]--;
  }

  // ダブリングの表をセットする
  REP(i, D - 1) REP(j, n) { to[i + 1][j] = to[i][to[i][j]]; }

  // k を2の指数表記で分解して，
  // 指数の大きい方から辿っていく．
  int v = 0;
  for (int i = D - 1; i >= 0; i--) {
    long long l = 1ll << i;
    if (l <= k) {
      v = to[i][v];
      k -= l;
    }
  }

  cout << v + 1 << '\n';
  return 0;
}
Problem p02684 (C++)

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

using namespace std;

int main() {
  long long n, k;
  bool b[200001];
  int a[200001];

  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = false;
  }
  int c = 0;
  int p = 1;
  while (b[p] == false) {
    b[p] = true;
    p = a[p];
    c++;
  }
  //    cout << p << endl;
  int x = 1;
  int c0 = 1;
  if (p != 1) {
    while (a[x] != p) {
      x = a[x];
      c0++;
    }
  }
  //    cout << c0 << endl;
  //    cout << c << endl;

  if (k <= c) {
  } else {
    k = ((k - c0) % (c - c0)) + c0;
  }
  p = 1;
  while (k > 0) {
    p = a[p];
    k--;
  }
  cout << p << endl;

  return 0;
}


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

using namespace std;

int main() {
  long long n, k;
  bool b[200001];
  int a[200001];

  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = false;
  }
  int c = 0;
  int p = 1;
  while (b[p] == false) {
    b[p] = true;
    p = a[p];
    c++;
  }
  //    cout << p << endl;
  int x = 1;
  int c0 = 0;
  while (x != p) {
    x = a[x];
    c0++;
  }
  //    cout << c0 << endl;
  //    cout << c << endl;

  if (k <= c) {
  } else {
    k = ((k - c0) % (c - c0)) + c0;
  }
  p = 1;
  while (k > 0) {
    p = a[p];
    k--;
  }
  cout << p << endl;

  return 0;
}
Problem p02684 (C++)

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

using namespace std;

long long A[200005], C[200005];

int main() {

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  long long N, K;
  cin >> N >> K;
  for (long long i = 1; i <= N; ++i) {
    cin >> A[i];
  }
  long long curr = 1, move = 1, len, moves;
  vector<long long> cycle;
  C[curr] = move;
  while (1) {
    move++;
    curr = A[curr];
    if (C[curr] > 0) {
      len = move - C[curr];
      break;
    }
    C[curr] = move;
  }
  long long shift = C[curr] - C[1];
  for (long long i = 0; i < len; ++i) {
    cycle.push_back(curr);
    curr = A[curr];
  }
  moves = (K - shift) % len;
  long long town = cycle[moves];
  cout << town << '\n';
  return 0;
}

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

using namespace std;

long long A[200005], C[200005];

int main() {

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  long long N, K;
  cin >> N >> K;
  for (long long i = 1; i <= N; ++i) {
    cin >> A[i];
  }
  long long curr = 1, move = 1, len, moves;
  if (K <= N) {
    while (K--)
      curr = A[curr];
    cout << curr << '\n';
    return 0;
  }
  vector<long long> cycle;
  C[curr] = move;
  while (1) {
    move++;
    curr = A[curr];
    if (C[curr] > 0) {
      len = move - C[curr];
      break;
    }
    C[curr] = move;
  }
  long long shift = C[curr] - C[1];
  for (long long i = 0; i < len; ++i) {
    cycle.push_back(curr);
    curr = A[curr];
  }
  moves = (K - shift) % len;
  long long town = cycle[moves];
  cout << town << '\n';
  return 0;
}
Problem p02684 (C++)

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

#define mod 1e9 + 7;
#define INF 1e9 + 9;
#define ps(x, y) fixed << setprecision(y) << x
typedef long long ll;
typedef unsigned long long ull;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  for (ll i = 1; i < n + 1; i++)
    cin >> a[i];
  vector<ll> v;
  unordered_set<ll> us;
  v.push_back(1);
  us.insert(1);
  int i = 0, t = 10, req;
  while (t--) {
    // cout<<a[v[i]]<<"  ";
    if (us.count(a[v[i]]) == 0) {
      v.push_back(a[v[i]]);
      us.insert(a[v[i]]);
    } else {
      req = a[v[i]];
      break;
    }
    i++;
  }
  ll nn = v.size();
  ll bc, ac;
  for (int i = 0; i < nn; i++) {
    if (v[i] == req) {
      bc = i;
      ac = nn - i;
      break;
    }
  }
  // cout<<bc<<" "<<ac<<endl;
  if (k < bc) {
    cout << v[k] << endl;
  } else {
    k = k - bc;

    ll need = k % ac;
    cout << v[need + bc] << endl;
  }

  /*for(ll x :v)
          cout<<x<<"  ";
  cout<<endl;*/

  return 0;
}

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

#define mod 1e9 + 7;
#define INF 1e9 + 9;
#define ps(x, y) fixed << setprecision(y) << x
typedef long long ll;
typedef unsigned long long ull;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll a[n + 1];
  for (ll i = 1; i < n + 1; i++)
    cin >> a[i];
  vector<ll> v;
  unordered_set<ll> us;
  v.push_back(1);
  us.insert(1);
  int i = 0, req;
  while (1) {
    // cout<<a[v[i]]<<"  ";
    if (us.count(a[v[i]]) == 0) {
      v.push_back(a[v[i]]);
      us.insert(a[v[i]]);
    } else {
      req = a[v[i]];
      break;
    }
    i++;
  }
  ll nn = v.size();
  ll bc, ac;
  for (int i = 0; i < nn; i++) {
    if (v[i] == req) {
      bc = i;
      ac = nn - i;
      break;
    }
  }
  // cout<<bc<<" "<<ac<<endl;
  if (k < bc) {
    cout << v[k] << endl;
  } else {
    k = k - bc;

    ll need = k % ac;
    cout << v[need + bc] << endl;
  }

  /*for(ll x :v)
          cout<<x<<"  ";
  cout<<endl;*/

  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
/*
   Created by Ezio112
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define inf ll(10e16)
#define mod ll(10e8 + 7)
#define ff first
#define ss second
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    vector<ll> ar(n + 1);
    for (ll i = 1; i <= n; ++i)
      cin >> ar[i];

    unordered_map<ll, ll> appeared;

    ll start = 1, jumps = 1, loop_size = 0;

    while (k--) {
      appeared[start] = jumps++;
      start = ar[start];
      if (appeared.count(start)) {
        loop_size = jumps - appeared[start];
        break;
      }
    }
    k %= loop_size;
    while (k--) {
      start = ar[start];
    }
    cout << start;
  }
  return 0;
}


Fixed submission:
/*
   Created by Ezio112
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define inf ll(10e16)
#define mod ll(10e8 + 7)
#define ff first
#define ss second
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    vector<ll> ar(n + 1);
    for (ll i = 1; i <= n; ++i)
      cin >> ar[i];

    unordered_map<ll, ll> appeared;

    ll start = 1, jumps = 1, loop_size = 1;

    while (k--) {
      appeared[start] = jumps++;
      start = ar[start];
      if (appeared.count(start)) {
        loop_size = jumps - appeared[start];
        break;
      }
    }
    k %= loop_size;
    while (k--) {
      start = ar[start];
    }
    cout << start;
  }
  return 0;
}