alexjercan/bugnet huggingface dataset:
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define print(n) cout << n << endl
#define all(n) n.begin(), n.end()

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

  ll k, n;
  cin >> k >> n;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  a[n] = k + a[0];

  vector<ll> b(n);
  rep(i, n) b[i] = a[i + 1] - a[i];

  sort(all(b));
  print(accumulate(all(b), 0LL) - b[n - 1]);
  return 0;
}

Error: 0

Fixed submission:

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

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define print(n) cout << n << endl
#define all(n) n.begin(), n.end()

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

  ll k, n;
  cin >> k >> n;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  a[n] = k + a[0];

  vector<ll> b(n);
  rep(i, n + 1) b[i] = a[i + 1] - a[i];

  sort(all(b));
  print(accumulate(all(b), 0LL) - b[n - 1]);
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const int N = 2e5 + 10;
int a[N];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; ++i) {
    cin >> a[i];
  }
  int ans = n;
  for (int i = 2; i <= k; ++i) {
    ans = min(ans, n - a[i] + a[i - 1]);
  }
  ans = min(ans, a[k] - a[1]);
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const int N = 1e5 + 10;
int a[N];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; ++i) {
    cin >> a[i];
  }
  int ans = n;
  for (int i = 2; i <= k; ++i) {
    ans = min(ans, n - a[i] + a[i - 1]);
  }
  ans = min(ans, a[k] - a[1]);
  cout << ans << '\\n';
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long int int64;
typedef unsigned long long uint64;

int main() {
  int k, n;
  cin >> k >> n;
  vector<int> a(n);
  int max = 0;
  int l;
  rep(i, n) cin >> a[i];
  int i = 0;
  while (i < n) {
    l = 0;
    if (i == n - 1) {
      l = k - a[n - 1];
      l += a[0];
      if (max < l)
        max = l;
      ++i;
    } else
      l = a[i + 1] - a[i];
    if (max < l)
      max = l;
    ++i;
  }
  cout << k - max << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long int int64;
typedef unsigned long long uint64;

int main() {
  int k, n;
  cin >> k >> n;
  vector<int> a(n);
  int min = 1000001;
  int sum;
  int l;
  rep(i, n) cin >> a[i];
  rep(i, n) {
    int j = i;
    int counter = 0;
    sum = 0;
    while (counter < n - 1) {
      if (j == n - 1) {
        sum += abs(k - a[j]);
        sum += a[0];
        ++counter;
        j = 0;
        continue;
      } else {
        sum += abs(a[j] - a[j + 1]);
        ++j;
        ++counter;
      }
    }
    if (min > sum)
      min = sum;
  }
  cout << min << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  int k, n;
  cin >> k >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  vector<int> d(n);
  rep(i, n) {
    if (v[i] <= v[(i + 1) % n])
      d[i] = v[i + 1] - v[i];
    else {
      d[i] = (k - v[i]) + v[(i + 1) % n];
    }
  }
  int min = k + 1, sum = accumulate(d.begin(), d.end(), 0);
  sum -= *std::max_element(d.begin(), d.end());
  /*rep(i,n){
    if(sum-d[i]<min)min=sum;
  }*/
  cout << sum;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  int k, n;
  cin >> k >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  vector<int> d(n);
  rep(i, n) {
    if (v[i] <= v[(i + 1) % n])
      d[i] = v[i + 1] - v[i];
    else {
      d[i] = (k - v[i]) + v[(i + 1) % n];
    }
  }
  int min = k + 1, sum;
  rep(i, n) {
    sum = 0;
    int j = i, c = 0;
    while (1) {
      sum += d[j % n];
      c++;
      j++;
      if (c == n - 1)
        break;
    }
    if (sum < min)
      min = sum;
  }
  cout << min;

  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int k, n;
  cin >> k >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  a.push_back(a[0] + k);
  int l = 0;
  for (int i = 0; i < n; i++) {
    l = max(l, a[i + 1] - a[i]);
  }
  int ans;
  ans = k - l;
  cout << ans << endl;
  return 0;
}


Error: -6
double free or corruption (out)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int k, n;
  cin >> k >> n;
  vector<int> a(n);
  vector<int> d(n - 1);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (n >= 1) {
      d[i - 1] = abs(a[i] - a[i - 1]);
    }
  }
  int res;
  int mx;
  res = k - a[n - 1];
  mx = *max_element(d.begin(), d.end());
  int sum;
  if (mx <= res) {
    for (int i = 0; i < n - 1; i++) {
      sum = sum + abs(a[i + 1] - a[i]);
    }
  } else {
    for (int i = n - 1; i >= 1; i--) {
      if (i == n - 1) {
        sum = sum + a[0] + res;
      } else {
        sum = sum + abs(a[i - 1] - a[i]);
      }
    }
  }
  cout << sum << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int k, n, i, max = 0;
  int a[200000];
  cin >> k >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n - 1; i++) {
    if (a[i + 1] - a[i] > max)
      max = a[i + 1] - a[i];
  }
  if (k + a[0] - a[n - 1] > max)
    max = k + a[0] - a[n - 1];
  cout << k - max << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int k, n, i, max = 0;
  int a[2000];
  cin >> k >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n - 1; i++) {
    if (a[i + 1] - a[i] > max)
      max = a[i + 1] - a[i];
  }
  if (k + a[0] - a[n - 1] > max)
    max = k + a[0] - a[n - 1];
  cout << k - max << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 2e5 + 100;
