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

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define ll long long
#define test(t)                                                                \\
  ll int t;                                                                    \\
  cin >> t;                                                                    \\
  while (t--)
#define F(i, L, R) for (ll int i = L; i < R; i++)
#define F2(i, L, R) for (ll int i = L; i >= R; i--)
#define get1(a)                                                                \\
  ll int a;                                                                    \\
  cin >> a;
#define get2(a, b)                                                             \\
  ll int a, b;                                                                 \\
  cin >> a >> b;
#define get3(a, b, c)                                                          \\
  ll int a, b, c;                                                              \\
  cin >> a >> b >> c;
#define pb push_back
#define mp make_pair
#define MAX 1000000001
using namespace std;

int main() {

  ios_base::sync_with_stdio(0);

  get1(a);

  cout << (1000 * (a / 500) + 5 * ((a % 500) / 5));

  return 0;
}


Error: WA

Fixed submission:

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define ll long long
#define test(t)                                                                \\
  ll int t;                                                                    \\
  cin >> t;                                                                    \\
  while (t--)
#define F(i, L, R) for (ll int i = L; i < R; i++)
#define F2(i, L, R) for (ll int i = L; i >= R; i--)
#define get1(a)                                                                \\
  ll int a;                                                                    \\
  cin >> a;
#define get2(a, b)                                                             \\
  ll int a, b;                                                                 \\
  cin >> a >> b;
#define get3(a, b, c)                                                          \\
  ll int a, b, c;                                                              \\
  cin >> a >> b >> c;
#define pb push_back
#define mp make_pair
#define MAX 1000000001
using namespace std;

int main() {

  ios_base::sync_with_stdio(0);

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

  get1(a);

  cout << (1000 * (a / 500) + 5 * ((a % 500) / 5));

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

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
const int INF = 1000100010;
int main() {
  int x;
  cin >> x;
  int count = 0;
  while (x >= 500) {
    count += 1000;
    x -= 500;
  }

  while (x >= 5) {
    count += 5;
    x -= 5;
  }

  cout << count << endl;
}

Error: 19

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
const int INF = 1000100010;
int main() {
  int x;
  cin >> x;
  int count = 0;
  while (x != 0) {
    if (x >= 500) {
      count += 1000;
      x -= 500;
    }
    if (x < 500)
      break;
  }
  while (x != 0) {
    if (x >= 5) {
      count += 5;
      x -= 5;
      return x;
    }
    if (x < 5)
      break;
  }
  cout << count << endl;
}
Problem p02724 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  ll happy = 0;

  int n;
  cin >> n;

  if (n / 500) {
    happy += n / 500 * 1000;
    n %= 500;
  }

  if (n / 100) {
    happy += n / 100 * 100;
    n %= 100;
  }

  if (n / 50) {
    happy += n / 50 * 50;
    n %= 50;
  }

  if (n / 10) {
    happy += n / 10 * 10;
    n %= 10;
  }

  if (n / 5) {
    happy += n / 5 * 5;
    n %= 10;
  }

  cout << happy;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  ll happy = 0;

  int n;
  cin >> n;

  while (n) {
    if (n / 500) {
      happy += n / 500 * 1000;
      n %= 500;
    }

    if (n / 100) {
      happy += n / 100 * 100;
      n %= 100;
    }

    if (n / 50) {
      happy += n / 50 * 50;
      n %= 50;
    }

    if (n / 10) {
      happy += n / 10 * 10;
      n %= 10;
    }

    if (n / 5) {
      happy += n / 5 * 5;
      n %= 10;
    }

    if (n < 5)
      break;
  }

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

/**
 * ATCODER 160 B
 * Kartikeya (doomsday861)
 **/
#include <bits/stdc++.h>
#include <time.h>
#define ll long long
#define testcase                                                               \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define timeb auto start = high_resolution_clock::now();
#define timee                                                                  \\
  auto stop = high_resolution_clock::now();                                    \\
  auto duration = duration_cast<seconds>(stop - start);                        \\
  cout << "Time taken by function: " << duration.count() << "seconds" << endl;
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  using namespace std::chrono;
  // timeb
  ll x;
  cin >> x;
  ll h = 0;
  ll n = x / 500;
  h += n * 1000;
  x %= 500;
  // cout<<x<<endl;
  while (x > 5) {
    ll d = x / 5;
    h += d * 5;
    x %= 5;
  }
  cout << h << endl;
  // timee
  return 0;
}

