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

// C
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
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.at(i);
  }
  sort(a.begin(), a.end());
  int result = k;
  int b = a.at(0) + k - a.at(n - 1);
  result -= b;
  for (int i = 0; i < n - 1; i++) {
    result = min(result, k - a.at(i + 1) + a.at(i));
  }
  cout << result << endl;
}


Error: WA

Fixed submission:

// C
#include <iostream>
#include <vector>

using namespace std;

int search(vector<int> place, int start, int min,
           int around) { // 場所一覧,開始地点(配列の位置),打ち切り用最小値,周長
  int resultDist = 0;
  int pos = start;               // 配列内の位置 最初はstart
  vector<int> placeWork = place; // C++の仕様知らないから一応移す
  for (;;) {
    if (min < resultDist) {
      return -1;
    }                           // 最短距離を上回ったとき
    if (placeWork.size() < 3) { // 残り2つになったら確定して終わらせる
      resultDist += place.at(1) - place.at(0);
      return resultDist;
    }
    int backDist, forwDist;
    if (0 < pos) { // 若い番号が存在するとき
      backDist = place.at(pos) - place.at(pos - 1);
    } else {
      backDist = place.at(0) + around - place.at(place.size() - 1);
    }
    if (pos < placeWork.size()) { // より大きい番号が存在するとき
      forwDist = place.at(pos + 1) - place.at(pos);
    } else {
      forwDist = place.at(0) + around - place.at(place.size() - 1);
    }

    int prevPos = pos;
    if (backDist < forwDist) { // 若いほうが近い
      pos--;
      resultDist += backDist;
    } else if (backDist == forwDist) {
      resultDist += backDist;
      placeWork.erase(placeWork.begin() + prevPos);
      int rslt0 = search(placeWork, pos - 1, min - resultDist, around);
      int rslt1 = search(placeWork, pos, min - resultDist,
                         around); // 要素を消しているため開始はpos
      if (rslt0 == -1 && rslt1 == -1) {
        return -1;
      } // どっちも出なかったとき
      if (rslt0 == -1) {
        resultDist += rslt1;
      }
      if (rslt1 == -1) {
        resultDist += rslt0;
      }
      if (rslt0 < rslt1) {
        resultDist += rslt0;

      } else {
        resultDist += rslt1;
      }
      return resultDist;
    } else {
      resultDist += forwDist;
    }
    placeWork.erase(placeWork.begin() + prevPos);
  }
}

int main(void) {
  int around, num; // 周長,軒数
  cin >> around >> num;
  int point[around]; //[場所]でそこにある家の数
  for (int i = 0; i < around; i++) {
    point[i] = 0;
  }
  for (int i = 0; i < num; i++) {
    int input;
    cin >> input;
    point[input]++;
  }
  vector<int> place(1); // 存在する場所の配列
  char initFlag = 0;    // 最初だけ追加しないようにするため
  for (int i = 0; i < around; i++) {
    if (point[i] < 1) {
      continue;
    }
    if (initFlag == 0) {
      initFlag++;
      place.at(0) = i;
    }
    place.push_back(i);
  }
  int sumMin = around; // 合計長,1週より長くはならないはず
  for (int i = 0; i < place.size(); i++) {
    if (point[i] < 1) {
      continue;
    } // 軒数0の部分は無視
    int resultLoop = search(place, i, sumMin, around); // 探索
    if (resultLoop != -1 && resultLoop < sumMin) {
      sumMin = resultLoop;
    }
  }
  cout << sumMin << 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);
  int max_distance = 0;
  int distance;

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


Error: 0

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long k;
  int n;

  cin >> k >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int d;
  d = k - a[n - 1] + a[0];
  for (int i = 0; i < n - 1; i++) {
    if (d < (a[i + 1] - a[i]))
      d = a[i + 1] - a[i];
  }
  cout << (k - d);
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int k, n, a[n];
  cin >> k >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  int d;
  d = k - a[n - 1] + a[0];
  for (int i = 0; i < n - 1; i++) {
    if (d < (a[i + 1] - a[i]))
      d = a[i + 1] - a[i];
  }
  cout << k - d;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <vector>
