stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
// luogu-judger-enable-o2

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <cctype>

using namespace std;

inline int read(int f = 1, int x = 0, char ch = ' ')

{

    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;

    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();

    return f*x;

}

const int N = 1e5+5;

struct Edge

{

    int next, to;

    Edge(int next = 0, int to = 0):next(next), to(to) {};

}edge[N<<1];

int tot, head[N];

void _add(int x, int y) { edge[++tot] = Edge(head[x], y); head[x] = tot; }

void add(int x, int y) { _add(x, y); _add(y, x); }

int n, m, cn, w[N], f[N], fa[N], s[N], p, q, ans;

int find(int x) { return f[x] == x?x:f[x] = find(f[x]); }

void dfs(int x, int f, int c)

{

    fa[x] = f, s[x] = c;

    for(int i = head[x]; i; i = edge[i].next)

    {

        int y = edge[i].to; if(y == f) continue;

        dfs(y, x, -c); s[x] += s[y];

    }

}

int main()

{

    n = read(), m = read(), p = 1;

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

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

    {

        int x = read(), y = read();

        if(find(x) == find(y)) p = x, q = y;

        else f[find(y)] = find(x), add(x, y);

    }

    dfs(p, 0, 1);

    if(n == m)

    {

        for(int x = q; x; x = fa[x]) w[++cn] = s[x];

        if(cn&1)

        {

            if(s[p]&1) return puts("-1"), 0;

            for(int x = q; x; x = fa[x]) s[x] -= s[p]>>1;

        } 

        else

        {

            if(s[p]) return puts("-1"), 0;

            sort(w+1, w+1+cn);   

            for(int x = q; x; x = fa[x]) s[x] -= w[cn>>1];

        }

    }

    else if(s[p]) return puts("-1"), 0;

    for(int x = 1; x <= n; ++x) ans += abs(s[x]);

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

    return 0;

}

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

using namespace std;

#define N 200010

int n,m,a[N],v[N],nxt[N],h[N],ec,hx,hy,p[N],f[N],st[N],tp;

int fd(int x){return p[x]==x?x:p[x]=fd(p[x]);}

void add(int x,int y){v[++ec]=y;nxt[ec]=h[x];h[x]=ec;}

void dfs(int x,int fa,int va){

	a[x]=va;

	for(int i=h[x];i;i=nxt[i])

		if(v[i]!=fa){

			f[v[i]]=x;

			dfs(v[i],x,-va);

			a[x]+=a[v[i]];

		}

}

int main(){

	cin>>n>>m;

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

	hx=hy=1;

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

		int x,y;

		cin>>x>>y;

		if(fd(x)!=fd(y))p[fd(x)]=fd(y);

		else{

			hx=x;

			hy=y;

			continue;

		}

		add(x,y);add(y,x);

	}

	dfs(hx,0,-1);

	if(n==m){

		for(int i=hy;i;i=f[i])

			st[++tp]=a[i];

		if(tp&1){

			if(a[hx]&1){

				puts("-1");

				return 0;

			}

			for(int i=hy;i;i=f[i])

				a[i]-=a[hx]/2;

		}

		else{

			if(a[hx]){

				puts("-1");

				return 0;

			}

			sort(st+1,st+tp+1);

			for(int i=hy;i;i=f[i])

				a[i]-=st[tp/2];

		}

	}

	else if(a[1]){

		puts("-1");

		return 0;

	}

	int r=0;

	for(int i=1;i<=n;i++)r+=abs(a[i]);

	cout<<r;

}

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

#define N 100005

#define ll long long

#define NoSolution return puts("-1"),0 

#define For(i,x,y) for(int i=(x);i<=(y);++i)

#define Rof(i,x,y) for(int i=(x);i>=(y);--i)

#define Edge(x) for(int i=head[x];i;i=e[i].nxt)

#define mset(x,y) memset(x,y,sizeof(x))

#define p_b push_back 

using namespace std;



int f[N],fa[N],dep[N],cnt=0,b[N],_u,_v;

vector<int> g[N];



