alexjercan/bugnet huggingface dataset:
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int K, N;
  cin >> K >> N;
  int data[N];
  for (int i = 0; i < N; i++) {
    cin >> data[i];
  }
  int count = 0;
  int countMax;
  for (int s = N - 1; s > 0; s--) {
    int tmp = data[s] - data[s - 1];
    count += tmp;
    if (s == N - 1) {
      countMax = tmp;
    } else {
      countMax = max(tmp, countMax);
    }
  }

  int tmp = (K - data[N - 1]) + data[0];
  count += tmp;
  countMax = max(tmp, countMax);
  count -= countMax;
  cout << count << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int K, N;
  cin >> K >> N;
  int data[N];
  for (int i = 0; i < N; i++) {
    cin >> data[i];
  }
  int count = 0;
  int countMin;
  int count2 = 0;
  int countMin2;
  for (int s = 0; s < N; s++) {
    // スタート s
    int left = s;
    int right = s + 1;
    count = 0;
    // 左へ
    int left2 = s - 1;
    int right2 = s;
    count2 = 0;
    for (int i = 0; i < N - 1; i++) {
      // 右へ
      if (left >= N)
        left = 0;
      if (right >= N)
        right = 0;
      int data1 = data[left];
      int data2 = data[right];
      if (right == 0) {
        count += (K - data[left]) + data[right];
      } else {
        count += data2 - data1;
      }
      right++;
      left++;
      // 左へ
      if (left2 < 0)
        left2 = N - 1;
      if (right2 < 0)
        right2 = N - 1;
      int data3 = data[left2];
      int data4 = data[right2];

      if (right2 == 0) {
        count2 += (K - data[left2]) + data[right2];
      } else {
        count2 += data4 - data3;
      }
      right2--;
      left2--;
      right++;
      left++;
    }
    if (s == 0) {
      int minTmp = min(count, count2);
      countMin = minTmp;
    } else {
      int minTmp = min(count, count2);
      countMin = min(countMin, minTmp);
    }
  }
  cout << countMin << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

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

  cin >> k >> n;
  houselist.resize(n);
  for (auto i = 0; i < n; i++) {
    cin >> houselist[i];
  }

  int min = k;
  int kyori;
  for (auto i = 0; i < n; i++) {
    int base = houselist[i];
    int tail = (i - 1 + n) % n;
    kyori = houselist[tail] < base ? houselist[tail] + k - base
                                   : houselist[tail] - base;
    if (kyori < min) {
      min = kyori;
    }
  }
  cout << min << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

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

  cin >> k >> n;
  houselist.resize(n);
  for (auto i = 0; i < n; i++) {
    cin >> houselist[i];
  }

  int min = k;
  int base = 0;
  int idx = 0;
  int kyori = 0;
  for (auto i = 0; i < n; i++) {
    // 時計順走査
    base = houselist[i];
    kyori = 0;
    for (auto j = 1; j < n; j++) {
      idx = (i + j) % n;
      kyori += (houselist[idx] < base) ? (houselist[idx] + k) - base
                                       : houselist[idx] - base;
      base = houselist[idx];
      if (kyori > min) {
        break;
      }
    }
    if (kyori < min) {
      min = kyori;
    }

    // 反時計順走査
    base = k - houselist[i];
    kyori = 0;
    for (auto j = 1; j < n; j++) {
      idx = ((i - j) + n) % n;
      kyori += ((k - houselist[idx]) < base) ? ((k - houselist[idx] + k) - base)
                                             : (k - houselist[idx]) - base;
      base = k - houselist[idx];
      if (kyori > min) {
        break;
      }
    }
    if (kyori < min) {
      min = kyori;
    }
  }
  cout << min << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <new>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define rep(i, N) for (ll i = 0; i < (ll)(N); ++i)

const ll inf = 1e18 + 7;
const ll mod = 1e9 + 7;