#define ll long long int
#define mp make_pair
#define pb push_back
#define vi vector<int>
using namespace std;
ll solve() {
  ll k, n;
  cin >> k >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll ans = k + a[0] - a[n - 1];
  for (int i = 1; i < n; i++) {
    ans = max(ans, a[i] - a[i - 1]);
  }
  return k - ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << solve();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <vector>
#define ll long long int
#define mp make_pair
#define pb push_back
#define vi vector<int>
using namespace std;
ll solve() {
  ll k, n;
  cin >> k >> n;
  ll a[n];
  for (int i = 0; i, n; i++) {
    cin >> a[i];
  }
  ll ans = k + a[0] - a[n - 1];
  for (int i = 1; i < n; i++) {
    ans = min(ans, a[i] - a[i - 1]);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << solve();
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int N = 1000010;

int k, n, a[N], maxn = -1;

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

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int k, n, a[N], maxn = -1;

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

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

#include <bits/stdc++.h>
using namespace std;
int k, n, arr[200005], mini;
int main() {
  cin >> k >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  mini = arr[n - 1] - arr[0];
  for (int i = 1; i < n; ++i) {
    int tmp;
    tmp = k - arr[i] + arr[i - 1];
    mini = min(mini, tmp);
  }
  cout << mini << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int k, n, arr[100005], mini;
int main() {
  cin >> k >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  mini = arr[n - 1] - arr[0];
  for (int i = 1; i < n; ++i) {
    int tmp;
    tmp = k - arr[i] + arr[i - 1];
    mini = min(mini, tmp);
  }
  cout << mini << "\\n";
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int k, n, a[200005], res;

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

  cin >> k >> n;

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

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

  cout << k - res << "\\n";
  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int k, n, a[200005], res;

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

  cin >> n >> k;

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

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

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

const int N = 2e5 + 40;

long long a[N], k, mn = 1e18, n;

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

Error: WA

Fixed submission:

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

const int N = 1e5 + 40;

long long a[N], k, mn = 1e18, n;

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

#include <algorithm>
#include <bitset>
#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
int K, N;
vector<int> A(2 * 10 * 10 * 10 * 10 * 10);
int tmp = 0;
int ans = 0;
int tmpMaxDist = 0;
int edgeDist = 0;

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

  for (int i = 1; i < N; i++) {
    if (A[i] - A[i - 1] > tmpMaxDist) {
      tmpMaxDist = A[i] - A[i - 1];
    }
  }

  edgeDist = (K - A[N - 1]) + A[0];

  if (edgeDist > tmpMaxDist) {
    ans = K - edgeDist;
  } else {
    ans = K - tmpMaxDist;
  }

  cout << ans;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
int K, N;
vector<int> A(2 * 10 ^ 5);
int tmp = 0;
int ans = 0;
int tmpMaxDist = 0;
int edgeDist = 0;

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

  for (int i = 1; i < N; i++) {
    if (A[i] - A[i - 1] > tmpMaxDist) {
      tmpMaxDist = A[i] - A[i - 1];
    }
  }

  edgeDist = (K - A[N - 1]) + A[0];

  if (edgeDist > tmpMaxDist) {
    ans = K - edgeDist;
  } else {
    ans = K - tmpMaxDist;
  }

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

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

int main() {
  long long k, n, ans = 2000000;
  cin >> k >> n;
  long long a[500000];
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    a[i] = l;
  }
  for (int i = 0; i < n; i++) {
    a[i + n] = a[i] + k;
  }
  for (int i = 0; i < n; i++) {
    ans = min(ans, a[i + n - 1] - a[i]);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

int main() {
  long long k, n, ans = 2000000;
  cin >> k >> n;
  long long a[300000];
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    a[i] = l;
  }
  for (int i = 0; i < n; i++) {
    a[i + n] = a[i] + k;
  }
  for (int i = 0; i < n; i++) {
    ans = min(ans, a[i + n - 1] - a[i]);
  }
  cout << ans << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

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

  m = max(m, (K - A[N - 1]) + A[0]);
  cout << K - m << endl;
  return 0;
}

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


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

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

  m = max(m, (K - A[N - 1]) + A[0]);
  cout << K - m << 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;
  for (int i = 0; i < n; ++i) {
    long long int x;
    cin >> x;
    a.push_back(x);
  }
  a.push_back(a[0] + k);

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

  cout << k - l << endl;

  return 0;
}


Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

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

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

  vector<long long int> diff(n);
  for (int i = 0; i < n; ++i) {
    diff[i] = abs(a[i] - a[i + 1]);
  }
  sort(diff.begin(), diff.end());

  long long int ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    ans += diff[i];
  }

  cout << ans << endl;

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

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

const int N = 1000005;

int k, n;
int a[N];

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

  int ans = a[n - 1] - a[0];
  for (int i = 1; i < n; ++i)
    ans = min(ans, k - (a[i] - a[i - 1]));
  printf("%d\\n", ans);
  return 0;
}

int main() {
  int t = 1;
  // scanf("%d", &t);
  while (t--)
    solve();
  return 0;
}


Error: 0

Fixed submission:

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

const int N = 100005;

int k, n;
int a[N];

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

  int ans = a[n - 1] - a[0];
  for (int i = 1; i < n; ++i)
    ans = min(ans, k - (a[i] - a[i - 1]));
  printf("%d\\n", ans);
  return 0;
}

int main() {
  int t = 1;
  // scanf("%d", &t);
  while (t--)
    solve();
  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> a(N);
  for (int j = 0; j < N - 1; j++) {
    a.at(j) = A.at(j + 1) - A.at(j);
  }
  a.at(N - 1) = A.at(0) + K - A.at(N - 1);
  sort(a.begin(), a.end());
  a.pop_back();
  int S = 0;
  for (int i = 0; i < N - 1; i++) {
    S += a.at(i);
  }
  cout << S << endl;
}


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


Fixed submission:

#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> a(N);
  for (int j = 0; j < N - 1; j++) {
    a.at(j) = A.at(j + 1) - A.at(j);
  }
  a.at(N - 1) = A.at(0) + K - A.at(N - 1);
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  a.pop_back();
  int S = 0;
  for (int i = 0; i < N; i++) {
    S += a.at(i);
  }
  cout << S << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

signed main() {
  int k, n;
  cin >> k >> n;
  bool distances[1000000];
  for (int i = 0; i < k; i++) {
    distances[i] = false;
  }
  for (int i = 0; i < n; i++) {
    int dist;
    cin >> dist;
    distances[dist] = true;
  }

  int firstDist = -1;
  int lastDist = -1;
  int maxDist = 0;
  for (int i = 0; i < k; i++) {
    if (distances[i]) {
      if (firstDist == -1) {
        firstDist = i;
      } else if (maxDist < i - lastDist) {
        maxDist = i - lastDist;
      }
      lastDist = i;
    }
  };
  int lastPart = k - lastDist + firstDist;
  if (lastPart > maxDist) {
    maxDist = lastPart;
  }
  cout << k - maxDist << endl;
}


Error: 0

Fixed submission:

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

signed main() {
  int houses[200000];
  bool distances[200000];
  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    int dist;
    cin >> dist;
    distances[dist - 1] = true;
    houses[i] = dist;
  }

  int maxDistHouses = 0;
  for (int i = 0; i < n; i++) {
    int movedR = 0;
    int movedL = 0;
    int j = houses[i];
    while (!distances[j]) {
      j++;
      if (j >= k) {
        j = 0;
      }
      movedR++;
    }
    j = houses[i];
    while (!distances[j]) {
      j--;
      if (j <= 0) {
        j = k - 1;
      }
      movedL++;
    }
    if (maxDistHouses < movedR)
      maxDistHouses = movedR;
    if (maxDistHouses < movedL)
      maxDistHouses = movedL;
  }
  int result = k - (maxDistHouses + 1);
  cout << result << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int K, N;
  int b;
  cin >> K >> N;
  vector<int> a(N);
  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(a.at(i + 1) - a.at(i), b);
  }
  cout << K - b << endl;
}


