Problem p03602 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define mid ((x + y) / 2)
#define left (ind * 2)
#define right (ind * 2 + 1)
#define mp make_pair
#define timer ((double)clock() / CLOCKS_PER_SEC)
#define endl "\n"
#define spc " "
#define d1(x) cerr << #x << ":" << x << endl
#define d2(x, y) cerr << #x << ":" << x << " " << #y << ":" << y << endl
#define d3(x, y, z)                                                            \
  cerr << #x << ":" << x << " " << #y << ":" << y << " " << #z << ":" << z     \
       << endl
#define fast_io()                                                              \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;

typedef long long int lli;
typedef pair<int, int> ii;
typedef pair<lli, ii> iii;
typedef pair<double, double> dd;

const int N = (int)(3e2 + 5);
const int LOG = (int)(20);
const lli inf = (lli)(1e15);

lli n, sp[N][N], ind, ar[N][N], ans;
iii p[N];

int main() {
  fast_io();
  // freopen("inp.in", "r", stdin);

  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> sp[i][j];
      if (i < j) {
        p[++ind].se = {i, j};
        p[ind].fi = sp[i][j];
      }
    }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        if (sp[i][k] + sp[k][j] < sp[i][j]) {
          cout << -1;
          return 0;
        }
      }
    }
  }

  sort(p + 1, p + ind + 1);
  for (int i = 1; i < N; i++) {
    for (int j = 1; j < N; j++) {
      ar[i][j] = inf;
    }
    ar[i][i] = 0;
  }

  for (int i = 1; i <= ind; i++) {
    int a = p[i].se.fi, b = p[i].se.se, c = p[i].fi;
    if (ar[a][b] == sp[a][b])
      continue;
    ar[a][b] = c;
    ar[b][a] = c;
    ans += c;
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        if (ar[j][a] + ar[b][k] + ar[a][b] < ar[j][k]) {
          ar[j][k] = ar[j][a] + ar[b][k] + ar[a][b];
          ar[k][j] = ar[j][a] + ar[b][k] + ar[a][b];
        }
      }
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define mid ((x + y) / 2)
#define left (ind * 2)
#define right (ind * 2 + 1)
#define mp make_pair
#define timer ((double)clock() / CLOCKS_PER_SEC)
#define endl "\n"
#define spc " "
#define d1(x) cerr << #x << ":" << x << endl
#define d2(x, y) cerr << #x << ":" << x << " " << #y << ":" << y << endl
#define d3(x, y, z)                                                            \
  cerr << #x << ":" << x << " " << #y << ":" << y << " " << #z << ":" << z     \
       << endl
#define fast_io()                                                              \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;

typedef long long int lli;
typedef pair<int, int> ii;
typedef pair<lli, ii> iii;
typedef pair<double, double> dd;

const int N = (int)(3e2 + 5);
const int LOG = (int)(20);
const lli inf = (lli)(1e15);

lli n, sp[N][N], ind, ar[N][N], ans;
iii p[N * N];

int main() {
  fast_io();
  // freopen("inp.in", "r", stdin);

  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> sp[i][j];
      if (i < j) {
        p[++ind].se = {i, j};
        p[ind].fi = sp[i][j];
      }
    }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        if (sp[i][k] + sp[k][j] < sp[i][j]) {
          cout << -1;
          return 0;
        }
      }
    }
  }

  sort(p + 1, p + ind + 1);
  for (int i = 1; i < N; i++) {
    for (int j = 1; j < N; j++) {
      ar[i][j] = inf;
    }
    ar[i][i] = 0;
  }

  for (int i = 1; i <= ind; i++) {
    int a = p[i].se.fi, b = p[i].se.se, c = p[i].fi;
    if (ar[a][b] == sp[a][b])
      continue;
    ar[a][b] = c;
    ar[b][a] = c;
    ans += c;
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        if (ar[j][a] + ar[b][k] + ar[a][b] < ar[j][k]) {
          ar[j][k] = ar[j][a] + ar[b][k] + ar[a][b];
          ar[k][j] = ar[j][a] + ar[b][k] + ar[a][b];
        }
      }
    }
  }
  cout << ans;
}