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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define MOD 1000000007
#define A first
#define B second
#define all(x) (x).begin(), (x).end

typedef long long ll;

inline int mulmd(ll a, ll b) {
  ll ret = (a * b) % MOD;
  return (int)ret;
}

inline int power(ll x, ll y, int m) {
  ll res = 1;
  x = x % m;
  while (y > 0) {
    if (y & 1) {
      res = mulmd(res, x);
    }
    y = y >> 1;
    x = mulmd(x, x);
  }
  return (int)res;
}

inline int submd(ll a, ll b) {
  ll ret = (a - b);
  if (ret < 0)
    ret += MOD;
  return (int)ret;
}

inline int addmd(ll a, ll b) {
  ll ret = (a + b) % MOD;
  return (int)ret;
}

inline int invPow(ll a) { return power(a, MOD - 2, MOD); }

inline int divmd(ll a, ll b) {
  ll ret = mulmd(a, invPow(b));
  return (int)ret;
}

const int N = 2e5 + 5;

int arr[N];

void solve() {
  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  vector<int> diff;
  for (int i = 1; i < n; i++) {
    diff.push_back(arr[i] - arr[i - 1]);
  }
  diff.push_back(k - arr[n - 1] + arr[0]);
  sort(diff.begin(), diff.end());
  int ans = k - diff[diff.size() - 1];
  cout << ans;
}