void dfs(int x,int v){

	f[x]=v;

	for(auto to:g[x]){

		if(!dep[to]){

			fa[to]=x,dep[to]=dep[x]+1;

			dfs(to,-v);

			f[x]+=f[to];

		} else if(to!=fa[x]){

			_u=x,_v=to;

		}

	}

}

int main(){

	int n,m,u,v,ans=INT_MAX,mid,tmp,cnt;

	dep[1]=1;

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

	For(i,1,m){

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

		g[u].p_b(v),g[v].p_b(u);

	}

	dfs(1,1);

	if(m==n-1){

		ans=0;

		if(f[1]) NoSolution;

		For(i,1,n) ans+=abs(f[i]);

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

	} else{

		if((dep[_v]-dep[_u]+1)%2==0){

			if(f[1]) NoSolution;

			tmp=0,cnt=0;

			For(i,1,n) tmp+=abs(f[i]);

			ans=min(ans,tmp);

			for(int i=_v;i!=_u;i=fa[i]) tmp-=abs(f[i]),b[++cnt]=f[i];

			sort(b+1,b+cnt+1);

			cnt&1?mid=b[(cnt+1)/2]:mid=(b[cnt/2]+b[cnt/2+1])/2;

			tmp+=abs(mid);

			for(int i=_v;i!=_u;i=fa[i]) tmp+=abs(f[i]-mid);

			ans=min(ans,tmp);

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

		} else{

			if(f[1]%2) NoSolution;

			int tmp=f[1]/2;ans=abs(tmp);

			for(int i=_u;i;i=fa[i]) f[i]-=tmp;

			for(int i=_v;i;i=fa[i]) f[i]-=tmp;

			For(i,1,n) ans+=abs(f[i]);

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

		}

	}

}

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

using namespace std;

#define N 200010

int n,m,a[N],v[N],nxt[N],h[N],ec,hx,hy,p[N],f[N],st[N],tp;

int fd(int x){return p[x]==x?x:p[x]=fd(p[x]);}

void add(int x,int y){v[++ec]=y;nxt[ec]=h[x];h[x]=ec;}

void dfs(int x,int fa,int va){

	a[x]=va;

	for(int i=h[x];i;i=nxt[i])

		if(v[i]!=fa){

			f[v[i]]=x;

			dfs(v[i],x,-va);

			a[x]+=a[v[i]];

		}

}

int main(){

	cin>>n>>m;

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

	hx=hy=1;

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

		int x,y;

		cin>>x>>y;

		if(fd(x)!=fd(y))p[fd(x)]=fd(y);

		else{

			hx=x;

			hy=y;

			continue;

		}

		add(x,y);add(y,x);

	}

	dfs(hx,0,-1);

	if(n==m){

		for(int i=hy;i;i=f[i])

			st[++tp]=a[i];

		if(tp&1){

			if(a[hx]&1){

				puts("-1");

				return 0;

			}

			for(int i=hy;i;i=f[i])

				a[i]-=a[hx]/2;

		}

		else{

			if(a[hx]){

				puts("-1");

				return 0;

			}

			sort(st+1,st+tp+1);

			for(int i=hy;i;i=f[i])

				a[i]-=st[tp/2];

		}

	}

	else if(a[1]){

		puts("-1");

		return 0;

	}

	int r=0;

	for(int i=1;i<=n;i++)r+=abs(a[i]);

	cout<<r;

}

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

using namespace std;

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

#define DREP(i,a,b) for(int i=(a),_ed=(b);i>=_ed;--i)

#define pb(x) push_back((x))

typedef long long ll;

inline int read(){

    register int x=0,f=1;register char ch=getchar();

    while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}

    while(isdigit(ch)){x=x*10+(ch^'0');ch=getchar();}

    return f?x:-x;

}



const int N=1e5+5;

int n,m,dep[N],s,t,flg,S[N],k[N],stk[N],tp;

ll ans;

vector<int> E[N];



inline void Dfs(int u,int pa){

    for(int v:E[u]){

	if(v==pa)continue;

	if(dep[v]){flg=(dep[u]==dep[v]);s=u,t=v;continue;}

	dep[v]=dep[u]*-1;Dfs(v,u);

    }

}

