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

#include <bits/stdc++.h>
using namespace std;
int ar[202030];
int main() {
  int k, n;
  cin >> k >> n >> ar[0] >> ar[1];
  int maxi = ar[1] - ar[0];
  for (int i = 2; i < n; i++) {
    cin >> ar[i];
    if (ar[i] - ar[i - 1] > maxi)
      maxi = ar[i] - ar[i - 1];
  }
  int max2 = ar[0] - (ar[n - 1] - k);
  if (max2 > maxi)
    cout << k - max2;
  else
    cout << k - maxi;
  cout << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int ar[102030];
int main() {
  int k, n;
  cin >> k >> n >> ar[0] >> ar[1];
  int maxi = ar[1] - ar[0];
  for (int i = 2; i < n; i++) {
    cin >> ar[i];
    if (ar[i] - ar[i - 1] > maxi)
      maxi = ar[i] - ar[i - 1];
  }
  int max2 = ar[0] - (ar[n - 1] - k);
  if (max2 > maxi)
    cout << k - max2;
  else
    cout << maxi;
  cout << "\\n";
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ios::sync_with_stdio(false);
  int k, n;
  ll a[200005];
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll max1 = 0;
  for (int i = 0; i < n - 1; i++) {
    max1 = max(abs(a[i + 1] - a[i]), max1);
  }
  max1 = max(k - a[n - 1] + a[0], max1);
  cout << k - max1;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ios::sync_with_stdio(false);
  int k, n;
  int a[400005];
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll min1 = 999999999999, max1 = 0;
  for (int i = n; i < 2 * n; i++) {
    a[i] = a[i - n];
  }
  ll s = 0, x;
  for (int i = 0; i < n; i++) {
    s = 0;
    for (int j = i; j < n + i - 1; j++) {
      x = abs(a[j + 1] - a[j]);
      if (x < k / 2)
        s += x;
      else
        s += k - x;
    }
    min1 = min(min1, s);
  }
  cout << min1;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
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;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

int main() {
  ll K, N;
  cin >> K >> N;
  vector<ll> vec(N);
  for (ll i = 0; i < N; i++) {
    ll a;
    cin >> a;
    vec.at(i) = a;
  }
  vector<ll> len(N);
  for (ll i = 0; i < N - 1; i++) {
    len.at(i) = vec.at(i + 1) - vec.at(i);
  }
  len.at(N - 1) = K - vec.at(N - 1) + vec.at(0);
  sort(len.begin(), len.end());
  ll ans = 0;
  for (ll i = 0; i < N - 1; i++) {
    ans += len.at(i);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
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;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

int main() {
  ll K, N;
  cin >> K >> N;
  vector<ll> vec(N);
  for (ll i = 0; i < N; i++) {
    ll a;
    cin >> a;
    vec.at(i) = a;
  }
  vector<ll> len(3);
  for (ll i = 0; i < N - 1; i++) {
    len.at(i) = vec.at(i + 1) - vec.at(i);
  }
  len.at(N - 1) = K - vec.at(N - 1) + vec.at(0);
  sort(len.begin(), len.end());
  ll ans = 0;
  for (ll i = 0; i < N - 1; i++) {
    ans += len.at(i);
  }
  cout << ans << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define int long long

const int N = 100005;
const int MOD = 1e9 + 7;

// lower_bound - which does not compare less than val
// upper_bound - which compares greater than val
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /* stuff you should look for
   * int overflow, array bounds
   * special cases (n=1?)
   * do smth instead of nothing and stay organized
   */

  int k, n;
  cin >> k >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int mx = arr[0] + k - arr[n - 1];
  // trace1(mx);
  for (int i = 0; i < n - 1; i++) {

    mx = max(mx, arr[i + 1] - arr[i]);
    // trace1(mx);
  }
  cout << k - mx << endl;
}


Error: 0
mx: 10
mx: 10


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define int long long

const int N = 100005;
const int MOD = 1e9 + 7;

// lower_bound - which does not compare less than val
// upper_bound - which compares greater than val
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /* stuff you should look for
   * int overflow, array bounds
   * special cases (n=1?)
   * do smth instead of nothing and stay organized
   */

  int k, n;
  cin >> k >> n;
  int arr[N];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int mx = arr[0] + k - arr[n - 1];
  // trace1(mx);
  for (int i = 0; i < n - 1; i++) {

    // mx=max(mx,arr[i+1]-arr[i]);
    trace1(mx);
  }
  cout << k - mx << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, n;
  cin >> m >> n;
  int a[1000005], maxn = -1;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    maxn = max(a[i + 1] - a[i], maxn);
  }
  maxn = max(m + a[0] - a[n - 1], maxn);
  cout << m - maxn;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, n;
  cin >> m >> n;
  int a[1005], maxn = -1;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    maxn = max(a[i + 1] - a[i], maxn);
  }
  maxn = max(m + a[0] - a[n - 1], maxn);
  cout << m - maxn;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

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

  cout << k - x << endl;

  return 0;
}

Error: 0

Fixed submission:

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

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

  cout << k - x << endl;

  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.at(i);
  }
  vector<int> m(N);
  for (int i = 0; i + 1 < N; i++) {
    m.at(i) = A.at(i + 1) - A.at(i);
  }
  m.at(N - 1) = (K - A.at(N - 1)) + A.at(0);
  sort(m.begin(), m.end());
  int result = 0;
  for (int i = 0; i + 1 < N; i++) {
    result += m.at(i);
  }
  cout << result << endl;
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

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

int main() {
  int K, N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<int> m(N);
  for (int i = 0; i + 1 < N; i++) {
    m.at(i) = A.at(i + 1) - A.at(i);
  }
  m.at(N - 1) = K - A.at(N - 1);
  sort(m.begin(), m.end());
  int result = 0;
  for (int i = 0; i + 1 < N; i++) {
    result += m.at(i);
  }
  cout << result << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
int n;
int alllength;
int length;
int distance;
int maxmum = 0;
int main() {
  scanf("%d", &alllength);
  scanf("%d", &n);
  int array[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &array[i]);
  }
  maxmum = alllength - array[n - 1] + array[0];
  for (int i = 0; i < (n - 1); i++) {
    distance = abs(array[i + 1] - array[i]);
    if (maxmum < distance) {
      maxmum = distance;
    }
  }

  length = (alllength - maxmum);
  printf("%d", length);
}

Error: -11

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
int n;
int alllength;
int length;
int distance;
int maxmum = 0;
int main() {
  scanf("%d", &alllength);
  scanf("%d", &n);
  int array[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", array[i]);
  }
  for (int i = 0; i < (n - 1); i++) {
    distance = abs(array[i + 1] - array[i]);
    if (maxmum < distance) {
      maxmum = distance;
    }
  }
  length = (alllength - maxmum);
  printf("%d", length);
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 10;
int c[N];
int main() {
  int k, n;
  scanf("%d %d", &k, &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  int ans = c[n] - c[1];
  for (int i = 2; i <= n; i++) {
    ans = min(ans, k - c[i] + c[i - 1]);
  }
  printf("%d", ans);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;
int c[N];
int main() {
  int k, n;
  scanf("%d %d", &k, &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  int ans = c[n] - c[1];
  for (int i = 2; i <= n; i++) {
    ans = min(ans, k - c[i] + c[i - 1]);
  }
  printf("%d", ans);
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int k, n, i, max = 0;
  cin >> k >> n;
  vector<int> a(n);
  for (i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (i = 0; i < n - 1; i++) {
    int j = i + 1;
    if (max < a.at(j) - a.at(i)) {
      max = a.at(j) - a.at(i);
    }
  }
  if (k - a.at(n - 1) + a.at(0) > max) {
    max = k - a.at(n - 1) + a.at(0);
  }
  cout << k - max << endl;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int k, n, i, max = 0;
  vector<int> a(n);
  cin >> k >> n;
  for (i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (i = 0; i < n - 1; i++) {
    int j = i + 1;
    if (max < a.at(j) - a.at(i)) {
      max = a.at(j) - a.at(i);
    }
  }
  if (k - a.at(n - 1) + a.at(0) > max) {
    max = k - a.at(n - 1) + a.at(0);
  }
  cout << k - max << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  long long K, N;
  vector<long long> A(N);
  cin >> K >> N;

  for (int i = 0; i < N; i++)
    cin >> A[i];

  int ans = A[N - 1] - A[0];

  for (int i = 0; i < N - 1; i++) {
    if (K - (A[i + 1] - A[i]) < ans)
      ans = K - (A[i + 1] - A[i]);
  }

  cout << ans << endl;
}


Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

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

int main() {

  int K, N;
  vector<int> A(N);
  cin >> K >> N;

  for (int i = 0; i < N; i++)
    cin >> A[i];

  int ans = A[N - 1] - A[0];

  for (int i = 0; i < N - 1; i++) {
    if (K - (A[i + 1] - A[i]) < ans)
      ans = K - (A[i + 1] - A[i]);
  }

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

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

int main() {
  long long k, n;
  cin >> k >> n;
  vector<long long> data(n);
  for (long long i = 0; i < n; i++) {
    cin >> data.at(i);
  }

  long long a = data.at(n - 1) - data.at(0);
  vector<long long> table(1000000, 0);
  table.at(a) = 1;
  for (long long i = 0; i < n - 1; i++) {
    long long sum = k - data.at(i + 1) + data.at(i);
    table.at(sum) = 1;
  }
  for (long long i = 0; i < 1000000; i++) {
    if (table.at(i) == 1) {
      cout << i << endl;
      break;
    }
  }
}


Error: 0

Fixed submission:

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

int main() {
  long long k, n;
  cin >> k >> n;
  vector<long long> data(n);
  for (int i = 0; i < n; i++) {
    cin >> data.at(i);
  }

  int a = data.at(n - 1) - data.at(0);
  vector<long long> table(100100, 0);
  table.at(a) = 1;
  for (int i = 0; i < n - 1; i++) {
    long long sum = k - data.at(i + 1) + data.at(i);
    table.at(sum) = 1;
  }
  for (unsigned int i = 0; i < 100100; i++) {
    if (table.at(i) == 1) {
      cout << i << endl;
      break;
    }
  }
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _DEBUG 1
#define _GLIBCXX_DEBUG

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define dump3(x, y, z)
#define check(s)
#endif

#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

void solve(ll K, ll N, std::vector<ll> A) {
  ll d0 = K + A.at(0) - A.at(N - 1);
  ll max = d0;
  ll sum = d0;
  ll p = A.at(0);
  for (int i = 1; i < N; i++) {
    ll d = A.at(i) - p;
    sum += d;
    if (d > max) {
      max = d;
    }
    p = A.at(i);
  }
  cout << sum - max << endl;
}

int main() {
  ll K;
  scanf("%lld", &K);
  ll N;
  scanf("%lld", &N);
  std::vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(K, N, std::move(A));
  return 0;
}


Error: TLE

Fixed submission:

#define _DEBUG 1
#define _GLIBCXX_DEBUG

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define dump3(x, y, z)
#define check(s)
#endif

#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

void solve(ll K, ll N, std::vector<ll> A) {
  ll min = -1;
  rep(i, N) {
    ll j = i;
    ll p = A.at(j);
    ll d = 0;

    do {
      ll w = A.at(j) - p;
      if (w < 0) {
        w += K;
      }
      d += w;

      p = A.at(j);
      if (++j == N) {
        j = 0;
      }
    } while (j != i);

    if (d < min || min == -1) {
      min = d;
    }
  }
  cout << min << endl;
}

int main() {
  ll K;
  scanf("%lld", &K);
  ll N;
  scanf("%lld", &N);
  std::vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(K, N, std::move(A));
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main(void) {
  int i, K, N, M;
  if ((scanf("%d %d", &K, &N)) == 1)
    ;
  ;
  int A[N];
  for (i = 0; i < N; i++) {
    if ((scanf("%d", &A[i])) == 1)
      ;
    ;
  }
  M = K - A[N - 1] + A[0];
  for (i = 0; i < N - 1; i++) {
    if ((A[i + 1] - A[i]) > M)
      M = A[i + 1] - A[i];
  }
  printf("%d", K - M);
}


Error: WA

Fixed submission:

#include <stdio.h>
int main(void) {
  int i, K, N, M;
  int A[N];
  if ((scanf("%d %d", &K, &N)) == 1)
    ;
  ;
  for (i = 0; i < N; i++) {
    if ((scanf("%d", &A[i])) == 1)
      ;
    ;
  }
  M = K - A[N - 1] + A[0];
  for (i = 0; i < N - 1; i++) {
    if ((A[i + 1] - A[i]) > M)
      M = A[i + 1] - A[i];
  }
  printf("%d", K - M);
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int k, n;
  int sum = 0;

  cin >> k >> n;

  int a[n];

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int max = 0;
  for (int i = n - 1; i > 0; i--) {
    int diff = a[i] - a[i - 1];
    if (max <= diff) {
      max = diff;
    }
  }

  int c = k - a[n - 1] + a[0];
  if (max <= c) {
    max = c;
  }

  cout << k - max << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[1 >> 17];
int main() {
  int k, n;
  int sum = 0;

  cin >> k >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int max = 0;
  for (int i = n - 1; i >= 0; i--) {
    int diff = a[i] - a[i - 1];
    if (max <= diff) {
      max = diff;
    }
  }

  int c = k - a[n - 1] - a[0];
  if (max <= c) {
    max = c;
  }

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

#include <algorithm>
#include <iostream>

using namespace std;

long long a[200002];
long long n, i, k, s;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> k >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  a[n + 1] = k + a[1];
  s = abs(a[1] - a[2]);
  for (i = 2; i < n + 1; i++)
    s = max(s, abs(a[i] - a[i + 1]));
  cout << k - s;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

long long a[100002];
long long n, i, k, s;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> k >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  a[n + 1] = k + a[1];
  s = abs(a[1] - a[2]);
  for (i = 2; i < n + 1; i++)
    s = max(s, abs(a[i] - a[i + 1]));
  cout << s;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded



#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int Km, N;
  cin >> Km >> N;

  vector<int> HOME;
  HOME.resize(N);

  for (int i = 0; i < N; i++) {
    cin >> HOME[i];
  }

  int dis_min = HOME[N - 1] - HOME[0];
  for (int i = 1; i < N; i++) {
    int comparison;
    comparison = HOME[N - 1] - HOME[i];
    if (comparison < dis_min) {
      comparison += HOME[i - 1] + Km - HOME[N - 1];
      if (comparison < dis_min) {
        dis_min = comparison;
      }
    }
  }

  cout << dis_min << endl;

  return 0;
}

Error: TLE

Fixed submission:



#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int Km, N;
  cin >> Km >> N;

  vector<vector<int>> HOME;
  HOME.resize(N);

  HOME[0].resize(2);
  cin >> HOME[0][0]; // Reverse_distance
  for (int i = 1; i < N; i++) {
    HOME[i].resize(2);
    cin >> HOME[i][0];
    HOME[i][1] = HOME[i][0] - HOME[i - 1][0]; // Reverse_distance
  }
  HOME[0][1] = HOME[0][0] + (Km - HOME[N - 1][0]);

  int dis_min = HOME[N - 1][0] - HOME[0][0];
  int comparison;

  for (int i = 1; i < N; i++) { // F
    comparison = HOME[N - 1][0] - HOME[i][0];
    for (int j = 0; j < i; j++) {
      if (comparison > dis_min)
        break;
      comparison += HOME[j][1];
    }
    if (dis_min > comparison) {
      dis_min = comparison;
    }
  }

  cout << dis_min << endl;

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

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  double K, maxDist;
  long N;
  double A[200100];
  cin >> K >> N;

  rep(i, N) cin >> A[i];
  A[N] = A[0] + K;
  maxDist = 0;

  rep(i, N + 1) {
    if (A[i + 1] - A[i] > maxDist) {
      maxDist = A[i + 1] - A[i];
    }
  }
  cout << K - maxDist << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  long K, N, tmp, maxDist;
  long A[20010];
  //     vector<long> A;
  cin >> K >> N;
  /*     rep(i,N){
       cin >> tmp;
       A.push_back(tmp)
   }
   rep(i,N){

   }*/
  rep(i, N) cin >> A[i];
  A[N] = A[0] + K;
  maxDist = 0;
  //    cout << minDist;
  rep(i, N + 1) {
    if (A[i + 1] - A[i] > maxDist) {
      maxDist = A[i + 1] - A[i];
    }
  }
  cout << K - maxDist << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main(void) {

  int K, N;
  cin >> K >> N;
  int A[200100];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int dist = 0;
  int result = 0;
  int max = 0;
  for (int i = 0; i < N - 1; i++) {
    dist = -(A[i] - A[i + 1]);
    if (max < dist) {
      max = dist;
    }
  }
  if (max < K - A[N - 1] + A[0]) {
    max = K - A[N - 1] + A[0];
  }
  result = K - max;
  cout << result << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {

  int64_t K, N;
  cin >> K >> N;
  int64_t A[100100];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int64_t dist = 0;
  int64_t result = 0;
  int64_t max = 0;
  for (int i = 0; i < N - 1; i++) {
    dist = -(A[i] - A[i + 1]);
    if (max < dist) {
      max = dist;
    }
  }
  if (max < K - A[N - 1] + A[0]) {
    max = K - A[N - 1] + A[0];
  }
  result = K - max;
  cout << result << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define pb push_back
#define ss second
#define ff first
#define N 200005
#define inf 1000000009
#define ll long long
#define all(a) a.begin(), a.end()
#define mid(a, b) (a + b) / 2
#define pii pair<int, int>

using namespace std;

int k, n, a[N], d[N], mx;

int main() {
  scanf("%d%d", &k, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    d[i] = a[i] - a[i - 1];
    mx = max(mx, d[i]);
  }
  mx = max(mx, k - a[n] + a[1]);
  cout << k - mx;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define pb push_back
#define ss second
#define ff first
#define N 100005
#define inf 1000000009
#define ll long long
#define all(a) a.begin(), a.end()
#define mid(a, b) (a + b) / 2
#define pii pair<int, int>

using namespace std;

int k, n, a[N], d[N], mx;

int main() {
  scanf("%d%d", &k, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    d[i] = a[i] - a[i - 1];
    mx = max(mx, d[i]);
  }
  mx = max(mx, k - a[n] + a[1]);
  cout << k - mx;
}