Error: WA

Fixed submission:

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

int main() {
  int K, N;
  int b;
  cin >> K, N;
  vector<int> a(N);
  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(a.at(i + 1) - a.at(i), b);
  }
  cout << K - b << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, k, i, j, sum = 0, l;
  cin >> k >> n;
  int ar[n];

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

  int ans = k;
  for (i = 1; i < n; i++) {
    ans = min(ans, (k - (ar[i] - ar[i - 1])));
    // cout<<ans<<endl;
  }

  ans = min(ans, (k - (ar[0] + k - ar[n - 1])));
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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

  for (int i = 0; i < N; i++) // for every element in A
  {
    int dist = 0, prev = A[i];

    for (int j = 0; j < N; j++) {
      dist += (A[(i + j) % N] - prev) >= 0 ? (A[(i + j) % N] - prev)
                                           : (K + (A[(i + j) % N] - prev));
      prev = A[(i + j) % N];
    }

    if (dist < minDist)
      minDist = dist;
  }

  cout << minDist << endl;

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


#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using ll = long long int;
using ul = unsigned long long int;
const ll LL_MAX(1LL << 60);
const ll MOD(1000000007);

#define rep(i, s, e) for (ll i = (s); i < (e); i++)

using namespace std;

using T = tuple<ll, ll>;
using Graph = vector<vector<T>>;

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

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

  rep(i, 0, n) a.push_back(a[i] + k);

  ll max_itv(-LL_MAX);
  ll e1, e2;
  rep(i, 1, n + 1) {
    ll itv = a[i] - a[i - 1];
    if (max_itv < itv)
      max_itv = itv, e1 = i, e2 = i - 1;
    // cout << a[i] << ' ' << a[i-1] << endl;
  }

  if (e1 > n - 1)
    e1 -= n;
  if (e2 < e1)
    e2 += n;

  // cout << max_itv << ' ' << e1 << ' ' << e2 << endl;

  ll dist(0);
  rep(i, e1 + 1, e2 + 1) {
    ll itv = a[i] - a[i - 1];
    dist += itv;
  }

  cout << dist << endl;

  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using ll = long long int;
