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

#include <bits/stdc++.h>
#define ll long long
#define clr(c) memset(c, 0, sizeof(c))
#define inf 0x7fffffff
const int mod = 1e9 + 7;
const int M = 4e5 + 5;
const int N = 2e5 + 10;
using namespace std;
inline ll read() {
  ll sum = 0, b = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      b = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return b * sum;
}
ll t, n, m, k;
ll a, b, c;
ll ans;
constexpr ll MOD = 1000000007ll;
// constexpr ll MOD=998244353ll;
constexpr int MAX = 510000;
ll fact[MAX], fact_inv[MAX];

// 繰り返し二乗法
ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

ll comb(ll n, ll r) {
  return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD;
}

int main() {
  ll K;
  cin >> K;
  ll N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> distance(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    if (i > 0) {
      distance.at(i - 1) = A.at(i) - A.at(i - 1);
    }
  }
  distance.at(N - 1) = abs(K - A.at(N - 1) + A.at(0));

  ll min = 9999999999999;
  ll temp = 0;
  for (int i = 0; i < N; i++) {
    temp = K - distance.at(i);
    if (temp < min)
      min = temp;

    if (i - 1 == -1) {
      temp = K - distance.at(N - 1);
    } else {
      temp = K - distance.at(i - 1);
    }
    if (temp < min)
      min = temp;
  }
  cout << min;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define clr(c) memset(c, 0, sizeof(c))
#define inf 0x7fffffff
const int mod = 1e9 + 7;
const int M = 4e5 + 5;
const int N = 2e5 + 10;
using namespace std;
inline ll read() {
  ll sum = 0, b = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      b = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return b * sum;
}
ll t, n, m, k;
ll a, b, c;
ll ans;
constexpr ll MOD = 1000000007ll;
// constexpr ll MOD=998244353ll;
constexpr int MAX = 510000;
ll fact[MAX], fact_inv[MAX];

// 繰り返し二乗法
ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

ll comb(ll n, ll r) {
  return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD;
}

int main() {
  ll K;
  cin >> K;
  ll N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> distance(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    if (i > 0) {
      distance.at(i - 1) = A.at(i) - A.at(i - 1);
    }
  }
  distance.at(N - 1) = abs(20 - A.at(N - 1) + A.at(0));

  ll min = 9999999999999;
  ll temp = 0;
  for (int i = 0; i < N; i++) {
    temp = 0;
    int r = i;
    int count = 0;
    while (count < N - 1) {
      if (r < N) {
        temp += distance.at(r);
      }
      if (r >= N) {
        temp += distance.at(r - N);
      }
      r++;
      count++;
    }

    if (min > temp) {
      min = temp;
    }
    int l = i;
    count = 0;
    temp = 0;
    while (count < N - 1) {
      if (l >= 0) {
        temp += distance.at(l);
      } else {
        temp += distance.at(N + l);
      }
      l--;
      count++;
    }
    if (min > temp) {
      min = temp;
    }
  }
  cout << min;

  return 0;
}