Language: C++

#include <bits/stdc++.h>
#define LL long long
#define Rg register
#define RI register int
#define RC register char
#define ed puts("") 
#define Pair pair<LL,LL>
#define space putchar(' ')
using namespace std;   
template <typename tp> inline void read(tp & x) 
{	
    x=0;
    RI f=1; 
    RC c=getchar();  
    while(c<'0' || c>'9'){if(c=='-') f=-1;c=getchar();} 
    while(c>='0' && c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();} 
 	x*=f;
} 	
    	
template <typename Tp> inline void print(Tp x)  
{							  	
    if(x<0) x=-x,putchar('-');  
    if(x>9) print(x/10);      
    putchar(x%10+'0');		  	
}

#define int long long
const int maxn=2e6+10;
int n;
int tot; 
int a[maxn];
int b[maxn];
int c[maxn]; 
int L[maxn];
int R[maxn]; 
bool vis[maxn];
int ans;
priority_queue<Pair> q;

inline bool check()
{
	for(RI i=1;i<=n;++i)
		if(a[i]!=b[i])
			return false;
	return true;
}

signed main()
{
	read(n);
	for(RI i=1;i<=n;++i)
		read(a[i]),a[i+n]=a[i],c[i]=a[i];
	a[0]=a[n];
	a[n+1]=a[1];
	for(RI i=1;i<=n;++i)
		read(b[i]),b[i+n]=b[i],q.push(make_pair(b[i],i)),L[i]=i-1,R[i]=i+1;
	for(RI i=1;i<=n;++i)
	{
		if(a[i]!=b[i])
			break;
		if(i==n)
		{
			print(0);
			return 0; 
		}
	}
		
	for(RI i=1;i<=n;++i)
	{
		if(a[i]==b[i])
			tot++,continue;
		c[i]+=a[i-1]+a[i+1];
		if(c[i]>b[i])
		{
			print(-1);
			return 0;
		}
	}
		
	L[1]=n;
	R[n]=1;
	int ans=0;	
	
	int temp=0;
	while(q.size())
	{
		int num=q.top().second;
		q.pop(); 
		if(b[num]!=a[num]) 
			b[num]-=b[R[num]]+b[L[num]],ans++;
		if(b[num]<a[num])
		{
			print(-1);
			return 0;
		} 	
		if(b[num]==a[num])
		{
			tot++;
			if(tot==n)
			{
				print(ans);
				return 0;
			}
			continue;
		}
		q.push(make_pair(b[num],num)); 
	}
	return 0;
}

Compiler output:
a.cc: In function 'void read(tp&)':
a.cc:13:8: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   13 |     RI f=1;
      |        ^
a.cc:14:8: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   14 |     RC c=getchar();
      |        ^
a.cc: In function 'bool check()':
a.cc:42:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   42 |         for(RI i=1;i<=n;++i)
      |                ^
a.cc: In function 'int main()':
a.cc:51:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   51 |         for(RI i=1;i<=n;++i)
      |                ^
a.cc:55:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   55 |         for(RI i=1;i<=n;++i)
      |                ^
a.cc:57:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   57 |         for(RI i=1;i<=n;++i)
      |                ^
a.cc:68:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   68 |         for(RI i=1;i<=n;++i)
      |                ^
a.cc:71:31: error: expected primary-expression before 'continue'
   71 |                         tot++,continue;
      |                               ^~~~~~~~
a.cc: In instantiation of 'void read(tp&) [with tp = long long int]':
a.cc:50:6:   required from here
   50 |         read(n);
      |         ~~~~^~~
a.cc:13:8: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   13 |     RI f=1;
      |        ^
a.cc:14:8: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   14 |     RC c=getchar();
      |        ^