inline void Cal(int u,int pa){

    for(int v:E[u]){

	if(v==pa||(u==s&&v==t)||(v==s&&u==t))continue;

	Cal(v,u);

	S[u]+=S[v];k[u]+=k[v];

    }

}



int main(){

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

    n=read(),m=read();

    REP(i,1,m){int u=read(),v=read();E[u].pb(v),E[v].pb(u);}

    dep[1]=1;Dfs(1,0);

    int sum=0;

    REP(i,1,n)sum+=(S[i]=dep[i]);

    if(m==n-1){if(sum)return puts("-1"),0;}

    else if((flg&&(sum&1))||(!flg&&sum))return puts("-1"),0;

    if(m==n){

	if(flg){

	    S[s]-=sum/2,S[t]-=sum/2;

	    ans+=abs(sum/2);

	}

	else k[s]=1,k[t]=-1;

    }

    Cal(1,0);

    REP(i,1,n)

	if(!k[i])ans+=abs(S[i]);

	else if(~k[i])stk[++tp]=-S[i];

	else stk[++tp]=S[i];

    stk[++tp]=0;

    sort(stk+1,stk+tp+1);

    int x=stk[(tp+1)>>1];

    REP(i,1,tp)ans+=abs(stk[i]-x);

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

    return 0;

}


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

#define R register int

#define G if(++ip==ie)if(fread(ip=buf,1,SZ,stdin))

using namespace std;

const int SZ=1<<19,N=1e5+9,M=2*N;

char buf[SZ],*ie=buf+SZ,*ip=ie-1;

inline int in(){

    G;while(*ip<'-')G;

    R x=*ip&15;G;

    while(*ip>'-'){x*=10;x+=*ip&15;G;}

    return x;

}

int he[N],ne[M],to[M],f[N],a[N],b[N];

int Getf(R x){

    return x==f[x]?x:f[x]=Getf(f[x]);

}

void Dfs(R x,R op){

    a[x]=op;

    for(R y,i=he[x];i;i=ne[i])

        if((y=to[i])!=f[x])

            f[y]=x,Dfs(y,-op),a[x]+=a[y];

}

int main(){

    R n=in(),m=in(),rt=1,re=1,p=0,ans=0;

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

    for(R p=0,i=1;i<=m;++i){

        R x=in(),y=in();

        if(Getf(x)!=Getf(y))f[f[x]]=f[y];

        else{rt=x,re=y;continue;}

        ne[++p]=he[x];to[he[x]=p]=y;

        ne[++p]=he[y];to[he[y]=p]=x;

    }

    f[rt]=0;Dfs(rt,1);

    if(n==m){

        for(R x=re;x;x=f[x])b[++p]=a[x];

        if(p&1){

            if(a[rt]&1)return puts("-1"),0;

            for(R x=re;x;x=f[x])a[x]-=a[rt]/2;

        }

        else{

            if(a[rt])return puts("-1"),0;

            sort(b+1,b+p+1);

            for(R x=re;x;x=f[x])a[x]-=b[p/2];

        }

    }

    else if(a[rt])return puts("-1"),0;

    for(R i=1;i<=n;++i)ans+=abs(a[i]);

    return cout<<ans<<endl,0;

}

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



#include <stdio.h>

using namespace std; 

typedef long ll;



 int main(){



    int length;

    ll arr[200000];

    ll res[200000]={0};

    ll temp = 0;

    ll m = 2147483647;

    scanf("%d",&length);

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

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

   	}

   	res[0] = arr[0];

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

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

    }

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

    	temp = abs(res[length-1]-res[i-1]*2);

    	m = min(temp,m);

    }

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





 	return 0;

 }


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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

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



long long int abs_1(long long int i){

	if(i<0) return -i;

	else return i;

}

int main()

