Problem p02725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
// 登録しよう！→ https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ
using namespace std;
using ll = long long;
ll gcd(ll a, ll b) {
  return b ? gcd(b, a % b) : a;
} // a,bの最大公約数(gcd)を求める
ll lcm(ll a, ll b) {
  return abs(a * b) / gcd(a, b);
} // a,bの最小公倍数(lcm)を求める
vector<ll> enum_div(ll n) {
  vector<ll> ret;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i != 1 && i * i != n)
        ret.push_back(n / i);
    }
  }
  ret.push_back(n);
  return ret;
}
// ↑nの約数を求める
vector<bool> IsPrime;
void sieve(size_t max) {
  if (max + 1 > IsPrime.size())
    IsPrime.resize(max + 1, true);
  IsPrime[0] = false;
  IsPrime[1] = false;
  for (size_t i = 2; i * i <= max; ++i)
    if (IsPrime[i])
      for (size_t j = 2; i * j <= max; ++j)
        IsPrime[i * j] = false;
}
// ↑エラトステネスの篩で素数を求める
#define roundup(divisor, dividend)                                             \\
  (divisor + (dividend - 1)) / dividend // 切り上げ割り算
#define all(x) (x).begin(), (x).end() // xの初めから終わりまでのポインタ
#define size_t ll                     // size_tは自動でllに変換される
#define pb(x) push_back(x)
#define gre greater                     // sortを降順にする
#define pri_queue priority_queue        // 優先度付きキュー
#define syo(x) fixed << setprecision(x) // iostreamで小数をx桁表示

void solve(long long K, long long N, std::vector<long long> A) {
  pri_queue<ll> x;
  ll n = 0;
  for (int i = 0; i < N - 1; i++) {
    x.push(abs(A[i] - A[i + 1]));
    n += abs(A[i] - A[i + 1]);
  }
  x.push(K - abs(A[0] - A[N - 1]));
  n += K - abs(A[0] - A[N - 1]);
  cout << n - x.top() << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long K;
  scanf("%lld", &K);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(K, N, std::move(A));
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// 登録しよう！→ https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ
using namespace std;
using ll = long long;
ll gcd(ll a, ll b) {
  return b ? gcd(b, a % b) : a;
} // a,bの最大公約数(gcd)を求める
ll lcm(ll a, ll b) {
  return abs(a * b) / gcd(a, b);
} // a,bの最小公倍数(lcm)を求める
vector<ll> enum_div(ll n) {
  vector<ll> ret;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i != 1 && i * i != n)
        ret.push_back(n / i);
    }
  }
  ret.push_back(n);
  return ret;
}
// ↑nの約数を求める
vector<bool> IsPrime;
void sieve(size_t max) {
  if (max + 1 > IsPrime.size())
    IsPrime.resize(max + 1, true);
  IsPrime[0] = false;
  IsPrime[1] = false;
  for (size_t i = 2; i * i <= max; ++i)
    if (IsPrime[i])
      for (size_t j = 2; i * j <= max; ++j)
        IsPrime[i * j] = false;
}
// ↑エラトステネスの篩で素数を求める
#define roundup(divisor, dividend)                                             \\
  (divisor + (dividend - 1)) / dividend // 切り上げ割り算
#define all(x) (x).begin(), (x).end() // xの初めから終わりまでのポインタ
#define size_t ll                     // size_tは自動でllに変換される
#define pb(x) push_back(x)
#define gre greater                     // sortを降順にする
#define pri_queue priority_queue        // 優先度付きキュー
#define syo(x) fixed << setprecision(x) // iostreamで小数をx桁表示

void solve(long long K, long long N, std::deque<long long> A) {
  ll ans = 999999999;
  for (int i = 0; i < N; i++) {
    ll x = 0;
    for (int l = 0; l < N - 1; l++) {
      x += min(abs(A[l] - A[l + 1]), K - A[l]);
    }
    ans = min(ans, x);
    A.push_back(A.front());
    A.pop_front();
  }
  cout << ans << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long K;
  scanf("%lld", &K);
  long long N;
  scanf("%lld", &N);
  std::deque<long long> A;
  for (int i = 0; i < N; i++) {
    ll x;
    scanf("%lld", &x);
    A.push_front(x);
  }
  solve(K, N, std::move(A));
}