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

#include<algorithm>

#include<vector>

using namespace std;



#define x first

#define y second

typedef pair<int,int> pii;

typedef long long ll;

const int MAXN=100005;



int n,m;

int co[MAXN];

bool vis[MAXN];

vector<int> E[MAXN];



bool dfs(int u){

    bool tag=1;

    vis[u]=1;

    for(int i=0;i<(int)E[u].size();i++){

        int v=E[u][i];

        if(vis[v]) tag&=(co[v]!=co[u]);

        else{

            co[v]=co[u]^1;

            tag&=dfs(v);

        }

    }

    return tag;

}



int main(){

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

    //freopen("squared.out","w",stdout);



    scanf("%d%d",&n,&m);

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

        int u,v;

        scanf("%d%d",&u,&v);

        E[u].push_back(v);

        E[v].push_back(u);

    }

    int cnt=0,p=0,q=0;

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

        if(vis[i]) continue;

        if(!E[i].size()) cnt++;

        else{

            if(dfs(i)) q++;

            else p++;

        }

    }

    printf("%lld\n",(ll)cnt*cnt+2ll*cnt*(n-cnt)+p*p+2ll*p*q+2ll*q*q);



    fclose(stdin);

    fclose(stdout);

}


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

#define maxn 100005

#define maxm 200005

int n,m,ia,pa,qa;

bool f,val[maxn],vis[maxn];

struct node { int v; node *nxt; } edge[maxm*2],*head[maxn],*ncnt;

void addedge(int u,int v)

{

	ncnt++;

	ncnt->v=v,ncnt->nxt=head[u];

	head[u]=ncnt;

}

void dfs(int u)

{

	vis[u]=true;

	for(node *p=head[u];p;p=p->nxt)

	{

		int v=p->v;

		if(!vis[v]) { val[v]=val[u]^1; dfs(v); }

		else if(val[u]==val[v]) f=true;

	}

}

int main()