using ul = unsigned long long int;
const ll LL_MAX(1LL << 60);
const ll MOD(1000000007);

#define rep(i, s, e) for (ll i = (s); i < (e); i++)

using namespace std;

using T = tuple<ll, ll>;
using Graph = vector<vector<T>>;

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

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

  // a.push_back(k);
  rep(i, 0, n) a.push_back(a[i] + k);

  // for (auto i: a)
  //     cout << i << endl;

  ll ans(LL_MAX);
  rep(j, 0, n) {
    ll dist(0);
    rep(i, j + 1, n + j) {
      dist += a[i] - a[i - 1];
      // cout << a[i] << ' ' << a[i-1] << endl;
    }
    // cout << endl;
    ans = min(ans, dist);
  }

  cout << ans << endl;

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

#include <stdio.h>
int main() {
  int n, m, max;
  scanf("%d%d", &n, &m);
  int a[m + 1], b[m + 1];
  for (int i = 0; i < m; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < m - 1; i++) {
    b[i] = a[i + 1] - a[i];
  }
  b[m - 1] = n - a[m - 1] + a[0];
  max = b[0];
  for (int i = 1; i < m; i++) {
    if (b[i] > max)
      max = b[i];
  }
  printf("%d\\n", n - max);
  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
int main() {
  int n, m, max = -1000;
  scanf("%d%d", &n, &m);
  int a[m + 1], b[m + 1];
  for (int i = 0; i < m; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < m - 1; i++) {
    b[i] = a[i + 1] - a[i];
  }
  b[m - 1] = n - a[n - 1] + a[0];
  for (int i = 0; i < m; i++) {
    if (b[i] > max)
      max = b[i];
  }
  printf("%d\\n", n - max);
  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> site(N);
  for (int i = 0; i < N; i++) {
    cin >> site.at(i);
  }
  vector<int> X(N);
  for (int i = 0; i < N - 1; i++) {
    X.at(i) = site.at(i + 1) - site.at(i);
  }
  X.at(N - 1) = K - site.at(N - 1) + site.at(0);
  sort(X.begin(), X.end());
  cout << K - X.at(N - 1) << endl;
}


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