{

	 int N,i,j;

	 long long int sum1=0,sum=0;

	scanf("%d",&N);

	long long int a[200000]={0},x,min=2e+9;

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

	{

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

		sum+=a[i];

	}

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

	{

		sum1+=a[i];

		x=abs_1(2*sum1-sum);

		if(x<=min)

		{

			min=x;

		}

	}

	printf("%lld",min);

	return 0;

}

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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

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

using namespace std;

int main()

{

	int a[200010];

	long long int b[200010];

	int N = 2;

	cin >> N;

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

	{

		cin >> a[i];

	}

	long long int sum = 0;

	long long int m, n;

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

	{

		sum = sum + a[j];

	}

	m = sum;

	n = sum;

	long long int x[200009], y[200009];

	for (int j = 1; j <= (N - 1); j++)

	{

		y[j] = sum - a[j];

		sum = sum - a[j];

	}

	for (int k = 1; k <= (N - 1); k++)

	{

		x[k] = m - y[k];

		m = n;

	}

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

	{

		b[n] = abs(x[n] - y[n]);

	}

	long long int min = b[1];

	for (int i = 2; i <= N - 1; i++)

	{

		if (min > b[i])

			min = b[i];

	}

	cout << min<<endl;

	return 0;

}

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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

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

#include<cstdlib>

using namespace std;



int main()

{

       long long N;

       long long Sum_t[200000]={0};



       cin>>N;

       cin>>Sum_t[0];

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

       {

             cin>>Sum_t[i];

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

        }



       long long min=abs(Sum_t[N-1]-2*Sum_t[0]);

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

        {

                long long temp=abs(Sum_t[N-1]-2*Sum_t[i]);

                if(temp<min) min=temp;

         }

   

        cout<<min;

        return 0;

}

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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

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

using namespace std;

long long n,m,a[200005],ans;

int main()

{

	cin>>n;

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

		cin>>a[i];

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

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

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

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

		ans=min(ans,abs(a[i]-(a[n]-a[i])));

	cout<<ans;

}

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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

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

using namespace std;



const int N = 2e5 + 5;

typedef long long ll;



ll n, a[N], sum[N], ans = 1LL << 62;



int main() {

	ios::sync_with_stdio(false);

	cin.tie(0);

	cin >> n;

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

		cin >> a[i];

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

	}

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

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

	}

	cout << ans << endl;

	return 0;

}

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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

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

using namespace std;



int main(){

    long int a[200000],n,cha;

    

    cin>>n;

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

        //scanf("%ld",&s[i]);

        cin>>a[i];

    }

    

    long int back = 0;

    for(long int j=1;j<n;j++){

        back += a[j];

    }

    

    long int front = a[0];

    

    cha = abs(back - front);

    

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

        front += a[k];

        back -= a[k];

        if(abs(front - back)<cha){

            cha = abs(front -back);

        }

    }

    cout<<cha<<endl;

    //printf("%ld\n",cha);

}


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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

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

#include<cmath>

using namespace std;

long long a[200000];



int main()

{

	int num;

	long long min, carnum;

	int i = 0;

	cin >> num;

	while ((i <= num - 1)&&cin >> carnum)

	{

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

		++i;

	}

	min = abs(a[num] - a[1] - a[1]);

	for (int j = 2; j < num; j++)

	{

		if (abs(a[num] - a[j] - a[j])<min)

		min = abs(a[num] - a[j] - a[j]);

	}

	cout << min;

    return 0;

}

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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

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

#include<cmath>

using namespace std;



long long int sa(long long int*a,int c,int d){

	long long int res=0;

	for(int i=c;i<=d;i++){

		res += a[i];

	}

	return res;

}





int main(void)

{

	int n = 0;

	cin>>n;

	long long int a[n]={0};

	long long int b[n+1]={0};

	long long int sac = 0;

	long long int pre = 0;long long int las = 0;long long int re = 0;

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

		long long int h =0;

		cin>>h;

		a[k]=h;

		b[k+1]=b[k]+h;

	}

	sac = sa(a,0,n-1);

	long long int res = 0;

	long long int min = 9223372036854775807;

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

	{

		res = abs(sac- 2*b[i+1]);

		if (res < min) min=res;

	}

	cout <<min;

	return 0;

}








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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

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