{

	ncnt=&edge[0];

	scanf("%d%d",&n,&m);

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

	{

		int u,v;

		scanf("%d%d",&u,&v);

		addedge(u,v); addedge(v,u);

	}

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

	{

		if(!head[i]) ia++;

		else if(!vis[i])

		{

			f=false;

			dfs(i);

			if(f) pa++;

			else qa++;

		}

	}

	printf("%lld\n",1ll*ia*ia+2ll*ia*(n-ia)+1ll*pa*pa+2ll*qa*qa+2ll*pa*qa);

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

#include<cstring>

#include<vector>

#include<algorithm>

using namespace std;

const long long MAXN=100005;

long long n,m;

vector<int>w[MAXN];

bool vis[MAXN];

long long cl[MAXN];

bool dfs(long long u)

{

	vis[u]=1;

	long long re=1;

	for(long long i=0;i<int(w[u].size());i++)

	{

		long long v=w[u][i];

		if(vis[v])

			re&=(cl[u]!=cl[v]);

		else

		{

			cl[v]=cl[u]^1;

			re&=dfs(v);

		}

	}

	return re;

}

int main()

{

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

	//freopen("squared.out","w",stdout);

	scanf("%lld %lld",&n,&m);

	for(long long i=1;i<=m;i++)

	{

		long long u,v;

		scanf("%lld %lld",&u,&v);

		w[u].push_back(v);

		w[v].push_back(u);

	}

	long long I=0,O=0,J=0;

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

	{

		if(!vis[i])

		{

			if(int(w[i].size())==0)

				I++;

			else if(dfs(i))

				O++;

			else

				J++;

		}

	}

	printf("%lld\n",I*I+I*(n-I)*2+O*O*2+J*J+O*J*2);

	fclose(stdin);

	fclose(stdout);;

}

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

#define maxn 100005

#define maxm 200005

int n,m,ia,pa,qa;

bool f,val[maxn],vis[maxn];

struct node { int v; node *nxt; } edge[maxm*2],*head[maxn],*ncnt;

void addedge(int u,int v)

{

	ncnt++;

	ncnt->v=v,ncnt->nxt=head[u];

	head[u]=ncnt;

}

void dfs(int u)

{

	vis[u]=true;

	for(node *p=head[u];p;p=p->nxt)

	{

		int v=p->v;

		if(!vis[v]) { val[v]=val[u]^1; dfs(v); }

		else if(val[u]==val[v]) f=true;

	}

}

int main()

{

	ncnt=&edge[0];

	scanf("%d%d",&n,&m);

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

	{

		int u,v;

		scanf("%d%d",&u,&v);

		addedge(u,v); addedge(v,u);

	}

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

	{

		if(!head[i]) ia++;

		else if(!vis[i])

		{

			f=false;

			dfs(i);

			if(f) pa++;

			else qa++;

		}

	}

	printf("%lld\n",1ll*ia*ia+2ll*ia*(n-ia)+1ll*pa*pa+2ll*qa*qa+2ll*pa*qa);

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

#include<vector>

#include<algorithm>

#define MAXN 200006

using namespace std;

int u,v,deep[MAXN],n,m,sum[MAXN],vis[MAXN],ok;

long long a,q,p,ans;

vector<int> road[MAXN];

void DFS(int x,int father)

{

	vis[x]=1;

	sum[x]=1;

	deep[x]=deep[father]+1;

	for(int i=0;i<(int)road[x].size();i++)

	{

		int nex=road[x][i];

		if(nex==father) continue;

		if(vis[nex])

		{

			if((deep[nex]+deep[x])%2==0) ok=0;

			continue;

		}

		DFS(nex,x);

		sum[x]+=sum[nex];

	}

}

int main()

{

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

	//freopen("squared.out","w",stdout);

	scanf("%d%d",&n,&m);

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

	{

		scanf("%d%d",&u,&v);

		road[u].push_back(v);

		road[v].push_back(u);

	}

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

	{

		if(vis[i]) continue;

		ok=1;

		DFS(i,0);

		if(sum[i]==1) a++;

		else 

		{

			if(ok==0) p++;

			else q++;

		}

	}

	ans=a*a+2*a*(n-a)+p*p+p*q*2+2*q*q;

	printf("%lld",ans);

}

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

#include <cstring>

#include <vector>

#include <algorithm>

#define N 110000

using namespace std;

int n,m,f[N],flag[N],pr[N],sz[N],p,c,d,u,v,df[N];

vector<int>a[N];

int get(int x){

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

	return f[x]=get(f[x]);

}

bool dfs(int u,int fa){

	if(flag[u] and pr[u]!=pr[fa]^1) return false;

	if(flag[u]) return true;

	pr[u]=pr[fa]^1;

	flag[u]=1;

	for(int i=0;i<a[u].size();++i)

		if(a[u][i]!=fa) if(!dfs(a[u][i],u)) return false;

	return true;

}

int main(){

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

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;++i) f[i]=i;

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

		scanf("%d%d",&u,&v);

		a[u].push_back(v),a[v].push_back(u);

		int xx=get(u),yy=get(v);

		if(xx!=yy) f[xx]=yy;

	}

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

		if(get(i)==i) df[i]=dfs(i,0);

	for(int i=1;i<=n;++i) sz[get(i)]++;

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

		if(get(i)==i)

			if(sz[i]==1) p++;

			else if(df[i]) c++;

			else d++;

		}

	printf("%lld\n",1ll*p*n+1ll*(n-p)*p+1ll*c*c*2+1ll*d*(d+c*2));

	return 0;

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

#include <vector>

using namespace std;

int n, m, a, b, cnt, col[100009]; vector<int> g[100009];

bool dfs(int pos) {

	bool ret = true; cnt++;

	for (int i : g[pos]) {

		if (col[i] == col[pos]) ret = false;

		if (col[i] == 0) {

			col[i] = 3 - col[pos];

			if (!dfs(i)) ret = false;

		}

	}

	return ret;

}

