stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <algorithm>

#include <iostream>

#include <cstring>

#include <cstdio>

#include <cmath>



using namespace std;



long long sum[300000];



int main ()

{

	int n;

	long long ans,a;

	scanf ("%d",&n);

	sum[0]=0;

	for (int i=1;i<=n;i++)

	{

		scanf ("%lld",&a);

		sum[i]=sum[i-1]+a;

	}

	ans=abs(sum[1]*2-sum[n]);

	for (int i=2;i<n;i++)

		ans=min(ans,abs(sum[i]*2-sum[n]));

	printf ("%lld\n",ans);

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.1713183671199845x):
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#define N 2*1000000

#define min(x,y) (x)<(y)?(x):(y)

long long a[N];

long long sum[N + 400];

long long Min =1e10;

int main() {

	long long n, m;

	scanf("%lld", &n);

	sum[0] = 0;

	for (int i = 1; i <= n; i++) {

		scanf("%lld", &a[i]);

		sum[i] = sum[i - 1] + a[i];

	}

	for (int i = 1; i <= n - 1; i++) {

		Min = min(abs(sum[i] - (sum[n] - sum[i])), Min);

	}

	printf("%lld\n", Min);

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

const int s = 2e5+10;

int arr[s];

long long pre[s];

int main()

{

	int n;

	long long sum,mn;

	while(~scanf("%d",&n)){

		mn=0xffffffffffffffl;

		sum=0;

		for(int i=0;i<n;++i){

			scanf("%d",&arr[i]);

			sum+=arr[i];

		}

		pre[0]=arr[0];

		for(int i=1;i<n;++i){

			pre[i]=pre[i-1]+arr[i];

		}

		for(int i=0;i<n-1;++i){

			mn=min(mn,abs(sum-2*pre[i]));

		}

		printf("%lld\n",mn);

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.1740912429800145x):
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#define N 2*1000000

#define min(x,y) (x)<(y)?(x):(y)

long long a[N];

long long sum[N + 400];

long long Min =1e10;

int main() {

	long long n, m;

	scanf("%lld", &n);

	sum[0] = 0;

	for (int i = 1; i <= n; i++) {

		scanf("%lld", &a[i]);

		sum[i] = sum[i - 1] + a[i];

	}

	for (int i = 1; i <= n - 1; i++) {

		Min = min(abs(sum[i] - (sum[n] - sum[i])), Min);

	}

	printf("%lld\n", Min);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<math.h>

using namespace std;



#define long_long_max 9223372036854775807



int main()

{

	int n;

	cin >> n;

	long long total = 0;

	long long *p = (long long*)malloc(n * sizeof(long long));

	long long *p1 = (long long*)malloc(n * sizeof(long long));

	for (int i = 0; i < n; i++)

	{

		cin >> p[i];

		total = total + p[i];

	}

	for (int i = 0; i < n; i++)

	{

		if (i != 0)

		{

			p1[i] = p1[i - 1] + p[i];

		}

		else

		{

			p1[i] = p[i];

		}

	}

	long long min = long_long_max;

	for (int i = 0; i < n - 1; i++)

	{

		if (abs(2 * p1[i] - total) < min)

		{

			min = abs(2 * p1[i] - total);

		}

	}

	cout << min << endl;

	free(p); free(p1);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.25282628481459x):
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#define N 2*1000000

#define min(x,y) (x)<(y)?(x):(y)

long long a[N];

long long sum[N + 400];

long long Min =1e10;

int main() {

	long long n, m;

	scanf("%lld", &n);

	sum[0] = 0;

	for (int i = 1; i <= n; i++) {

		scanf("%lld", &a[i]);

		sum[i] = sum[i - 1] + a[i];

	}

	for (int i = 1; i <= n - 1; i++) {

		Min = min(abs(sum[i] - (sum[n] - sum[i])), Min);

	}

	printf("%lld\n", Min);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cmath>

using namespace std;



int main()

{

	int n;

	long long int temp,*a;

	long long int sum=0;

	cin>>n;

	a=new long long int[n];

	for(int i=0;i<n;i++)

	{

		cin>>a[i];

		sum+=a[i];

	}

	sum-=2*a[0];

	temp=abs(sum);

	for(int i=1;i<n-1;i++)

	{

		sum-=2*a[i];

		if(temp>abs(sum))

			temp=abs(sum);

		}

	cout<<temp;

	return 0;

 }

  

// Faster C++ solution for the same problem (measured speedup: 5.241873790982227x):
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#define N 2*1000000

#define min(x,y) (x)<(y)?(x):(y)

long long a[N];

long long sum[N + 400];

long long Min =1e10;

int main() {

	long long n, m;

	scanf("%lld", &n);

	sum[0] = 0;

	for (int i = 1; i <= n; i++) {

		scanf("%lld", &a[i]);

		sum[i] = sum[i - 1] + a[i];

	}

	for (int i = 1; i <= n - 1; i++) {

		Min = min(abs(sum[i] - (sum[n] - sum[i])), Min);

	}

	printf("%lld\n", Min);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cmath>



using namespace std;

const int M = 2e5;

long long a[M];

long long sum[M];

int main()

{

	int n;

	while (cin >> n)

	{

		long long s = 0;

		sum[0] = 0;

		for (int i = 1; i <= n; i++)

		{

			cin >> a[i];

			sum[i] = sum[i - 1] + a[i];

			s += a[i];

		}



		long long Min = abs(2 * sum[1] - s);

		for (int i = 1; i<n; i++)

		{

			if (Min > abs(2 * sum[i] - s))

				Min = abs(2 * sum[i] - s);

		}

		cout << Min << endl;

	}



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.2491242394711195x):
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#define N 2*1000000

#define min(x,y) (x)<(y)?(x):(y)

long long a[N];

long long sum[N + 400];

long long Min =1e10;

int main() {

	long long n, m;

	scanf("%lld", &n);

	sum[0] = 0;

	for (int i = 1; i <= n; i++) {

		scanf("%lld", &a[i]);

		sum[i] = sum[i - 1] + a[i];

	}

	for (int i = 1; i <= n - 1; i++) {

		Min = min(abs(sum[i] - (sum[n] - sum[i])), Min);

	}

	printf("%lld\n", Min);

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

const ll LL_INF = 0x3f3f3f3f3f3f3f3f;



ll a[200005];

int main() {

    int n;

    cin >> n;

    for (int i = 0; i < n; i++) {

        scanf("%lld", &a[i]);

        if (i > 0)

            a[i] += a[i - 1];

    }

    ll ans = LL_INF;

    for (int i = 0; i < n - 1; i++) {

        if (abs(a[n - 1] - 2 * a[i]) < ans)

            ans = abs(a[n - 1] - 2 * a[i]);

    }

    cout << ans << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.250188028785232x):
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#define N 2*1000000

#define min(x,y) (x)<(y)?(x):(y)

long long a[N];

long long sum[N + 400];

long long Min =1e10;

int main() {

	long long n, m;

	scanf("%lld", &n);

	sum[0] = 0;

	for (int i = 1; i <= n; i++) {

		scanf("%lld", &a[i]);

		sum[i] = sum[i - 1] + a[i];

	}

	for (int i = 1; i <= n - 1; i++) {

		Min = min(abs(sum[i] - (sum[n] - sum[i])), Min);

	}

	printf("%lld\n", Min);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cmath>

#include<cstdio>

#include<vector>

using namespace std;



int main(){

  long long int a;

  long long int b;



  while(cin >> a >> b){

    int ans;

    bool p[2000000];

    vector<vector<int> > part;

    

    ans = 2*b+1;

    part.resize(b*2+1);

    int N = (int)sqrt(a+b)+1;

    for(int i=0;i<=N;i++)p[i] = true;

    p[0] = false;

    p[1] = false;

    p[2] = true;

    

    int o = 0;

    if((a-b)%2)o++;

    for(int i=o;i<=2*b;i+=2)part[i].push_back(2);

    

    int c = 3;

    while(c*c<=N){

      for(int i=3*c;i<=N;i+=2*c)p[i] = false;

      do{c+=2;}while(!p[c]);

    }

    

    

    c = 3;

    while(c<=N){

      int t = (a-b)%c;

      for(int i=(c-t)%c;i<=2*b;i+=c)part[i].push_back(c);

      do{c+=2;}while(!p[c]);

    }

    

    

    for(int i=0;i<=2*b;i++){

      long long int tmp = a-b+i;

      int prv = 1<<30;

      if(tmp<=1)ans--;

      else{

	

	if(!part[i].empty()){

	  bool f = false;

	  for(int j=0;j<(int)part[i].size();j++){

	    int cnt = 0;

	    while(!(tmp%part[i][j])){

	      tmp /= part[i][j];

	      cnt++;

	    }

	    if(prv < cnt){

	      f=true;

	      break;

	    }else{

	      prv = cnt;

	    }

	    if(tmp==1)break;

	  }

	  if(f)ans--;

	}

	

      }

    }

    

    printf("%d\n",ans);

  }

}

// Faster C++ solution for the same problem (measured speedup: 1.236152330328408x):
#include<iostream>

#include<cmath>

#include<cstdio>

#include<vector>

using namespace std;



int main(){

  long long int a;

  long long int b;

 

  while(cin >> a >> b){

    int ans;

    bool p[1001001];

    int num[1001001];



    for(int i=0;i<=2*b;i++){

      if(a-b+i<2)num[i] = 0;

      else num[i] = 1<<30;

    }



    int N = (int)sqrt(a+b) + 1;

    for(int i=2;i<=N;i++)p[i] = true;

    p[0] = false;

    p[1] = false;

    

    int c = 2;

    while(c<=N){

      for(int i=2*c;i<=N;i+=c)p[i] = false;



      int s = (c - (a-b)%c)%c;

      for(int j=s;j<=2*b;j+=c){

	long long int tmp = a-b+j;

	if(num[j]){



	  int cnt = 0;

	  while(!(tmp%c)){

	    tmp /= c;

	    cnt++;

	  }



	  if(cnt){

	    if(num[j] < cnt)num[j] = 0;

	    else num[j] = cnt;

	  }

	}

      }



      while(!p[++c]);

    }



    ans = 0;

    for(int i=0;i<=2*b;i++){

      if(num[i])ans++;

    }



    printf("%d\n",ans);

  }

}
// Slower C++ solution:
#include<iostream>

#include<cmath>

#include<cstdio>

#include<vector>

using namespace std;



int main(){

  long long int a;

  long long int b;



  while(cin >> a >> b){

    int ans;

    bool p[2000000];

    vector<vector<int> > part;

    

    ans = 2*b+1;

    part.resize(b*2+1);

    int N = (int)sqrt(a+b)+1;

    for(int i=0;i<=N;i++)p[i] = true;

    p[0] = false;

    p[1] = false;

    p[2] = true;

    

    int o = 0;

    if((a-b)%2)o++;

    for(int i=o;i<=2*b;i+=2)part[i].push_back(2);

    

    int c = 3;

    while(c*c<=N){

      for(int i=3*c;i<=N;i+=2*c)p[i] = false;

      do{c+=2;}while(!p[c]);

    }

    

    

    c = 3;

    while(c<=N){

      int t = (a-b)%c;

      for(int i=(c-t)%c;i<=2*b;i+=c)part[i].push_back(c);

      do{c+=2;}while(!p[c]);

    }

    

    

    for(int i=0;i<=2*b;i++){

      long long int tmp = a-b+i;

      int prv = 1<<30;

      if(tmp<=1)ans--;

      else{

	

	if(!part[i].empty()){

	  bool f = false;

	  for(int j=0;j<(int)part[i].size();j++){

	    int cnt = 0;

	    while(!(tmp%part[i][j])){

	      tmp /= part[i][j];

	      cnt++;

	    }

	    if(prv < cnt){

	      f=true;

	      break;

	    }else{

	      prv = cnt;

	    }

	    if(tmp==1)break;

	  }

	  if(f)ans--;

	}

	

      }

    }

    

    printf("%d\n",ans);

  }

}

// Faster C++ solution for the same problem (measured speedup: 1.2673724884032735x):
#include<iostream>

#include<cmath>

#include<cstdio>

using namespace std;



int main(){

  long long int a;

  long long int b;

  while(cin >> a >> b){

    int ans;

    bool p[1001001];

    int num[1000001];



    for(int i=0;i<=2*b;i++){

      if(a-b+i<2)num[i] = 0;

      else num[i] = 1<<30;

    }



    int N = (int)sqrt(a+b);

    for(int i=2;i<=N;i++)p[i] = true;

    

    int c = 2;

    while(c<=N){

      for(int i=2*c;i<=N;i+=c)p[i] = false;



      int s = (c-(a-b)%c)%c;

      for(int j=s;j<=2*b;j+=c){

	long long int tmp = a-b+j;

	if(num[j]){



	  int cnt = 0;

	  while(!(tmp%c)){

	    tmp /= c;

	    cnt++;

	  }



	  if(cnt){

	    if(num[j] < cnt)num[j] = 0;

	    else num[j] = cnt;

	  }

	}

      }



      while(!p[++c]);

    }



    ans = 0;

    for(int i=0;i<=2*b;i++){

      if(num[i])ans++;

    }



    printf("%d\n",ans);

  }

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

 

typedef long long LL;

typedef pair <int,int> pii;

const double pi = acos(-1.0);

const double eps = 1e-8;

const int mod = 1e9 + 7;

const int inf = 1e9;

const LL INF = 1e18;

const int N = 1e5 + 5;

 

int main() {

  ios_base::sync_with_stdio(0); cin.tie(0);

  #ifdef LOCAL

    freopen("input.txt", "r", stdin);

  #endif

  LL a, b, c, k;

  cin >> a >> b >> c >> k;

  LL ans = 0;

  ans += min(k, a);

  k -= min(k, a);

  k -= min(k, b);

  ans -= min(k, c);

  cout << ans << endl;

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.319066181341707x):
#include <stdio.h>





int main()

{	 

	int a, b, c, k, x;

	

	scanf("%d%d%d%d", &a, &b, &c, &k);

	if(a > k) x = k;

	else{

		x = a, k -= a;

		if(k > b) x -= k - b;

	}

	printf("%d\n", x);

		

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int main()

{

  int64_t A,B,C,K;

  cin>>A>>B>>C>>K;

  if(A>=K)return cout<<K<<endl,0;

  K-=A;

  if(B>=K)return cout<<A<<endl,0;

  K-=B;

  return cout<<A-K<<endl,0;

}

// Faster C++ solution for the same problem (measured speedup: 5.249649250371963x):
#include<stdio.h>

int main(void)

{

  int A,B,C,K;

  if(scanf("%d %d %d %d",&A,&B,&C,&K)==4)

  {

    if(A>=K)return printf("%d\n",K),0;

    else if(B>=K-A)return printf("%d\n",A),0;

    else return printf("%d\n",2*A+B-K),0;

  }

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

using namespace std;

int n, t, a[305][305], b[305][305];

long long s;

int main() {

	int i, j, k;

	scanf("%d", &n);

	for(i=0; i<n; i++) for(j=0; j<n; j++) scanf("%d", &a[i][j]);

	for(k=0; k<n; k++) for(i=0; i<n; i++) for(j=0; j<n; j++) {

		t = a[i][k] + a[k][j];

		if(a[i][j]>t) {puts("-1"); return 0;}

		else if(a[i][j]==t && k!=i && k!=j) b[i][j] = 1;

	}

	for(i=0; i<n; i++) for(j=i+1; j<n; j++) s += a[i][j] * !b[i][j];

	cout<<s;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.196722836371459x):
#include <cstdio>

int n, t, a[305][305], b[305][305];

long long s;

int main() {

	int i, j, k;

	scanf("%d", &n);

	for(i=0; i<n; i++) for(j=0; j<n; j++) scanf("%d", &a[i][j]);

	for(k=0; k<n; k++) for(i=0; i<n; i++) for(j=0; j<n; j++) {

		t = a[i][k] + a[k][j];

		if(a[i][j]>t) {puts("-1"); return 0;}

		else if(a[i][j]==t && k!=i && k!=j) b[i][j] = 1;

	}

	for(i=0; i<n; i++) for(j=i+1; j<n; j++) s += a[i][j] * !b[i][j];

	printf("%lld", s);

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <list>

#include <vector>

#include <unistd.h>

#include <algorithm>

#define ll long long

using namespace std;

ll N;

ll m[301][301]; 

int main(){

	cin >> N;

	memset(m, -1, sizeof(m));

	for ( ll i = 0 ; i < N ; i ++ ){

		for ( ll j = 0 ; j < N ; j ++ ){

			cin >> m[i][j];

		}

	}

	ll tot = 0;

	for ( ll i = 0 ; i < N ; i ++ ){

		for ( ll j = 0 ; j < N ; j ++ ){

			if ( i == j ) continue;

			bool flag = true;

			for ( ll k = 0 ; k < N ; k ++ ){

				if ( k == i || k == j ) continue;

				if ( m[i][k] + m[k][j] < m[i][j] )  {

					cout << -1 << endl;

					return 0;

				}

				if ( m[i][k] + m[k][j] == m[i][j] ) flag = false;

			}

			if ( flag ){

					tot += m[i][j];

			}                

		}

	}

	cout << tot / 2 << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.7324494048444594x):
#include<bits/stdc++.h>

using namespace std;



#define ll      long long

#define MAX     1000007

#define inf     99999999999999

#define MOD     1000000007

#define pii     pair<int,int>

#define FIN     freopen("input.txt","r",stdin);

#define FOUT    freopen("output.txt","w",stdout);



ll adj[305][305];

ll dis[305][305];

bool mark[305][305];

ll i,j,k;



int main ()

{

    ll n;

    scanf("%lld",&n);

    for(int i=1;i<=n;i++)

    {

        for(j=1;j<=n;j++)

        {

            scanf("%lld",&adj[i][j]);

            dis[i][j] = adj[i][j];

        }

    }

    for(k=1;k<=n;k++)

    {

        for(i=1;i<=n;i++)

        {

            for(j=1;j<=n;j++)

            {

                if(dis[i][j] > dis[i][k] + dis[k][j])

                {



                    dis[i][j] = dis[i][k] + dis[k][j];

                }

                else if(i!=k && k!=j &&  dis[i][j] == dis[i][k] + dis[k][j])

                {

                    mark[i][j] = 1;

                }

            }

        }

    }

    for(i=1;i<=n;i++)

    {

        for(j=1;j<=n;j++)

        {

            if(dis[i][j]!=adj[i][j])

            {

                printf("-1\n");

                return 0;

            }

        }

    }

    ll res = 0;

    for(i=1;i<=n;i++)

    {

        for(j=1;j<=n;j++)

        {

            if(!mark[i][j])

            {

                res += dis[i][j];

                mark[j][i] = 1;

            }

        }

    }

    printf("%lld\n",res);

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <list>

#include <vector>

#include <unistd.h>

#include <algorithm>

#define ll long long

using namespace std;

ll N;

ll m[301][301]; 

int main(){

	cin >> N;

	memset(m, -1, sizeof(m));

	for ( ll i = 0 ; i < N ; i ++ ){

		for ( ll j = 0 ; j < N ; j ++ ){

			cin >> m[i][j];

		}

	}

	ll tot = 0;

	for ( ll i = 0 ; i < N ; i ++ ){

		for ( ll j = 0 ; j < N ; j ++ ){

			if ( i == j ) continue;

			bool flag = true;

			for ( ll k = 0 ; k < N ; k ++ ){

				if ( k == i || k == j ) continue;

				if ( m[i][k] + m[k][j] < m[i][j] )  {

					cout << -1 << endl;

					return 0;

				}

				if ( m[i][k] + m[k][j] == m[i][j] ) flag = false;

			}

			if ( flag ){

					tot += m[i][j];

			}                

		}

	}

	cout << tot / 2 << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.7258888292803563x):
#include<bits/stdc++.h>

using namespace std;

#define MX 309

#define inf 1e15+5

long long n;

long long g[MX][MX];

long long d[MX][MX];

bool v[MX][MX];

void fw(int n)

{



    for(int k=0; k<n; k++)

    {

        for(int i=0; i<n; i++)

            for(int j=0; j<n; j++)

            {

                d[i][j]=min(d[i][k]+d[k][j],d[i][j]);

            }

    }



    for(int i=0;i<n;i++){



        for(int j=0;j<n;j++){



            if(d[i][j]!=g[i][j]){



                cout<<-1<<endl;

                return;



            }

        }



    }



    for(int k=0; k<n; k++)

    {

        for(int i=0; i<n; i++)



            for(int j=0; j<n; j++)

            {

                long long x=g[i][j];

                long long y=g[i][k]+g[k][j];

                if(i!=k && j!=k && x==y) v[i][j]=v[j][i]=true;



            }

    }



    long long ans=0;

    for(int i=0;i<n;i++){



        for(int j=0;j<n;j++){

            if(v[i][j]!=true)

            {

                ans+=g[i][j];

            }

        }



    }



    cout<< ans/2 <<endl;

return;

}



int main()

{





        cin>>n;

        for(int i=0;i<n;i++)

            for(int j=0;j<n;j++)

                {

                cin>>g[i][j];

                d[i][j]=g[i][j];

                }

        fw(n);

        return 0;



}
// Slower C++ solution:
#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <list>

#include <vector>

#include <unistd.h>

#include <algorithm>

#define ll long long

using namespace std;

ll N;

ll m[301][301]; 

int main(){

	cin >> N;

	memset(m, -1, sizeof(m));

	for ( ll i = 0 ; i < N ; i ++ ){

		for ( ll j = 0 ; j < N ; j ++ ){

			cin >> m[i][j];

		}

	}

	ll tot = 0;

	for ( ll i = 0 ; i < N ; i ++ ){

		for ( ll j = 0 ; j < N ; j ++ ){

			if ( i == j ) continue;

			bool flag = true;

			for ( ll k = 0 ; k < N ; k ++ ){

				if ( k == i || k == j ) continue;

				if ( m[i][k] + m[k][j] < m[i][j] )  {

					cout << -1 << endl;

					return 0;

				}

				if ( m[i][k] + m[k][j] == m[i][j] ) flag = false;

			}

			if ( flag ){

					tot += m[i][j];

			}                

		}

	}

	cout << tot / 2 << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2442096481348712x):
#include <bits/stdc++.h>

using namespace std;

#define Size 309

unsigned graph[Size][Size], dis[Size][Size];

int main() {

    //freopen("input.txt", "r", stdin);

    int N;

    scanf("%d", &N);

    for(int i=0; i<N; i++)

        for(int j=0; j<N; j++)

            cin>>graph[i][j];

    memset(dis, -1, sizeof(dis));

    for(int i=0; i<N; i++)

        for(int j=0; j<N; j++)

            for(int k=0; k<N; k++)

                dis[i][j] = min(dis[i][j], graph[i][k]+graph[k][j]);

    for(int i=0; i<N; i++)

        for(int j=0; j<N; j++)

            if(dis[i][j]!=graph[i][j])

                return puts("-1")*0;

    long long ans = 0;

    for(int i=0; i<N; i++)

        for(int j=i+1; j<N; j++) {

            bool ck = true;

            for(int k=0; k<N; k++)

                if((dis[i][j] == dis[i][k]+dis[k][j]) && (dis[i][j] != dis[i][k]) && (dis[i][j]!=dis[k][j])) {

                    ck = false;

                    break;

                }

            if(ck)

                ans += dis[i][j];

        }

    cout<<ans<<endl;

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define F(i,a,b) for(int i=a;i<=b;i++)

using namespace std;

typedef long long ll;

const int N=305;

ll nw[N][N],a[N][N],fa[N],rnk[N];

int n;

ll sum;

struct Edge {

	int u,v,w;

};

vector <Edge> E;

bool cmp(Edge a,Edge b) {

	return a.w<b.w;

}

int find(int x) {

	if(x==fa[x]) return x;

	else return fa[x]=find(fa[x]);

}

int uni(int a,int b) {

	int f_a=find(a),f_b=find(b);

	if(f_a==f_b) return 0;

	if(rnk[f_a]<=rnk[f_b]) {

		fa[f_a]=f_b;

		if(rnk[f_a]==rnk[f_b])

			rnk[f_b]++;

	}

	else fa[f_b]=f_a;

}

int main() {

//	freopen("1.in","r",stdin);

	memset(nw,127/2,sizeof(nw));

	cin>>n;

	F(i,0,n) nw[i][i]=0,fa[i]=i;

	F(i,1,n) F(j,1,n) cin>>a[i][j];

	F(i,1,n) F(j,1,n) {

		if(a[i][j]!=a[j][i]) {cout<<-1;return 0;}

		if(i==j&&a[i][j]) {cout<<-1;return 0;}

	}

	F(i,1,n) F(j,i+1,n) E.push_back((Edge) {i,j,a[i][j]});

	sort(E.begin(),E.end(),cmp);

	int lim=E.size(),t=-1;

	F(i,1,n*(n-1)/2) {

		if(++t>=lim) break;

		int u=E[t].u,v=E[t].v,w=E[t].w;

		if(nw[u][v]<=w) continue;

		sum+=w;

		nw[u][v]=nw[v][u]=w;

		F(i,1,n) F(j,1,n) nw[i][j]=min(nw[i][j],min(nw[i][u]+w+nw[v][j],nw[i][v]+w+nw[u][j]));

		int pd=0;

		F(i,1,n) F(j,1,n) if(nw[i][j]!=a[i][j]) pd=1;

//		F(i,1,n){ F(j,1,n) printf("%lld ",nw[i][j]);cout<<"\n";}

//		cout<<endl;

		if(pd==1) continue;

		else break;

		if(t+1==lim) {cout<<"-1";return 0;}

		break;

//		else nw[E[t].u][E[t].v]=nw[E[t].v][E[t].u]=E[t].w,sum+=E[t].w;

	}

//	F(k,1,n) 

//		F(i,1,n)

//			F(j,1,n)

//				nw[i][j]=min(nw[i][j],nw[i][k]+nw[k][j]);

//	F(i,1,n) F(j,1,n) if(nw[i][j]!=a[i][j]) {cout<<-1;return 0;}

	int pd=0;

		F(i,1,n) F(j,1,n) if(nw[i][j]!=a[i][j]) pd=1;

	if(pd==1) cout<<-1;

	else cout<<sum;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.756130294434119x):
#include<bits/stdc++.h>

#define F(i,a,b) for(int i=a;i<=b;i++)

using namespace std;

typedef long long ll;

const int N=305;

ll sum;

int a[N][N],p[N][N];

int n;

int main() {

//	freopen("1.in","r",stdin);

	cin>>n;

	F(i,1,n) F(j,1,n) cin>>a[i][j],sum+=a[i][j];

	F(k,1,n) F(i,1,n) F(j,1,n) 

		if(a[i][k]+a[k][j]<a[i][j]){

			cout<<-1;return 0;

		}

	F(k,1,n) F(i,1,n) F(j,1,n) 

		if(a[i][k]+a[k][j]==a[i][j]&&i!=k&&k!=j&&i!=j)

			p[i][j]=1;

	F(i,1,n) F(j,1,n)

		if(p[i][j]) sum-=a[i][j];

	cout<<sum/2;

}