stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#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;

 }

  

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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);

  }

}

// Optimized solution to the same problem (faster):
#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);

  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



#define int long long



#define rep(i,n) for(int i=0;i<(n);i++)

#define pb push_back

#define all(v) (v).begin(),(v).end()

#define fi first

#define se second

typedef vector<int>vint;

typedef pair<int,int>pint;

typedef vector<pint>vpint;



template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}

template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}



int N;

int A[333][333];



const int INF=1001001001001001001ll;

int d[333][333];



signed main(){

    cin>>N;

    rep(i,N)rep(j,N)cin>>A[i][j];



    fill_n(*d,333*333,INF);

    rep(i,N)d[i][i]=0;

    rep(i,N)rep(j,N)chmin(d[i][j],A[i][j]);



    rep(k,N)rep(i,N)rep(j,N)chmin(d[i][j],d[i][k]+d[k][j]);



    rep(i,N)rep(j,N)if(d[i][j]!=A[i][j]){

    cout<<-1<<endl;

    return 0;}



    int ans=0;

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

        bool f=false;

        rep(k,N){

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

            if(d[i][k]+d[k][j]==d[i][j])f=true;

        }

        if(!f)ans+=d[i][j];

    }



    cout<<ans<<endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;



#define int long long



#define rep(i,n) for(int i=0;i<(n);i++)

#define pb push_back

#define all(v) (v).begin(),(v).end()

#define fi first

#define se second

typedef vector<int>vint;

typedef pair<int,int>pint;

typedef vector<pint>vpint;



template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}

template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}



int N;

int A[333][333];



signed main(){

    cin>>N;

    rep(i,N)rep(j,N)cin>>A[i][j];



    int ans=0;

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

        bool flag=false;

        rep(k,N){

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

                cout<<-1<<endl;

                return 0;

            }

            if(A[i][k]+A[k][j]>A[i][j])continue;

            if(i!=k&&j!=k)flag=true;

        }

        if(!flag)ans+=A[i][j];

    }

    cout<<ans<<endl;

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



using namespace std;



int main() { 

	

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

	

	int t;

	scanf ("%d", &t);

	

	long a[t][t];

	

	for (int i=0; i<t; i++) for (int j=0; j<t; j++) scanf ("%ld", &a[i][j]);

	

	long sum = 0;

	bool mark[t][t];

	memset(mark, false, sizeof mark);

	bool impossible = false;

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

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

			for (int k=j+1; k<t; k++) { 

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

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

					impossible = true;

					break;

				}

			}

		}		

	}

	if(impossible) { printf ("-1\n");  return 0;}

	for (int i=0; i<t; i++) for (int j=i+1; j<t;j++) if(!mark[i][j]) sum+=a[i][j];

	

	

	

		printf ("%ld\n", sum);

	

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



int N;

int p[8],q[8];

vector<int> dic(40325);

int idx=0;



int kaijo(int n){

  int ans=1;

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

  return ans;

}



bool is_d_in_x(int d, int x){

  while(x>0){

    int res= x%10;

    if(res==d) return false;

    x/=10;

  }



  return true;

}



int digits(int x){

  int d=0;

  while(x>0){

    x/=10;

    d++;

  }

  return d;

}



void dfs(int d){

  if(digits(d)==N) {

    dic[idx]=d;

    idx++;



    return;

  }

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



    if(is_d_in_x(i,d)){

      dfs(d*10+i);

    }

  }

}

int main() {



  cin>>N;



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

  for(int i=0;i<N;i++) cin>>q[i];

  int P=0,Q=0;

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

    P+=p[N-1-i]*long(pow(10,i));

  }

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

    Q+=q[N-1-i]*long(pow(10,i));

  }



  int l,r;



  dfs(0);

  sort(dic.begin(),dic.begin()+kaijo(N));



  int a,b;

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

    if(dic[i]==P) a=i;

    if(dic[i]==Q) b=i;

  }

  cout<<abs(a-b)<<endl;









}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



int N;



int kaijo(int n){

  int r=1;

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

    r*=i;

  }

  return r;

}



bool same(vector<long> x,vector<long> y){

  bool flg=true;

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

    if(x[i]!=y[i]) flg=false;

  }

  return flg;

}

int main() {





cin>>N;

vector<long> p(N);

vector<long> q(N);



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

for(int i=0;i<N;i++) cin>>q[i];



vector<long> a(N);

for(int i=0;i<N;i++) a[i]=i+1;

long P,Q;

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

  if(same(a,p)) P=i;

  if(same(a,q)) Q=i;

  next_permutation(a.begin(),a.end());

}