int main() {

	scanf("%d %d", &n, &m);

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

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

		g[a].push_back(b);

		g[b].push_back(a);

	}

	int c0 = 0, c1 = 0, c2 = 0;

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

		if (col[i] == 0) {

			col[i] = 1; cnt = 0;

			bool res = dfs(i);

			if (cnt == 1) c0++;

			else if (!res) c1++;

			else c2++;

		}

	}

	printf("%lld\n", 1LL * (c1 + c2) * (c1 + c2) + 1LL * c2 * c2 + 1LL * n * n - 1LL * (n - c0) * (n - c0));

	return 0;

}

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

#include <vector>

#pragma warning(disable : 4996)

using namespace std;

int n, m, a[200009], b[200009], e[400009], c[100009], s[100009], cnt, col[100009];

bool dfs(int pos) {

	bool ret = true; cnt++;

	for (int i = c[pos]; i < c[pos + 1]; i++) {

		if (col[e[i]] == col[pos]) ret = false;

		if (col[e[i]] == 0) {

			col[e[i]] = 3 - col[pos];

			if (!dfs(e[i])) ret = false;

		}

	}

	return ret;

}

int main() {

	scanf("%d %d", &n, &m);

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

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

		c[--a[i]]++; c[--b[i]]++;

	}

	for (int i = 0; i < n; i++) s[i + 1] = s[i] + c[i]; c[0] = 0;

	for (int i = 0; i < n; i++) c[i + 1] = s[i + 1];

	for (int i = 0; i < m; i++) e[s[a[i]]++] = b[i], e[s[b[i]]++] = a[i];

	int c0 = 0, c1 = 0, c2 = 0;

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

		if (col[i] == 0) {

			col[i] = 1; cnt = 0;

			bool res = dfs(i);

			if (cnt == 1) c0++;

			else if (!res) c1++;

			else c2++;

		}

	}

	printf("%lld\n", 1LL * (c1 + c2) * (c1 + c2) + 1LL * c2 * c2 + 1LL * n * n - 1LL * (n - c0) * (n - c0));

	return 0;

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

#include <vector>

using namespace std;

int n, m, a, b, cnt, col[100009]; vector<int> g[100009];

bool dfs(int pos) {

	bool ret = true; cnt++;

	for (int i : g[pos]) {

		if (col[i] == col[pos]) ret = false;

		if (col[i] == 0) {

			col[i] = 3 - col[pos];

			if (!dfs(i)) ret = false;

		}

	}

	return ret;

}

int main() {

	scanf("%d %d", &n, &m);

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

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

		g[a].push_back(b);

		g[b].push_back(a);

	}

	int c0 = 0, c1 = 0, c2 = 0;

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

		if (col[i] == 0) {

			col[i] = 1; cnt = 0;

			bool res = dfs(i);

			if (cnt == 1) c0++;

			else if (!res) c1++;

			else c2++;

		}

	}

	printf("%lld\n", 1LL * (c1 + c2) * (c1 + c2) + 1LL * c2 * c2 + 1LL * n * n - 1LL * (n - c0) * (n - c0));

	return 0;

}

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

#include <vector>

using namespace std;

int n, m, a[200009], b[200009], e[400009], c[100009], s[100009], cnt, col[100009];

bool dfs(int pos) {

	bool ret = true; cnt++;

	for (int i = c[pos]; i < c[pos + 1]; i++) {

		if (col[e[i]] == col[pos]) ret = false;

		if (col[e[i]] == 0) {

			col[e[i]] = 3 - col[pos];

			if (!dfs(e[i])) ret = false;

		}

	}

	return ret;

}

void reader(int *x) {

	for (char c = getchar_unlocked(); 48 <= c && c <= 57; c = getchar()) *x = *x * 10 + c - 48;

}

int main() {

	reader(&n); reader(&m);

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

		reader(&a[i]); reader(&b[i]);

		c[--a[i]]++; c[--b[i]]++;

	}

	for (int i = 0; i < n; i++) s[i + 1] = s[i] + c[i]; c[0] = 0;

	for (int i = 0; i < n; i++) c[i + 1] = s[i + 1];

	for (int i = 0; i < m; i++) e[s[a[i]]++] = b[i], e[s[b[i]]++] = a[i];

	int c0 = 0, c1 = 0, c2 = 0;

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

		if (col[i] == 0) {

			col[i] = 1; cnt = 0;

			bool res = dfs(i);

			if (cnt == 1) c0++;

			else if (!res) c1++;

			else c2++;

		}

	}

	printf("%lld\n", 1LL * (c1 + c2) * (c1 + c2) + 1LL * c2 * c2 + 1LL * n * n - 1LL * (n - c0) * (n - c0));

	return 0;

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

#include <vector>

using namespace std;

int n, m, a, b, cnt, col[100009]; vector<int> g[100009];

bool dfs(int pos) {

	bool ret = true; cnt++;

	for (int i : g[pos]) {

		if (col[i] == col[pos]) ret = false;

		if (col[i] == 0) {

			col[i] = 3 - col[pos];

			if (!dfs(i)) ret = false;

		}

	}

	return ret;

}

int main() {

	scanf("%d %d", &n, &m);

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

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

		g[a].push_back(b);

		g[b].push_back(a);

	}

	int c0 = 0, c1 = 0, c2 = 0;

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

		if (col[i] == 0) {

			col[i] = 1; cnt = 0;

			bool res = dfs(i);

			if (cnt == 1) c0++;

			else if (!res) c1++;

			else c2++;

		}

	}

	printf("%lld\n", 1LL * (c1 + c2) * (c1 + c2) + 1LL * c2 * c2 + 1LL * n * n - 1LL * (n - c0) * (n - c0));

	return 0;

}

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

int n, m, a[200009], b[200009], e[400009], c[100009], s[100009], cnt, col[100009];

bool dfs(int pos) {

	bool ret = true; cnt++;

	for (int i = c[pos]; i < c[pos + 1]; i++) {

		if (col[e[i]] == col[pos]) ret = false;

		if (col[e[i]] == 0) {

			col[e[i]] = 3 - col[pos];

			if (!dfs(e[i])) ret = false;

		}

	}

	return ret;

}

void reader(int *x) {

	for (char c = getchar_unlocked(); 48 <= c && c <= 57; c = getchar()) *x = *x * 10 + c - 48;

}

int main() {

	reader(&n); reader(&m);

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

		reader(&a[i]); reader(&b[i]);

		c[--a[i]]++; c[--b[i]]++;

	}

	for (int i = 0; i < n; i++) s[i + 1] = s[i] + c[i]; c[0] = 0;

	for (int i = 0; i < n; i++) c[i + 1] = s[i + 1];

	for (int i = 0; i < m; i++) e[s[a[i]]++] = b[i], e[s[b[i]]++] = a[i];

	int c0 = 0, c1 = 0, c2 = 0;

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

		if (col[i] == 0) {

			col[i] = 1; cnt = 0;

			bool res = dfs(i);

			if (cnt == 1) c0++;

			else if (!res) c1++;

			else c2++;

		}

	}

	printf("%lld\n", 1LL * (c1 + c2) * (c1 + c2) + 1LL * c2 * c2 + 1LL * n * n - 1LL * (n - c0) * (n - c0));

	return 0;

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

using namespace std;



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

#define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); i--)

#define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define FILL(a, value) memset(a, value, sizeof(a))



#define SZ(a) (int) a.size()

#define ALL(a) a.begin(),a.end()

#define PB push_back

#define MP make_pair



typedef long long LL;

typedef vector<int> VI;

typedef pair<int, int> PII;



const double PI = acos(-1.0);

const LL INF = 1000 * 1000 * 1000 + 7;

const LL LINF = INF * (LL)INF;



const int MAX = 100100;



VI g[MAX];



int U[MAX];

int C[MAX];



int sz;



bool dfs(int x, int c)

{

	U[x] = true;



	C[x] = c;



	int res = false;

	sz++;



	FOR (i, 0, SZ(g[x]))

	{

		int to = g[x][i];

		if (U[to])

		{

			if (C[to] == C[x]) res = true;

		}

		else

		{

			if (dfs(to, c ^ 1)) res = true;

		}

	}



	return res;

}



int main()

{

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

	//ios::sync_with_stdio(false); cin.tie(0);



	int n, m;

	scanf("%d%d", &n, &m);

	FOR (i, 0, m)

	{

		int x, y;

		scanf("%d%d", &x, &y);

		x--;

		y--;



		g[x].PB(y);

		g[y].PB(x);

	}



	LL c1 = 0;

	LL c[2] = {0, 0};



	FOR (i, 0, n)

	{

		if (!U[i])

		{

			sz = 0;

			int r = dfs(i, 0);



			if (sz == 1) c1++;

			else c[r]++;

		}

	}



	LL res = 0;

	res += c1 * c1;





	res += (n-c1) * c1 * 2;



	res += c[1] * c[1];

	res += c[1] * c[0] * 2;

	res += c[0] * c[0] * 2;



	cout<<res<<endl;



}


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

#include<cstring>

#include<algorithm>

#include<queue>

using namespace std;

const int N=1000000+5;

typedef long long ll;

struct node{

    int u,v,nxt;

}edge[N*2];

int head[N],mcnt;

int du[N];

void add_edge(int u,int v){

    mcnt++;

    edge[mcnt].u=u;

    edge[mcnt].v=v;

    edge[mcnt].nxt=head[u];

    head[u]=mcnt;

    du[u]++;

}

ll I,P,Q;

bool tag[N];

bool vis[N];

bool flag;

void dfs(int u){

    vis[u]=1;

    for(int i=head[u];i;i=edge[i].nxt){

        int v=edge[i].v;

        if(!vis[v]){

            tag[v]=tag[u]^1;

            dfs(v);

        }

        else

            if(tag[u]==tag[v])

                flag=false;

    }

}

int main()

{

    int n,m;

    scanf("%d%d",&n,&m);

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

        int u,v;

        scanf("%d%d",&u,&v);

        add_edge(u,v);

        add_edge(v,u);

    }

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

        if(vis[i])

            continue;

        if(du[i]==0){

            I++;

            continue;

        }

        flag=true;

        dfs(i);

        if(flag)

            Q++;

        else

            P++;

    }

    ll ans=1ll*2*I*n-1ll*I*I+1ll*P*P+2ll*P*Q+2ll*Q*Q;//

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

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

#define rank tipa_rank

#define pb push_back

#define mp make_pair

#define ll long long

#define ull unsigned long long

#define INF 1000000000

#define EPS 1e-6

#define free tipa_free

#define EPS2 1e-8

using namespace std;

const ll MOD = INF + 7;

using namespace std;

ll ans = 0;

int n , m;

int visited[100000];

ll trees , cycles , free;

vector<vector<int> > g(100000);

int edgesCnt = 0 , verticesCnt;

bool bipatride;

void dfs(int u)

{

	for(int i = 0; i < g[u].size(); i++)

	{

		edgesCnt++;

		int v = g[u][i];

		bool Dfs = false;

		if(!visited[v])

			Dfs = true;

		if(visited[u] == 1)

		{

			if(visited[v] == 1)

				bipatride = false;

			visited[v] = 2;

		}

		if(visited[u] == 2)

		{

			if(visited[v] == 2)

				bipatride = false;

			visited[v] = 1;

		}

		if(Dfs)

			dfs(v);

	}

}

int main()