Error: WA

Fixed submission:

/**
 * ATCODER 160 B
 * Kartikeya (doomsday861)
 **/
#include <bits/stdc++.h>
#include <time.h>
#define ll long long
#define testcase                                                               \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define timeb auto start = high_resolution_clock::now();
#define timee                                                                  \\
  auto stop = high_resolution_clock::now();                                    \\
  auto duration = duration_cast<seconds>(stop - start);                        \\
  cout << "Time taken by function: " << duration.count() << "seconds" << endl;
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("output.ans", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  using namespace std::chrono;
  // timeb
  ll x;
  cin >> x;
  ll h = 0;
  ll n = x / 500;
  h += n * 1000;
  x %= 500;
  // cout<<x<<endl;
  while (x > 5) {
    ll d = x / 5;
    h += d * 5;
    x %= 5;
  }
  cout << h << endl;
  // timee
  return 0;
}
Problem p02724 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int ans = 0;
  while (n >= 500) {
    n -= 500;
    ans += 1000;
  }
  while (n >= 5) {
    n -= 5;
    ans += 5;
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define mod 1000000007
// int max(int a, int b, int c){ if(a>b){if(a>c) return a; else return
// c;}else{if(b>c)return b;else return c;}}
int power(int a, int b) {
  int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int32_t main() {
  IOS;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  int ans = 0;
  while (n >= 500) {
    n -= 500;
    ans += 1000;
  }
  while (n >= 5) {
    n -= 5;
    ans += 5;
  }
  cout << ans;
  return 0;
}
Problem p02724 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int v, i = 0, ans = 0;
  cin >> v;
  cout << 1000 * (v / 500) + 5 * ((v % 500) / 5) << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int c[] = {500, 5, 1};
int p[] = {1000, 5, 0};

int main() {
  int v, i = 0, ans = 0;
  cin >> v;
  while (true) {
    if (v - c[i] >= 0) {
      v -= c[i];
      ans += p[i];
    } else if (i <= 2)
      i++;
    else
      break;
  }

  cout << ans << '\\n';

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

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

int main() {
  int n, k;
  scanf("%d", &n);
  k = n / 500 * 1000 + n % 500 / 5 * 5;
  printf("%d\\n", k);

  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  char A[6];

  scanf("%s", A);
  if (A[2] == A[3] && A[4] == A[5])
    printf("Yes\\n");
  else
    printf("No\\n");
  return 0;
}
Problem p02724 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int X, happy = 0;
  cin >> X;

  if (X >= 500) {
    happy += 1000 * (X / 500);
    X = X - 500 * (X / 500);
  }
  if (X >= 5) {

    happy += 5 * (X / 5);
    X = X - 5 * (X / 5);
  }

  cout << happy;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int X, happy = 0;
  cin >> X;
  while (X / 5 >= 0) {
    if (X > 500) {
      happy += 1000 * (X / 500);
      X = X - 500 * (X / 500);
    } else {
      happy += 5 * (X / 5);
      X = X - 5 * (X / 5);
    }
  }
  cout << happy;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, h = 0;
  cin >> x;
  if (x < 5) {
    cout << h << endl;
  } else {
    h = h + (x / 500) * 1000;
    x = x % 500;
    h = h + (x / 5) * 5;
    cout << h << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, h = 0;
  cin >> x;
  if (x < 5) {
    cout << h << endl;
  } else {

    while (x >= 5) {
      if (x > 500) {
        h = h + (x / 500) * 1000;
      } else if (x > 5) {
        h = h + (x / 5) * 5;
      }
    }

    cout << h << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int a[200005];
  int k, n;
  cin >> k >> n;
  a[0] = 0;
  a[n + 1] = k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int ans = a[n] - a[1];
  for (int i = 1; i <= n; i++) {
    ans = min(ans, k - (a[i + 1] - a[i]));
    ans = min(ans, k - (a[i] - a[i - 1]));
  }
  printf("%d\\n", ans);
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int a[100005];
  int k, n;
  cin >> k >> n;
  a[0] = 0;
  a[n + 1] = k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int ans = a[n] - a[1];
  for (int i = 1; i <= n; i++) {
    ans = min(ans, k - (a[i + 1] - a[i]));
    ans = min(ans, k - (a[i] - a[i - 1]));
  }
  printf("%d\\n", ans);
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int k, n;
  cin >> k >> n;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  vector<int> b(n);
  rep(i, n - 1) b.at(i) = a.at(i + 1) - a.at(i);
  b.at(n - 1) = k + a.at(0) - a.at(n - 1);
  sort(b.begin(), b.end());
  cout << k - b.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;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int k, n;
  cin >> k >> n;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  vector<int> b(n);
  rep(i, n - 1) b.at(i) = a.at(i + 1) - a.at(i);
  b.at(n) = k + a.at(0) - a.at(n);
  sort(b.begin(), b.end());
  cout << k - b.at(n) << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
#define MODE 0
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \\
  {}
#define ARDEB(i, X)                                                            \\
  {}
#define END                                                                    \\
  {}
#endif
typedef long long ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
using namespace std;

int k, n;
int a[211111];
signed main() {

  cin >> k >> n;
  rep(i, n) cin >> a[i];
  a[n] = a[0] + k;
  int ans = 0;
  rep(i, n) { ans = max(a[i + 1] - a[i], ans); }
  cout << k - ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
#define MODE 0
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \\
  {}
#define ARDEB(i, X)                                                            \\
  {}
#define END                                                                    \\
  {}
#endif
typedef long long ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
using namespace std;

int k, n;
int a[111111];
signed main() {

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

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

signed main(void) {
  long long k, n, ans, sum, tmp;

  cin >> k >> n;
  long long a[n], dis[n];
  rep(i, n) cin >> a[i];
  rep(i, n - 1) dis[i] = a[i + 1] - a[i];
  dis[n - 1] = k - a[n - 1] + a[0];
  sum = 0;
  rep(i, n) sum += dis[i];
  tmp = sum;
  ans = INF;
  rep(i, n) {
    sum = tmp;
    sum -= dis[i];
    ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

signed main(void) {
  int K, n, ans, sum;
  cin >> K >> n;
  int dp[n][n];
  rep(i, n) rep(j, n) dp[i][j] = INF;
  int a[n];
  rep(i, n) cin >> a[i];

  for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++) {
      dp[i][j] = a[i + 1] - a[i];
      dp[j][i] = a[i + 1] - a[i];
    }
  rep(i, n) dp[i][i] = 0;
  rep(k, n)     // via
      rep(i, n) // from
      rep(j, n) // to
      dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
  ans = INF;
  sum = 0;
  rep(i, n) {
    rep(j, n) { sum += dp[i][j]; }
    ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int dis(int s, int e, int r) {
  // cout << s<<e<<r<<endl;
  return min(abs(e - s), e - s + r);
}

int main() {
  int radius, total;
  cin >> radius >> total;
  int a[total];
  for (int i = 0; i < total; i++) {
    cin >> a[i];
  }
  int tmp = 0;
  for (int i = 0; i < total; i++) {
    tmp = max(tmp, dis(a[i], a[(i + 1) % total], radius));
    // tmp=max(tmp,dis(a[i],a[i-1+radius%total],radius));
    // cout << tmp << endl;
  }
  tmp -= radius;
  cout << abs(tmp) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int dis(int s, int e, int r) { return min(abs(e - s), e - s + r); }

int main() {
  int radius, total;
  cin >> radius >> total;
  int a[total];
  for (int i = 0; i < total; i++) {
    cin >> a[i];
  }
  int tDis = 1000000;
  for (int i = 0; i < total; i++) {
    int tmp = 0;
    for (int j = i; j != (i - 1 + total) % total; j = (j + 1) % total) {
      tmp += dis(a[j], a[(j + 1) % total], radius);
    }
    tDis = min(tDis, tmp);
  }

  cout << tDis << endl;

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

#include <bits/stdc++.h>
#define ll long long int
#define F(i, j, k, in) for (int i = j; i < k; i += in)
#define DF(i, j, k, in) for (int i = j; i >= k; i -= in)
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define PB push_back
#define ub upper_bound
#define lb lower_bound
#define ins insert
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll K, N, A[1000000], sec[1000000];
  cin >> K >> N;
  F(i, 0, N, 1)
  cin >> A[i];
  ll j = 0;
  F(i, 0, N - 1, 1) {
    sec[j] = A[i + 1] - A[i];
    j++;
  }
  sec[j] = K - (A[N - 1] - A[0]);
  ll mini = INT_MAX, sum = 0, sumi;
  F(i, 0, j + 1, 1) { sum += sec[i]; }
  F(i, 0, j + 1, 1) {
    sumi = sum - sec[i];
    if (mini > sumi) {
      mini = sumi;
    }
  }

  cout << mini;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define F(i, j, k, in) for (int i = j; i < k; i += in)
#define DF(i, j, k, in) for (int i = j; i >= k; i -= in)
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define PB push_back
#define ub upper_bound
#define lb lower_bound
#define ins insert
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll K, N, A[10000], sec[10000];
  cin >> K >> N;
  F(i, 0, N, 1)
  cin >> A[i];
  ll j = 0;
  F(i, 0, N - 1, 1) {
    sec[j] = A[i + 1] - A[i];
    j++;
  }
  sec[j] = K - (A[N - 1] - A[0]);
  ll mini = INT_MAX, sum = 0, sumi;
  F(i, 0, j + 1, 1) { sum += sec[i]; }
  F(i, 0, j + 1, 1) {
    sumi = sum - sec[i];
    if (mini > sumi) {
      mini = sumi;
    }
  }

  cout << mini;

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

// Problem: https://atcoder.jp/contests/abc160/tasks/abc160_c
// Type:

#include <bits/stdc++.h>

using namespace std;

typedef int32_t i32;
typedef uint32_t u32;
typedef int64_t i64;
typedef uint64_t u64;

int main() {

  cin.tie(NULL);
  ios_base::sync_with_stdio(false);

  int k, n;
  cin >> k >> n;

  vector<int> m(n);
  for (auto &e : m) {
    cin >> e;
  }

  int d = 0;
  int max_segment = 0;
  for (int i = 1; i < m.size(); ++i) {
    max_segment = max(max_segment, m[i] - m[i - 1]);
    d += m[i] - m[i - 1];
  }
  d += m[0] + (k - m[m.size() - 1]);
  max_segment = max(max_segment, m[0] + (k - m[m.size() - 1]));
  cout << d - max_segment << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

// Problem: https://atcoder.jp/contests/abc160/tasks/abc160_c
// Type:

#include <bits/stdc++.h>

using namespace std;

typedef int32_t i32;
typedef uint32_t u32;
typedef int64_t i64;
typedef uint64_t u64;

int calc_cost(vector<int> &d) {
  int mc = INT_MAX;
  for (int i = 0; i < d.size(); ++i) {
    int c = 0;
    int j = i;
    int w = 0;
    while (w != d.size() - 1) {
      c += d[j];
      j = (j + 1) % d.size();
      ++w;
    }
    mc = min(mc, c);
  }
  return mc;
}

int main() {

  cin.tie(NULL);
  ios_base::sync_with_stdio(false);

  int k, n;
  cin >> k >> n;

  vector<int> m(n);
  for (auto &e : m) {
    cin >> e;
  }

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

  cout << calc_cost(d);

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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
int n, k, a, maxx, fi, pre, now;
int main() {
  scanf("%d%d", &k, &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    pre = now;
    now = a;
    if (i == 0)
      fi = a;
    if (i >= 1)
      maxx = max(maxx, now - pre);
  }
  maxx = max(maxx, k - now + fi);
  printf("%d\\n", k - maxx);
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
int n, k, a[100005], maxx;
int main() {
  scanf("%d%d", &k, &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i < n; i++)
    maxx = max(maxx, a[i] - a[i - 1]);
  maxx = max(maxx, k - a[n - 1] + a[0]);
  printf("%d\\n", k - maxx);
  return 0;
}
Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int k, n, ans;

int main() {
  cin >> k >> n;
  vector<int> A(n);
  rep(i, n) { cin >> A[i]; }

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

  ans = k - *max_element(begin(d), end(d));
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int ans;
int k;
int n;

int main() {
  cin >> k >> n;
  vector<int> A(n);
  rep(i, n) { cin >> A[i]; }

  vector<int> f(n);
  vector<int> b(n);
  vector<int> d(n);

  rep(s, n) {
    rep(i, n) {
      int ff = A[i] - A[s];
      int bb = k - A[i] + A[s];

      if (ff >= 0)
        f[i] = ff;
      else
        f[i] = ff + k;

      if (bb >= 0)
        b[i] = bb;
      else
        b[i] = bb + k;
    }

    int mf = *max_element(f.begin(), f.end());
    int mb = *max_element(b.begin(), b.end());
    d[s] = min(mf, mb);
  }

  ans = *min_element(d.begin(), d.end());
  cout << ans << endl;

  return 0;
}