#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

using namespace std;

typedef long long LL;

const int MAXN=2e5+5;

const int MOD=1000000000;

const LL INF=(2e5)*(1e9)+5;

LL a[MAXN];

LL sum[MAXN];



int main()

{

    ios::sync_with_stdio(false);

    int n;

    while(cin>>n)

    {

        LL tot=0;

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

        {

            cin>>a[i];

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

            tot+=a[i];

        }

        LL Min=INF;

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

        {

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

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

        }

        cout<<Min<<endl;

    }



    return 0;

}

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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

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

#include<cmath>

using namespace std;

const int MAXN = 2e5 + 100;

long long x,sum[MAXN],n,min_,i;

int main()

{

    cin>>n;

    sum[0]=0;

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

       {

           cin>>x;

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

       }

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

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

   {

       min_=min_<abs(2*sum[i]-sum[n])?min_:abs(2*sum[i]-sum[n]);

   }

    cout<<min_;

}

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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

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

#include<cmath>

using namespace std;

const int MAXN = 2e5 + 100;

long long x,sum[MAXN],n,min_,i;

int main()

{

    cin>>n;

    sum[0]=0;

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

       {

           cin>>x;

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

       }

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

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

   {

       min_=min_<abs(2*sum[i]-sum[n])?min_:abs(2*sum[i]-sum[n]);

   }

    cout<<min_;

}


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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/*g++ main.cpp -o main.out*/

/*./main.out*/

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



#include<bits/stdc++.h>

using namespace std;



#define LL long long

#define Mod 1000000007

#define L_Mod 17100000013



const double eps = 1e-9;

const int INF = 0x3f3f3f3f;

const double PI = 3.1415926535;



#define NO_TLE std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);



#define ree(n,a) memset(n,a,sizeof(n));



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

#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))

#define red(i,l,u) for(int (i)=(int)(l);(i)>=(int)(u);--(i))



int a[200005];

LL s[200005];



int main()

{

    int n;

    scanf("%d",&n);

    s[0]=0;

    rep(i,n)

    {

        cin>>a[i];

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

    }

    LL mn=200000000000001;

    rer(i,1,n-1)

    {

        mn=min(mn,abs(s[n]-s[i]-s[i]));

    }

    cout<<mn<<endl;

    return 0;

}


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

#include <math.h>

#include <algorithm>

using namespace std;

#define ll long long

const int N=2e5+10;

ll a[N];

int main()

{

	ll n,m,i,j,k,s,t,sum,l,r;

      scanf("%lld",&n);

	{

		sum=0;

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

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

		l=0;

		m=1e15;

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

		 {

		 	l+=a[i];

		 	r=sum-l;

		 	m=min(m,llabs(l-r));

		 }

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

	}

	return 0;

}

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

  

  long long a[200005];

  

  long long abs(long long in){

    return in > 0 ? in : -in;

  }

  

  int main(){

    int n;

    long long min = 2e9 + 5;

    long long sum = 0;

    scanf("%d", &n);

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

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

      sum += a[i];

    }

    if(n == 2) {

      printf("%lld\n", abs(a[0] - a[1]));

      return 0;

    }

    else {

      for(long long j = 1, x = a[0];j < n - 1;++j){

        x += a[j];

        //for(int j = 1;j <= i;++j) x += a[j];

        //printf("%d %d\n", x, sum - x);

        if(abs(sum - 2 * x) < min) min = abs(sum - 2 * x);

      }

    }

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

    return 0;

  }

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

#include<stdlib.h>

#include<math.h>



//记得修改scanf_s

int main()

{

	long int n,*a;

	scanf("%ld", &n);

	a = (long int *)malloc(n * sizeof(long int));

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

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



	double sum = 0, sum1 = 0;

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

		sum += a[i];



	long int min = fabs(2 * a[0] - sum);

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

	{

		sum1 += a[i];

		if (min > fabs(2 * sum1 - sum))

			min = fabs(2 * sum1 - sum);

	}

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

	return 0;

}

// Fastest known solution for this problem: