Problem:
Score : 200 points

Problem Statement

Kyoto University decided to build a straight wall on the west side of the university to protect against gorillas that attack the university from the west every night.
Since it is difficult to protect the university at some points along the wall where gorillas attack violently, reinforcement materials are also built at those points.
Although the number of the materials is limited, the state-of-the-art technology can make a prediction about the points where gorillas will attack next and the number of gorillas that will attack at each point.
The materials are moved along the wall everyday according to the prediction.
You, a smart student majoring in computer science, are called to find the way to move the materials more efficiently.

Theare are N points where reinforcement materials can be build along the straight wall. They are numbered 1 through N.
Because of the protection against the last attack of gorillas, A_i materials are build at point i (1 \leq i \leq N).
For the next attack, the materials need to be rearranged such that at least B_i materials are built at point i (1 \leq i \leq N).
It costs |i - j| to move 1 material from point i to point j.
Find the minimum total cost required to satisfy the condition by moving materials.
You do not need to consider the attack after the next.

Constraints

1 \leq N \leq 10^5

A_i \geq 1

B_i \geq 1

A_1 + A_2 + ... + A_N \leq 10^{12}

B_1 + B_2 + ... + B_N \leq A_1 + A_2 + ... + A_N

There is at least one way to satisfy the condition.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

Print the minimum total cost required to satisfy the condition.

Partial Scores

30 points will be awarded for passing the test set satisfying the following:

N \leq 100

A_1 + A_2 + ... + A_N \leq 400

Another 30 points will be awarded for passing the test set satisfying the following:

N \leq 10^3

Another 140 points will be awarded for passing the test set without addtional constraints and you can get 200 points in total.

Sample Input 1

2
1 5
3 1

Sample Output 1

2

It costs least to move 2 materials from point 2 to point 1.

Sample Input 2

5
1 2 3 4 5
3 3 1 1 1

Sample Output 2

6

Sample Input 3

27
46 3 4 2 10 2 5 2 6 7 20 13 9 49 3 8 4 3 19 9 3 5 4 13 9 5 7
10 2 5 6 2 6 3 2 2 5 3 11 13 2 2 7 7 3 9 5 13 4 17 2 2 2 4

Sample Output 3

48

The input of this test case satisfies both the first and second additional constraints.

Sample Input 4

18
3878348 423911 8031742 1035156 24256 10344593 19379 3867285 4481365 1475384 1959412 1383457 164869 4633165 6674637 9732852 10459147 2810788
1236501 770807 4003004 131688 1965412 266841 3980782 565060 816313 192940 541896 250801 217586 3806049 1220252 1161079 31168 2008961

Sample Output 4

6302172

The input of this test case satisfies the second additional constraint.

Sample Input 5

2
1 99999999999
1234567891 1

Sample Output 5

1234567890

