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

/**Bismillahir Rahmanir Rahim.**/

/**
 *    author:  nasimnoob
 **/

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>

using namespace std;

//----------------------------------------------------------//

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

//-----------------------------------------------------------//
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

#define cube(x) (x * x * x)
#define sqr(x) (x * x)

#define INPUT freopen("input.txt", "r", stdin)
#define OUTPUT freopen("output.txt", "w", stdout)

#define srev reverse(s.begin(), s.end())
#define sortstr sort(s.begin(), s.end());
#define gtl(x) getline(cin, (x))
#define dot(x) fixed << setprecision(x)

#define Case cout << "Case " << ++tt << ": ";

#define PI acos(-1) // 3.1415926535897932
#define MIN 9999999

#define Faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

// scanf("%[^\\n]%*c", str);
// sort(s,s+n,greater<int>());
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);

//--------------------------------------Solution------------------------------//

int main() {
  // Faster;

  int tt = 0;

  string s;
  ll n;

  cin >> n;

  vector<ll> ar;
  ll b;

  for (ll i = 0; i < n; i++) {
    cin >> b;
    ar.push_back(b);
  }

  ll sum = 0;

  for (int i = 0; i < n - 1; i++) {
    ll dif = 0;
    if (ar[i] > ar[i + 1]) {
      dif = ar[i] - ar[i + 1];
      ar[i + 1] = ar[i + 1] + dif;
      sum += dif;
    }
  }

  cout << sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

/**Bismillahir Rahmanir Rahim.**/

/**
 *    author:  nasimnoob
 **/

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>

using namespace std;

//----------------------------------------------------------//

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

//-----------------------------------------------------------//
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

#define cube(x) (x * x * x)
#define sqr(x) (x * x)

#define INPUT freopen("input.txt", "r", stdin)
#define OUTPUT freopen("output.txt", "w", stdout)

#define srev reverse(s.begin(), s.end())
#define sortstr sort(s.begin(), s.end());
#define gtl(x) getline(cin, (x))
#define dot(x) fixed << setprecision(x)

#define Case cout << "Case " << ++tt << ": ";

#define PI acos(-1) // 3.1415926535897932
#define MIN 9999999

#define Faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

// scanf("%[^\\n]%*c", str);
// sort(s,s+n,greater<int>());
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);

//--------------------------------------Solution------------------------------//

int main() {
  // Faster;

  int tt = 0;

  string s;
  ll n;

  cin >> n;

  vector<ll> ar;
  ll b;

  for (ll i = 0; i < n; i++) {
    cin >> b;
    ar.push_back(b);
  }

  ll cnt = 0;
  int temp;

  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j < n; j++) {
      if (ar[i] > ar[j]) {
        swap(ar[i], ar[j]);
        cnt++;
      }
    }
  }

  cout << cnt << endl;

  return 0;
}