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

// AnkitCode99 here....
#include <bits/stdc++.h>
#define endl "\\n"
typedef long long int ll;
#define MOD 1000000007
#define mp make_pair
#define pll pair<ll, ll>
#define pb push_back
// max xor btw range of two numbers..
#define max_XOR(a, b) (1 << int(log2(a ^ b) + 1)) - 1
#define vl vector<ll>
#define inf ll(1e18)
#define all(v) v.begin(), v.end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define rep(i, a, b) for (long long int i = a; i < b; i++)
#define nrep(i, a, b) for (long long int i = a; i >= b; i--)
#define ceil(a, b) a / b + ((a % b == 0) ? 0 : 1)
using namespace std;

const ll sz = 100005;
const ll szz = 1000006;

int main() {
  fast;

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

  ll k, n;
  cin >> k >> n;

  ll ar[n];
  rep(i, 0, n) { cin >> ar[i]; }
  //    ll sum=0;
  //    rep(i,0,n-1)
  //    {
  //    	sum+=(ar[i+1]-ar[i]);
  //    }
  // cout<<sum<<endl;
  ll dif[n];
  dif[0] = (k - ar[n - 1]) + ar[0];
  // cout<<dif[0]<<" ";
  ll sum = dif[0];
  rep(i, 1, n) {
    dif[i] = ar[i] - ar[i - 1];
    sum += dif[i];
    // 	cout<<dif[i]<<" ";
  }
  // cout<<endl;
  //  ll j;
  //    rep(i,1,n)
  //    {
  //    	ll j=(i+1)%n;
  //    	ll temp=0;
  //    	while(j!=i)
  //    	{
  //    		temp+=dif[j];
  //    		j=j+1;
  //    		j%=n;
  //    	}
  //    	//cout<<temp<<" ";
  //    	sum=min(sum,temp);
  //    }

  ll ans = inf;
  rep(i, 0, n) {
    if (sum - dif[i] < ans) {
      ans = sum - dif[i];
    }
  }
  cout << ans;
}
// Goodbye...


Error: TLE

Fixed submission:

// AnkitCode99 here....
#include <bits/stdc++.h>
#define endl "\\n"
typedef long long int ll;
#define MOD 1000000007
#define mp make_pair
#define pll pair<ll, ll>
#define pb push_back
// max xor btw range of two numbers..
#define max_XOR(a, b) (1 << int(log2(a ^ b) + 1)) - 1
#define vl vector<ll>
#define inf ll(1e18)
#define all(v) v.begin(), v.end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define rep(i, a, b) for (long long int i = a; i < b; i++)
#define nrep(i, a, b) for (long long int i = a; i >= b; i--)
#define ceil(a, b) a / b + ((a % b == 0) ? 0 : 1)
using namespace std;

const ll sz = 100005;
const ll szz = 1000006;

int main() {
  fast;

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

  ll k, n;
  cin >> k >> n;

  ll ar[n];
  rep(i, 0, n) { cin >> ar[i]; }
  ll sum = 0;
  rep(i, 0, n - 1) { sum += (ar[i + 1] - ar[i]); }
  // cout<<sum<<endl;
  ll dif[n];
  dif[0] = (k - ar[n - 1]) + ar[0];
  // cout<<dif[0]<<" ";
  rep(i, 1, n) {
    dif[i] = ar[i] - ar[i - 1];
    // 	cout<<dif[i]<<" ";
  }
  // cout<<endl;
  //  ll j;
  rep(i, 1, n) {
    ll j = (i + 1) % n;
    ll temp = 0;
    while (j != i) {
      temp += dif[j];
      j = j + 1;
      j %= n;
    }
    // cout<<temp<<" ";
    sum = min(sum, temp);
  }
  cout << sum;
}
// Goodbye...