The input and output values may exceed the range of 32-bit integer.

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static void Main(){
		Sc sc=new Sc();
		var n=sc.I;
		var a=sc.La;
		var b=sc.La;
		var fl=new Mcfp(n+2,(n-1)*2+n*2);
		long inf=(long)1e18;
		int p=0;
		for(int i = 0;i<n;i++) {
			if(i!=0){fl.Edge(i,i-1,inf,1);}
			if(i!=n-1){fl.Edge(i,i+1,inf,1);}
			if(b[i]<a[i]){
				fl.Edge(n,i,(a[i]-b[i]),0);
			}
			else{
				fl.Edge(i,n+1,(b[i]-a[i]),0);
				p+=(int)(b[i]-a[i]);
			}
		}
		Console.WriteLine("{0}",fl.Pd1(n,n+1,p));
	}
}
public class Mcfp{
	private List<int>[] li;
	private Eg[] h;
	private int[][] pt;
	private int[] z,l,r;
	private long[] u;
	private bool[] b1,b2;
	private int n,p=0;
	private const int inf=int.MaxValue;
	public class Eg{
		public int n;
		public long e;
		public int c;
		public Eg(int n,long e,int c){this.n=n;this.e=e;this.c=c;}
	}
	public Mcfp(int n,int m){
		this.n=n;
		m<<=1;
		li=new List<int>[n];
		z=new int[n];
		l=new int[n];
		r=new int[n];
		u=new long[n];
		h=new Eg[m];
		for(int i=0;i<n;i++){
			li[i]=new List<int>();
			z[i]=inf;
			u[i]=long.MaxValue;
		}
	}
	public void Edge(int a,int b,long e,int c){
		if(a==b){return;}
		li[a].Add(p);
		li[b].Add(p+1);
		h[p]=new Eg(b,e,c);
		h[p+1]=new Eg(a,0,-c);
		p+=2;
	}
	public long Pd1(int s,int t,int c){
		long ans=0,d=0;
		pt=new int[][]{new int[n],new int[n]};
		int k=0;
		while(c>d){
			b1=new bool[n];
			b2=new bool[n];
			var pq=new Pq<Dt<int>>(32,true);
			z[s]=0;
			l[s]=-1;
			b2[s]=true;
			pq.Push(new Dt<int>(0,s));
			bool bo=false;
			while(pq.cnt>0){
				var e=pq.Top;
				pq.Pop();
				if(b1[e.d]){continue;}
				b1[e.d]=true;
				if(e.d==t){bo=true;}
				for(int i=0;i<li[e.d].Count;i++){
					if(h[li[e.d][i]].e==0){continue;}
					int co=e.n+h[li[e.d][i]].c-pt[k^1][h[li[e.d][i]].n]+pt[k^1][e.d];
					if(!b2[h[li[e.d][i]].n]||z[h[li[e.d][i]].n]>co){
						b2[h[li[e.d][i]].n]=true;
						z[h[li[e.d][i]].n]=co;
						pt[k][h[li[e.d][i]].n]=pt[k^1][h[li[e.d][i]].n]+co;
						l[h[li[e.d][i]].n]=e.d;
						r[h[li[e.d][i]].n]=li[e.d][i];
						u[h[li[e.d][i]].n]=Min(u[e.d],h[li[e.d][i]].e);
						pq.Push(new Dt<int>(z[h[li[e.d][i]].n],h[li[e.d][i]].n));
					}
					else if(z[h[li[e.d][i]].n]==co){
						var o=Min(u[e.d],h[li[e.d][i]].e);
						if(u[h[li[e.d][i]].n]<o){
							u[h[li[e.d][i]].n]=o;
							l[h[li[e.d][i]].n]=e.d;
							r[h[li[e.d][i]].n]=li[e.d][i];
						}
					}
				}
			}
			if(!bo){break;}
			u[t]=(int)Min(c-d,u[t]);
			for(int i = t;i!=s;i=l[i]) {
				ans+=u[t]*h[r[i]].c;
				h[r[i]].e-=u[t];
				h[r[i]^1].e+=u[t];
			}
			k^=1;
			d+=u[t];
		}
		return c==d?ans:-1;
	}
	public long Pd2(int s,int t,int c){
		long ans=0,d=0;
		z[s]=0;
		l[s]=-1;
		b1=new bool[n];
		pt=new int[][]{new int[n],new int[n]};
		int m=p;
		bool bo=true;
		for(int i=0;bo&&i<n;i++){
			bo=false;
			for(int j=0;j<m;j++){
				if(h[j].e!=0&&z[h[j^1].n]!=inf&&z[h[j].n]>z[h[j^1].n]+h[j].c){
					z[h[j].n]=z[h[j^1].n]+h[j].c;
					pt[0][h[j].n]=z[h[j].n];
					l[h[j].n]=h[j^1].n;
					r[h[j].n]=j;
					u[h[j].n]=Min(u[h[j^1].n],h[j].e);
					bo=true;
					if(i==n){b1[h[j].n]=true;}
				}
			}
		}
		u[t]=(int)Min(c-d,u[t]);
		for(int i = t;i!=s;i=l[i]) {
			ans+=u[t]*h[r[i]].c;
			h[r[i]].e-=u[t];
			h[r[i]^1].e+=u[t];
		}
		d+=u[t];
		int k=1;
		if(c==d){return ans;}
		while(c>d){
			b1=new bool[n];
			b2=new bool[n];
			u=new long[n];
			var pq=new Pq<Dt<int>>(32,true);
			z[s]=0;
			l[s]=-1;
			u[s]=inf;
			b2[s]=true;
			pq.Push(new Dt<int>(0,s));
			bo=false;
			while(pq.cnt>0){
				var e=pq.Top;
				pq.Pop();
				if(b1[e.d]){continue;}
				b1[e.d]=true;
				if(e.d==t){bo=true;}
				for(int i=0;i<li[e.d].Count;i++){
					if(h[li[e.d][i]].e==0){continue;}
					int co=e.n+h[li[e.d][i]].c-pt[k^1][h[li[e.d][i]].n]+pt[k^1][e.d];
					if(!b2[h[li[e.d][i]].n]||z[h[li[e.d][i]].n]>co){
						b2[h[li[e.d][i]].n]=true;
						z[h[li[e.d][i]].n]=co;
						pt[k][h[li[e.d][i]].n]=pt[k^1][h[li[e.d][i]].n]+co;
						l[h[li[e.d][i]].n]=e.d;
						r[h[li[e.d][i]].n]=li[e.d][i];
						u[h[li[e.d][i]].n]=Min(u[e.d],h[li[e.d][i]].e);
						pq.Push(new Dt<int>(z[h[li[e.d][i]].n],h[li[e.d][i]].n));
					}
					else if(z[h[li[e.d][i]].n]==co){
						var o=Min(u[e.d],h[li[e.d][i]].e);
						if(u[h[li[e.d][i]].n]<o){
							u[h[li[e.d][i]].n]=o;
							l[h[li[e.d][i]].n]=e.d;
							r[h[li[e.d][i]].n]=li[e.d][i];
						}
					}
				}
			}
			if(!bo){break;}
			u[t]=(int)Min(c-d,u[t]);
			for(int i = t;i!=s;i=l[i]) {
				ans+=u[t]*h[r[i]].c;
				h[r[i]].e-=u[t];
				h[r[i]^1].e+=u[t];
			}
			k^=1;
			d+=u[t];
		}
		return c==d?ans:-1;
	}
	public void Th(Action<int,int> f){
		for(int i = 0;i<p;i+=2) {
			if(h[i+1].e!=0){f(h[i+1].n,h[i].n);}
		}
	}
}
public class Dt<T>:IComparable{
	public int n;
	public T d;
	public Dt(int n,T d){this.n=n;this.d=d;}
	public int CompareTo(object obj){
		Dt<T> mymo=(Dt<T>)obj;
		if(mymo.n>n){return -1;}
		else if(mymo.n<n){return 1;}
		else{return 0;}
	}
}
public class Pq<T> where T:IComparable{
	private T[] he;
	public int cnt=0,max=0;
	private Func<T,T,int> compare;
	public Pq(int max,bool mm){
		this.max=max;
		he=new T[max];
		if(mm){compare=Ao;}
		else{compare=Do;}
	}
	public void Push(T x){
		if(cnt==max){Extend();}
		int j=cnt;
		while(j!=0&&compare(x,he[(j-1)>>1])>0){he[j]=he[(j-1)>>1];j=(j-1)>>1;}
		he[j]=x;
		cnt++;
	}
	public void Pop(){
		cnt--;
		T r=he[cnt];
		int j=0;
		while(true){
			if(j*2+1<cnt){
				if(compare(he[j*2+1],he[j*2+2])>0){j=j*2+1;}
				else{j=j*2+2;}
			}
			else if(j*2<cnt){j=j*2+1;}
			else{break;}
			if(compare(he[j],r)<=0){j=(j-1)>>1;break;}
			he[(j-1)>>1]=he[j];
		}
		he[j]=r;
	}
	private int Ao(T x,T y){return y.CompareTo(x);}
	private int Do(T x,T y){return x.CompareTo(y);}
	public T Top{get{return he[0];}}
	private void Extend(){
		T[] nhe=new T[max<<1];
		Array.Copy(he,nhe,max);
		he=nhe;
		max<<=1;
	}
}

public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}