Source Code
#include <bits/stdc++.h>
#define int long long  
#define all(a) a.begin(),a.end() 
#define pii pair<int,int> 
#define F first 
#define S second 
#define mp make_pair 
#define md ((int)1e9+7)
using namespace std; 
signed main(){
	int n ; 
	cin>>n ; 
	vector<int> a(n); 
	int ball = 0 , bul = 0 ; 
	for(int&x:a) cin>>x , ball+=x ; 
	vector<int> b(n); 
	for(int&x:b)cin>>x,bul+=x ; 
	if(bul<ball){
		cout << -1  ; 
		return 0 ; 
	}
	int sum = 0 ; 
	set<int> in ; 
	int sec=n-1+ball; 
	for(int i=0;i<n;i++){
	//	cout << endl ; 
		sum-=a[i] ; 
		sum+=b[i] ;
//		cout << i << " " ; 
		if(sum>=0){
			if(in.empty()) continue ; 
//			cout << "back to " << *in.begin()  ; 
			sec+=(i-*in.begin())*(i==n-1?1:2); 
			in.clear() ;
		}
		else{
			in.insert(i) ; 
		}
	}

	cout << sec  ;
	return 0 ; 
}
Copy
Shooting Balloons Boredom
GNU G++17
0 ms
544 KB
Wrong Answer