int n, a[N], k;

int main() {

  cin >> k >> n;

  int ans = 0x3f3f3f3f;

  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    int pre = (i - 1 + n) % n;
    int nex = (i + 1) % n;

    int x = (a[pre] - a[i] + k) % k;
    ans = min(ans, x);
    x = (a[i] - a[nex] + k) % k;
    ans = min(ans, x);
  }

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 110;
int n, a[N], k;

int main() {

  cin >> k >> n;

  int ans = 0x3f3f3f3f;

  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    int pre = (i - 1 + n) % n;
    int nex = (i + 1) % n;

    int x = (a[pre] - a[i] + k) % k;
    ans = min(ans, x);
    x = (a[i] - a[nex] + k) % k;
    ans = min(ans, x);
  }

  cout << ans << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

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

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int k, n;
  cin >> k >> n;
  int a[n];
  rep(i, n) cin >> a[i];

  int ans = 1 << 30;
  rep(i, n) {
    int start = i;
    int v1 = a[start];

    int end = i - 1;
    int v2;
    if (end < 0) {
      end += n;
      v2 = a[end];
    } else {
      v2 = k + a[end];
    }

    ans = min(ans, v2 - v1);
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

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

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int k, n;
  cin >> k >> n;
  int a[n];
  rep(i, n) cin >> a[i];

  int ans = 1 << 30;
  rep(i, n) {
    int dist = 0;
    int index1 = i;
    int cnt = 0;
    while (true) {
      int index2 = index1 + 1;
      int v1 = 0;
      int v2 = 0;

      if (index1 >= n) {
        index1 -= n;
        v1 = k;
      }

      if (index2 >= n) {
        index2 -= n;
        v2 = k;
      }
      if (i == index2)
        cnt++;
      if (cnt == 1)
        break;

      v1 += a[index1];
      v2 += a[index2];
      dist += v2 - v1;
      index1++;
    }
    ans = min(ans, dist);
  }

  cout << ans << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int k, n, a[200005];

int main() {
  cin >> k >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int maxSegment = 0;

  for (int i = 1; i < n; i++) {
    int segment = (abs(a[i] - a[i - 1] + k)) % k;
    maxSegment = max(maxSegment, segment);
  }

  int segment = (abs(a[0] - a[n - 1] + k)) % k;
  maxSegment = max(maxSegment, segment);

  cout << k - maxSegment << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int k, n, a[200005];
bool vis[200005];

long long solve(int h) {
  vis[h] = true;

  long long ans = -1;

  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      int dist = (abs(a[h] - a[i] + k)) % k;
      if (ans == -1)
        ans = dist + solve(i);
      else
        ans = min(ans, dist + solve(i));
    }
  }

  vis[h] = false;

  return ans == -1 ? 0 : ans;
}