cout<<abs(P-Q)<<endl;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<algorithm>

#include<string.h>

#include<math.h>

#include<stdio.h>

#include<queue>

#define INF 0x3f3f3f3f

typedef long long ll;

using namespace std;



#define  N 15

int n;

int dis[N],maps[N][N];

int main()

{

    int i,a,b,c,j,k;

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

    {

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

        {

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

            {

                maps[i][j]=(i==j)?0:INF;

            }

        }



        int m=-1;

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

        {

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

            m=max(a,m);

            m=max(b,m);

            maps[a][b]=maps[b][a]=c;

        }



        for(k=0;k<=m;k++)

        {

            for(i=0;i<=m;i++)

            {

                for(j=0;j<=m;j++)

                {

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

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

                }

            }

        }



        int  Min=INF;

        int index=-1;

        memset(dis,0,sizeof(dis));

        for(i=0;i<=m;i++)

        {

            for(j=0;j<=m;j++)

            {

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

            }

            if(Min>dis[i])

            {

                index=i;

                Min=dis[i];

            }

        }

        printf("%d %d\n",index,Min);

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<string.h>

using namespace std;

int main()

{

	int t,map[12][12],sum,ans,s,i,j,k,max0,a,b,c;

	while(scanf("%d",&t) && t)

	{

		memset(map,1,sizeof(map));

		max0=0;

		for(i=0;i<t;i++)

		{

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

			map[a][b]=map[b][a]=c;

			max0=max(max0,max(a,b));

		}

	/*	printf("%d\n\n",max0);

		 for(i=0;i<=max0;i++)

		 {

		 	for(j=0;j<=max0;j++)

		 	{

		 		printf("%d  ",map[i][j]);

		 		if(j == max0)

		 			printf("\n");

			 }

		 }*/

		for(k=0;k<=max0;k++)

		{

			for(i=0;i<=max0;i++)

			{

				for(j=0;j<=max0;j++)

				{

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

				}

			}

		}

		sum=10000000,ans=0;

		for(i=0;i<=max0;i++)

		{

			s=0;

			for(j=0;j<=max0;j++)

			{

				if(i != j)

				{

					s+=map[i][j];

				}

			}

			if(s < sum)

			{

				sum=s;

				ans=i;

			}

		}

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

	}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<stdio.h>

#include<string.h>

#include<algorithm>

#define INF 0xfffffff

using namespace std;

int map[1100][1100];

int main()

{

	int n,m,k,i,j,a,b,c,sum1;

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

	{

		for(i=0;i<1100;i++)

		for(j=0;j<1100;j++)

		{

			map[i][j]=INF;

			map[i][i]=0;

		}

		int Max=0;

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

		{

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

			Max=max(Max,a);

			Max=max(Max,b);

			map[a][b]=c;

			map[b][a]=c;

		}

		for(k=0;k<=Max;k++)

		for(i=0;i<=Max;i++)

		for(j=0;j<=Max;j++)

		{

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

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

		}

		int sum=INF;

		int ans=0;

		for(i=0;i<=Max;i++)

		{

			sum1=0;

			for(j=0;j<=Max;j++)

		    {

			   sum1+=map[i][j];

		    }

		    if(sum1<sum)

			{

				sum=sum1;

				ans=i;

			}

		}

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

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<string.h>

using namespace std;

int main()

{

	int t,map[12][12],sum,ans,s,i,j,k,max0,a,b,c;

	while(scanf("%d",&t) && t)

	{

		memset(map,1,sizeof(map));

		max0=0;

		for(i=0;i<t;i++)

		{

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

			map[a][b]=map[b][a]=c;

			max0=max(max0,max(a,b));

		}

	/*	printf("%d\n\n",max0);

		 for(i=0;i<=max0;i++)

		 {

		 	for(j=0;j<=max0;j++)

		 	{

		 		printf("%d  ",map[i][j]);

		 		if(j == max0)

		 			printf("\n");

			 }

		 }*/

		for(k=0;k<=max0;k++)

		{

			for(i=0;i<=max0;i++)

			{

				for(j=0;j<=max0;j++)

				{

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

				}

			}

		}

		sum=10000000,ans=0;

		for(i=0;i<=max0;i++)

		{

			s=0;

			for(j=0;j<=max0;j++)

			{

				if(i != j)

				{

					s+=map[i][j];

				}

			}

			if(s < sum)

			{

				sum=s;

				ans=i;

			}

		}

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

	}

}

// Fastest known solution for this problem: