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

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

#define FOR(a, b, c) for (int a = b; a <= c; ++a)
#define FORW(a, b, c) for (int a = b; a >= c; --a)
#define fi first
#define se second
#define pb push_back
#define int long long

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int N = 2e5 + 100;
const int oo = 1e8;
const int mod = 1e9 + 7;

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

signed main() {
  //    freopen("test.inp", "r", stdin);
  //    freopen("test.out", "w", stdout);
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin >> k >> n;

  int cur = 0;
  FOR(i, 1, n) {
    cin >> a[i];
    if (i > 1)
      cur += a[i] - a[i - 1];
  }
  ans = cur;
  FOR(i, 2, n) {
    cur -= (a[i] - a[i - 1]);
    if (i == 2)
      cur += k - a[n] + a[1];
    else
      cur += a[i - 1] - a[i - 2];
    ans = min(ans, cur);
  }
  cout << ans;
}


Error: WA

Fixed submission:

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

#define FOR(a, b, c) for (int a = b; a <= c; ++a)
#define FORW(a, b, c) for (int a = b; a >= c; --a)
#define fi first
#define se second
#define pb push_back
#define int long long

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int N = 3e3 + 100;
const int oo = 1e8;
const int mod = 1e9 + 7;

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

signed main() {
  //    freopen("test.inp", "r", stdin);
  //    freopen("test.out", "w", stdout);
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin >> k >> n;

  int cur = 0;
  FOR(i, 1, n) {
    cin >> a[i];
    if (i > 1)
      cur += a[i] - a[i - 1];
  }
  ans = cur;
  FOR(i, 2, n) {
    cur -= (a[i] - a[i - 1]);
    if (i == 2)
      cur += k - a[n] + a[1];
    else
      cur += a[i - 1] - a[i - 2];
    ans = min(ans, cur);
  }
  cout << ans;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define endl "\\n"
#define fx(n) fixed << setprecision(n)
#define mk make_pair

void fast() {
  ios::sync_with_stdio(NULL);
  cout.tie(NULL);
  cin.tie(NULL);
}
void File() {
#ifndef ONLINE_JUDGE
  freopen("output.txt", "w", stdout);
  freopen("input.txt", "r", stdin);
#endif
#ifdef ONLINE_JUDGE
  /*freopen("output.txt", "w", stdout);
  freopen("story.in", "r", stdin);*/
#endif
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e5 + 15;
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};

int main() {
  fast();
  // File();
  int n, m;
  cin >> m >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  sort(all(arr));
  ll sum = 0;
  vector<int> res;
  for (int i = 0; i < n - 1; i++) {
    res.push_back(arr[i + 1] - arr[i]);
  }
  res.push_back(m - arr[n - 1] + arr[0]);
  sort(all(res));
  for (int i = 0; i < n - 1; i++)
    sum += res[i];
  cout << sum << endl;
  return 0;
}

Error: WA

Fixed submission:

#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define endl "\\n"
#define fx(n) fixed << setprecision(n)
#define mk make_pair

void fast() {
  ios::sync_with_stdio(NULL);
  cout.tie(NULL);
  cin.tie(NULL);
}
void File() {
#ifndef ONLINE_JUDGE
  freopen("output.txt", "w", stdout);
  freopen("input.txt", "r", stdin);
#endif
#ifdef ONLINE_JUDGE
  /*freopen("output.txt", "w", stdout);
  freopen("story.in", "r", stdin);*/
#endif
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e5 + 15;
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};

int main() {
  fast();
  File();
  int n, m;
  cin >> m >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  sort(all(arr));
  ll sum = 0;
  vector<int> res;
  for (int i = 0; i < n - 1; i++) {
    res.push_back(arr[i + 1] - arr[i]);
  }
  res.push_back(m - arr[n - 1] + arr[0]);
  sort(all(res));
  for (int i = 0; i < n - 1; i++)
    sum += res[i];
  cout << sum << 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[i];
  }

  vector<int> d(n);
  d[0] = max(A[1] - A[0], A[2] - A[1]);

  for (int j = 1; j < n - 2; j++) {
    d[j] = max(d[j - 1], A[j + 2] - A[j + 1]);
  }

  d[n - 2] = max(d[n - 3], k - A[n - 1] + A[0]);

  cout << k - d[n - 2] << 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;

  // 100要素の配列で初期化
  vector<int> A(N);

  // 100個の入力を受け取る
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  vector<int> B(N);

  for (int j = 0; j < N - 1; j++) {
    B.at(j) = A.at(j + 1) - A.at(j);
  }

  B.at(N - 1) = K - A.at(N - 1);

  int max;

  for (int k = 0; k < N - 1; k++) {
    if (B.at(k) >= B.at(k + 1)) {
      max = B.at(k);
    } else {
      max = B.at(k + 1);
    }

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

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int K, N;
  cin >> K >> N;
  vector<int> A(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  vector<int> D(N, 0);
  int dmax = 0;
  int total = 0;
  for (int i = 0; i < N - 1; i++) {
    D[i] = A[i + 1] - A[i];
    if (D[i] > dmax)
      dmax = D[i];
    total += D[i];
  }
  D[N - 1] = K - A[N - 1] + A[0];
  total += D[N - 1];
  if (D[N - 1] > dmax)
    dmax = D[N - 1];
  int ans = total - dmax;
  cout << ans << endl;
}

Error: -11

Fixed submission:

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int K, N;
  vector<int> A;
  cin >> K, N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  vector<int> D;
  int dmax = 0;
  int total = 0;
  for (int i = 0; i < N - 1; i++) {
    D[i] = A[i + 1] - A[i];
    if (D[i] > dmax)
      dmax = D[i];
    total += D[i];
  }
  D[N - 1] = K - A[N - 1];
  total += D[N - 1];
  if (D[N - 1] > dmax)
    dmax = D[N - 1];
  int ans = total - dmax;
  cout << ans << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(int argc, const char *argv[]) {
  uint64_t K, N;
  cin >> K;
  cin >> N;

  vector<uint64_t> A;
  for (uint64_t i = 0; i < N; i++) {
    uint64_t tmp;
    cin >> tmp;
    A.push_back(tmp);
  }

  uint64_t max_dist = 0;
  for (uint64_t i = 0; i < N - 1; i++) {
    uint64_t dist = A[i + 1] - A[i];
    max_dist = max(max_dist, dist);
  }
  max_dist = max(max_dist, A[0] - A[N - 1] + K);
  uint64_t ans = K - max_dist;
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

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

int main(int argc, const char *argv[]) {
  uint64_t K, N;
  cin >> K;
  cin >> N;

  vector<uint64_t> A;
  for (uint64_t i = 0; i < N; i++) {
    uint64_t tmp;
    cin >> tmp;
    A.push_back(tmp);
  }

  uint64_t i = 0;
  uint64_t ans = UINT64_MAX;
  const uint64_t max_idx = A.size() - 1;
  do {
    uint64_t tmp = 0;
    uint64_t count;
    uint64_t idx0;
    uint64_t idx1;

    // 時計回り。
    count = 0;
    idx0 = (i <= max_idx) ? i : 0;
    idx1 = (idx0 + 1 <= max_idx) ? idx0 + 1 : 0;
    while (count < A.size() - 1) {
      int64_t a0 = (int64_t)A[idx0];
      int64_t a1 = (int64_t)A[idx1];
      uint64_t t = (a1 - a0 >= 0) ? a1 - a0 : K + a1 - a0;
      tmp += t;

      idx0 = (idx0 + 1 <= max_idx) ? idx0 + 1 : 0;
      idx1 = (idx0 + 1 <= max_idx) ? idx0 + 1 : 0;
      count++;
    }
    ans = min(ans, tmp);
#if 0
        // 反時計回り。
        count = 0;
        idx0 = (i <= 0) ? i : max_idx;
        idx1 = (idx0 - 1 <= 0) ? idx0 - 1 : max_idx;
        while(count < A.size()-1) {
            int64_t a0 = (int64_t)A[idx0];
            int64_t a1 = (int64_t)A[idx1];
            uint64_t t = (a1 - a0 >= 0) ? a1 - a0 : K + a1 - a0;
            tmp += t;
            
            idx0 = (idx0 - 1 <= 0) ? idx0 - 1 : max_idx;
            idx1 = (idx0 - 1 <= 0) ? idx0 - 1 : max_idx;
            count++;
        }
        ans = min(ans, tmp);
#endif
    i++;
  } while (i < A.size());

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  scanf("%d%d", &k, &n);
  int ar[200000 + 5];
  for (int i = 0; i < n; i++)
    scanf("%d", &ar[i]);
  sort(ar, ar + n);
  int ans = INT_MIN;
  for (int i = 0; i < n - 1; i++) {
    if (ans < ar[i + 1] - ar[i])
      ans = ar[i + 1] - ar[i];
  }
  ans = max(ans, k - ar[n - 1] + ar[0]);
  printf("%d\\n", k - ans);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  int n = a, k = b;
  int ar[200000 + 5];
  for (int i = 0; i < a; i++)
    scanf("%d", &ar[i]);
  sort(ar, ar + a);
  int ans = INT_MIN;
  for (int i = 0; i < a - 1; i++) {
    if (ans < ar[i + 1] - ar[i])
      ans = ar[i + 1] - ar[i];
  }
  ans = max(ans, k - ar[a - 1] + ar[0]);
  printf("%d\\n", k - ans);
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  int k, n, a[200050], b[200050];
  scanf("%d%d", &k, &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  int maxn = 0;
  for (int i = 1; i < n; i++) {
    b[i] = a[i] - a[i - 1];

    maxn = max(maxn, b[i]);
  }
  maxn = max(maxn, a[0] + k - a[n - 1]);
  // printf("%d",maxn);
  printf("%d", k - maxn);
}

Error: WA

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int main() {
  int k, n, a[100050], b[100050];
  scanf("%d%d", &k, &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  int s = 0, maxn = 0;
  for (int i = 1; i < n; i++) {
    b[i] = a[i] - a[i - 1];
    maxn = max(maxn, b[i]);
  }
  maxn = max(maxn, a[0] + k - a[n - 1]);
  printf("%d", k - maxn);
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int k, n, distance = 0;
  cin >> k >> n;
  vector<int> a(n + 100);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);
  for (int i = 0; i < n - 1; i++) {
    if (a.at(i + 1) - a.at(i) > distance)
      distance = a.at(i + 1) - a.at(i);
  }
  if (a.at(0) + k - a.at(n - 1) > distance)
    distance = a.at(0) + k - a.at(n - 1);

  cout << k - distance;
  return 0;
}

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


Fixed submission:

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

int main() {
  int k, n, distance = 0;
  vector<int> a(n + 100);
  cin >> k >> n;
  for (int i = 0; i < n; i++)
    cin >> a.at(i);
  for (int i = 0; i < n - 1; i++) {
    if (a.at(i + 1) - a.at(i) > distance)
      distance = a.at(i + 1) - a.at(i);
  }
  if (a.at(0) + k - a.at(n - 1) > distance)
    distance = a.at(0) + k - a.at(n - 1);

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

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

#define roundup(n, d) (((n) + ((d)-1)) / (d))
#define ll long long

using namespace std;

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

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

  sort(a.begin(), a.end(), greater<int>());

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

  cout << k - maxlen << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

#define roundup(n, d) (((n) + ((d)-1)) / (d))
#define ll long long

using namespace std;

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

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

  // v: 被りのないa
  vector<int> v = a;
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());

  int ans = INT32_MAX;
  for (int i = 0; i < v.size(); i++) {
    int addval = v[i];
    int valmin = INT32_MAX;
    int valmax = INT32_MIN;
    for (int j = 0; j < n; j++) {
      int value = (a[j] + addval) % k;
      valmax = max(valmax, value);
      valmin = min(valmin, value);
    }
    ans = min(ans, valmax - valmin);
    // cout << "fir: " << *val.first << "sec: " << *val.second <<"ans:
    // "<<*val.second - *val.first << endl;
  }
  cout << ans;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define MAXN 200000

int main() {
  int k, n, distance, mindis;
  int a[MAXN];

  scanf("%d %d", &k, &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

  for (int i = 0; i < n; i++) {
    if (i)
      distance = k - a[i] + a[i - 1];
    else
      distance = a[n - 1] - a[i];
    if (!i || distance < mindis)
      mindis = distance;
  }
  printf("%d", mindis);

  return 0;
}


Error: WA

Fixed submission:

#include <stdio.h>
#define MAXN 100

int main() {
  int k, n, distance, mindis;
  int a[MAXN];

  scanf("%d %d", &k, &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

  for (int i = 0; i < n; i++) {
    if (i)
      distance = 20 - (a[i] - a[i - 1]);
    else
      distance = 20 - (a[i] + (20 - a[n - 1]));
    if (!i || distance < mindis)
      mindis = distance;
  }
  printf("%d", mindis);

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

#include <bits/stdc++.h>
#define int long long
#define mod 1e18 + 10
#define mx 1000002
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
int a[2000005];
int32_t main() {
  int n, k, i;
  int ans = mod;
  cin >> n >> k;
  for (i = 1; i <= k; i++)
    cin >> a[i];
  for (i = 2; i <= k; i++)
    ans = min(ans, n - a[i] + a[i - 1]);
  ans = min(ans, n - (n + a[1] % n - a[k]));
  // cout<<"dist is "<<n-(n+a[0]%n-a[k])<<"\\n";
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
#define mx 1000002
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
int a[200005];
int32_t main() {
  int n, k, i;
  int ans = mod;
  cin >> n >> k;
  for (i = 0; i < k; i++)
    cin >> a[i];

  for (i = 1; i < k; i++) {
    ans = min(ans, n - a[i] + a[i - 1]);
  }
  ans = min(ans, n - a[n - 1] + a[0]);
  cout << ans << "\\n";
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  int k, n;
  cin >> k >> n;
  vector<int> a(2 * n);
  rep(i, n) cin >> a[i];
  rep(i, n) a[i + n] = a[i] + k;
  int answer = k;
  rep(i, n) {
    if (a[i + n - 1] - a[i] < answer)
      answer = a[i + n - 1] - a[i];
  }
  cout << answer << endl;
}

Error: 0

Fixed submission:

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

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

/******************************************
/@Author: LeafBelief
/@Date: 2020-04-07
/@Remark:
/@FileName: minumdistance
******************************************/
#include <bits/stdc++.h>
#define CSE(x, y) memset(x, y, sizeof(x))
#define lowbit(x) (x & (-x))
#define INF 0x3f3f3f3f
#define FAST                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;

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

const int maxn = 211111;
ll dis[maxn];

int main() {
  FAST;
  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> dis[i];
  }
  ll all = 0, mxdis = 0;
  for (int i = 1; i < n; i++) {
    all += dis[i] - dis[i - 1];
    mxdis = max(mxdis, dis[i] - dis[i - 1]);
  }
  all += k - dis[n - 1] + dis[0] - 0;
  mxdis = max(mxdis, k - dis[n - 1] + dis[0] - 0);
  cout << all - mxdis << endl;
  return 0;
}

Error: 0

Fixed submission:

/******************************************
/@Author: LeafBelief
/@Date: 2020-04-07
/@Remark:
/@FileName: minumdistance
******************************************/
#include <bits/stdc++.h>
#define CSE(x, y) memset(x, y, sizeof(x))
#define lowbit(x) (x & (-x))
#define INF 0x3f3f3f3f
#define FAST                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;

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

const int maxn = 111111;
ll dis[maxn];

int main() {
  FAST;
  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> dis[i];
  }
  ll all = 0, mxdis = 0;
  for (int i = 1; i < n; i++) {
    all += dis[i] - dis[i - 1];
    mxdis = max(mxdis, dis[i] - dis[i - 1]);
  }
  all += k - dis[n - 1] + dis[0] - 0;
  mxdis = max(mxdis, k - dis[n - 1] + dis[0] - 0);
  cout << all - mxdis << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
ll qpow(ll a, ll b) {
  ll res = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int a[maxn];
int main() {
  int n, k;
  scanf("%d%d", &k, &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  int ans = 0;
  ans = a[n] - a[1];
  for (int i = 2; i <= n; ++i) {
    int tmp = a[i - 1] + k - a[i];
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
ll qpow(ll a, ll b) {
  ll res = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int a[maxn];
int main() {
#ifndef ONLINE_JUDGE
  freopen("simple.in", "r", stdin);
  freopen("simple.out", "w", stdout);
#endif
  int n, k;
  scanf("%d%d", &k, &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    int tmp = min(a[i] - a[1], k - a[i] + a[1]);
    int tmp2 = min(a[i] - a[i - 1], k - a[i] + a[i - 1]);
    ans = max(ans, max(tmp, tmp2));
  }
  cout << ans << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <memory>
#include <string>
#include <vector>
typedef long long ll;

int main() {
  int k, n;
  std::vector<int> A;
  std::cin >> k >> n;
  for (int i = 0; i < n; i++) {
    int a;
    std::cin >> a;
    A.emplace_back(a);
  }
  std::vector<int> backet;
  for (int i = 0; i < k; i++) {
    backet.emplace_back(0);
  }
  for (auto iter = A.begin(); iter != A.end(); iter++) {
    backet[*iter]++;
  }
  int c = 0;
  for (int i = 0; i < n; i++) {
    while (1) {
      if (backet[c] != 0) {
        A[i] = c;
        backet[c]--;
        break;
      }
      c++;
    }
  }
  int max = 0;
  for (auto iter = A.begin(); iter != A.end() - 1; iter++) {
    if (max < *(iter + 1) - *iter) {
      max = *(iter + 1) - *iter;
    }
  }
  if (max < k - A[n - 1] + A[0]) {
    max = k - A[n - 1] + A[0];
  }
  std::cout << k - max << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
void swap(int *a, int *b) {
  int t = *a;
  *a = *b;
  *b = t;
}

int main() {
  int k;
  int n;
  cin >> k >> n;
  std::vector<int> A;
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    A.emplace_back(m);
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < n - 1 - i; j++) {
      if (A[j] > A[j + 1]) {
        swap(&A[0] + j, &A[0] + j + 1);
      }
    }
  }
  int max = 0;
  for (int i = 0; i < n - 1; i++) {
    if (A[i + 1] - A[i] > max) {
      max = A[i + 1] - A[i];
    }
  }
  if (k - A[n - 1] + A[0] > max) {
    max = k - A[n - 1] + A[0];
  }
  std::cout << k - max << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int mod = 1007050321;
const int base = 521;
const int N = 4e5 + 5;
const int MOD = 1e9 + 7;
const int inf = 2e9;

int n, K, a[N];

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

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

  int ans = inf;
  for (int i = 1; i <= n; ++i)
    ans = min(ans, a[i + n - 1] - a[i]);

  cout << ans;
  return 0;
}

/// a[i] -> a[n] -> a[i] -> a[1]
/// a[n] - a[i-1] + a[n] - a[i-1] + a[i] - a[1]

/// a[i] -> a[n] -> a[1] -> a[i-1]
/// a[n] - a[i-1] + K - a[n] + a[1] + a[i-1] - a[1]


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int mod = 1007050321;
const int base = 521;
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
const int inf = 2e9;

int n, K, a[N];

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

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

  int ans = inf;
  for (int i = 1; i <= n; ++i)
    ans = min(ans, a[i + n - 1] - a[i]);

  cout << ans;
  return 0;
}

/// a[i] -> a[n] -> a[i] -> a[1]
/// a[n] - a[i-1] + a[n] - a[i-1] + a[i] - a[1]

/// a[i] -> a[n] -> a[1] -> a[i-1]
/// a[n] - a[i-1] + K - a[n] + a[1] + a[i-1] - a[1]
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);
  vector<int> P(N);

  for (int i = 0; i < N; ++i) {
    cin >> A.at(i);
    P.at(i) = i;
  }

  int dist = 0, dist_sum = 0;
  int max_d = 0;

  const int left0 = abs(A.at(N - 1) - A.at(0));
  dist = min(left0, K - left0);
  dist_sum += dist;
  max_d = max(max_d, dist);

  for (int j = 1; j < N; ++j) {
    const int left = abs(A.at(j) - A.at(j - 1));
    dist = min(left, K - left);
    dist_sum += dist;
    max_d = max(max_d, dist);
  }

  cout << dist_sum - max_d << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int K, N;

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

  for (int i = 0; i < N; ++i) {
    cin >> A.at(i);
    P.at(i) = i;
  }
  int dist_min = numeric_limits<int>::max();

  do {
    int prev = *P.begin();
    int dist = 0;
    for (int now : P) {
      const int left = abs(A.at(now) - A.at(prev));
      dist += min(left, K - left);
      prev = now;
    }
    dist_min = min(dist, dist_min);
  } while (next_permutation(P.begin(), P.end()));

  cout << dist_min << endl;
}