{

	scanf("%d %d" , &n , &m);

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

	{

		int u , v;

		scanf("%d %d" , &u , &v);

		u--;v--;

		g[u].pb(v);

		g[v].pb(u);

	}

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

	{

		if(!visited[i])

		{

			edgesCnt = 0;

			bipatride = true;

			visited[i] = 1;

			//edgesCnt++;

			dfs(i);

			if(!edgesCnt)

				free++;

			else if(bipatride)

			{

				trees++;

			}

			else

				cycles++;

		}

	}

	//cout << trees << " " << cycles << " " << free << endl;

	int x = n * 2 - 1;

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

	{

		ans += x;

		x -= 2;

	}

	ans += cycles * (trees + cycles) + trees * (cycles + 2 * trees);

	cout << ans;

}


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

#include<cstring>

#include<algorithm>

#include<queue>

using namespace std;

const int N=1000000+5;

typedef long long ll;

struct node{

    int u,v,nxt;

}edge[N*2];

int head[N],mcnt;

int du[N];

void add_edge(int u,int v){

    mcnt++;

    edge[mcnt].u=u;

    edge[mcnt].v=v;

    edge[mcnt].nxt=head[u];

    head[u]=mcnt;

    du[u]++;

}

ll I,P,Q;

bool tag[N];

bool vis[N];

bool flag;

void dfs(int u){

    vis[u]=1;

    for(int i=head[u];i;i=edge[i].nxt){

        int v=edge[i].v;

        if(!vis[v]){

            tag[v]=tag[u]^1;

            dfs(v);

        }

        else

            if(tag[u]==tag[v])

                flag=false;

    }

}

int main()

{

    int n,m;

    scanf("%d%d",&n,&m);

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

        int u,v;

        scanf("%d%d",&u,&v);

        add_edge(u,v);

        add_edge(v,u);

    }

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

        if(vis[i])

            continue;

        if(du[i]==0){

            I++;

            continue;

        }

        flag=true;

        dfs(i);

        if(flag)

            Q++;

        else

            P++;

    }

    ll ans=1ll*2*I*n-1ll*I*I+1ll*P*P+2ll*P*Q+2ll*Q*Q;//

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

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

#include <algorithm>

#include <cmath>



using namespace std ;



int arr [ 100000 ] ;



int main()

{

    int n , cntp = 0 , cntn = 0 ; long long sum = 0 ;

    cin >> n ;

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

    {

        cin >> arr [ i ] ;

        if ( arr [ i ] > 0 )

        {

            cntp ++ ;

            sum += arr [ i ] ;

        }

        else

        {

            cntn ++ ;

            sum -= arr [ i ] ;

        }

    }

    sort ( arr , arr + n ) ;

    if ( cntp && cntn )

    {

        cout << sum << endl ;

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

        {

            cout << arr [ 0 ] << " " << arr [ i ] << endl ;

            arr [ 0 ] -= arr [ i ] ;

        }

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

        {

            cout << arr [ n - 1 ] << " " << arr [ i ] << endl ;

            arr [ n - 1 ] -= arr [ i ] ;

        }

    }

    else if ( cntn == 0 )

    {

        sum -= 2 * arr [ 0 ] ;

        cout << sum << endl ;

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

        {

            cout << arr [ 0 ] << " " << arr [ i ] << endl ;

            arr [ 0 ] -= arr [ i ] ;

        }

        cout << arr [ n - 1 ] << " " << arr [ 0 ] << endl ;

    }

    else

    {

        sum += 2 * arr [ n - 1 ] ;

        cout <<sum <<endl ;

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

        {

            cout << arr [ n - 1 ] << " " << arr [ i ] << endl ;

            arr [ n - 1 ] -= arr [ i ] ;

        }

    }





}


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

#include <cstring>

#include <cmath>

#include <algorithm>

using namespace std;

const int SIZEN = 100010;

typedef long long LL;

int N;

LL a[SIZEN] = {0};