int main() {
  int t;
  t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define MOD 1000000007
#define A first
#define B second
#define all(x) (x).begin(), (x).end

typedef long long ll;

inline int mulmd(ll a, ll b) {
  ll ret = (a * b) % MOD;
  return (int)ret;
}

inline int power(ll x, ll y, int m) {
  ll res = 1;
  x = x % m;
  while (y > 0) {
    if (y & 1) {
      res = mulmd(res, x);
    }
    y = y >> 1;
    x = mulmd(x, x);
  }
  return (int)res;
}

inline int submd(ll a, ll b) {
  ll ret = (a - b);
  if (ret < 0)
    ret += MOD;
  return (int)ret;
}

inline int addmd(ll a, ll b) {
  ll ret = (a + b) % MOD;
  return (int)ret;
}

inline int invPow(ll a) { return power(a, MOD - 2, MOD); }

inline int divmd(ll a, ll b) {
  ll ret = mulmd(a, invPow(b));
  return (int)ret;
}

const int N = 1e5 + 5;

int arr[N];

void solve() {
  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  vector<int> diff;
  for (int i = 1; i < n; i++) {
    diff.push_back(arr[i] - arr[i - 1]);
  }
  diff.push_back(k - arr[n - 1] + arr[0]);
  sort(diff.begin(), diff.end());
  int ans = k - diff[diff.size() - 1];
  cout << ans;
}

int main() {
  int t;
  t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long k;
  cin >> k;
  long long n;
  cin >> n;
  long long A[n];
  long long R[n];
  for (long long i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (long long i = 0; i < n - 1; i++) {
    R[i] = abs(A[i] - A[i + 1]);
  }
  R[n - 1] = abs(k - A[n - 1] + A[0]);

  /*for(long long i=0;i<n-1;i++){
    for(long long j=i+1;j<n;j++){
      if(R[i]>R[j]){
        swap(R[i],R[j]);
      }
    }
  }*/
  /*for(int i=0;i<n;i++){
    cout<<R[i]<<endl;
  }*/
  long long sum = 0;
  /*for(long long i=0;i<n-1;i++){
    sum+=R[i];
  }*/
  long long max = 0;
  for (long long i = 1; i < n; i++) {
    if (R[max] < R[i]) {
      max = i;
    }
  }
  for (long long i = 0; i < n; i++) {
    if (i != max) {
      sum += R[i];
    }
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long k;
  cin >> k;
  long long n;
  cin >> n;
  long long A[n];
  for (long long i = 0; i < n; i++) {
    cin >> A[i];
  }
  // long long N=n+1;
  long long R[n];
  for (long long i = 0; i < n - 1; i++) {
    R[i] = abs(A[i] - A[i + 1]);
  }
  R[n - 1] = abs(k - A[n - 1] + A[0]);

  for (long long i = 0; i < n - 1; i++) {
    for (long long j = i + 1; j < n; j++) {
      if (R[i] > R[j]) {
        swap(R[i], R[j]);
      }
    }
  }

  /*for(int i=0;i<n;i++){
    cout<<R[i]<<endl;
  }*/
  long long sum = 0;
  for (long long i = 0; i < n - 1; i++) {
    sum += R[i];
  }
  cout << sum << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

// Best practice

#include <bits/stdc++.h>

#define int long long
#define pb push_back
#define pf emplace_front
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pii pair<int, int>
#define psi pair<string, int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vi>
#define sz(x) (int)(x).size()
#define x first
#define y second
#define endl '\\n'
#define tezz                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define MOD 1000000007
#define hell 998244353
#define output(x) cout << (x ? "YES" : "NO") << endl;
using namespace std;

// Code from Here
// -------------------------------------------------------------------------------

signed main() {
  tezz int p, n;
  cin >> p >> n;
  int dis = 0;
  vi a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i != 0) {
      dis = max(dis, a[i] - a[i - 1]);
    }
  }
  dis = max(dis, a[0] + p - a[n - 1]);
  cout << p - dis;

  return 0;
}


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


Fixed submission:

// Best practice

#include <bits/stdc++.h>

#define int long long
#define pb push_back
#define pf emplace_front
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pii pair<int, int>
#define psi pair<string, int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vi>
#define sz(x) (int)(x).size()
#define x first
#define y second
#define endl '\\n'
#define tezz                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define MOD 1000000007
#define hell 998244353
#define output(x) cout << (x ? "YES" : "NO") << endl;
using namespace std;

// Code from Here
// -------------------------------------------------------------------------------

signed main() {
  tezz

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

  int p, n;
  cin >> p >> n;
  int dis = 0;
  vi a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i != 0) {
      dis = max(dis, a[i] - a[i - 1]);
    }
  }
  dis = max(dis, a[0] + p - a[n - 1]);
  cout << p - dis;

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

#include <bits/stdc++.h>
#include <iostream>

using namespace std;
int main() {
  long long int n, k, i, ans = 0, dis = 0;
  cin >> n >> k;
  long long int a[k + 1];
  for (i = 0; i < k; i++) {
    cin >> a[i];
  }

  for (i = 0; i < k - 1; i++) {
    if ((a[i + 1] - a[i]) > dis) {
      dis = a[i + 1] - a[i];
    }
    if (i == k - 2 && dis < (n - a[k - 1] + a[0]))
      dis = n - a[k - 1] + a[0];
  }
  cout << n - dis << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int i = 0, j = 0, k = 0, arr[100005], n = 0, large = 0, small = 0, a = 0,
      b = 0;
  cin >> k;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  large = arr[1];
  for (i = 1; i <= n; i++) {
    if (arr[i] > large) {
      large = arr[i];
    }
  }
  small = large;
  for (i = 1; i <= n; i++) {
    if (arr[i] == 0) {

    } else {
      if (arr[i] < small) {
        small = arr[i];
      }
    }
  }

  a = large - small;
  b = (k + small) - large;
  if (a >= b) {
    cout << b;
  } else {
    cout << a;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x

int main() {

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

  ll k, n;
  cin >> k >> n;
  ll a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ll ans = LLONG_MIN;
  for (int i = 0; i < n; ++i) {
    if (i == n - 1)
      ans = max(k - a[i] + a[0], ans);
    else
      ans = max(a[i + 1] - a[i], ans);
  }
  cout << k - ans;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>

int max(std::vector<int> &a) {
  int m = a[0];
  for (int i = 1; i < a.size(); i++)
    if (a[i] > m)
      m = a[i];
  return m;
}

int main() {
  int k, n;
  std::vector<int> a(n);
  std::cin >> k >> n;
  for (int i = 0; i < n; i++)
    std::cin >> a[i];
  std::vector<int> v(n);
  for (int i = 1; i < n; i++)
    v[i - 1] = a[i] - a[i - 1];
  v[n - 1] = k - a[n - 1] + a[0];
  std::cout << max(v);
  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 = k - l;
  cout << ans << endl;
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)


Fixed submission:

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

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

  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  if (vec.at(N) - vec.at(0) <= (K / 2)) {
    cout << K / 2 << endl;
  } else {
    cout << K / 2 << endl;
  }
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  b[0] = k - a[n] + a[1];

  int max = 0;

  for (int i = 0; i < n; i++) {
    if (b[i] > max) {
      max = b[i];
    }
  }

  int ans = 0 - max;

  for (int i = 0; i < n; i++) {
    ans += b[i];
  }

  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  int K = 0;
  int N = 0;
  cin >> K, N;

  vector<int> a;
  vector<int> b;

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

  b[0] = K - a[i] + a[0];
  int max = b[0];

  int log = 0;
  for (i = 1; i < N; i++) {
    b[i] = a[i] - a[i - 1];
    if (max < b[i]) {
      max = b[i];
      log = i;
    }
  }

  if (max == b[0]) {
    cout << K - b[0] << endl;
  } else {
    cout << K - max << endl;
  }
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

int Max(vector<int> x) {
  if (x.size() == 1) {
    return x.at(0);
  }
  auto itr1 = x.begin();
  auto itr2 = itr1 + 1;
  *itr1 = max(*itr1, *itr2);
  x.erase(itr2);
  return Max(x);
}

int main() {
  int K, N;
  cin >> K >> N;
  vector<int> a(N);
  int b;
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  b = K - a.at(N - 1) + a.at(0);
  for (int i = 0; i < N - 1; i++) {
    b = max(b, a.at(i + 1) - a.at(i));
  }
  // b.at(N-1)=K-a.at(N-1)+a.at(0);
  cout << K - b;
}


Error: WA

Fixed submission:

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

int Max(vector<int> x) {
  if (x.size() == 1) {
    return x.at(0);
  }
  auto itr1 = x.begin();
  auto itr2 = itr1 + 1;
  *itr1 = max(*itr1, *itr2);
  x.erase(itr2);
  return Max(x);
}

int main() {
  int K, N;
  cin >> K >> N;
  vector<int> a(N);
  vector<int> b(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < N - 1; i++) {
    b.at(i) = a.at(i + 1) - a.at(i);
  }
  b.at(N - 1) = K - a.at(N - 1) + a.at(0);
  cout << K - Max(b);
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int vet[200005];

int main() {
  int k, n;
  cin >> k >> n;
  for (int i = 1; i <= n; i++)
    cin >> vet[i];

  int minimo = (vet[n] - vet[1]);

  for (int i = 2; i <= n; i++)
    minimo = min(minimo, vet[i - 1] + k - vet[i]);

  cout << minimo << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int vet[200005];

int main() {
  int k, n;
  cin >> k >> n;
  for (int i = 1; i <= n; i++)
    cin >> vet[i], vet[i + n] = vet[i] + k;

  int minimo = 10000000;

  for (int i = 1; i <= n; i++) {
    minimo = min(minimo, vet[i - 1] + k - vet[i]);
  }

  cout << minimo << endl;

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

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

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int k, n;
  int max = 0;
  cin >> k >> n;
  int i;
  vector<int> a(200000);
  for (i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (i = 0; i < n; i++) {
    if (max < (a.at(i + 1) - a.at(i))) {
      max = (a.at(i + 1) - a.at(i));
    }
  }
  if (max < (k - a.at(n - 1) + a.at(0))) {
    max = (k - a.at(n - 1) + a.at(0));
  }
  cout << k - max << "\\n";
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main(void) {
  int k, n;
  int max = 0, m = 0;
  int a[200000] = {0};
  int b[200001] = {0};

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

  for (int i = 0; i < n; i++) {
    if (i == 0)
      b[i] = a[i];
    else
      b[i] = a[i] - a[i - 1];

    if (max <= b[i]) {
      max = b[i];
      m = i;
    }
  }

  b[n] = a[0] + (k - a[n - 1]);
  if (max <= b[n])
    m = n;

  cout << k - b[m] << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main(void) {
  int k, n, e;
  int max = 0, m = 0;
  int a[200001] = {0};

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

  cout << k - a[m] << "\\n";

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

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

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

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long int k, n, a[20000], min;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  min = a[n - 1] - a[0];
  for (int i = 0; i < n - 1; i++) {
    if (k - (a[i + 1] - a[i]) < min) {
      min = k - (a[i + 1] - a[i]);
    }
  }
  cout << min << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int k, n, c, tmpn;
  cin >> k >> n;
  tmpn = n;
  vector<int> a(n);
  rep(i, 0, n) { cin >> a[i]; }

  long long int maxd, ans = 0;
  maxd = a[1] - a[0];
  ans += maxd;
  rep(i, 1, n) {
    long long int tmpmaxd;
    if (i != n - 1) {
      tmpmaxd = a[i + 1] - a[i];
      ans += tmpmaxd;
    } else {
      tmpmaxd = (k - a[i]) + a[0];
      ans += tmpmaxd;
    }
    maxd = max(maxd, tmpmaxd);
    // cout<<ans<<endl;
  }

  cout << ans - maxd << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int k, n, c, tmpn;
  cin >> k >> n;
  tmpn = n;
  vector<int> a(n);
  rep(i, 0, n) { cin >> a[i]; }

  long long int ans = 0, ans1 = 0, ans2 = 0;
  c = n / 2;
  rep(i, c, n) {
    if (i + 1 < n) {
      ans1 += abs(a[i + 1] - a[i]);
    } else {
      ans1 += abs(a[i - n] + (k - a[i]));
      n -= i;
      i = 0;
    }
    // cout<<ans1<<endl;
  }
  int m = 0;
  n = tmpn;
  for (int i = c; i >= m; i--) {
    if (i != 0) {
      ans2 += abs(a[i] - a[i - 1]);
    } else {
      ans2 += abs((k - a[n - 1]) + a[i]);
      m = c + 1;
    }
    // cout<<ans2<<endl;
  }

  ans = min(ans1, ans2);

  int cnt = 0;
  if (ans1 < ans2) {
    while (cnt < n) {
      long long int tmpans = 0;
      int l = cnt;
      rep(i, 0, n) {
        if (l + 1 < n) {
          tmpans += abs(a[l + 1] - a[l]);
        } else {
          tmpans += abs(a[l - n] + (k - a[l]));
          l = -1;
        }
        l++;
      }
      ans = min(ans, tmpans);
      cnt++;
    }
  }

  cnt = n - 1;
  if (ans1 > ans2) {
    while (cnt >= 0) {
      m = 0;
      long long int tmpans = 0;
      int j = cnt;
      rep(i, 0, n) {
        if (j != 0) {
          tmpans += abs(a[j] - a[j - 1]);
        } else {
          tmpans += abs((k - a[n - 1]) + a[j]);
          m = cnt;
          j = n;
        }
        j--;
      }
      ans = min(ans, tmpans);
      cnt--;
    }
  }

  cout << ans << endl;

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

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

vector<int> a;
int solve(int k, int n) {
  int maxValue = 0;
  for (int i = 1; i < n; i++) {
    int diff = a[i] - a[i - 1];
    maxValue = max(maxValue, diff);
  }
  maxValue = max(maxValue, k - (a[n - 1] - a[0]));
  return k - maxValue;
}

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

  int k;
  int n;

  cin >> k;
  cin >> n;
  a.resize(n);
  rep(i, n) { cin >> a[i]; }

  sort(a.begin(), a.end());

  cout << solve(k, n) << endl;
  return 0;
}

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


Fixed submission:

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

int solve(int k, int n, vector<int> &a) {
  int maxValue = 0;
  for (int i = 1; i < n; i++) {
    int diff = a[i] - a[i - 1];
    maxValue = max(maxValue, diff);
  }
  maxValue = max(maxValue, k - (a[n - 1] - a[0]));
  return k - maxValue;
}

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

  int k;
  int n;
  vector<int> a(n);

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

  sort(a.begin(), a.end());

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>

#define MAX_N 200'000

using namespace std;

int A[MAX_N];
int D[MAX_N];

int main() {
  int K, N;
  cin >> K >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  int max_d = 0;
  int max_idx = 0;
  for (int i = 0; i < N - 1; ++i) {
    D[i] = min(abs(A[i + 1] - A[i]), abs(A[i] + (K - A[i + 1])));
    if (max_d < D[i]) {
      max_d = D[i];
      max_idx = i;
    }
  }
  D[N - 1] = min(abs(A[N - 1] - A[0]), abs(A[0] + (K - A[N - 1])));
  if (max_d < D[N - 1]) {
    max_idx = N - 1;
  }

  int res = 0;
  for (int i = 0; i < N; ++i) {
    if (i != max_idx) {
      res += D[i];
    }
  }
  printf("%d\\n", res);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>

#define MAX_N 200'000
#define INF 1'000'000'000

using namespace std;

int A[MAX_N];
int D[MAX_N];

int main() {
  int K, N;
  cin >> K >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  for (int i = 0; i < N - 1; ++i) {
    D[i] = min(abs(A[i + 1] - A[i]), abs(A[i] + (K - A[i - 1])));
  }
  D[N - 1] = min(abs(A[N - 1] - A[0]), abs(A[0] + (K - A[N - 1])));

  int res = INF;
  for (int i = 0; i < N; ++i) {
    int d = 0;
    int j = i;
    int k = (j + 1) % N;
    while (k != i) {
      d += k - j == 1 ? D[j] : D[N - 1];
      j = k;
      k = (j + 1) % N;
    }
    res = min(res, d);
    d = 0;
    j = i;
    k = (j - 1 + N) % N;
    while (k != i) {
      d += k - j == -1 ? D[k] : D[N - 1];
      j = k;
      k = (j - 1 + N) % N;
    }
    res = min(res, d);
  }
  printf("%d\\n", res);
}