int main() {
  cin >> k >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  long long ans = 0;

  for (int i = 0; i < n; i++) {
    if (i == 0)
      ans = solve(i);
    else
      ans = min(ans, solve(i));
    cout << "\\n";
  }

  cout << ans << endl;

  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, k, ans;
int a[200010];

int main() {
  scanf("%d%d", &k, &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  ans = a[2] - a[1];
  for (int i = 2; i < n; i++)
    ans = max(ans, a[i + 1] - a[i]);
  ans = max(ans, a[1] + k - a[n]);
  printf("%d\\n", k - ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long k, n, temp, t;
  cin >> k >> n;
  long long a[n], m[n - 1];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < n - 1; i++) {
    for (int j = i; j <= n; j++) {
      if (a[i] > a[j]) {
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
      }
    }
  }
  t = k - ((a[0] + k) - a[n - 1]);

  for (int j = 0; j < n - 1; j++) {
    m[j] = abs(k - (abs(a[j] - a[j + 1])));
  }
  int max = m[0];
  for (int i = 0; i < n - 1; i++) {
    if (m[i] < max)
      max = m[i];
  }

  if (t < max)
    cout << t << "\\n";
  if (t > max)
    cout << max << "\\n";
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int k, n, a[200010];

int main() {
  int maxt = 0;
  scanf("%d%d%d", &k, &n, &a[1]);
  for (int i = 2; i <= n; i++) {
    scanf("%d", &a[i]);
    maxt = max(maxt, a[i] - a[i - 1]);
  }
  maxt = max(maxt, a[1] - a[n] + k);
  printf("%d", k - maxt);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
int a[N];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int s, n;
  cin >> s >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  if (n == 1 || n == 0) {
    cout << 0 << endl;
    return 0;
  }
  int maxl = 0;
  for (int i = 1; i < n; i++) {
    maxl = max(maxl, a[i] - a[i - 1]);
  }
  maxl = max(maxl, a[0] + s - a[n - 1]);
  cout << s - maxl << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

/*
 * Owner   : Tatsuhiro Hashimoto
 * Created : 2020-04-22 (Wed)
 */

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

#define endl "\\n"

int main() {
  int K = 0, N = 0;
  cin >> K >> N;
  vector<int> A(N, 0);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  A.push_back(A[0] + K);
  int tmp = 0;
  for (int i = 0; i < N; ++i) {
    tmp = max(tmp, A[i + 1] - A[i]);
  }
  cout << K - tmp << endl;
}

Error: 0

Fixed submission:

/*
 * Owner   : Tatsuhiro Hashimoto
 * Created : 2020-04-22 (Wed)
 */

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

#define endl "\\n"
#define FSP(n) cout << fixed << setprecision(n);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
static inline ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
static inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#define prepare                                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios::sync_with_stdio(false);

int K, N;
vector<int> A(N + 1);

void solve() {
  A[N] = K + A[0];
  int tmp;
  for (int i = 0; i < N; ++i) {
    tmp = max(tmp, A[i + 1] - A[i]);
  }
  cout << K - tmp << endl;
}

signed main() {
  prepare cin >> K >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  solve();
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int k, n, b, length, ans, tmp, start, now, prev;
  now = -1;
  prev = 0;
  length = -1;
  cin >> k >> n;
  cin >> start;
  prev = start;
  for (int i = 1; i < n + 1; i++) {
    cin >> now;
    if (i < n)
      tmp = now - prev;
    else
      tmp = start + k - now;
    length = max(length, tmp);
    prev = now;
  }
  ans = k - length;
  cout << ans << endl;
}

Error: -11

Fixed submission:

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

int main() {
  int k, n, b, length, ans, tmp;
  int a[n];
  length = -1;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int j = 0; j < n; j++) {
    if (j < n - 1) {
      tmp = a[j + 1] - a[j];
    } else {
      tmp = a[0] + k - a[n - 1];
    }
    length = max(length, tmp);
  }
  ans = k - length;
  cout << ans << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int routeSum(vector<int> data, int from, int n, int k) {
  int result = 0;
  int f = from;
  int t = f + 1;

  do {
    if (f == n - 1) {
      result += (k - data.at(f)) + data.at(0);
      f = 0;
      t = 1;
      continue;
    }

    result += data.at(t) - data.at(f);
    f++;
    t++;
    if (t == n) {
      t = 0;
    }
  } while (t != from);

  return result;
}

int main() {
  int n, k;
  vector<int> points(0);

  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    points.push_back(a);
  }

  int maxDist = (k - points.at(n - 1)) + points.at(0);
  int maxPoint = 0;
  for (int i = 1; i < n; i++) {
    int dist = points.at(i) - points.at(i - 1);
    if (dist > maxDist) {
      maxDist = dist;
      maxPoint = i;
    }
  }

  cout << routeSum(points, maxPoint, n, k) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int routeSum(vector<int> data, int from, int n, int k) {
  int result = 0;
  int f = from;
  int t = f + 1;

  do {
    if (f == n - 1) {
      result += (k - data.at(f)) + data.at(0);
      f = 0;
      t = 1;
      continue;
    }

    result += data.at(t) - data.at(f);
    f++;
    t++;
    if (t == n) {
      t = 0;
    }
  } while (t != from);

  return result;
}

int main() {
  int n, k;
  vector<int> points(0);

  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    points.push_back(a);
  }

  priority_queue<int, vector<int>, greater<int>> routes;
  for (int i = 0; i < n; i++) {
    int r = routeSum(points, i, n, k);
    routes.push(r);
  }

  cout << routes.top() << endl;

  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  int k;         // Size of lake
  int n;         // Number of houses
  vector<int> a; // Positions of houses along the lake;

  // Read input
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    int e;
    cin >> e;
    a.push_back(e);
  }

  int minDist = 1e9;
  for (int i = 0; i < n; i++) {
    int dist;
    int start = a[i];
    int end = a[(i - 1 < 0 ? (n - 1) : (i - 1))];
    if (end < start)
      end = end + k;
    dist = end - start;
    if (dist < minDist)
      minDist = dist;
  }
  cout << minDist << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <set>
#include <string>
#include <vector>

#define PI 3.14159265358979323846

using namespace std;

int main(void) {
  int k, n;
  cin >> k >> n;

  vector<int> houses;
  while (n--) {
    int h;
    cin >> h;
    houses.push_back(h);
  }

  int minDist = 1e9;
  for (int i = 0; i < houses.size(); i++) {
    int dist = 0;
    for (int j = 0; j < houses.size() - 1; j++) {
      int d =
          houses[(i + j + 1) % houses.size()] - houses[(i + j) % houses.size()];
      if (d < 0)
        d = d + k;
      dist += d;
    }
    if (dist < minDist)
      minDist = dist;
  }
  cout << minDist << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  a.push_back(a[0] + k);

  int l = 0;
  for (int i = 0; i < n; i++) {
    l = max(l, a[i + 1] - a[i]);
  }
  int ans = k - l;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

/*
Nに関して全探索でいける？
とりあえず、ある点での最短距離を考える。
ある点からある点への最短距離を考えて順にたどる？
いや、基本的に、各点において、時計回りか反時計周りかで調べればOKっぽい。
弧がかぶるのは全体として最短ではない。

では各点からスタートしたときの時計回りでの距離と反時計周りでの距離の調べかた
O(1)でいけそうなんだが。
右の要素に反時計周りでアクセス or
左の要素に時計周りでアクセス？
modを使えば、インデックスがはみ出たときのアクセスもできるかな。
時計回りでは数字が増えていく。
反時計周りでは数字が減っていく。
これも20でわったあまりで計算できる？
*/

int calc_dist(int i, vector<int> A, int k) {
  int clock_dist, r_clock_dist;

  // clock turn
  int goal_i;
  if (i == 0) {
    goal_i = A.size() - 1;
  } else {
    goal_i = i - 1;
  }
  if (A[i] < A[goal_i]) {
    clock_dist = A[goal_i] - A[i];
  } else {
    clock_dist = (k - A[i]) + A[goal_i];
  }

  // reverse trun
  if (i == A.size() - 1) {
    goal_i = 0;
  } else {
    goal_i = i + 1;
  }

  if (A[i] < A[goal_i]) {
    r_clock_dist = A[i] + (k - A[goal_i]);
  } else {
    r_clock_dist = A[i] - A[goal_i];
  }

  return min(clock_dist, r_clock_dist);
}

int main() {

  int k, n;
  cin >> k >> n;

  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

  int min_dist = k;
  for (int i = 0; i < n; i++) {
    int dist = calc_dist(i, A, k);
    // calc distance
    min_dist = min(dist, min_dist);
  }

  cout << min_dist << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

int A[200010];
int main() {
  int K, N;
  cin >> K >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int ans = mod;
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      ans = min(ans, K - (K - (A[N - 1] - A[i])));
      ans = min(ans, K - (A[i + 1] - A[i]));
    } else if (i == N - 1) {
      ans = min(ans, K - (A[i] - A[i - 1]));
      ans = min(ans, K - (K - (A[N - 1] - A[0])));
    } else {
      ans = min(ans, K - (A[i] - A[i - 1]));
      ans = min(ans, K - (A[i + 1] - A[i]));
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

int A[100010];
int main() {
  int K, N;
  cin >> K >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int ans = mod;
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      ans = min(ans, K - (K - (A[N - 1] - A[i])));
      ans = min(ans, K - (A[i + 1] - A[i]));
    } else if (i == N - 1) {
      ans = min(ans, K - (A[i] - A[i - 1]));
      ans = min(ans, K - (K - (A[N - 1] - A[0])));
    } else {
      ans = min(ans, K - (A[i] - A[i - 1]));
      ans = min(ans, K - (A[i + 1] - A[i]));
    }
  }
  cout << ans << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <sstream>
using namespace std;
#include <algorithm>
#include <bits/stdc++.h>
#include <iterator>
#include <utility>
#define go return 0
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep(j, n) for (int j = 0; j < n; ++j)
#define REP(i, n) for (int i = 1; i < n; ++i)
#define all(v) ((v).begin()), ((v).end())
#define len(s) ((int)s.size())
#define F first
#define S second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef double db;
typedef vector<ll> ve;
typedef vector<vector<int>> vee;
typedef pair<int, int> pa;
typedef vector<pair<ll, ll>> vepa;
typedef set<int> se;
typedef map<string, int> mp;
const ll N = 1e5 + 5;
ll arr[N], A[N], ARR[N];
bool visited[N] = {};
ll cnt[N], freq[N];
bool f, ff;
int main() {
  ll n, k, sum(0);
  cin >> n >> k;
  ve v(k);
  for (ll &i : v)
    cin >> i;
  ve vf(k);
  vf[0] = v[0] + n - v[k - 1];
  REP(i, k) {
    vf[i] = v[i] - v[i - 1];
    ;
  }
  sort(all(vf));
  vf.pop_back();
  sum = accumulate(all(vf), 0);
  cout << sum;
  go;
}


Error: WA

Fixed submission:

#include <iostream>
#include <sstream>
using namespace std;
#include <algorithm>
#include <bits/stdc++.h>
#include <iterator>
#include <utility>
#define go return 0
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep(j, n) for (int j = 0; j < n; ++j)
#define REP(i, n) for (int i = 1; i <= n; ++i)
#define all(v) ((v).begin()), ((v).end())
#define len(s) ((int)s.size())
#define F first
#define S second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef double db;
typedef vector<int> ve;
typedef vector<vector<int>> vee;
typedef pair<int, int> pa;
typedef vector<pair<ll, ll>> vepa;
typedef set<int> se;
typedef map<string, int> mp;
const ll N = 1e5 + 5;
ll arr[N], A[N], ARR[N];
bool visited[N] = {};
ll cnt[N], freq[N];
bool f, ff;
int main() {
  ll n, k, ans = 0;
  cin >> n >> k;
  rep(i, k && cin >> arr[i]);
  sort(arr, arr + k);
  rep(i, k - 1) {
    if (arr[i] == 0)
      continue;
    ans += (arr[i + 1] - arr[i]);
  }
  cout << ans;

  go;
}