Language: C++

#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <queue>
#include <map>
#include <utility>
#include <algorithm>
using namespace std;

#define int long long
#define rep(i, n) for (int i=0;i<(int)(n);++i)
#define rep1(i, n) for (int i=1;i<=(int)(n);++i)
#define all(a) begin(a),end(a)

using vi = vector<int>;
using pii = pair<int, int>;

const int inf = 1e9 + 5;

int N;

int data[305][305];
int dist[305][305];

signed main()
{
	cin >> N;

	rep(i, N) rep(j, N) {
		cin >> data[i][j];
	}

	vector<pair<int, pii>> a;

	rep(i, N) rep(j, i) {
		if (i == j) continue;

		a.emplace_back(data[j][i], pii(j, i));
	}

	sort(all(a));

	rep(i, N) rep(j, N) dist[i][j] = i == j ? 0 : inf;

	int ans = 0;

	rep(i, a.size()) {
		auto item = a[i];

		int d = item.first;
		int u = item.second.first, v = item.second.second;

		if (dist[u][v] < d) {
			cout << -1 << endl;
			return 0;
		}

		if (dist[u][v] == d) continue;

		ans += d;
		dist[u][v] = dist[v][u] = d;

		rep(j, N) {
			dist[u][j] = dist[j][u] = min(dist[u][j], dist[u][v] + dist[v][j]);
		}
		rep(j, N) {
			dist[v][j] = dist[j][v] = min(dist[v][j], dist[v][u] + dist[u][j]);
		}
	}

	cout << ans << endl;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:31:24: error: reference to 'data' is ambiguous
   31 |                 cin >> data[i][j];
      |                        ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:23:5: note:                 'long long int data [305][305]'
   23 | int data[305][305];
      |     ^~~~
a.cc:39:32: error: reference to 'data' is ambiguous
   39 |                 a.emplace_back(data[j][i], pii(j, i));
      |                                ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:23:5: note:                 'long long int data [305][305]'
   23 | int data[305][305];
      |     ^~~~