int main() {
  ll k, n, ans = 0, ma = 0;
  cin >> k >> n;
  vector<ll> A(n, 0);
  vector<ll> S(n, 0);
  rep(i, n) {
    cin >> A[i];
    ma = max(ma, A[i]);
  }

  for (ll i = 0; i < n; i++) {
    if (i == n - 1) {
      S[i] = k - ma + A[0];
    } else {
      S[i] = abs(A[i] - A[i + 1]);
    }
  }

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

  for (ll i = 0; i < n - 1; i++) {
    ans += S[i];
  }

  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <new>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define rep(i, N) for (ll i = 0; i < (ll)(N); ++i)

const ll inf = 1e18 + 7;
const ll mod = 1e9 + 7;

int main() {
  ll k, n, ans = 0;
  cin >> k >> n;
  vector<ll> A(n, 0);
  vector<ll> S(n, 0);
  rep(i, n) cin >> A[i];

  for (ll i = 0; i < n; i++) {
    if (i = n - 1) {
      S[i] = abs(A[0] - A[n - 1]);
    } else {
      S[i] = abs(A[i] - A[i + 1]);
    }
  }

  sort(S.end(), S.begin());
  for (ll i = 1; i < n; i++) {
    ans += S[i];
  }

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

/*
 * @Author: Kaizyn
 * @Date: 2020-03-28 21:12:21
 * @LastEditTime: 2020-03-28 21:16:53
 */
#include <bits/stdc++.h>

// #define DEBUG

using namespace std;

const int N = 2e5 + 7;
const int MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;

int n, k;
int a[N];

inline void solve() {
  cin >> k >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  int res = a[1] - a[n] + k;
  for (int i = 1; i < n; ++i)
    res = max(res, a[i + 1] - a[i]);
  cout << k - res << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int testcase = 1;
  // cin >> testcase;
  for (int i = 1; i <= testcase; ++i) {
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

/*
 * @Author: Kaizyn
 * @Date: 2020-03-28 21:12:21
 * @LastEditTime: 2020-03-28 21:16:53
 */
#include <bits/stdc++.h>

// #define DEBUG

using namespace std;

const int N = 1e5 + 7;
const int MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;

int n, k;
int a[N];

inline void solve() {
  cin >> k >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  int res = a[1] - a[n] + k;
  for (int i = 1; i < n; ++i)
    res = max(res, a[i + 1] - a[i]);
  cout << k - res << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int testcase = 1;
  // cin >> testcase;
  for (int i = 1; i <= testcase; ++i) {
    solve();
  }
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 200006;
int a[N];
int b[N];
bool vis[N];
int k;
int n;
int main() {
  while (cin >> k >> n) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int temp = -1;
    for (int i = 0; i < n; i++) {
      if (i != n - 1) {
        b[i] = (abs(a[i + 1] - a[i]));
        if (temp < b[i]) {
          temp = b[i];
        }
      } else {
        b[i] = k + a[0] - a[i];
        if (temp < b[i]) {
          temp = b[i];
        }
      }
      vis[b[i]] = 1;
    }
    bool swi = true;
    int sum = 0;
    for (int i = 0; i < n; i++) {
      if (b[i] != temp) {
        sum += b[i];
      } else {
        if (swi == 1) {
          swi = 0;
          continue;
        } else {
          sum += b[i];
        }
      }
    }
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 20006;
int a[N];
int b[N];
queue<int> q1;
int k;
int n;
int remx(int a[], int n) {
  int sum = 0;
  int temp = -1;
  for (int i = 0; i < n; i++) {
    if (temp < a[i]) {
      temp = a[i];
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] != temp) {
      sum += a[i];
    }
  }
  return sum;
}
int main() {
  while (cin >> k >> n) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
      if (i != n - 1) {
        b[i] = (abs(a[i + 1] - a[i]));
      } else {
        b[i] = k + a[0] - a[i];
      }
    }
    int sum = remx(b, n);
    cout << sum << endl;
  }
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

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

int K, N;
int A[200001];

int main() {
  cin >> K >> N;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  A[N + 1] = A[1] + K;
  int kyori = 0;
  for (int j = 1; j <= N; j++) {
    if (kyori < A[j + 1] - A[j])
      kyori = A[j + 1] - A[j];
  }
  cout << K - kyori;
  return 0;
}

Error: WA

Fixed submission:

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

long K, N;
long A[200001];

void solve() {
  long kyori = 0;
  for (long j = 1; j <= N; j++) {
    if (kyori < A[j + 1] - A[j])
      kyori = A[j + 1] - A[j];
  }
  cout << K - kyori;
  return;
}

int main() {
  cin >> K >> N;
  for (long i = 1; i <= N; i++) {
    cin >> A[i];
  }
  A[N + 1] = A[1] + K;
  solve();
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(void) {
  int k, n;
  scanf("%d %d", &k, &n);
  vector<int> a(n + 1);
  rep(i, n) scanf("%d", &a[i]);
  a[n] = k + a[0];

  int ans = 0;
  int tmp = 0;
  int dis = 0;
  rep(i, n) {
    dis = a[i + 1] - a[i];
    if (dis > tmp) {
      ans += tmp;
      tmp = dis;
    } else {
      ans += dis;
    }
  }

  printf("%d", ans);

  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(void) {
  int k, n;
  scanf("%d %d", &k, &n);
  vector<int> a(n);
  rep(i, n) scanf("%d", &a[i]);

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

  int skip = 0, num = 0;
  rep(i, n) {
    if (d[i] > num) {
      skip = i;
      num = d[i];
    }
  }
  // cout << skip << endl;
  int i;
  if (skip == n - 1)
    i = -1;
  else
    i = skip;
  int ans = 0;
  while (true) {
    i++;
    if (i == n)
      i = 0;
    ans += d[i];
    // cout << "ans=" << ans << ", i=" << i << ", d[i]=" << d[i] << endl;
    if (i == skip - 1)
      break;
  }

  cout << ans;

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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
#define MAX_N 200000

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;

// Input
int K, N;
int A[MAX_N];

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

  int max_diff = 0;
  int tmp = A[N - 1];
  for (int i = N - 1; i > 0; i--) {
    A[i] -= A[i - 1];
  }
  A[0] = A[0] + K - tmp;
  for (int i = 0; i < N; i++) {
    chmax(max_diff, A[i]);
  }
  cout << K - max_diff << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
#define MAX_N 400

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;

// Input
int K, N;
int A[MAX_N];

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

  int max_diff = 0;
  int tmp = A[N - 1];
  for (int i = N - 1; i > 0; i--) {
    A[i] -= A[i - 1];
  }
  A[0] = A[0] + K - tmp;
  for (int i = 0; i < N; i++) {
    chmax(max_diff, A[i]);
  }
  cout << K - max_diff << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
#define MOD1 1000000007
#define MOD2 998244353
#define BOOST                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

using namespace std;

ll k, n;
vector<ll> h;
void solve() {
  cin >> k >> n;
  h.resize(n);
  for (auto &i : h)
    cin >> i;
  ll ans = k - h[n - 1] + h[0];
  for (int i = 1; i < n; i++)
    ans = max(ans, h[i] - h[i - 1]);
  cout << k - ans;
}

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

  ll t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define MOD1 1000000007
#define MOD2 998244353
#define BOOST                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

using namespace std;

ll k, n;
vector<ll> h;
void solve() {
  cin >> k >> n;
  h.resize(n);
  for (auto &i : h)
    cin >> i;
  ll ans = k - h[n - 1] + h[0];
  for (int i = 1; i < n; i++)
    ans = max(ans, h[i] - h[i - 1]);
  cout << k - ans;
}

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

  ll t = 1;
  // cin >> t;
  while (t--)
    solve();

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, k, l;
int main() {
  cin >> k >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  v.push_back(v[0] + k);
  for (int i = 0; i < n; i++)
    l = max(l, v[i + 1] - v[i]);
  cout << k - l;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n, k, l = 0;
  cin >> k >> n;
  int v[20005];
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  v[n + 1] = v[1] + k;
  for (int i = 1; i <= n; i++)
    l = max(l, v[i + 1] - v[i]);
  cout << k - l;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
ll a, b, c, n, i, j, k, m, t, arr[200005], ar[200005];
int main() {
  string s;
  cin >> n >> m;
  for (i = 0; i < m; i++)
    cin >> arr[i];
  a = arr[m - 1] - arr[0];
  ll minn = arr[0];
  for (i = 1; i < m; i++) {
    minn = minn > arr[i] - arr[i - 1] ? minn : (arr[i] - arr[i - 1]);
  }
  a = a < a + arr[0] - minn + n - arr[m - 1]
          ? a
          : (a + arr[0] - minn + n - arr[m - 1]);
  cout << a;
}


Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
ll a, b, c, n, i, j, k, m, t, arr[200005], ar[200005];
int main() {
  string s;
  cin >> n >> m;
  for (i = 0; i < m; i++)
    cin >> arr[i];
  a = arr[m - 1] - arr[0];
  ll minn = arr[0];
  for (i = 1; i < n; i++) {
    minn = minn > arr[i] - arr[i - 1] ? minn : (arr[i] - arr[i - 1]);
  }
  a = a < a + arr[0] - minn + n - arr[m - 1]
          ? a
          : (a + arr[0] - minn + n - arr[m - 1]);
  cout << a;
}
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> b(N);
  for (int i = 0; i < N - 1; i++) {
    b[i] = a[i + 1] - a[i];
  }
  b[N - 1] = K + a[0] - a[N - 1];

  int temp = 0;
  for (int i = 0; i < N; i++) {
    temp = max(temp, b[i]);
  }

  cout << K - temp << endl;

  return 0;
}

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


Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;

int main(int argc, const char *argv[]) {
  // insert code here...
  // int a,b,c;
  // string s;
  // int a;
  // cin>>a;

  // int b = a / 500;
  // int c = (a % 500) / 5;

  // cout << b * 1000 + c * 5 << endl;
  // return 0;

  // c

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

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

  if (b < a[0] + k - a[n - 1]) {
    cout << a[n - 1] - a[0] << endl;

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

#include <iostream>
#include <map>
#include <numeric>
#include <vector>
using namespace std;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int main() {
  int n, k;
  cin >> k >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  a.push_back(a[0] + k);
  int l = 0;
  rep(i, n) { l = max(l, a[i + 1] - a[i]); }
  int ans = k - l;
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int main() {
  int n, k;
  cin >> k >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int mi = 1e8;
  int dist = 0;
  rep(j, n) {
    int center = a[j];
    rep(i, n) {
      int diff1 = abs(center - a[i]);
      int diff2 = abs(k - a[i]);
      center = a[i];
      dist += min(diff1, diff2);
    }
    if (dist < mi)
      mi = dist;
  }
  cout << mi << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

// #include<ext/pb_ds/assoc_container.hpp>
// #nclude<ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>pb_ds;

#define pii pair<int, ll>
#define pi pair<int, ll>
#define F first
#define S second
#define pb push_back
#define pp pop_back
#define eps 10E-10
#define mod 1000000007
#define pai acos(-1)
// #define mx 1000000000
#define N 200005

ll ar[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int k, n;
  cin >> k >> n;
  for (int i = 1; i <= n; i++)
    cin >> ar[i];
  vector<ll> vc;
  vc.pb(0);
  for (int i = 1; i < n; i++)
    vc.pb(ar[i + 1] - ar[i]);
  ll mx = 0;
  int id = 1;
  ll s1, s2;
  s1 = s2 = 0;
  for (int i = 1; i < vc.size(); i++) {
    if (vc[i] > mx) {
      mx = vc[i];
      id = i;
    }
    s1 += vc[i];
  }
  if (mx < (ar[1] + k - ar[n]))
    cout << s1 << endl;
  else
    cout << s1 - mx + ar[1] + k - ar[n] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

// #include<ext/pb_ds/assoc_container.hpp>
// #nclude<ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>pb_ds;

#define pii pair<int, ll>
#define pi pair<int, ll>
#define F first
#define S second
#define pb push_back
#define pp pop_back
#define eps 10E-10
#define mod 1000000007
#define pai acos(-1)
// #define mx 1000000000
#define N 100005

ll ar[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int k, n;
  cin >> k >> n;
  for (int i = 1; i <= n; i++)
    cin >> ar[i];
  vector<ll> vc;
  vc.pb(0);
  for (int i = 1; i < n; i++)
    vc.pb(ar[i + 1] - ar[i]);
  ll mx = 0;
  int id = 1;
  for (int i = 1; i < vc.size(); i++) {
    if (vc[i] > mx) {
      mx = vc[i];
      id = i;
    }
  }
  ll s1, s2;
  s1 = s2 = 0;
  ar[n + 1] = k;
  bool f = 0;
  for (int i = id; i < n; i++)
    s1 += ar[i + 1] - ar[i];
  for (int i = id - 1; i >= 1; i--)
    f = 1, s1 += ar[i] - ar[i - 1];
  if (f)
    s1 += (k - ar[n]);

  for (int i = id; i >= 1; i--)
    s2 += ar[i] - ar[i - 1];
  for (int i = id + 1; i <= n; i++)
    s2 += ar[i + 1] - ar[i];
  cout << min(s1, s2) << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define lli long long int

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  lli K, N;
  lli A0, Aprev, Anow;
  lli max = 0;
  cin >> K >> N;
  cin >> A0;
  for (lli i = 1; i < N; ++i) {
    cin >> Anow;
    max = (max > Anow - Aprev) ? max : Anow - Aprev;
    Aprev = Anow;
  }
  max = (max > K + A0 - Anow) ? max : K + A0 - Anow;

  cout << K - max << "\\n";

  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int K, N;
  int A[N];
  int max = 0;
  cin >> K >> N;
  cin >> A[0];
  for (int i = 1; i < N; ++i) {
    cin >> A[i];
    max = (max > A[i] - A[i - 1]) ? max : A[i] - A[i - 1];
  }
  max = (max > K + A[0] - A[N - 1]) ? max : K + A[0] - A[N - 1];

  cout << 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> A(N);

  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int cnt = 0;
  int high = 0;
  for (int i = 0; i < N - 1; i++) {
    cnt += (A.at(i + 1) - A.at(i));
    if (A.at(i + 1) - A.at(i) >= high)
      high = A.at(i + 1) - A.at(i);
  }
  cnt += (K - A.at(N - 1) + A.at(0));
  int highest = max(high, K - A.at(N - 1) + A.at(0));
  int ans = cnt - highest;
  cout << ans << endl;
}

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


Fixed submission:

#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++) {
    cin >> A.at(i);
  }
  int cnt = 0;
  int high = 0;
  for (int i = 0; i < N - 1; i++) {
    cnt += (A.at(i + 1) - A.at(i));
    if (A.at(i + 1) - A.at(i) >= high)
      high = A.at(i + 1) - A.at(i);
  }
  cnt += (K - A.at(N - 1) - A.at(0));
  int highest = max(high, K - A.at(N - 1) - A.at(0));
  int ans = cnt - highest;
  cout << ans << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int ll
typedef long double ld;

typedef pair<int, int> pii;
#define x first
#define y second

const int N = 2e5 + 100;

int a[N];

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

  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int mx = a[0] - a[n - 1] + k;
  for (int i = 0; i < n - 1; i++)
    mx = max(mx, a[i + 1] - a[i]);
  cout << k - mx;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int ll
typedef long double ld;

typedef pair<int, int> pii;
#define x first
#define y second

const int N = 1e5 + 100;

int a[N];

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

  int k, n;
  cin >> k >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int mx = a[0] - a[n - 1] + k;
  for (int i = 0; i < n - 1; i++)
    mx = max(mx, a[i + 1] - a[i]);
  cout << k - mx;
}