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

#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#define ll long long
#define ull unsigned long long
#define uint unsigned
#define l_double long double
#define PB push_back
#define EB emplace_back
#define gcd __gcd
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define PIE (3.1415926535897932384626433832795)
#define MOD 1'00'00'00'007
#define TEST                                                                   \\
  uint _{};                                                                    \\
  cin >> _;                                                                    \\
  while (_--)
using namespace std;
ull lcm(ull a, ull b) { return ((a * b) / gcd(a, b)); }
ull countDigits(const ull &N) { return (floor(log10(N)) + 1); }
bool isPowOfTwo(ull const &N) { return (N && (!(N & (N - 1)))); }
// ----------------------------------------------------------

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << boolalpha << fixed << setprecision(3);
  // --------------------------------------

  int p{}, n{};
  cin >> p >> n;
  vector<int> arr(n);
  for (auto &val : arr)
    cin >> val;
  arr.EB(p + arr.at(0));

  int ans{INT_MAX};
  for (int i{}; i < n; ++i) {
    // cout<<"TEST: "<<p - (arr.at(i+1) - arr.at(i))<<endl;
    ans = min(ans, p - (arr.at(i + 1) - arr.at(i)));
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#define ll long long
#define ull unsigned long long
#define uint unsigned
#define l_double long double
#define PB push_back
#define EB emplace_back
#define gcd __gcd
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define PIE (3.1415926535897932384626433832795)
#define MOD 1'00'00'00'007
#define TEST                                                                   \\
  uint _{};                                                                    \\
  cin >> _;                                                                    \\
  while (_--)
using namespace std;
ull lcm(ull a, ull b) { return ((a * b) / gcd(a, b)); }
ull countDigits(const ull &N) { return (floor(log10(N)) + 1); }
bool isPowOfTwo(ull const &N) { return (N && (!(N & (N - 1)))); }
// ----------------------------------------------------------

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << boolalpha << fixed << setprecision(3);
  // --------------------------------------

  int p{}, n{};
  cin >> p >> n;
  vector<int> arr(n);
  for (auto &val : arr)
    cin >> val;

  int ans{INT_MAX};
  for (int i{}; i < n; ++i) {
    int traveled{};
    int steps{n - 1};
    for (int j{i}; steps--; ++j) {
      int dist{arr.at((j + 1) % n) - arr.at(j % n)};
      dist = ((dist < 0) ? 20 + dist : dist);

      traveled += dist;
    }
    // cout<<"TEST: "<<traveled<<endl;
    ans = min(traveled, ans);
  }
  cout << ans << endl;
  return 0;
}