Problem p02583 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
//  using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define forn(i, n) for (int i = 1; i <= n; i++)
#define me0(x) memset(x, 0, sizeof(x))
#define me1(x) memset(x, -1, sizeof(x))
#define all(v) (v.begin(), v.end())
#define tests(x)                                                               \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> pbds;
int INF = 1e18;
const int N = 1e6 + 2;
void run() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("intput.txt", "r", stdin);
  freopen("o2.txt", "w", stdout);
#endif
}

int solve() {
  int n;
  cin >> n;
  int a[n + 2], ans = 0;
  forn(i, n) cin >> a[i];
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      for (int k = j + 1; k <= n; k++) {
        if (a[i] + a[j] > a[k]) {
          if (a[i] != a[j] && a[j] != a[k])
            ans++;
        }
      }
    }
  }
  return ans;
}
int32_t main() {
  run();
  // tests(t) {
  cout << solve() << "\\n";
  //}
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
//  using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define forn(i, n) for (int i = 1; i <= n; i++)
#define me0(x) memset(x, 0, sizeof(x))
#define me1(x) memset(x, -1, sizeof(x))
#define all(v) (v.begin(), v.end())
#define tests(x)                                                               \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> pbds;
int INF = 1e18;
const int N = 1e6 + 2;
void run() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("intput.txt", "r", stdin);
  freopen("o2.txt", "w", stdout);
#endif
}

int solve() {
  int n;
  cin >> n;
  int a[+2], ans = 0;
  forn(i, n) cin >> a[i];
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      for (int k = j + 1; k <= n; k++) {
        if (a[i] + a[j] > a[k])
          ans++;
      }
    }
  }
  return ans;
}
int32_t main() {
  run();
  // tests(t) {
  cout << solve() << "\\n";
  //}
  return 0;
}