int main(){

	scanf("%d",&N);

	for(int i = 1;i <= N;i++)scanf("%lld",&a[i]);

	sort(a + 1,a + 1 + N);

	LL ans = 0;

	if(a[1] >= 0){

		for(int i = 2;i <= N;i++)ans += a[i];

		ans -= a[1];

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

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

			printf("%lld %lld\n",a[1],a[i]);

			a[1] -= a[i];

		}

		printf("%lld %lld\n",a[N],a[1]);

	}

	else if(a[N] <= 0){

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

		ans = a[N] - ans;	

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

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

			printf("%lld %lld\n",a[N],a[i]);

			a[N] -= a[i];

		}

	}

	else{

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

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

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

			if(a[i] < 0)printf("%lld %lld\n",a[N],a[i]),a[N] -= a[i];

			else printf("%lld %lld\n",a[1],a[i]),a[1] -= a[i];

		}

		printf("%lld %lld\n",a[N],a[1]);

	}

	return 0;

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

#include<math.h>

#include<iomanip>

#include <string>

#include <cstdio>

#include<stdio.h>

#include <cstring>

#include <algorithm>

#include <queue>

#define ll long long

#define INF 2147483647

#define N 100000

using namespace std;int stop;

int n,m,k;

int arr[N];



int main(){

    int i,j,x,y;

    scanf("%d",&n);

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

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

    }

    sort(arr+1,arr+1+n);

    ll sum = 0;

    int biaoji  = -1;

    if(arr[1] < 0 && arr[n] > 0){

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

            if(arr[i] <= 0){

                sum+=(-arr[i]);

                biaoji = i;

            }else{

                sum+=arr[i];

            }

        }

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

        int x = arr[1],y = arr[n];

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

            printf("%d %d\n",x,arr[i]);

            x -= arr[i];

        }

        printf("%d %d\n",y,x);y-=x;

        for(i=2;i<=biaoji;i++){

            printf("%d %d\n",y,arr[i]);

            y-=arr[i];

        }



    }else if(arr[1] >= 0){

        sum = -arr[1];

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

            sum+=arr[i];

        }

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

        x = arr[1];

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

            printf("%d %d\n",x,arr[i]);

            x-=arr[i];

        }

        printf("%d %d\n",arr[n],x);

    }else if(arr[n] <= 0){

        sum = arr[n];

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

            sum-=arr[i];

        }

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



        int x = arr[n];

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

            printf("%d %d\n",x,arr[i]);

            x-=arr[i];

        }

     }

    //else{

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

    //         sum+=arr[i];

    //     }

    //     printf("%lld\n",sum);

    //     int x = arr[1];

    //     for(i=2;i<n;i++){

    //         printf("%d %d\n",x,arr[i]);

    //         x-=arr[i];

    //     }

    //     printf("%d %d\n",arr[n],x);

    // }

    

    return 0;

}

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

#include <cstring>

#include <cmath>

#include <algorithm>

using namespace std;

const int SIZEN = 100010;

typedef long long LL;

int N;

LL a[SIZEN] = {0};

int main(){

	scanf("%d",&N);

	for(int i = 1;i <= N;i++)scanf("%lld",&a[i]);

	sort(a + 1,a + 1 + N);

	LL ans = 0;

	if(a[1] >= 0){

		for(int i = 2;i <= N;i++)ans += a[i];

		ans -= a[1];

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

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

			printf("%lld %lld\n",a[1],a[i]);

			a[1] -= a[i];

		}

		printf("%lld %lld\n",a[N],a[1]);

	}

	else if(a[N] <= 0){

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

		ans = a[N] - ans;	

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

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

			printf("%lld %lld\n",a[N],a[i]);

			a[N] -= a[i];

		}

	}

	else{

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

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

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

			if(a[i] < 0)printf("%lld %lld\n",a[N],a[i]),a[N] -= a[i];

			else printf("%lld %lld\n",a[1],a[i]),a[1] -= a[i];

		}

		printf("%lld %lld\n",a[N],a[1]);

	}

	return 0;

}