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 avl=new Avl();
		for(int i = 0;i<n;i++) {
			if(b[i]<a[i]){avl.Ud(i,a[i]-b[i]);}
		}
		long ans=0;
		for(int i = 0;i<n;i++) {
			b[i]-=a[i];
			while(b[i]>0){
				var e1=avl.Lb(i);
				var e2=avl.Ub(i);
				Avl.Nd e=null;
				if(e1!=null&&e2!=null){e=Abs(e1.n-i)<Abs(e2.n-i)?e1:e2;}
				else if(e1==null){e=e2;}
				else{e=e1;}
				if(e.d<=b[i]){
					ans+=Abs(e.n-i)*e.d;
					b[i]-=e.d;
					avl.Dl(e.n);
				}
				else{
					ans+=Abs(e.n-i)*b[i];
					e.d-=b[i];
					b[i]=0;
				}
			}
		}
		Console.WriteLine("{0}",ans);
	}
}
public class Avl{
	public class Nd{
		public int h=1,c=1;
		public readonly int n;
		public long d,ds;
		public Nd l,r;
		public Nd(int n,long d,Nd l,Nd r){this.n=n;this.d=d;ds=d;this.l=l;this.r=r;}
	}
	public Nd root,du;
	public int cnt=0;
	public Avl(){
		du=new Nd(int.MinValue,0,du,du);du.h=0;du.c=0;
		root=du;
	}
	public void Ud(int n,long d){
		Nd mn=new Nd(n,d,du,du);
		if(root==du){root=mn;}
		else{
			Fu(root,n,mn);
			if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
		}
		cnt++;
	}
	private void Fu(Nd t,int n,Nd mn){
		t.c++;
		t.ds+=mn.d;
		if(t.n>n){
			if(t.l!=du){
				Fu(t.l,n,mn);
				if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);}
			}
			else{t.l=mn;}
		}
		else{
			if(t.r!=du){
				Fu(t.r,n,mn);
				if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);}
			}
			else{t.r=mn;}
		}
		t.h=Max(t.l.h,t.r.h)+1;
	}
	private Nd Rotate(Nd t){
		Nd nd=du;
		if(t.l.h>t.r.h){
			if(t.l.l.h>t.l.r.h){
				nd=t.l;t.l=t.l.r;nd.r=t;
				Ft(t);
			}
			else{
				nd=t.l.r;
				t.l.r=nd.l;nd.l=t.l;
				Ft(t.l);
				t.l=nd.r;nd.r=t;
				Ft(t);
			}
		}
		else{
			if(t.r.l.h>t.r.r.h){
				nd=t.r.l;
				t.r.l=nd.r;nd.r=t.r;
				Ft(t.r);
				t.r=nd.l;nd.l=t;
				Ft(t);
			}
			else{
				nd=t.r;t.r=t.r.l;nd.l=t;
				Ft(t);
			}
		}
		Ft(nd);
		return nd;
	}
	private void Ft(Nd t){
		t.h=Max(t.l.h,t.r.h)+1;
		t.c=t.l.c+t.r.c+1;
		t.ds=t.l.ds+t.r.ds+t.d;
	}
	public bool Dl(int n){
		if(cnt==0){return false;}
		Nd t=root;
		if(t.n==n){
			root=Fd2(root);
			if(cnt==0){return true;}
			else if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
			else{Ft(root);}
			return true;
		}
		bool bo=Fd1(root,n);
		if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
		else{Ft(root);}
		return bo;
	}
	private bool Fd1(Nd t,int n){
		if(t.n>n){
			if(t.l==du){return false;}
			else if(t.l.n!=n){
				bool bo=Fd1(t.l,n);
				if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);}
				else{Ft(t.l);}
				return bo;
			}
			else{
				t.l=Fd2(t.l);
				Ft(t);
				return true;
			}
		}
		else{
			if(t.r==du){return false;}
			else if(t.r.n!=n){
				bool bo=Fd1(t.r,n);
				if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);}
				else{Ft(t.r);}
				return bo;
			}
			else{
				t.r=Fd2(t.r);
				Ft(t);
				return true;
			}
		}
	}
	private Nd Fd2(Nd dn){
		cnt--;
		if(dn.l==du){return dn.r;}
		if(dn.r==du){return dn.l;}
		Nd u=dn.l;
		if(u.r==du){
			u.r=dn.r;
			Ft(u);
			return u;
		}
		u=Fd3(u,dn);
		if(Abs(dn.l.l.h-dn.l.r.h)>1){u.l=Rotate(dn.l);}
		else{Ft(dn.l);}
		Ft(u);
		return u;
	}
	private Nd Fd3(Nd u,Nd dn){
		if(u.r.r!=du){
			Nd v=Fd3(u.r,dn);
			if(Abs(u.r.l.h-u.r.r.h)>1){u.r=Rotate(u.r);}
			else{Ft(u.r);}
			return v;
		}
		else{
			Nd v=u.r;u.r=u.r.l;v.l=dn.l;v.r=dn.r;
			return v;
		}
	}
	public Nd Hs(int n){
		Nd t=root;
		if(cnt==0){return null;}
		if(t.n==n){return t;}
		Nd a=null;
		while(true){
			if(t.n>n){
				if(t.l==du){break;}
				else if(t.l.n!=n){t=t.l;}
				else{a=t.l;break;}
			}
			else{
				if(t.r==du){break;}
				else if(t.r.n!=n){t=t.r;}
				else{a=t.r;break;}
			}
		}
		return a;
	}
	public Nd Ra(int n){return cnt>=n?Fr(root,n):null;}
	private Nd Fr(Nd t,int n){
		if(t.l.c>n-1){return Fr(t.l,n);}
		if(t.l.c<n-1){return Fr(t.r,n-t.l.c-1);}
		return t;
	}
	public Nd Lb(int n){return cnt!=0?Flb(root,n):null;}
	private Nd Flb(Nd t,int n){
		if(t.n<n){
			if(t.r==du){return null;}
			return Flb(t.r,n);
		}
		if(t.n>n){
			if(t.l==du){return t;}
			Nd u=Flb(t.l,n);
			return u==null?t:u;
		}
		return t;
	}
	public Nd Ub(int n){return cnt!=0?Fub(root,n):null;}
	private Nd Fub(Nd t,int n){
		if(t.n>n){
			if(t.l==du){return null;}
			return Fub(t.l,n);
		}
		if(t.n<n){
			if(t.r==du){return t;}
			Nd u=Fub(t.r,n);
			return u==null?t:u;
		}
		return t;
	}
	public int Rg(int a,int b){return cnt!=0?Frg(root,a,b,false,false):0;}
	private int Frg(Nd t,int a,int b,bool l,bool r){
		if(t.h==0){return 0;}
		if(l&&r){return t.c;}
		if(t.n<a){return Frg(t.r,a,b,t.n>=a,r);}
		if(t.n>b){return Frg(t.l,a,b,l,t.n<=b);}
		return Frg(t.l,a,b,l,t.n<=b)+Frg(t.r,a,b,t.n>=a,r)+1;
	}
	public long Rd(int a,int b){return cnt!=0?Frd(root,a,b,false,false):0;}
	private long Frd(Nd t,int a,int b,bool l,bool r){
		if(t.h==0){return 0;}
		if(l&&r){return t.ds;}
		if(t.n<a){return Frd(t.r,a,b,t.n>=a,r);}
		if(t.n>b){return Frd(t.l,a,b,l,t.n<=b);}
		return Frd(t.l,a,b,l,t.n<=b)+Frd(t.r,a,b,t.n>=a,r)+t.d;
	}
	public void Ad(int n,int d){
		Nd t=root;
		if(cnt==0){return;}
		if(t.n==n){t.d+=d;t.ds+=d;return;}
		while(true){
			t.ds+=d;
			if(t.n>n){
				if(t.l==du){break;}
				else if(t.l.n!=n){t=t.l;}
				else{t.l.d+=d;t.l.ds+=d;break;}
			}
			else{
				if(t.r==du){break;}
				else if(t.r.n!=n){t=t.r;}
				else{t.r.d+=d;t.r.ds+=d;break;}
			}
		}
	}
}

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;}
}